summaryrefslogtreecommitdiff
path: root/synth/parser.cpp
blob: 1b8befb9bdf135821328587fe030561b0bb9d917 (plain)
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#include <cstdlib>
#include <fstream>
#include <cmath>

#include "parser.h"
#include "defines.h"
#include "programs.h"
#include "globals.h"
#include "util.h"
#include "readwave.h"


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());
}

list<string> extract_terms(string s)
{
	list<string> result;
	
	size_t p=-1,p2;
	
	s="+"+s+"+";
	
	p=0;
	p2=s.find_first_of("+-",p+1);
	
	while(p2!=string::npos)
	{
		result.push_back(s.substr(p,p2-p));	
		p=p2;
		p2=s.find_first_of("+-",p+1);
	}
	return result;
}

list<string> extract_factors(string s)
{
	list<string> result;
	
	size_t p=-1,p2;
	
	s="*"+s+"*";
	
	p=0;
	p2=s.find_first_of("*/",p+1);
	
	while(p2!=string::npos)
	{
		result.push_back(s.substr(p,p2-p));		
		p=p2;
		p2=s.find_first_of("*/",p+1);
	}
	return result;
}

list<term_t> extract_formula(string s)
{
	list<term_t> result;
	term_t tmp;
	list<string> terms=extract_terms(s);
	
	for (list<string>::iterator term=terms.begin(); term!=terms.end(); ++term)
	{
		list<string> factors=extract_factors(term->substr(1));
		double fac=	((*term)[0]=='+') ? 1.0 : -1.0;
		string cont="";
		for (list<string>::iterator factor=factors.begin(); factor!=factors.end(); ++factor)
		{
			if (factor->find_first_not_of("0123456789.*/+-")==string::npos)
			{
				if ((*factor)[0]=='*')
					fac*=atof((*factor).substr(1).c_str());
				else
				{
					if (atof((*factor).substr(1).c_str())==0)
						throw string("dividing by zero is not allowed");
			
					fac/=atof((*factor).substr(1).c_str());
				}
			}
			else
			{
				if (cont!="")
					throw string("multiplicating controllers is not allowed");
				
				if ((*factor)[0]!='*')
					throw string("dividing through a controller is not allowed");
				
				cont=(*factor).substr(1);
			}
		}
		if (cont=="")
			tmp.c=NO_CONT;
		else
		{
			if (extract_array_name(cont)!="cont")
				throw string("expected 'cont', found '"+extract_array_name(cont)+"'");

			tmp.c=extract_array_index(cont,1);
			if ((tmp.c<0) || (tmp.c>127))
				throw string("invalid controller specified");
		}		
		tmp.f=fac*ONE;
		result.push_back(tmp);
	}
	return result;
}

param_factor_t parse_pfactor(string s) //TODO fast dasselbe wie oben. mergen?
{ //TODO cont müsste vel heißen       FINDMICH --->  ^ ^ ^
	param_factor_t result;
	result.offset=0;
	result.vel_amount=0;
	
	list<string> terms=extract_terms(s);
	
	for (list<string>::iterator term=terms.begin(); term!=terms.end(); ++term)
	{
		list<string> factors=extract_factors(term->substr(1));
		double fac=	((*term)[0]=='+') ? 1.0 : -1.0;
		string cont="";
		for (list<string>::iterator factor=factors.begin(); factor!=factors.end(); ++factor)
		{
			if (factor->find_first_not_of("0123456789.*/+-")==string::npos)
			{
				if ((*factor)[0]=='*')
					fac*=atof((*factor).substr(1).c_str());
				else
				{
					if (atof((*factor).substr(1).c_str())==0)
						throw string("dividing by zero is not allowed");
						
					fac/=atof((*factor).substr(1).c_str());
				}
			}
			else
			{
				if (cont!="")
					throw string("multiplicating velocity is not allowed");
				
				if ((*factor)[0]!='*')
					throw string("dividing through velocity is not allowed");
				
				cont=(*factor).substr(1);
			}
		}
		if (cont=="")
		{
			result.offset+= fac*ONE;
		}
		else
		{
			if (cont!="vel")
				throw string("expected 'vel', found '"+cont+"'");
			
			result.vel_amount+= fac*ONE;
		}		
	}
	return result;
}

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];
		for (int j=0;j<n_osc;++j)
			osc[i].fm_strength[j]=0;
		
		osc[i].output=0;
		osc[i].waveform=0;
		osc[i].factor=ONE;
		osc[i].tremolo_depth=0;
		osc[i].tremolo_lfo=0;
		osc[i].vibrato_depth=0;
		osc[i].vibrato_lfo=0;
		osc[i].custom_wave=NULL;
		
		osc[i].freq_env_amount=0;
		osc[i].freq_env.attack=0;
		osc[i].freq_env.decay=0;
		osc[i].freq_env.sustain=ONE;
		osc[i].freq_env.release=-1;
		osc[i].freq_env.hold=true;
	}
}

