1 module soundtab.ui.settings;
2 
3 import dlangui.core.settings;
4 import dlangui.dialogs.settingsdialog;
5 import dlangui.core.i18n;
6 import dlangui.widgets.lists;
7 import soundtab.audio.instruments;
8 
9 /// local settings for project (not supposed to put under source control)
10 class AudioSettings : SettingsFile {
11     this(string filename) {
12         super(filename);
13     }
14 
15     /// override to do something after loading - e.g. set defaults
16     override void afterLoad() {
17     }
18 
19     override void updateDefaults() {
20         Setting audio = audioSettings();
21         audio.setStringDef("device", "default");
22         audio.setBooleanDef("exclusiveMode", false);
23         audio.setIntegerDef("minFrameMillis", 3);
24     }
25 
26     @property int accompanimentVolume() {
27         Setting accomp = accompSettings();
28         return cast(int)accomp.getInteger("volume", 1000);
29     }
30 
31     @property AudioSettings accompanimentVolume(int v) {
32         Setting accomp = accompSettings();
33         accomp.setInteger("volume", v);
34         return this;
35     }
36 
37     @property string accompanimentFile() {
38         Setting accomp = accompSettings();
39         return accomp.getString("file", null);
40     }
41 
42     @property AudioSettings accompanimentFile(string fn) {
43         Setting accomp = accompSettings();
44         accomp.setString("file", fn);
45         return this;
46     }
47 
48     @property Setting audioSettings() {
49         Setting res = _setting.objectByPath("audio/playback", true);
50         return res;
51     }
52 
53     @property Setting accompSettings() {
54         Setting res = _setting.objectByPath("accompaniment", true);
55         return res;
56     }
57 
58     @property Setting instrumentSettings() {
59         Setting res = _setting.objectByPath("instrument", true);
60         return res;
61     }
62 
63     @property Setting controllerSettings() {
64         string instr = instrumentId;
65         Setting res = _setting.objectByPath("controllerSettings", true);
66         res = res.objectByPath(instr, true);
67         return res;
68     }
69 
70     @property string playbackDevice() {
71         return audioSettings.getString("device", "default");
72     }
73 
74     @property AudioSettings playbackDevice(string dev) {
75         audioSettings.setString("device", dev);
76         return this;
77     }
78 
79     @property bool exclusiveMode() {
80         return audioSettings.getBoolean("exclusiveMode", true);
81     }
82 
83     @property int minFrameMillis() {
84         return cast(int)audioSettings.getInteger("minFrameMillis", 3);
85     }
86 
87     @property string instrumentId() {
88         return instrumentSettings.getString("instrumentId", "sinewave");
89     }
90 
91     @property AudioSettings instrumentId(string instrId) {
92         instrumentSettings.setString("instrumentId", instrId);
93         return this;
94     }
95 
96     AudioSettings setControllerValue(ControllerId controllerId, int value) {
97         import std.conv : to;
98         Setting s = controllerSettings();
99         s.setInteger(to!string(controllerId), value);
100         return this;
101     }
102 
103     int getControllerValue(ControllerId controllerId, int defValue) {
104         import std.conv : to;
105         Setting s = controllerSettings();
106         int res = cast(int)s.getInteger(to!string(controllerId), defValue);
107         return res;
108     }
109 
110     @property int[2] noteRange() {
111         int start = cast(int)controllerSettings.getInteger("noteRangeStart", -25);
112         int end = cast(int)controllerSettings.getInteger("noteRangeEnd", 28);
113         return [start, end];
114     }
115 
116     @property AudioSettings noteRange(int[2] range) {
117         controllerSettings.setInteger("noteRangeStart", range[0]);
118         controllerSettings.setInteger("noteRangeEnd", range[1]);
119         return this;
120     }
121 
122 }
123 
124 SettingsPage createSettingsPages(StringListValue[] deviceList) {
125     SettingsPage res = new SettingsPage("", UIString(""d));
126     SettingsPage audio = res.addChild("audio", UIString("Audio"d));
127     audio.addStringComboBox("audio/playback/device", UIString("Audio Playback Device"d), deviceList);
128     audio.addCheckbox("audio/playback/exclusiveMode", UIString("Exclusive mode"d));
129     StringListValue[] minFrameList = [
130         StringListValue(1, "1 ms"d),
131         StringListValue(2, "2 ms"d),
132         StringListValue(3, "3 ms"d),
133         StringListValue(4, "4 ms"d),
134         StringListValue(5, "5 ms"d),
135         StringListValue(6, "6 ms"d),
136         StringListValue(10, "10 ms"d),
137     ];
138     audio.addIntComboBox("audio/playback/minFrameMillis", UIString("Minimal frame (milliseconds)"d), minFrameList);
139     return res;
140 }