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
|
#include <cstdlib>
#include <fstream>
#include "parser.h"
#include "programs.h"
#include "util.h"
#include "../synth/defines.h"
#include "../synth/fixed.h"
void init_oscs(int n_osc, oscillator_t *osc)
{
for (int i=0;i<n_osc;i++)
{
osc[i].n_osc=n_osc;
osc[i].fm_strength=new fixed_t[n_osc];
osc[i].fm_strength_const=new bool[n_osc];
for (int j=0;j<n_osc;j++)
{
osc[i].fm_strength[j]=0;
osc[i].fm_strength_const[j]=true;
}
osc[i].output=0;
osc[i].output_const=true;
osc[i].output_no_pfactor=false;
osc[i].waveform=0;
osc[i].waveform_const=true;
osc[i].factor=ONE;
osc[i].factor_const=true;
osc[i].tremolo_depth=0;
osc[i].tremolo_depth_const=true;
osc[i].tremolo_lfo=0;
osc[i].tremolo_lfo_const=true;
osc[i].vibrato_depth=0;
osc[i].vibrato_depth_const=true;
osc[i].vibrato_lfo=0;
osc[i].vibrato_lfo_const=true;
osc[i].have_custom_wave=false;
osc[i].sync=false;
osc[i].sync_const=true;
osc[i].ksr_const=true;
osc[i].ksl_const=true;
}
}
void init_envs(int n_osc, env_settings_t *env)
{
for (int i=0;i<n_osc;i++)
{
env[i].enabled=true;
env[i].attack=0;
env[i].attack_const=true;
env[i].decay=0;
env[i].decay_const=true;
env[i].sustain=1.0;
env[i].sustain_const=true;
env[i].release=0;
env[i].release_const=true;
env[i].hold=true;
env[i].hold_const=true;
}
}
void init_filter(filter_params_t &filter)
{
filter.enabled=false;
filter.enabled_const=true;
filter.env_amount=0;
filter.env_amount_const=true;
filter.env_settings.attack=0;
filter.env_settings.attack_const=true;
filter.env_settings.decay=0;
filter.env_settings.decay_const=true;
filter.env_settings.release=0;
filter.env_settings.release_const=true;
filter.env_settings.sustain=0;
filter.env_settings.sustain_const=true;
filter.env_settings.hold=true;
filter.env_settings.hold_const=true;
filter.freqfactor_offset=0;
filter.freqfactor_offset_const=true;
filter.resonance=0;
filter.resonance_const=true;
filter.trem_strength=0;
filter.trem_strength_const=true;
filter.trem_lfo=0;
filter.trem_lfo_const=true;
}
string extract_array_name(string s)
{
size_t p;
p=s.find('[');
if (p!=string::npos)
return s.substr(0,p);
else
return s;
}
int extract_array_index(string s, int dim)
{
size_t p=-1,p2;
for (int i=0;i<dim;i++)
{
p=s.find('[',p+1);
if (p==string::npos) return -1;
}
p2=s.find(']',p+1);
if (p2==string::npos) return -1;
return atoi(s.substr(p+1,p2-p-1).c_str());
}
program_t parse(string fn)
{
int n_osc=0;
oscillator_t *osc=NULL;
env_settings_t *env=NULL;
filter_params_t filter;
fixed_t sync_factor=0;
bool sync_factor_const=true;
char buf[2000];
string line;
string var;
string array;
string strval;
float val;
parameter_enum p;
int ind,ind2=0;
int state;
program_t result;
ifstream f;
f.open(fn.c_str());
if (f.good())
{
state=0;
while (!f.eof())
{
f.getline(buf,sizeof(buf)/sizeof(*buf)-1);
line=buf;
line=remove_all_spaces(buf);
if ((line!="") && (line[0]!='#')) //ignore comments and empty lines
{
if (line=="controllers:")
{
state=2;
continue;
}
else if (line=="defaults:")
{
state=3;
continue;
}
else if (line=="velocity:")
{
state=4;
continue;
}
else if (line=="variable:")
{
state=5;
continue;
}
var=extract_var(line);
array=extract_array_name(var);
strval=extract_val(line);
val=atof(strval.c_str());
switch (state)
{
case 0: //expect and read number of oscillators
if (var!="oscillators")
throw string("need to know number of oscillators");
else
n_osc=val;
if (n_osc<=0) throw string("invalid number of oscillators");
//init stuff
env=new env_settings_t[n_osc];
osc=new oscillator_t[n_osc];
init_oscs(n_osc, osc);
init_envs(n_osc, env);
init_filter(filter);
state=1;
break;
case 1: //read and set information about oscillator settings
p=param_to_enum(array);
ind=extract_array_index(var,1);
if ( param_needs_index(p) && (!((ind>=0) && (ind<n_osc))) )
throw string("out of array bounds");
switch (p)
{
case MODULATION:
ind2=extract_array_index(var,2);
if (!((ind2>=0) && (ind2<n_osc)))
throw string("out of array bounds");
osc[ind].fm_strength[ind2]=val*ONE;
break;
case OUTPUT:
osc[ind].output=val*ONE;
break;
case WAVEFORM:
if (isfloat(strval))
{
osc[ind].waveform=int(val);
}
else
{
osc[ind].have_custom_wave=true;
}
break;
case FACTOR:
osc[ind].factor=val*ONE;
break;
case TREMOLO:
osc[ind].tremolo_depth=int(val);
break;
case TREM_LFO:
if (strval=="snh")
osc[ind].tremolo_lfo=SNH_LFO;
else
{
osc[ind].tremolo_lfo= int(val);
if ((val<0) || (val>=N_LFOS))
throw string("invalid value for tremolo_lfo");
}
break;
case VIBRATO:
osc[ind].vibrato_depth=val;
break;
case VIB_LFO:
if (strval=="snh")
osc[ind].vibrato_lfo= SNH_LFO;
else
{
osc[ind].vibrato_lfo= int(val);
if ((val<0) || (val>=N_LFOS))
throw string("invalid value for vibrato_lfo");
}
break;
case ATTACK:
env[ind].attack=val;
break;
case DECAY:
env[ind].decay=val;
break;
case SUSTAIN:
env[ind].sustain=val;
break;
case RELEASE:
env[ind].release=val;
break;
case HOLD:
env[ind].hold=(val!=0);
break;
case KSR:
osc[ind].ksr=val;
break;
case KSL:
osc[ind].ksl=val;
break;
case SYNC:
osc[ind].sync=(val!=0);
break;
case FILTER_ENABLED:
filter.enabled=(val!=0);
break;
case FILTER_ENV_AMOUNT:
filter.env_amount=val;
break;
case FILTER_ATTACK:
filter.env_settings.attack=val;
break;
case FILTER_DECAY:
filter.env_settings.decay=val;
break;
case FILTER_SUSTAIN:
filter.env_settings.sustain=val;
break;
case FILTER_RELEASE:
filter.env_settings.release=val;
break;
case FILTER_HOLD:
filter.env_settings.hold=(val!=0);
break;
case FILTER_OFFSET:
filter.freqfactor_offset=val;
break;
case FILTER_RESONANCE:
filter.resonance=val;
break;
case FILTER_TREMOLO:
filter.trem_strength=int(val);
break;
case FILTER_TREM_LFO:
if (strval=="snh")
filter.trem_lfo=SNH_LFO;
else
{
filter.trem_lfo=int(val);
if ((val<0) || (val>=N_LFOS))
throw string("invalid value for filter_trem_lfo");
}
break;
case SYNC_FACTOR:
sync_factor=val*ONE;
break;
default:
throw string("unknown variable ('"+array+"')");
}
break;
case 5: //read which params shall be variable, even if
//there are currently no controllers which change them
case 4: //read velocity-influence over certain params
case 2: //read how controllers influence parameters
p=param_to_enum(array);
ind=extract_array_index(var,1);
if ( param_needs_index(p) && (!((ind>=0) && (ind<n_osc))) )
throw string("out of array bounds");
if (state==4) //velocity-influence
{
switch (p)
{
case MODULATION:
case OUTPUT:
case FILTER_ENV_AMOUNT:
case FILTER_RESONANCE:
case FILTER_OFFSET:
// everything ok, do nothing
break;
default: // other params than the above may not be influenced!
throw string("velocity cannot influence parameter '"+array+"'");
}
}
switch (p)
{
case MODULATION:
ind2=extract_array_index(var,2);
if (!((ind2>=0) && (ind2<n_osc)))
throw string("out of array bounds");
osc[ind].fm_strength_const[ind2]=false;
break;
case OUTPUT:
if (state==4) // vel.-influence
{
if (isfloat(strval)) //is it a plain number, not a formula?
osc[ind].output_no_pfactor=true;
}
else
osc[ind].output_const=false; break;
case WAVEFORM:
osc[ind].waveform_const=false; break;
case FACTOR:
osc[ind].factor_const=false; break;
case TREMOLO:
osc[ind].tremolo_depth_const=false; break;
case TREM_LFO:
osc[ind].tremolo_lfo_const=false; break;
case VIBRATO:
osc[ind].vibrato_depth_const=false; break;
case VIB_LFO:
osc[ind].vibrato_lfo_const=false; break;
case ATTACK:
env[ind].attack_const=false; break;
case DECAY:
env[ind].decay_const=false; break;
case SUSTAIN:
env[ind].sustain_const=false; break;
case RELEASE:
env[ind].release_const=false; break;
case HOLD:
env[ind].hold_const=false; break;
case KSR:
osc[ind].ksr_const=false; break;
case KSL:
osc[ind].ksl_const=false; break;
case SYNC:
osc[ind].sync_const=false; break;
case FILTER_ENABLED:
filter.enabled_const=false; break;
case FILTER_ENV_AMOUNT:
filter.env_amount_const=false; break;
case FILTER_ATTACK:
filter.env_settings.attack_const=false; break;
case FILTER_DECAY:
filter.env_settings.decay_const=false; break;
case FILTER_SUSTAIN:
filter.env_settings.sustain_const=false; break;
case FILTER_RELEASE:
filter.env_settings.release_const=false; break;
case FILTER_HOLD:
filter.env_settings.hold_const=false; break;
case FILTER_OFFSET:
filter.freqfactor_offset_const=false; break;
case FILTER_RESONANCE:
filter.resonance_const=false; break;
case FILTER_TREMOLO:
filter.trem_strength_const=false; break;
case FILTER_TREM_LFO:
filter.trem_lfo_const=false; break;
case SYNC_FACTOR:
sync_factor_const=false; break;
default:
throw string("unknown variable ('"+array+"')");
}
break;
case 3: //read controller default values
//ignored
break;
}
}
}
//some optimizations and checks
for (int i=0;i<n_osc;i++)
if ((env[i].attack==0) && (env[i].sustain==1.0)
&& (env[i].release>100)) //TODO FINDMICH besseres kriterium?
env[i].enabled=false;
if ( ((filter.env_settings.attack==0) && (filter.env_settings.sustain==1.0)
&& (filter.env_settings.release>100)) //TODO FINDMICH siehe oben
|| ((filter.env_amount==0) && (filter.env_amount_const==true)) )
filter.env_settings.enabled=false;
bool use_sync=false;
for (int i=0;i<n_osc;i++)
if ((osc[i].sync==true) || (osc[i].sync_const==false))
{
use_sync=true;
break;
}
if (!use_sync)
{
sync_factor=0;
sync_factor_const=true;
}
if ((sync_factor==0) && (sync_factor_const==true))
for (int i=0;i<n_osc;i++)
{
osc[i].sync=false;
osc[i].sync_const=true;
}
for (int i=0;i<n_osc;i++)
if ( (osc[i].output_no_pfactor==false) && !((osc[i].output==0) && (osc[i].output_const=true)) )
osc[i].output_const=false;
//end optimizations and checks
result.n_osc=n_osc;
result.osc=osc;
result.env=env;
result.filter=filter;
result.sync_factor=sync_factor;
result.sync_factor_const=sync_factor_const;
}
else
throw string ("could not open '"+fn+"'");
return result;
//no uninit / free of osc and env here, as it must be done by the caller
}
|