void init_envs(int n_osc, env_settings_t *env)
{
	for (int i=0;i<n_osc;++i)
	{
		env[i].attack=0;
		env[i].decay=0;
		env[i].sustain=ONE;
		env[i].release=0;
		env[i].hold=true;
	}
}

void init_filter(filter_params_t &filter)
{
	filter.enabled=false;
	filter.env_amount=0;
	filter.env_settings.attack=filter.env_settings.decay=
	   filter.env_settings.release=0;
	filter.env_settings.sustain=0;
	filter.env_settings.hold=true;
	
	filter.freqfactor_offset=0;
	filter.resonance=0;
	filter.trem_strength=0;
	filter.trem_lfo=0;
}

void init_pfactors(int n_osc, pfactor_formula_t &pfactor)
{
	pfactor.out=new param_factor_t [n_osc];
	pfactor.freq_env_amount=new param_factor_t [n_osc];
	pfactor.fm=new param_factor_t* [n_osc];
	
	pfactor.filter_env.offset=ONE;
	pfactor.filter_env.vel_amount=0;
	
	pfactor.filter_res.offset=ONE;
	pfactor.filter_res.vel_amount=0;
	
	pfactor.filter_offset.offset=ONE;
	pfactor.filter_offset.vel_amount=0;
	
	for (int i=0;i<n_osc;++i)
	{
		pfactor.out[i].offset=0;
		pfactor.out[i].vel_amount=ONE;
		
		pfactor.freq_env_amount[i].offset=0;
		pfactor.freq_env_amount[i].vel_amount=ONE;
		
		pfactor.fm[i]=new param_factor_t [n_osc];
		for (int j=0;j<n_osc;++j)
		{
			pfactor.fm[i][j].offset=ONE;
			pfactor.fm[i][j].vel_amount=0;			
		}
	}
}






