-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfo.c
More file actions
530 lines (377 loc) · 13.8 KB
/
lfo.c
File metadata and controls
530 lines (377 loc) · 13.8 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
@file lfo.c
@brief
This contains the function for the LFO which modifies a parameter using an oscillator waveform.
The LFO modifies one of the values using the oscillator.
The rate is set by the LFO_RATE parameter.
The amount of modification is set by the LFO_AMOUNT parameter.
The LFO takes its value to modify from one of two sources depending on whether or not the target value has
been modified by the user - whether or not the value has been modified is stored.
If the value has not been modified after loading a new patch, the value is taken from the loaded patch array
If the value has been modified, which we know by an AD value being updated, the value is taken from the AD reading
@ Created by Matt Heins, HackMe Electronics, 2011
This file is part of Rockit.
Rockit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Rockit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rockit. If not, see <http://www.gnu.org/licenses/>
*/
#include "eight_bit_synth_main.h"
#include "lfo.h"
#include "wavetables.h"
#include "midi.h"
#include "drone_loop.h"
/*This array is a decoder for which synth parameter is being effected by the
LFO. To get it to access different paramters, make changes here.*/
const unsigned char auc_lfo_dest_decode[2][6] = {{AMPLITUDE,
FILTER_FREQUENCY,
FILTER_Q,
FILTER_ENV_AMT,
PITCH_SHIFT,
OSC_DETUNE},
{OSC_MIX,
FILTER_FREQUENCY,
FILTER_Q,
LFO_1_RATE,
LFO_1_AMOUNT,
FILTER_ATTACK}};
/*Want faster or slower, muck with this*/
const unsigned int g_aun_lfo_rate_lut[32]= {1,2,4,8,16,32,48,64,80,96,112,128,192,
224,256,288,320,352,384,448,
512,576,640,704,778,896,1024,
1280,1536,2048,2560,3072};
void lfo(g_setting *p_global_setting)
{
static unsigned int aun_lfo_reference[NUMBER_OF_LFOS];
signed int sn_temp1;
static unsigned char uc_old_value;
unsigned char uc_temp1,
uc_temp2,
uc_lfo_dest,
uc_lfo_initial_param = 0,
uc_lfo_amount,
uc_modifier = 0,
uc_wave_shape,
uc_parameter_source,
lfsr_bit,
uc_reverse_index,
uc_midi_status_byte;
unsigned int un_lfo_rate,
un_modifier_calc;
static unsigned int lfsr = 0xACE1;
static unsigned char uc_morph_index,
uc_morph_timer,
uc_morph_state;
/*Sync the lfos by resetting the reference if the lfo sync parameter is set and the
flag denoting a note on is set*/
if(p_global_setting->auc_synth_params[LFO_SYNC] == 1 &&
g_uc_lfo_midi_sync_flag == 1)
{
aun_lfo_reference[LFO_1] = 0;
aun_lfo_reference[LFO_2] = 0;
g_uc_lfo_midi_sync_flag = 0;
uc_morph_timer = 0;
uc_morph_index = 0;
uc_morph_state = 0;
}
/*We have two LFOs, or more if you choose, but only one set of controls for the LFOs.
Only one is active at a time. The active one stores the rate and amount values in its
array. The inactive one only loads its values from the LFO parameter arrays.
The destinations and waveshapes are updated in the led_switch_handler function.*/
for(unsigned char i = 0; i <= 1; i++)
{
if(i == LFO_1)
{
uc_lfo_amount = p_global_setting->auc_synth_params[LFO_1_AMOUNT];//how much the parameter will vary
uc_temp1 = p_global_setting->auc_synth_params[LFO_1_RATE]>>3;//the rate is in a lookup table because the
uc_lfo_dest = auc_lfo_dest_decode[i][p_global_setting->auc_synth_params[LFO_1_DEST]]; //values 1-255 aren't sufficient
un_lfo_rate = g_aun_lfo_rate_lut[uc_temp1];
uc_wave_shape = p_global_setting->auc_synth_params[LFO_1_WAVESHAPE];
}
else
{
uc_lfo_amount = p_global_setting->auc_synth_params[LFO_2_AMOUNT];//how much the parameter will vary
uc_temp1 = p_global_setting->auc_synth_params[LFO_2_RATE]>>3;//the rate is in a lookup table because the
uc_lfo_dest = auc_lfo_dest_decode[i][p_global_setting->auc_synth_params[LFO_2_DEST]]; //values 1-255 aren't sufficient
un_lfo_rate = g_aun_lfo_rate_lut[uc_temp1];
uc_wave_shape = p_global_setting->auc_synth_params[LFO_2_WAVESHAPE];
}
/*The waveshape parameter is read from a pot which has a range of 0-255,
but there are only 16 waveshapes, so divide by 16*/
uc_wave_shape >>= 4;
uc_parameter_source = p_global_setting->auc_parameter_source[uc_lfo_dest];
switch(uc_parameter_source)
{
case SOURCE_AD:
uc_lfo_initial_param = p_global_setting->auc_ad_values[uc_lfo_dest];
break;
case SOURCE_LOOP:
uc_lfo_initial_param = get_loop_stored_parameter(i);
break;
case SOURCE_EXTERNAL:
uc_lfo_initial_param = p_global_setting->auc_external_params[uc_lfo_dest];
break;
default:
uc_lfo_initial_param = 0;
break;
}
sn_temp1 = uc_lfo_initial_param;
if(uc_lfo_amount > 0)
{
//get the value to modify, determined by looking at which value and whether that bit has been set
//in the unModifiedValueFlag bit vector
//the unModifiedValueFlag is set by the readAD algorithm, a 1 means the value has been changed
//from the loaded value, 0 means that we use the value loaded from eeprom
//the bit we look at is the destination of the LFO
//now that we have the value, modify it using the oscillator algorithm
//we update the sample reference using two
switch(uc_wave_shape)
{
case SQUARE:
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_modifier = G_AUC_SQUARE_SIMPLE_WAVETABLE_LUT[uc_temp1];
break;
case RAMP:
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_modifier = G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_temp1];
break;
case TRIANGLE:
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_modifier = G_AUC_TRIANGLE_SIMPLE_WAVETABLE_LUT[uc_temp1];
break;
case SIN:
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_modifier = G_AUC_SIN_LUT[uc_temp1];
break;
case MORPH_1:
if(uc_morph_timer == 0)
{
uc_morph_index++;
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
un_modifier_calc = G_AUC_SIN_LUT[uc_temp1];
un_modifier_calc = un_modifier_calc * G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_morph_index];
un_modifier_calc = un_modifier_calc >> 8;
uc_modifier = (unsigned char) un_modifier_calc;
break;
case MORPH_2:
if(uc_morph_timer == 0)
{
uc_morph_index++;
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
un_modifier_calc = G_AUC_TRIANGLE_SIMPLE_WAVETABLE_LUT[uc_temp1];
uc_temp1 = uc_morph_index >> 1;
un_modifier_calc = un_modifier_calc * G_AUC_HARDSYNC_2_SIMPLE_WAVETABLE_LUT[uc_temp1];
un_modifier_calc = un_modifier_calc >> 8;
uc_modifier = (unsigned char) un_modifier_calc;
break;
case MORPH_3:
if(uc_morph_timer == 0)
{
uc_morph_index++;
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_reverse_index = uc_temp1- uc_morph_index;
uc_temp1 = G_AUC_TRIANGLE_SIMPLE_WAVETABLE_LUT[uc_temp1];
sn_temp1 = uc_temp1 - G_AUC_SQUARE_SIMPLE_WAVETABLE_LUT[uc_reverse_index];
/*Now we'll have a positive or negative number. We have to center it around 127 and make sure
that the sample is never going to be over 255 or less than 0.*/
if(sn_temp1 > 127)
{
uc_modifier = 255;
}
else if(sn_temp1 < -128)
{
uc_modifier = 0;
}
else
{
uc_modifier = 128 + sn_temp1;
}
break;
case MORPH_4:
if(uc_morph_timer == 0)
{
uc_morph_index++;
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
un_modifier_calc = G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_temp1];
un_modifier_calc = un_modifier_calc * G_AUC_TRIANGLE_SIMPLE_WAVETABLE_LUT[uc_morph_index];
un_modifier_calc = un_modifier_calc >> 8;
uc_modifier = (unsigned char) un_modifier_calc;
break;
case MORPH_5:
if(uc_morph_timer == 0)
{
if(uc_morph_state == 0)
{
uc_morph_index++;
if(uc_morph_index == 255)
{
uc_morph_state = 1;
}
}
else
{
uc_morph_index--;
if(uc_morph_index == 0)
{
uc_morph_state = 0;
}
}
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_reverse_index = uc_temp1- uc_morph_index;
uc_temp1 = G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_temp1];
sn_temp1 = uc_temp1 - G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_reverse_index];
/*Now we'll have a positive or negative number. We have to center it around 127 and make sure
that the sample is never going to be over 255 or less than 0.*/
if(sn_temp1 > 127)
{
uc_modifier = 255;
}
else if(sn_temp1 < -128)
{
uc_modifier = 0;
}
else
{
uc_modifier = 128 + sn_temp1;
}
break;
case MORPH_6:
if(uc_morph_timer == 0)
{
uc_morph_index++;
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
un_modifier_calc = G_AUC_HARDSYNC_1_SIMPLE_WAVETABLE_LUT[uc_temp1];
un_modifier_calc = un_modifier_calc * G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_morph_index];
un_modifier_calc = un_modifier_calc >> 8;
uc_modifier = (unsigned char) un_modifier_calc;
break;
case MORPH_7://reverse ramp presently
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_temp1 = 255 - uc_temp1;
uc_modifier = G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_temp1];
break;
case MORPH_8:
/*This morphing waveshape is a square wave with varying pulse width.*/
if(uc_morph_timer == 0)
{
uc_morph_index++;
uc_morph_timer = p_global_setting->auc_synth_params[LFO_1_RATE] >> 3;
}
uc_morph_timer--;
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_reverse_index = uc_temp1- uc_morph_index;
uc_temp1 = G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_temp1];
sn_temp1 = uc_temp1 - G_AUC_RAMP_SIMPLE_WAVETABLE_LUT[uc_reverse_index];
/*Now we'll have a positive or negative number. We have to center it around 127 and make sure
that the sample is never going to be over 255 or less than 0.*/
if(sn_temp1 > 127)
{
uc_modifier = 255;
}
else if(sn_temp1 < -128)
{
uc_modifier = 0;
}
else
{
uc_modifier = 128 + sn_temp1;
}
break;
case MORPH_9:
uc_temp1 = aun_lfo_reference[i] >> 8;
uc_modifier = G_AUC_HARDSYNC_1_SIMPLE_WAVETABLE_LUT[uc_temp1];
break;
case HARD_SYNC:
uc_temp1 = aun_lfo_reference[i] >> 8;
uc_modifier = G_AUC_HARDSYNC_2_SIMPLE_WAVETABLE_LUT[uc_temp1];
break;
case NOISE:
/*A pseudo random number is generated using a linear feedback shift register
The polynomial expression used is: x^16 + x^14 + x^13 + x^11 + 1*/
lfsr_bit = ((lfsr >> 15) ^ (lfsr >> 13) ^ (lfsr ^ 12) ^ (lfsr >> 10)) & 1;
lfsr = (lfsr << 1) | (lfsr_bit);
uc_modifier = (unsigned char) lfsr;
break;
case RAW_SQUARE:
uc_temp1 = aun_lfo_reference[i] >> 8;
if(uc_temp1 > 127)
{
uc_modifier = 0;
}
else
{
uc_modifier = 255;
}
break;
default:
//Default to sin
uc_temp1 = aun_lfo_reference[i] >> 7;
uc_modifier = G_AUC_SIN_LUT[uc_temp1];
break;
}
//modify that value based on the current lfo Reference value and the LFO amount parameter
//multiply and divide scaling to get the value
sn_temp1 = uc_modifier;//max value 255
sn_temp1 -= 128;//from -128 to 127
sn_temp1 *= uc_lfo_amount;//-32640 to 32385
sn_temp1 = sn_temp1 >> 7;
//store the modified value in the Synth Params array
sn_temp1 += uc_lfo_initial_param;
if(sn_temp1 > 255)
{
sn_temp1 = 255;
}
else if(sn_temp1 < 0)
{
sn_temp1 = 0;
}
p_global_setting->auc_synth_params[uc_lfo_dest] = sn_temp1;
//Since a parameter changed, put the change in the outgoing MIDI FIFO to be transmitted
//as a MIDI message
if(uc_old_value != (unsigned char) sn_temp1)
{
uc_old_value = (unsigned char) sn_temp1;
sn_temp1 = sn_temp1 >> 1;
uc_temp2 = uc_lfo_dest + MIDI_CONTROLLER_0_INDEX;
uc_midi_status_byte = MESSAGE_TYPE_CONTROL_CHANGE | get_midi_channel_number();
put_midi_message_in_outgoing_fifo( uc_midi_status_byte,
uc_lfo_dest,
sn_temp1);
}
}
// When LFO amount is 0, do nothing - let other systems manage the parameter
// (AD reader, patch loader, MIDI CC, ADSR envelopes, etc.)
/*The LFO is ordinarily free running but will sychronize by zeroing out
the LFO reference if the midi sync flag is set.
increment the reference for the oscillator*/
aun_lfo_reference[i] += un_lfo_rate;
if(aun_lfo_reference[i] >= SAMPLE_MAX)
{
aun_lfo_reference[i] -= SAMPLE_MAX;
}
}
}