1 // Copyright 2012 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Settings
28 
29 #include "braids/settings.h"
30 
31 #include <cstring>
32 
33 #include "stmlib/system/storage.h"
34 #include "stmlib/utils/murmurhash3.h"
35 
36 namespace braids {
37 
38 using namespace stmlib;
39 
40 const SettingsData kInitSettings = {
41   MACRO_OSC_SHAPE_CSAW,
42 
43   RESOLUTION_16_BIT,
44   SAMPLE_RATE_96K,
45 
46   0,  // AD->timbre
47   false,  // Trig source
48   1,  // Trig delay
49   false,  // Meta modulation
50 
51   PITCH_RANGE_EXTERNAL,
52   2,
53   0,  // Quantizer is off
54   false,
55   false,
56   false,
57 
58   2,  // Brightness
59   0,  // AD attack
60   5,  // AD decay
61   0,  // AD->FM
62   0,  // AD->COLOR
63   0,  // AD->VCA
64   0,  // Quantizer root
65 
66   50,
67   15401,
68   2048,
69 
70   { 0, 0 },
71   { 32768, 32768 },
72   "GREETINGS FROM MUTABLE INSTRUMENTS *EDIT ME*",
73 };
74 
75 Storage<0x8020000, 4> storage;
76 
Init()77 void Settings::Init() {
78   if (!storage.ParsimoniousLoad(&data_, &version_token_)) {
79     Reset();
80   }
81   bool settings_within_range = true;
82   for (int32_t i = 0; i <= SETTING_LAST_EDITABLE_SETTING; ++i) {
83     const Setting setting = static_cast<Setting>(i);
84     const SettingMetadata& setting_metadata = metadata(setting);
85     uint8_t value = GetValue(setting);
86     settings_within_range = settings_within_range && \
87         value >= setting_metadata.min_value && \
88         value <= setting_metadata.max_value;
89   }
90   settings_within_range = settings_within_range && data_.magic_byte == 'M';
91   for (int i = 0; i < 2; ++i) {
92     settings_within_range = settings_within_range && \
93         data_.parameter_cv_scale[i] > 16384;
94     settings_within_range = settings_within_range && \
95         data_.parameter_cv_offset[i] < 8000;
96     settings_within_range = settings_within_range && \
97         data_.parameter_cv_offset[i] > -8000;
98   }
99   if (!settings_within_range) {
100     Reset();
101   }
102   CheckPaques();
103 }
104 
Reset()105 void Settings::Reset() {
106   memcpy(&data_, &kInitSettings, sizeof(SettingsData));
107   data_.magic_byte = 'M';
108 }
109 
Save()110 void Settings::Save() {
111   data_.magic_byte = 'M';
112   storage.ParsimoniousSave(data_, &version_token_);
113   CheckPaques();
114 }
115 
CheckPaques()116 void Settings::CheckPaques() {
117   paques_ = !strcmp(data_.marquee_text, "49");
118 }
119 
120 const char* const boolean_values[] = { "OFF ", "ON " };
121 const char* const intensity_values[] = {
122     "OFF ",
123     "   1",
124     "   2",
125     "   3",
126     "FULL" };
127 
128 const char* const zero_to_fifteen_values[] = {
129     "   0",
130     "   1",
131     "   2",
132     "   3",
133     "   4",
134     "   5",
135     "   6",
136     "   7",
137     "   8",
138     "   9",
139     "  10",
140     "  11",
141     "  12",
142     "  13",
143     "  14",
144     "  15"};
145 
146 const char* const algo_values[] = {
147     "CSAW",
148     "^\x88\x8D_",
149     "\x88\x8A\x8C\x8D",
150     "FOLD",
151     "\x8E\x8E\x8E\x8E",
152     "SUB\x8C",
153     "SUB\x88",
154     "SYN\x8C",
155     "SYN\x88",
156     "\x88\x88x3",
157     "\x8C_x3",
158     "/\\x3",
159     "SIx3",
160     "RING",
161     "\x88\x89\x88\x89",
162     "\x88\x88\x8E\x8E",
163     "TOY*",
164     "ZLPF",
165     "ZPKF",
166     "ZBPF",
167     "ZHPF",
168     "VOSM",
169     "VOWL",
170     "VFOF",
171     "HARM",
172     "FM  ",
173     "FBFM",
174     "WTFM",
175     "PLUK",
176     "BOWD",
177     "BLOW",
178     "FLUT",
179     "BELL",
180     "DRUM",
181     "KICK",
182     "CYMB",
183     "SNAR",
184     "WTBL",
185     "WMAP",
186     "WLIN",
187     "WTx4",
188     "NOIS",
189     "TWNQ",
190     "CLKN",
191     "CLOU",
192     "PRTC",
193     "QPSK",
194     // "NAME" // For your algorithm
195 };
196 
197 const char* const bits_values[] = {
198     "2BIT",
199     "3BIT",
200     "4BIT",
201     "6BIT",
202     "8BIT",
203     "12B",
204     "16B " };
205 
206 const char* const rates_values[] = {
207     "4KHZ",
208     "8KHZ",
209     "16K ",
210     "24K ",
211     "32K ",
212     "48K ",
213     "96K " };
214 
215 const char* const quantization_values[] = {
216     "OFF ",
217     "SEMI",
218     "IONI",
219     "DORI",
220     "PHRY",
221     "LYDI",
222     "MIXO",
223     "AEOL",
224     "LOCR",
225     "BLU+",
226     "BLU-",
227     "PEN+",
228     "PEN-",
229     "FOLK",
230     "JAPA",
231     "GAME",
232     "GYPS",
233     "ARAB",
234     "FLAM",
235     "WHOL",
236     "PYTH",
237     "EB/4",
238     "E /4",
239     "EA/4",
240     "BHAI",
241     "GUNA",
242     "MARW",
243     "SHRI",
244     "PURV",
245     "BILA",
246     "YAMA",
247     "KAFI",
248     "BHIM",
249     "DARB",
250     "RAGE",
251     "KHAM",
252     "MIMA",
253     "PARA",
254     "RANG",
255     "GANG",
256     "KAME",
257     "PAKA",
258     "NATB",
259     "KAUN",
260     "BAIR",
261     "BTOD",
262     "CHAN",
263     "KTOD",
264     "JOGE" };
265 
266 const char* const trig_source_values[] = { "EXT.", "AUTO" };
267 
268 const char* const pitch_range_values[] = {
269     "EXT.",
270     "FREE",
271     "XTND",
272     "440 ",
273     "LFO "
274 };
275 
276 const char* const octave_values[] = { "-2", "-1", "0", "1", "2" };
277 
278 const char* const trig_delay_values[] = {
279     "NONE",
280     "125u",
281     "250u",
282     "500u",
283     "1ms ",
284     "2ms ",
285     "4ms "
286 };
287 
288 const char* const brightness_values[] = {
289     "\xff   ",
290     "\xff\xff  ",
291     "\xff\xff\xff\xff",
292 };
293 
294 const char* const note_values[] = {
295     "C",
296     "Db",
297     "D",
298     "Eb",
299     "E",
300     "F",
301     "Gb",
302     "G",
303     "Ab",
304     "A",
305     "Bb",
306     "B",
307 };
308 
309 /* static */
310 const SettingMetadata Settings::metadata_[] = {
311   { 0, MACRO_OSC_SHAPE_LAST - 2, "WAVE", algo_values },
312   { 0, RESOLUTION_LAST - 1, "BITS", bits_values },
313   { 0, SAMPLE_RATE_LAST - 1, "RATE", rates_values },
314   { 0, 15, "\x8F""TIM", zero_to_fifteen_values },
315   { 0, 1, "TSRC", trig_source_values },
316   { 0, 6, "TDLY", trig_delay_values },
317   { 0, 1, "META", boolean_values },
318   { 0, 3, "RANG", pitch_range_values },
319   { 0, 4, "OCTV", octave_values },
320   { 0, 48, "QNTZ", quantization_values },
321   { 0, 1, "FLAT", boolean_values },
322   { 0, 4, "DRFT", intensity_values },
323   { 0, 4, "SIGN", intensity_values },
324   { 0, 2, "BRIG", brightness_values },
325   { 0, 15, "\x8F""ATT", zero_to_fifteen_values },
326   { 0, 15, "\x8F""DEC", zero_to_fifteen_values },
327   { 0, 15, "\x8F""FM ", zero_to_fifteen_values },
328   { 0, 15, "\x8F""COL", zero_to_fifteen_values },
329   { 0, 1, "\x8F""VCA", boolean_values },
330   { 0, 11, "ROOT", note_values },
331   { 0, 0, "CAL.", NULL },
332   { 0, 0, "    ", NULL },  // Placeholder for CV tester
333   { 0, 0, "    ", NULL },  // Placeholder for marquee
334   { 0, 0, "v1.9", NULL },  // Placeholder for version string
335 };
336 
337 /* static */
338 const Setting Settings::settings_order_[] = {
339   SETTING_OSCILLATOR_SHAPE,
340   SETTING_META_MODULATION,
341   SETTING_RESOLUTION,
342   SETTING_SAMPLE_RATE,
343   SETTING_TRIG_SOURCE,
344   SETTING_TRIG_DELAY,
345   SETTING_AD_ATTACK,
346   SETTING_AD_DECAY,
347   SETTING_AD_FM,
348   SETTING_AD_TIMBRE,
349   SETTING_AD_COLOR,
350   SETTING_AD_VCA,
351   SETTING_PITCH_RANGE,
352   SETTING_PITCH_OCTAVE,
353   SETTING_QUANTIZER_SCALE,
354   SETTING_QUANTIZER_ROOT,
355   SETTING_VCO_FLATTEN,
356   SETTING_VCO_DRIFT,
357   SETTING_SIGNATURE,
358   SETTING_BRIGHTNESS,
359   SETTING_CALIBRATION,
360   SETTING_CV_TESTER,
361   SETTING_MARQUEE,
362   SETTING_VERSION,
363 };
364 
365 /* extern */
366 Settings settings;
367 
368 }  // namespace braids
369