//if this function fails, this WILL BE fatal if unhandled in the
//caller function. so this function throws errors
//if this function fails the settings are in an undefined, illegal
//state. if these settings are given to some oscillator_t by
//operator=, it will probably die while trying to create an array
//with size 0 or so.
program_t parse(string fn)
{
	int n_osc=0;
	oscillator_t *osc=NULL;
	env_settings_t *env=NULL;

	set<parameter_t> affect[128];
	map< parameter_t, list<term_t> > formula;
	int controller_default[128];

	for (int i=0;i<128;++i)
		controller_default[i]=0;

	filter_params_t filter;
	
	pfactor_formula_t pfactor;
	
	fixed_t sync_factor=0;


	
	
	char buf[2000];
	list<term_t> terms;
	string line;
	string var;
	string array;
	string strval;
	float val;
	
	parameter_enum p;
	
	int ind,ind2=0;
	
	int state;
	
	ifstream f;
	f.open(fn.c_str());
	if (!f.good())
		throw string ("could not open '"+fn+"'");
	//else
		
	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;
			}

			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);
					init_pfactors(n_osc,pfactor);
					
					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 (isnum(strval))
							{
								osc[ind].waveform=int(val);
							}
							else
							{
								size_t pos=strval.find(':');
								if (pos==string::npos)
									throw string("expected 'freq:file.wav', found no ':'");
								
								float given_freq=atof(strval.substr(0,pos).c_str());									
								string wavefile=strval.substr(pos+1);

								if (given_freq<=0)
									throw string("illegal freq specified for custom wave '"+wavefile+"'");
								
								osc[ind].custom_wave=new custom_wave_t;
								read_wave(wavefile.c_str(), osc[ind].custom_wave);
								osc[ind].custom_wave->samp_rate/=given_freq;
							}
							break;
						case FACTOR:
							osc[ind].factor=pow(2.0,val/12.0) *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*samp_rate;
							break;
						case DECAY:
							env[ind].decay=val*samp_rate;
							break;
						case SUSTAIN:
							env[ind].sustain=val*ONE;
							break;
						case RELEASE:
							env[ind].release=val*samp_rate;
							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*samp_rate/filter_update_frames;
							break;
						case FILTER_DECAY:
							filter.env_settings.decay=val*samp_rate/filter_update_frames;
							break;
						case FILTER_SUSTAIN:
							filter.env_settings.sustain=val*ONE;
							break;
						case FILTER_RELEASE:
							filter.env_settings.release=val*samp_rate/filter_update_frames;
							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;

						case FREQ_ATTACK:
							osc[ind].freq_env.attack=val*samp_rate;
							break;
						case FREQ_DECAY:
							osc[ind].freq_env.decay=val*samp_rate;
							break;
						case FREQ_SUSTAIN:
							osc[ind].freq_env.sustain=val*ONE;
							break;
						case FREQ_RELEASE:
							osc[ind].freq_env.release=val*samp_rate;
							break;
						case FREQ_HOLD:
							osc[ind].freq_env.hold=(val!=0);
							break;
						case FREQ_ENV_AMOUNT:
							osc[ind].freq_env_amount=val;
							break;

						default:
							throw string("unknown variable ('"+array+"')");
					}
					break;

				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");
											
					parameter_t par;
					par.par=p;

					if (par.par==UNKNOWN)
						throw string("unknown variable ('"+array+"')");

					if (par.par==MODULATION)
					{
						ind2=extract_array_index(var,2);
						if (!((ind2>=0) && (ind2<n_osc)))
							throw string("out of array bounds");
					}

					par.osc=ind;
					par.index=ind2;

					terms=extract_formula(strval);
					for (list<term_t>::iterator it=terms.begin(); it!=terms.end(); ++it)
						if (it->c!=NO_CONT)
							affect[it->c].insert(par);
					
					
					formula[par]=terms;
					break;
				
				case 3: //read controller default values
					if (array=="cont")
					{
						ind=extract_array_index(var,1);
						
						if ((ind<0) || (ind>127))
							throw string("out of array bounds");
						
						if ((val<0) || (val>127))
							throw string("value out of range");
							
						controller_default[ind]=val;
					}
					else
						throw string("expected cont, found '"+array+"'");
					
					break;
					
				case 4: //read velocity-influence over certain params
					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");

							pfactor.fm[ind][ind2]=parse_pfactor(strval);
							break;
							
						case OUTPUT:
							pfactor.out[ind]=parse_pfactor(strval);
							break;
							
						case FILTER_ENV_AMOUNT:
							pfactor.filter_env=parse_pfactor(strval);
							break;
							
						case FILTER_RESONANCE:
							pfactor.filter_res=parse_pfactor(strval);
							break;
							
						case FILTER_OFFSET:
							pfactor.filter_offset=parse_pfactor(strval);
							break;
						
						case FREQ_ENV_AMOUNT:
							pfactor.freq_env_amount[ind]=parse_pfactor(strval);
							break;
							
						default:
							throw string("velocity cannot influence parameter '"+array+"'");
					

					}
			}
		}
	}


	bool neverending_tone=false;
	
	for (map< parameter_t, list<term_t> >::iterator it=formula.begin(); it!=formula.end(); ++it)
		if ((it->first.par==OUTPUT) && (env[it->first.osc].release<0))
			neverending_tone=true;
	
	for (int i=0;i<n_osc;++i)
		if ((osc[i].output!=0) && (env[i].release<0))
			neverending_tone=true;

	if (neverending_tone)
		throw string("you have an oscillator which\n"
			  "         may output something but has a never-ending envelope.\n"
			  "         this would result in a tone that will never end.");



		
	program_t result;

	result.n_osc=n_osc;

	copy(&affect[0],&affect[128],result.controller_affects);

	result.formula=formula;

	result.osc_settings=new oscillator_t[n_osc];

	copy(&osc[0],&osc[n_osc],result.osc_settings);
	result.env_settings=new env_settings_t[n_osc];
	copy(&env[0],&env[n_osc],result.env_settings);

	for (int i=0;i<128;++i)
		result.controller[i]=controller_default[i];
	
	result.filter_settings=filter;
	
	result.sync_factor=sync_factor;
	
	result.pfactor=pfactor;



	//clean up a bit
	
	for (int i=0;i<n_osc;++i)
		delete [] osc[i].fm_strength;
		
	delete [] osc;
	delete [] env;




	
	return result;
}