forked from modlfo/teensy-braids
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.h
336 lines (281 loc) · 8.29 KB
/
settings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2012 Olivier Gillet.
//
// Author: Olivier Gillet (ol.gillet@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Settings
#ifndef BRAIDS_SETTINGS_H_
#define BRAIDS_SETTINGS_H_
#include "stmlib/stmlib.h"
namespace braids {
enum MacroOscillatorShape {
MACRO_OSC_SHAPE_CSAW,
MACRO_OSC_SHAPE_MORPH,
MACRO_OSC_SHAPE_SAW_SQUARE,
MACRO_OSC_SHAPE_SQUARE_SYNC,
MACRO_OSC_SHAPE_SINE_TRIANGLE,
MACRO_OSC_SHAPE_BUZZ,
MACRO_OSC_SHAPE_TRIPLE_SAW,
MACRO_OSC_SHAPE_TRIPLE_SQUARE,
MACRO_OSC_SHAPE_TRIPLE_RING_MOD,
MACRO_OSC_SHAPE_SAW_SWARM,
MACRO_OSC_SHAPE_SAW_COMB,
MACRO_OSC_SHAPE_TOY,
MACRO_OSC_SHAPE_DIGITAL_FILTER_LP,
MACRO_OSC_SHAPE_DIGITAL_FILTER_PK,
MACRO_OSC_SHAPE_DIGITAL_FILTER_BP,
MACRO_OSC_SHAPE_DIGITAL_FILTER_HP,
MACRO_OSC_SHAPE_VOSIM,
MACRO_OSC_SHAPE_VOWEL,
MACRO_OSC_SHAPE_VOWEL_FOF,
MACRO_OSC_SHAPE_FM,
MACRO_OSC_SHAPE_FEEDBACK_FM,
MACRO_OSC_SHAPE_CHAOTIC_FEEDBACK_FM,
MACRO_OSC_SHAPE_STRUCK_BELL,
MACRO_OSC_SHAPE_STRUCK_DRUM,
MACRO_OSC_SHAPE_PLUCKED,
MACRO_OSC_SHAPE_BOWED,
MACRO_OSC_SHAPE_BLOWN,
MACRO_OSC_SHAPE_FLUTED,
MACRO_OSC_SHAPE_WAVETABLES,
MACRO_OSC_SHAPE_WAVE_MAP,
MACRO_OSC_SHAPE_WAVE_LINE,
MACRO_OSC_SHAPE_WAVE_PARAPHONIC,
MACRO_OSC_SHAPE_FILTERED_NOISE,
MACRO_OSC_SHAPE_TWIN_PEAKS_NOISE,
MACRO_OSC_SHAPE_CLOCKED_NOISE,
MACRO_OSC_SHAPE_GRANULAR_CLOUD,
MACRO_OSC_SHAPE_PARTICLE_NOISE,
MACRO_OSC_SHAPE_DIGITAL_MODULATION,
MACRO_OSC_SHAPE_QUESTION_MARK,
// MACRO_OSC_SHAPE_YOUR_ALGO
MACRO_OSC_SHAPE_LAST,
};
enum Resolution {
RESOLUTION_2_BIT,
RESOLUTION_3_BIT,
RESOLUTION_4_BIT,
RESOLUTION_6_BIT,
RESOLUTION_8_BIT,
RESOLUTION_12_BIT,
RESOLUTION_16_BIT,
RESOLUTION_LAST
};
enum SampleRate {
SAMPLE_RATE_4K,
SAMPLE_RATE_8K,
SAMPLE_RATE_16K,
SAMPLE_RATE_24k,
SAMPLE_RATE_32k,
SAMPLE_RATE_48K,
SAMPLE_RATE_96K,
SAMPLE_RATE_LAST
};
enum PitchQuantization {
PITCH_QUANTIZATION_OFF,
PITCH_QUANTIZATION_QUARTER_TONE,
PITCH_QUANTIZATION_SEMITONE,
PITCH_QUANTIZATION_LAST
};
enum PitchRange {
PITCH_RANGE_EXTERNAL,
PITCH_RANGE_FREE,
PITCH_RANGE_EXTENDED,
PITCH_RANGE_440,
PITCH_RANGE_LFO // This setting is hidden by default!
};
enum Setting {
SETTING_OSCILLATOR_SHAPE,
SETTING_RESOLUTION,
SETTING_SAMPLE_RATE,
SETTING_TRIG_DESTINATION,
SETTING_TRIG_SOURCE,
SETTING_TRIG_DELAY,
SETTING_META_MODULATION,
SETTING_PITCH_RANGE,
SETTING_PITCH_OCTAVE,
SETTING_PITCH_QUANTIZER,
SETTING_VCO_FLATTEN,
SETTING_VCO_DRIFT,
SETTING_SIGNATURE,
SETTING_BRIGHTNESS,
SETTING_TRIG_AD_SHAPE,
SETTING_LAST_EDITABLE_SETTING = SETTING_TRIG_AD_SHAPE,
// Not settings per-se, but used for menu display!
SETTING_CALIBRATION,
SETTING_CV_TESTER,
SETTING_MARQUEE,
SETTING_VERSION,
SETTING_LAST
};
struct SettingsData {
uint8_t shape;
uint8_t resolution;
uint8_t sample_rate;
uint8_t trig_destination;
uint8_t auto_trig;
uint8_t trig_delay;
uint8_t meta_modulation;
uint8_t pitch_range;
uint8_t pitch_octave;
uint8_t pitch_quantization;
uint8_t vco_flatten;
uint8_t vco_drift;
uint8_t signature;
uint8_t brightness;
uint8_t trig_ad_shape;
uint8_t padding[5];
int32_t pitch_cv_offset;
int32_t pitch_cv_scale;
int32_t fm_cv_offset;
char marquee_text[64];
};
struct SettingMetadata {
uint8_t min_value;
uint8_t max_value;
const char name[5];
const char* const* strings;
int16_t Clip(int16_t value) const {
if (value > max_value) {
value = max_value;
} else if (value < min_value) {
value = min_value;
}
return value;
}
};
class Settings {
public:
Settings() { }
~Settings() { }
void Init();
void Save();
void SetValue(Setting setting, uint8_t value) {
uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(&data_));
data[setting] = value;
}
uint8_t GetValue(Setting setting) const {
const uint8_t* data = static_cast<const uint8_t*>(
static_cast<const void*>(&data_));
return data[setting];
}
inline MacroOscillatorShape shape() const {
return static_cast<MacroOscillatorShape>(data_.shape);
}
inline Resolution resolution() const {
return static_cast<Resolution>(data_.resolution);
}
inline SampleRate sample_rate() const {
return static_cast<SampleRate>(data_.sample_rate);
}
inline PitchQuantization pitch_quantization() const {
return static_cast<PitchQuantization>(data_.pitch_quantization);
}
inline bool vco_flatten() const {
return data_.vco_flatten;
}
inline bool vco_drift() const {
return data_.vco_drift;
}
inline bool signature() const {
return data_.signature;
}
inline bool meta_modulation() const {
return data_.meta_modulation;
}
inline uint8_t trig_delay() const {
return data_.trig_delay;
}
inline const char* marquee_text() const {
return data_.marquee_text;
}
inline char* mutable_marquee_text() {
return data_.marquee_text;
}
inline const SettingsData& data() const { return data_; }
inline SettingsData* mutable_data() { return &data_; }
void Calibrate(
int32_t dac_code_c2,
int32_t dac_code_c4,
int32_t dac_code_fm) {
if (dac_code_c4 != dac_code_c2) {
int32_t scale = (24 * 128 * 4096L) / (dac_code_c4 - dac_code_c2);
data_.pitch_cv_scale = scale;
data_.pitch_cv_offset = (60 << 7) -
(scale * ((dac_code_c2 + dac_code_c4) >> 1) >> 12);
data_.fm_cv_offset = dac_code_fm;
}
Save();
}
inline int32_t dac_to_pitch(int32_t pitch_dac_code) const {
if (data_.pitch_range == PITCH_RANGE_EXTERNAL ||
data_.pitch_range == PITCH_RANGE_LFO) {
pitch_dac_code = pitch_dac_code * data_.pitch_cv_scale >> 12;
pitch_dac_code += data_.pitch_cv_offset;
} else if (data_.pitch_range == PITCH_RANGE_FREE) {
pitch_dac_code = (pitch_dac_code - 1638);
pitch_dac_code = pitch_dac_code * data_.pitch_cv_scale >> 12;
pitch_dac_code += 60 << 7;
} else if (data_.pitch_range == PITCH_RANGE_440) {
pitch_dac_code = 69 << 7;
} else {
pitch_dac_code = (pitch_dac_code - 1638) * 9 >> 1;
pitch_dac_code += 60 << 7;
}
return pitch_dac_code;
}
inline int32_t pitch_transposition() const {
int32_t t = data_.pitch_range == PITCH_RANGE_LFO ? -36 << 7 : 0;
t += (static_cast<int32_t>(data_.pitch_octave) - 2) * 12 * 128;
return t;
}
inline int32_t dac_to_fm(int32_t fm_dac_code) const {
fm_dac_code -= data_.fm_cv_offset;
fm_dac_code = fm_dac_code * 7680 >> 12;
if (data_.pitch_range == PITCH_RANGE_440) {
fm_dac_code = 0;
}
return fm_dac_code;
}
inline bool paques() const {
return paques_;
}
static const SettingMetadata& metadata(Setting setting) {
return metadata_[setting];
}
static const Setting setting_at_index(int16_t index) {
return settings_order_[index];
}
private:
void CheckPaques();
SettingsData data_;
uint16_t version_token_;
bool paques_;
static const SettingMetadata metadata_[SETTING_LAST];
static const Setting settings_order_[SETTING_LAST];
DISALLOW_COPY_AND_ASSIGN(Settings);
};
extern Settings settings;
} // namespace braids
#endif // BRAIDS_SETTINGS_H_