forked from neutral-labs/elmyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioUpdate.hpp
350 lines (294 loc) · 9.56 KB
/
ioUpdate.hpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifndef IOUPDATE_HPP
#define IOUPDATE_HPP
#include "synthCtx.hpp"
#define ENV_BYPASS (-1)
static inline int getValueFromTouchSensor(int input, int *oldValue, int smoothing_factor)
{
int newValue = analogRead(input);
*oldValue = (*oldValue * (smoothing_factor - 1) + newValue) / smoothing_factor;
return *oldValue;
}
static inline int getValueFromPot(int input, int *oldValue, int dead_zone, int smoothing_factor)
{
int newValue = analogRead(input);
if (newValue < *oldValue - dead_zone || newValue > *oldValue + dead_zone)
{
*oldValue = (*oldValue * (smoothing_factor - 1) + newValue) / smoothing_factor;
}
return *oldValue;
}
static inline int getValueFromPotTune(int input, int *oldValue, int dead_zone, int smoothing_factor)
{
int newValue = analogRead(input);
int factor;
if (newValue < *oldValue - 200 || newValue > *oldValue + 200)
{
factor = 5;
}
else
{
factor = 2000 / abs(*oldValue - newValue) + 2;
}
*oldValue = (*oldValue * (factor - 1) + newValue) / factor;
return *oldValue;
}
static inline int scalePotValue(int potValue, int scale)
{
return (potValue * scale) / POT_MAX;
}
static inline void updateInputTouch(synthCtx *ctx)
{
int i;
int old_touch_value_0 = ctx->touch_value[0];
ctx->touch_value[0] = getValueFromTouchSensor(PIN_IN_GSR_1, &ctx->touch_value[0], SMOOTHING_FACTOR_TOUCH);
ctx->touch_value[1] = getValueFromTouchSensor(PIN_IN_GSR_2, &ctx->touch_value[1], SMOOTHING_FACTOR_TOUCH);
ctx->touch_value[2] = getValueFromTouchSensor(PIN_IN_GSR_3, &ctx->touch_value[2], SMOOTHING_FACTOR_TOUCH);
if (ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RECORD_NUM])
{
if (old_touch_value_0 >= ENV_MIN && ctx->touch_value[0] < ENV_MIN)
{
ctx->sequencerDataTime[ctx->sequencerCurrentStep] = ctx->delay_time;
for (i = 0; i < NUM_VOICES; i++)
{
if (ctx->env_speed[i] != 1)
{
ctx->sequencerDataTune[i][ctx->sequencerCurrentStep] = ctx->osc_tune[i];
}
else
{
ctx->sequencerDataTune[i][ctx->sequencerCurrentStep] = 0;
}
}
ctx->sequencerCurrentStep++;
if (ctx->sequencerCurrentStep >= SEQUENCER_MAX_STEPS)
{
ctx->sequencerCurrentStep = 0;
ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RECORD_NUM] = 0;
}
}
}
#ifdef SERIAL_DEBUG_TOUCH
Serial.print("val1: ");
Serial.print(ctx->touch_value[0]);
Serial.print("\tval2: ");
Serial.print(ctx->touch_value[1]);
Serial.print("\tval3: ");
Serial.println(ctx->touch_value[2]);
#endif
}
static inline int updateSingleEnvSpeed(synthCtx *ctx, int voice, int input)
{
int stateChanged;
int state = ((digitalRead(input) == HIGH) ? 1 : ENV_SPEED_FACTOR);
if (ctx->envStateCountdown[voice] > 0)
{
ctx->envStateCountdown[voice]--;
}
if (ctx->envState[voice] != state)
{
stateChanged = 1;
ctx->envState[voice] = state;
ctx->envBypass[voice] = 0;
}
else
{
stateChanged = 0;
}
if (stateChanged)
{
if (ctx->envStateCountdown[voice] > 0)
{
ctx->envBypass[voice] = 1;
ctx->envStateCountdown[voice] = 0;
return ENV_BYPASS;
}
else
{
ctx->envStateCountdown[voice] = SPECIAL_MODE_ENABLE_TIME * IO_UPDATE_FREQ;
}
}
else
{
if (ctx->envBypass[voice])
{
return ENV_BYPASS;
}
}
ctx->envBypass[voice] = 0;
return state;
}
static inline void updateInputEnvSpeed(synthCtx *ctx)
{
ctx->env_speed[0] = updateSingleEnvSpeed(ctx, 0, PIN_IN_ENV_1);
ctx->env_speed[1] = updateSingleEnvSpeed(ctx, 1, PIN_IN_ENV_2);
ctx->env_speed[2] = updateSingleEnvSpeed(ctx, 2, PIN_IN_ENV_3);
}
static inline int updateSingleOscWave(synthCtx *ctx, int voice, int input)
{
int stateChanged;
int state = ((digitalRead(input) == HIGH) ? OSC_WAVE_SLEW_LOW : OSC_WAVE_SLEW_HIGH);
if (ctx->waveSpecialModeCountdown[voice] > 0)
{
ctx->waveSpecialModeCountdown[voice]--;
}
if (ctx->waveState[voice] != state)
{
stateChanged = 1;
ctx->waveState[voice] = state;
}
else
{
stateChanged = 0;
}
if (stateChanged)
{
if (ctx->waveSpecialModeCountdown[voice] > 0)
{
if (ctx->waveSpecialMode[voice] == 0)
{
ctx->waveSpecialMode[voice] = 1;
}
else
{
ctx->waveSpecialMode[voice] = 0;
}
ctx->waveSpecialModeCountdown[voice] = 0;
return state;
}
else
{
ctx->waveSpecialModeCountdown[voice] = SPECIAL_MODE_ENABLE_TIME * IO_UPDATE_FREQ;
}
}
return state;
}
static inline void updateInputOscWave(synthCtx *ctx)
{
int sequencerRunning = ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RUN_NUM];
int sequencerRecording = ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RECORD_NUM];
ctx->osc_slew[0] = updateSingleOscWave(ctx, 0, PIN_IN_WAVE_1);
ctx->osc_slew[1] = updateSingleOscWave(ctx, 1, PIN_IN_WAVE_2);
ctx->osc_slew[2] = updateSingleOscWave(ctx, 2, PIN_IN_WAVE_3);
if (sequencerRunning && ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RECORD_NUM])
{
ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RUN_NUM] = 0;
ctx->sequencerCurrentStep = 0;
return;
}
if (sequencerRecording && ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RUN_NUM])
{
ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RECORD_NUM] = 0;
ctx->sequencerMaxSteps = ctx->sequencerCurrentStep;
ctx->sequencerCurrentStep = 0;
return;
}
if (sequencerRecording && !ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RECORD_NUM])
{
ctx->sequencerMaxSteps = ctx->sequencerCurrentStep;
ctx->sequencerCurrentStep = 0;
return;
}
}
static inline void updateInputOscTune(synthCtx *ctx)
{
if (ctx->waveSpecialMode[SPECIAL_MODE_SCALE_NUM])
{
ctx->osc_tune[0] = scale[(getValueFromPotTune(PIN_IN_TUNE_1, &ctx->tune_value[0], POT_DEAD_ZONE_TUNE, SMOOTHING_FACTOR_TUNE) * SCALE_MAX) / POT_MAX];
ctx->osc_tune[1] = scale[(getValueFromPotTune(PIN_IN_TUNE_2, &ctx->tune_value[1], POT_DEAD_ZONE_TUNE, SMOOTHING_FACTOR_TUNE) * SCALE_MAX) / POT_MAX];
ctx->osc_tune[2] = scale[(getValueFromPotTune(PIN_IN_TUNE_3, &ctx->tune_value[2], POT_DEAD_ZONE_TUNE, SMOOTHING_FACTOR_TUNE) * SCALE_MAX) / POT_MAX];
}
else
{
ctx->osc_tune[0] = POT_TUNE_BASE + getValueFromPotTune(PIN_IN_TUNE_1, &ctx->tune_value[0], POT_DEAD_ZONE_TUNE, SMOOTHING_FACTOR_TUNE) * 100;
ctx->osc_tune[1] = POT_TUNE_BASE + getValueFromPotTune(PIN_IN_TUNE_2, &ctx->tune_value[1], POT_DEAD_ZONE_TUNE, SMOOTHING_FACTOR_TUNE) * 100;
ctx->osc_tune[2] = POT_TUNE_BASE + getValueFromPotTune(PIN_IN_TUNE_3, &ctx->tune_value[2], POT_DEAD_ZONE_TUNE, SMOOTHING_FACTOR_TUNE) * 100;
}
ctx->mod_value = scalePotValue(getValueFromPot(PIN_IN_MOD, &ctx->mod_value_raw, POT_DEAD_ZONE_NORMAL, SMOOTHING_FACTOR_NORMAL), OSC_MOD_AMOUNT_MAX);
}
static inline void updateOutputEnvLED(synthCtx *ctx)
{
digitalWrite(PIN_OUT_ENV_1, ctx->env_value[0] ? HIGH : LOW);
digitalWrite(PIN_OUT_ENV_2, ctx->env_value[1] ? HIGH : LOW);
digitalWrite(PIN_OUT_ENV_3, ctx->env_value[2] ? HIGH : LOW);
}
static inline void updateInputDelay(synthCtx *ctx)
{
int new_time;
if (ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RUN_NUM])
{
ctx->sequencerStepLen = SEQUENCER_MIN_STEP_LEN + ((SEQUENCER_MAX_STEP_LEN * getValueFromPot(PIN_IN_DELAY_TIME, &ctx->delay_time, POT_DEAD_ZONE_NORMAL, SMOOTHING_FACTOR_NORMAL)) / POT_MAX);
Serial.println(ctx->sequencerStepLen);
}
else
{
new_time = getValueFromPot(PIN_IN_DELAY_TIME, &ctx->delay_time, POT_DEAD_ZONE_NORMAL, SMOOTHING_FACTOR_NORMAL);
if (new_time < DELAY_TIME_FINETUNE_POINT)
{
new_time /= DELAY_TIME_FINETUNE_FACTOR;
}
else
{
new_time = DELAY_POT_SCALE_TIME + (new_time - DELAY_TIME_FINETUNE_POINT);
}
new_time = (DELAY_POT_SCALE_TIME * new_time) / (POT_MAX - (DELAY_TIME_FINETUNE_POINT / DELAY_TIME_FINETUNE_FACTOR));
ctx->dly.setTime(new_time);
}
ctx->dly.setFeedback(scalePotValue(getValueFromPot(PIN_IN_DELAY_FEEDBACK, &ctx->delay_feedback, POT_DEAD_ZONE_NORMAL, SMOOTHING_FACTOR_NORMAL), DELAY_POT_SCALE_FEEDBACK));
ctx->delay_wet = scalePotValue(getValueFromPot(PIN_IN_DELAY_MIX, &ctx->delay_wet_raw, POT_DEAD_ZONE_NORMAL, SMOOTHING_FACTOR_NORMAL), DELAY_POT_SCALE_MIX);
}
static inline void updateFromSequencer(synthCtx *ctx)
{
int i;
if (ctx->waveSpecialMode[SPECIAL_MODE_SEQUENCER_RUN_NUM])
{
ctx->dly.setTime(ctx->sequencerDataTime[ctx->sequencerCurrentStep]);
for (i = 0; i < NUM_VOICES; i++)
{
if (ctx->sequencerDataTune[i][ctx->sequencerCurrentStep] != 0)
{
ctx->osc_tune[i] = ctx->sequencerDataTune[i][ctx->sequencerCurrentStep];
}
}
if (++ctx->sequencerIntraStepCount >= ctx->sequencerStepLen)
{
ctx->sequencerIntraStepCount = 0;
ctx->sequencerCurrentStep++;
if (ctx->sequencerCurrentStep >= ctx->sequencerMaxSteps)
{
ctx->sequencerCurrentStep = 0;
}
}
}
}
static inline void updateVoices(synthCtx *ctx)
{
int i;
for (i = 0; i < NUM_VOICES; i++)
{
ctx->osc[i].setSlew(ctx->osc_slew[i]);
ctx->osc[i].setFreq(ctx->osc_tune[i]);
ctx->osc[i].setModAmount(ctx->mod_value);
if (ctx->env_speed[i] == ENV_BYPASS)
{
ctx->env_value[i] = AMP_MAX;
}
else
{
ctx->env[i].setAttack(ENV_ATTACK * ctx->env_speed[i]);
ctx->env[i].setRelease(ENV_RELEASE * ctx->env_speed[i]);
ctx->env_value[i] = ctx->env[i].getLevel(ctx->touch_value[i]);
}
}
}
void ioUpdate(synthCtx *ctx)
{
updateInputTouch(ctx);
updateInputEnvSpeed(ctx);
updateInputOscWave(ctx);
updateInputOscTune(ctx);
updateInputDelay(ctx);
updateFromSequencer(ctx);
updateVoices(ctx);
updateOutputEnvLED(ctx);
}
#endif