-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy_code.cpp
More file actions
2840 lines (2313 loc) · 79.8 KB
/
Copy patheasy_code.cpp
File metadata and controls
2840 lines (2313 loc) · 79.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Congratulations, you have found the central file for:
//
// EEE A SS Y Y 22
// E A A S Y Y 2 2
// EE A A SS Y Y 2
// E AAAAA S Y 2
// EEE A A SSS Y 2222
//
// Easy 2 is a script-based sound generator to make audio files.
// It is focused on certain types of sound effects for electronic
// purposes. The idea is to make a versatile system that is
// as easy as possible to use. This mainly focuses on keeping the language
// as simple as possible.
//
// Inspiration is drawn from analog synths, LISP, and many audio files
// that happened to come to my attention.
//
// This is the second attempt at a system like this Easy V1 was written
// in Python and was severely limited by that language. Aside from
// complaining about Python, some lessons learned were:
//
// - Audio generation is about numbers and numbers about numbers. As such
// the language to describe certain concepts quickly becomes abstract and
// lacking. It goes with the territory, so, sorry. I've tried.
//
// - The code that does actual audio wave generation is vulnerable to
// becoming an mess that is difficult to debug. There are
// some tricks to doing it right and simpler implemented here.
//
// - Implementing a language is hard especially when the subject
// is abstract. Implementing rigorous syntax checking to cover
// everything the user might type is harder. In Easy V1, even as the
// author, I would be mystified as to why the sound produced was
// not right (often missing!). Easy 2 attempts to help this
// by providing reasonable defaults and more carefree syntax.
//
// - WAV files are easy to write. MPEG4 files are a pain in the ass due
// to proprietary reasons. For this reason 48k WAV files are supported
// rather than 48k MPEG4. Your choice of sample rate matters as
// only SoX accurately transcodes 44.1 to 48k.
//
// - the external MP3 LAME encoder is used to convert the WAV file to MP3.
// This code is written for Linux and will need minor adaptation to
// Windows.
//
// The file extension ".e2" is suggested as the source script.
//
// The minimum content of a .e2 file is something like:
//
// output "filename.wav"
// sound 10
//
// ... or merely tell easy2 what filename to call it and
// make at least one sound.
//
//
// Here is the road map of the source files:
//
// easy2.l ... the input to lex for it to write the lex.yy.c file.
// I use this in C mode because the internet told me to.
// Indeed, when tried in C++ mode it got pissy.
//
// Note: while Lex is used, YACC is not. The Easy2 language is
// simple enough that the work normally done by YACC is handled
// in easy_code.cpp. The different language objects did not
// become clear until well into the development.
//
// easy_code.h ... any headers needed in C format
// easy.hpp ... any general C++ format headers
// easy_code.cpp ... this handles most the general work and is called
// in response to Lex parsing the input file. The routine
// processCommands() is the heart of the system.
//
// easy_node.cpp ... The node linked list class. This is really a C-style
// double linked list as it was written early on. Each
// item in the input text becomes a node in the linked list.
//
// easy_sound.cpp ... deals with sound generation and higher-level audio
// manipulations of the output buffer.
//
// easy_wav.cpp ... deals with lower-level writing into the output buffer.
// It handles the details of both 48k and 44.1k WAV files.
// This code actually writes the output.
//
// easy_debug.cpp ... extra code to help with debugging
//
// easy_mp3.cpp ... conversion routine for wav->mp3
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
extern "C" {
// C called from C++ must be in this block for dumb legacy reasons
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "easy_code.h"
#include "math.h"
extern void restart(void); // over in lex.yy.c
extern void pushIncludeFile (FILE *f);
extern void endOfFile(void);
}
#include <ctime>
#include <iostream>
#include <map>
#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include "easy.hpp"
#include "easy_wav.hpp"
#include "easy_node.hpp"
extern "C" {
extern int flag48;
FILE * copyyyin;
const char * copyinfile;
const char * originalinfile;
int lineNumber=0;
extern int subBlock;
}
void doMp3(const char * infile, const char * songname);
using namespace std;
void doMath0 (void);
void doMath1 (void);
void doMath2 (void);
void doAssignment ();
bool isNumerical(node *);
void listVariables(void);
bool swapVariables(void);
void replaceMacro(node * spot, node *nodes);
void doStuff(void);
double NumberRight (node * n);
node * ToRight (node * n);
char * StringRight (node * n);
void lookForRepeat(void);
bool loadSeq(node * n, Seq * , Ramps *);
node * GetLeft (node * center);
node * GetRight (node * center);
const char * debug_type (int dtype);
void scanForAssignments(void);
void midi_symbols(void);
void doPreset (const char * str, float number);
// these are over in easy_sound...
void doSound(double, bool);
void doMix(double);
void doSilence(double);
void doBoost(double, NumberDriver *);
void doReverb (double length, NumberDriver *amt, NumberDriver *del);
// GLOBALS...
char * outputFile=NULL;
node * copyNodes (node * n);
double masterTime=0;
WaveWriter *wavout;
long defaultFormat = 44100; // 16bit, 44kHz
const char * defaultFileout = "output.wav"; // because people will forget
uint32_t SR=999999; // sample rate
uint32_t soundLengthX; // length of current sound or boost in samples
Ramp *unusedRamp=NULL; // temp, until I figure out what to do with these
Osc *unusedOsc=NULL; // temp, until I figure out what to do with these
Ramps *unusedRamps=NULL;
RandSeq *unusedRand=NULL;
stack<double> rewindHistory; // remembers previous times of sounds
bool updateDefaults=true; // flag for updating default settings
//--------------------------
// handle variable / symbold storage
map<string,int> variables;
map<string,float> varValues;
map<string,int> varLines;
map<string,node *> varNodes;
map<string,long> varFilepos;
//----------------------------------------
void clearVariables (void) {
variables.clear();
varValues.clear();
varLines.clear();
varNodes.clear();
varFilepos.clear();
}
//--------------------------
// handle include stack
// This will be empty in the main file.
// The main file will appear in 1st level of include.
typedef struct {
string filename;
int lineno;
FILE *file;
fpos_t pos;
} fileRef;
stack<fileRef> fileStack;
//---------------------
// subroutine-related variables
class subEntry {
public:
string name;
string filename;
int line;
FILE * file;
fpos_t filepos;
};
map<string,subEntry *> subroutines;
string subName; // subroutine name currently being defined
int subLine; // starting line of subroutine currently being defined
FILE *subfile;
string subfilename;
fpos_t subpos;
typedef struct {
string filename;
int line;
} subStackItem;
stack<subStackItem> substack;
//------------------------------------------
NumberDriver::~NumberDriver() {}
//======================================================================
struct settings_struct defaults;
struct settings_struct_stacked settings;
//--------------------------------
float tt=0; // current time
extern node *elist;
extern node *begn;
extern node *current;
//===========================================================================
// We provide some auto conversion from different units.
// What this doesn't do is check that the units are used in appropriate
// situations. And this might be needed if we want oscillators
// to work in Hz and Periods (s) interchangably.
//
// % divides by 100
//
void numberhz (char * str) {
double val;
sscanf(str,"%lf",&val);
push(NUMBER,val,NULL);
}
void numberpct (char * str) { // convert % to fraction
double val;
sscanf(str,"%lf",&val);
val=val/100.;
push(NUMBER,val,NULL);
}
void numbers (char * str) {
double val;
sscanf(str,"%lf",&val);
push(NUMBER,val,NULL);
}
void number (char * str) {
double val;
sscanf(str,"%lf",&val);
push(NUMBER,val,NULL);
}
void numberPeriod (char * str) {
double val,inv;
sscanf(str,"%lf",&val);
if (val!=0) {
inv=1./val;
}
else {
inv=1;
}
push(NUMBER,inv,NULL);
}
// define all the midi note values as symbols
void midi_symbols (void) {
doPreset("_M127",12543.85);
doPreset("_M126",11839.82);
doPreset("_M125",11175.3);
doPreset("_M124",10548.08);
doPreset("_M123",9956.06);
doPreset("_M122",9397.27);
doPreset("_M121",8869.84);
doPreset("_M120",8372.02);
doPreset("_M119",7902.13);
doPreset("_M118",7458.62);
doPreset("_M117",7040);
doPreset("_M116",6644.88);
doPreset("_M115",6271.93);
doPreset("_M114",5919.91);
doPreset("_M113",5587.65);
doPreset("_M112",5274.04);
doPreset("_M111",4978.03);
doPreset("_M110",4698.64);
doPreset("_M109",4434.92);
doPreset("_M108",4186.01);
doPreset("_M107",3951.07);
doPreset("_M106",3729.31);
doPreset("_M105",3520);
doPreset("_M104",3322.44);
doPreset("_M103",3135.96);
doPreset("_M102",2959.96);
doPreset("_M101",2793.83);
doPreset("_M100",2637.02);
doPreset("_M99",2489.02);
doPreset("_M98",2349.32);
doPreset("_M97",2217.46);
doPreset("_M96",2093);
doPreset("_M95",1975.53);
doPreset("_M94",1864.66);
doPreset("_M93",1760);
doPreset("_M92",1661.22);
doPreset("_M91",1567.98);
doPreset("_M90",1479.98);
doPreset("_M89",1396.91);
doPreset("_M88",1318.51);
doPreset("_M87",1244.51);
doPreset("_M86",1174.66);
doPreset("_M85",1108.73);
doPreset("_M84",1046.5);
doPreset("_M83",987.77);
doPreset("_M82",932.33);
doPreset("_M81",880);
doPreset("_M80",830.61);
doPreset("_M79",783.99);
doPreset("_M78",739.99);
doPreset("_M77",698.46);
doPreset("_M76",659.26);
doPreset("_M75",622.25);
doPreset("_M74",587.33);
doPreset("_M73",554.37);
doPreset("_M72",523.25);
doPreset("_M71",493.88);
doPreset("_M70",466.16);
doPreset("_M69",440);
doPreset("_M68",415.3);
doPreset("_M67",392);
doPreset("_M66",369.99);
doPreset("_M65",349.23);
doPreset("_M64",329.63);
doPreset("_M63",311.13);
doPreset("_M62",293.66);
doPreset("_M61",277.18);
doPreset("_M60",261.63);
doPreset("_M59",246.94);
doPreset("_M58",233.08);
doPreset("_M57",220);
doPreset("_M56",207.65);
doPreset("_M55",196);
doPreset("_M54",185);
doPreset("_M53",174.61);
doPreset("_M52",164.81);
doPreset("_M51",155.56);
doPreset("_M50",146.83);
doPreset("_M49",138.59);
doPreset("_M48",130.81);
doPreset("_M47",123.47);
doPreset("_M46",116.54);
doPreset("_M45",110);
doPreset("_M44",103.83);
doPreset("_M43",98);
doPreset("_M42",92.5);
doPreset("_M41",87.31);
doPreset("_M40",82.41);
doPreset("_M39",77.78);
doPreset("_M38",73.42);
doPreset("_M37",69.3);
doPreset("_M36",65.41);
doPreset("_M35",61.74);
doPreset("_M34",58.27);
doPreset("_M33",55);
doPreset("_M32",51.91);
doPreset("_M31",49);
doPreset("_M30",46.25);
doPreset("_M29",43.65);
doPreset("_M28",41.2);
doPreset("_M27",38.89);
doPreset("_M26",36.71);
doPreset("_M25",34.65);
doPreset("_M24",32.7);
doPreset("_M23",30.87);
doPreset("_M22",29.14);
doPreset("_M21",27.5);
doPreset("_C8",4186.01);
doPreset("_B7",3951.07);
doPreset("_Bb7",3729.31);
doPreset("_A7",3520);
doPreset("_Ab7",3322.44);
doPreset("_G7",3135.96);
doPreset("_Gb7",2959.96);
doPreset("_F7",2793.83);
doPreset("_E7",2637.02);
doPreset("_Eb7",2489.02);
doPreset("_D7",2349.32);
doPreset("_Db7",2217.46);
doPreset("_C7",2093);
doPreset("_B6",1975.53);
doPreset("_Bb6",1864.66);
doPreset("_A6",1760);
doPreset("_Ab6",1661.22);
doPreset("_G6",1567.98);
doPreset("_Gb6",1479.98);
doPreset("_F6",1396.91);
doPreset("_E6",1318.51);
doPreset("_Eb6",1244.51);
doPreset("_D6",1174.66);
doPreset("_Db6",1108.73);
doPreset("_C6",1046.5);
doPreset("_B5",987.77);
doPreset("_Bb5",932.33);
doPreset("_A5",880);
doPreset("_Ab5",830.61);
doPreset("_G5",783.99);
doPreset("_Gb5",739.99);
doPreset("_F5",698.46);
doPreset("_E5",659.26);
doPreset("_Eb5",622.25);
doPreset("_D5",587.33);
doPreset("_D5b",554.37);
doPreset("_C5",523.25);
doPreset("_B4",493.88);
doPreset("_Bb4",466.16);
doPreset("_A4",440);
doPreset("_Ab4",415.3);
doPreset("_G4",392);
doPreset("_Gb4",369.99);
doPreset("_F4",349.23);
doPreset("_E4",329.63);
doPreset("_Eb4",311.13);
doPreset("_D4",293.66);
doPreset("_Db4",277.18);
doPreset("_C4",261.63);
doPreset("_B3",246.94);
doPreset("_Bb3",233.08);
doPreset("_A3",220);
doPreset("_Ab3",207.65);
doPreset("_G3",196);
doPreset("_Gb3",185);
doPreset("_F3",174.61);
doPreset("_E3",164.81);
doPreset("_Eb3",155.56);
doPreset("_D3",146.83);
doPreset("_Db3",138.59);
doPreset("_C3",130.81);
doPreset("_B2",123.47);
doPreset("_Bb2",116.54);
doPreset("_A2",110);
doPreset("_Ab2",103.83);
doPreset("_G2",98);
doPreset("_Gb2",92.5);
doPreset("_F2",87.31);
doPreset("_E2",82.41);
doPreset("_Eb2",77.78);
doPreset("_D2",73.42);
doPreset("_Db2",69.3);
doPreset("_C2",65.41);
doPreset("_B1",61.74);
doPreset("_Bb1",58.27);
doPreset("_A1",55);
doPreset("_Ab1",51.91);
doPreset("_G1",49);
doPreset("_Gb1",46.25);
doPreset("_F1",43.65);
doPreset("_E1",41.2);
doPreset("_Eb1",38.89);
doPreset("_D1",36.71);
doPreset("_Db1",34.65);
doPreset("_C1",32.7);
doPreset("_B0",30.87);
doPreset("_Bb0",29.14);
doPreset("_A0",27.5);
}
//----------------------------------------------------------------------
// gotoLine
//
// This is used by subs and loop to fast forward the current
// yyin FILE to the desired line. Whole lines must be used
// because lex's internal buffers will usually consume the whole file.
//
void gotoLine (int lineTarget) {
rewind(getyyin()); // start at beginning of input
restart();
lineNumber=0; // and reset our counter too
char buffer[4096];
while (!feof(getyyin())) {
fgets(buffer,4093,getyyin());
lineNumber++;
if (lineNumber==lineTarget) {
// input file should be positioned properly to
// continue in lex now (i hope)
return; // lets get out of here
}
}
// what if the loop is not constructed right...
printf("Error: in goto line: '%d'.\n",lineTarget);
exit(0);
}
//----------------------------------------------------------------------
// shape presets
//
// class definition over in easy.hpp but initialization
// doesn't belong there
//
// Important considerations here:
// - values and times are scaled 0-.99
// - there have to be the same number of values and times in each pair of tables
// - don't use the end time of 1., use something like .95 instead, as length of
// signal is used in the slope calculation
std::vector<double> notch1_entT {0, .1 ,.15 ,.2 ,.3 ,.9};
std::vector<double> notch1_entV {.99, .6 ,.3 ,.6 ,.9 ,.99};
std::vector<double> notch2_entT {0 ,.1 ,.2 ,.3 ,.5};
std::vector<double> notch2_entV {.99 ,.1 ,.4 ,.8 ,.99};
std::vector<double> notch3_entT {0, .1 ,.13 ,.2 ,.3};
std::vector<double> notch3_entV {.99, .6 ,.3 ,.6 ,.99};
std::vector<double> tease1_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> tease1_entV {.8 ,.9 ,1 ,.75 ,.8 ,.85 ,.8 ,.75 ,.84 ,.95 ,1.};
std::vector<double> tease2_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> tease2_entV {.8 ,.7 ,.75 ,.85 ,.9 ,.95 ,.99 ,.99 ,.99 ,.75 ,.85};
std::vector<double> tease3_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> tease3_entV {.99 ,.95 ,9 ,.95 ,.85 ,.95 ,.9 ,.99 ,.99 ,.9 ,.85};
std::vector<double> pulse1_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> pulse1_entV {0 ,.5 ,.6 ,.75 ,.8 ,.99 ,.8 ,.75 ,.6 ,.5 ,.2};
std::vector<double> pulse2_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> pulse2_entV {.5 ,.7 ,.8 ,.9 ,.95 ,.99 ,.95 ,.9 ,.8 ,.7 ,.5};
std::vector<double> pulse3_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> pulse3_entV {0 ,.4 ,.5 ,.65 ,.85 ,.95 ,.99 ,.95 ,.8 ,.6 ,.3};
std::vector<double> kick1_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> kick1_entV {.5 ,.5 ,.5 ,.75 ,.9 ,.99 ,.99 ,.9 ,.75 ,.5 ,.5};
std::vector<double> kick2_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> kick2_entV {.8 ,.8 ,.8 ,.8 ,.95 ,.99 ,.95 ,.8 ,.8 ,.8 ,.8};
std::vector<double> kick3_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> kick3_entV {.3 ,.3 ,.3 ,.3 ,.8 ,.95 ,.99 ,.99 ,.8 ,.3 ,.3};
std::vector<double> adsr1_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> adsr1_entV {.75 ,.99 ,.99 ,.75 ,.7 ,.7 ,.7 ,.7 ,.6 ,.4 ,.3};
std::vector<double> adsr2_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> adsr2_entV {.5 ,.8 ,.99 ,.99 ,.99 ,.7 ,.6 ,.5 ,.4 ,.3 ,.1};
std::vector<double> adsr3_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> adsr3_entV {.75 ,.99 ,.99 ,.99 ,.7 ,.6 ,.4 ,.4 ,.3 ,.2 ,.1};
std::vector<double> rev1_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> rev1_entV {.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.99 ,.99 ,.99 ,.8 ,.6};
std::vector<double> rev2_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> rev2_entV {.65 ,.7 ,85 ,.88 ,.9 ,.93 ,.96 ,.98 ,.99 ,.99 ,.99};
std::vector<double> rev3_entT {0 ,.1 ,.2 ,.3 ,.4 ,.5 ,.6 ,.7 ,.8 ,.9 ,.95};
std::vector<double> rev3_entV {.2 ,.4 ,.6 ,.7 ,.8 ,.9 ,.99 ,.99 ,.99 ,.99 ,.8};
std::vector<double> wedge1_entT {0 ,.4 ,.7 ,.95};
std::vector<double> wedge1_entV {.001 ,.001 ,.99 ,.99};
std::vector<double> wedge2_entT {0 ,.2 ,.5 ,.95};
std::vector<double> wedge2_entV {.001 ,.001 ,.99 ,.99};
std::vector<double> gap1_entT {0 ,.9 ,.91 ,.001};
std::vector<double> gap1_entV {.991 ,.99 ,.001 ,.99};
std::vector<double> gap2_entT {0 ,.15 ,.8 ,.81 ,.001};
std::vector<double> gap2_entV {.001 ,.99 ,.99 ,.001 ,.99};
void Shape::loadTable(void) {
std::vector<double>::iterator it;
// scale all values and times 0 to 1 full range
if (preset==SH_NOTCH1) {
entV=notch1_entV;
entT=notch1_entT;
}
else if (preset==SH_NOTCH2) {
entV=notch2_entV;
entT=notch2_entT;
}
else if (preset==SH_NOTCH3) {
entV=notch3_entV;
entT=notch3_entT;
}
else if (preset==SH_TEASE1) {
entV=tease1_entV;
entT=tease1_entT;
}
else if (preset==SH_TEASE2) {
entV=tease2_entV;
entT=tease2_entT;
}
else if (preset==SH_TEASE3) {
entV=tease3_entV;
entT=tease3_entT;
}
else if (preset==SH_PULSE1) {
entV=pulse1_entV;
entT=pulse1_entT;
}
else if (preset==SH_PULSE2) {
entV=pulse2_entV;
entT=pulse2_entT;
}
else if (preset==SH_PULSE3) {
entV=pulse3_entV;
entT=pulse3_entT;
}
else if (preset==SH_KICK1) {
entV=kick1_entV;
entT=kick1_entT;
}
else if (preset==SH_KICK2) {
entV=kick2_entV;
entT=kick2_entT;
}
else if (preset==SH_KICK3) {
entV=kick3_entV;
entT=kick3_entT;
}
else if (preset==SH_ADSR1) {
entV=adsr1_entV;
entT=adsr1_entT;
}
else if (preset==SH_ADSR2) {
entV=adsr2_entV;
entT=adsr2_entT;
}
else if (preset==SH_ADSR3) {
entV=adsr3_entV;
entT=adsr3_entT;
}
else if (preset==SH_REV1) {
entV=rev1_entV;
entT=rev1_entT;
}
else if (preset==SH_REV2) {
entV=rev2_entV;
entT=rev2_entT;
}
else if (preset==SH_WEDGE1) {
entV=wedge1_entV;
entT=wedge1_entT;
}
else if (preset==SH_WEDGE2) {
entV=wedge2_entV;
entT=wedge2_entT;
}
else if (preset==SH_GAP1) {
entV=gap1_entV;
entT=gap1_entT;
}
else if (preset==SH_GAP2) {
entV=gap2_entV;
entT=gap2_entT;
}
// add one more target to every table... for loop around
entV.push_back(entV[0]); // same as start
entT.push_back(1.0); // will scale to length
assert(entT.size()==entV.size());
steps=entT.size();
lenX=1.*length; // convert times scaled to length
for(int i=0; i<steps; i++) {
double value=entT.at(i); // annoying: C++ doesn't seem to do the right thing without this
uint32_t entTXval=value*length;
entTX.push_back(entTXval); // convert times to samples and scaled to length
}
va=entV[1];
vb=entV[0];
ta=entTX[1];
tb=entTX[0];
step=0;
// printf("initial shape (step %ld): %f -> %f %d -> %d\n",step,vb,va,tb,ta);
}
//--------------------------
// handle variable storage
//
// a variable can be a float (if the result of a math equation)
// or a list of nodes (if the left hand side works out to anything else)
//
// Tried this as a structure but defeated by C++ complexities.
#define V_FLOAT 0
#define V_STRING 1
//--------------------------------------------------
void listVariables(void) {
// std::cout << " List of all variables:\n";
for(auto it=variables.begin(); it != variables.end(); ++it) {
if (it->second==V_FLOAT) {
std::cout << " Key: " << it->first << "=" << varValues[it->first] << "\n";
}
else {
std::cout << " Key: " << it->first << "=" << "... is defined as something" << "\n";
}
}
}
//----------------------------------------------------------------------
// exits on an error with a message
// TODO: add a line number to this
void syntaxError (node * cur,const char * str) {
printf("%sERROR - line number %d - %s\n%s",RED,lineNumber,str,WHT);
exit(1);
}
//----------------------------------------------------------------------
// 4th or 5th time rewriting this
// It replaces the node at the spot given with the
// nodes listed in nodes.
//
// It does not do this by patching the linked list.
// It reconstructs the linked list from scratch.
//
// In general, patching out nodes has proven to be very fault-prone.
// Instead, our strategy is to mark them as NOOPs and skip over them
// when looking left or right.
//
//
void replaceMacro(node * spot, node *nodes) {
node * ptr;
// save old begn and elist in case they are needed
node *oldBegn=begn;
// node *oldElist=elist;
emptyList();
ptr=oldBegn; // working from left --> right
while (ptr!=spot) {
push (ptr->dtype,ptr->value,ptr->str);
ptr=ptr->rght;
}
node *tailend=spot->rght;
// we are skipping spot, which is the location of the macro name
// now copy the replacement nodes
ptr=nodes;
while (ptr!=NULL) {
push (ptr->dtype,ptr->value,ptr->str);
ptr=ptr->rght;
}
// okay - now we just have to copy any trailing nodes for the line
ptr=tailend;
while (ptr!=NULL) {
push (ptr->dtype,ptr->value,ptr->str);
ptr=ptr->rght;
}
// and this should be good
// unlike other methods, this should not require
// handling of special edge cases
}
//----------------------------------------------------------------------
// Finds the SYMBOL that is the target of a loop.
//
// In this case, easiest way is to match the 'loop ' at the front. There must be
// at least one space as per the lex rules.
//
// Note: this same method doesn't work for assignments.
char * loopvar(char *in) {
/* find loop and the space after */
char * spot=strstr(in, (char *) "loop ");
if (spot!=NULL) {
spot=spot+5; // what we want is after this...
}
else {
printf("DEBUG: did not find word loop in loop command\n");
exit(2);
}
char *cpy=strdup(spot);
printf("DEBUG loop to: %s\n",cpy);
return strdup(cpy);
}
//----------------------------------------------------------------------
// doRequire
//
// This command exits if the variable is not defined.
//
void doRequire (struct node * cur) {
string varname;
char * spot;
/* variable name is already in the node but we have to split it */
spot=strchr(cur->str,' ');
if (spot==NULL) {
syntaxError(cur,"Something wrong in 'require' command\n");
}
varname=string(spot+1);
// optionally there might be a backup value
// backup values must be numeric, we can't handle macros, see below
float backupValue=NumberRight(cur);
if (variables.find(varname)==variables.end()) {
if (backupValue==NO_NUMBER) {
printf("%srequire check failed: '%s' not defined\n%s",RED,varname.c_str(),WHT);
exit(2);
}
else {
// not defined? use the backup
// if the variable was a string (a macro) it is going to
// be transformed into a float
variables[varname]=V_FLOAT;
varValues[varname]=backupValue;
varLines[varname]=lineNumber;
}
}
}
//--------------------------------------------------
// this routine does the substituion of either
// a value (simple) or a macro (a string of nodes inserted)
//
// Here is the problem: if we process this end-to-begining
// we'll have problems with the loop command since we
// hit the command 'loop' after we've processed it's target label.
// But, if we process this begining-to-end we'll have problems
// with assignment because the assignment variable will be swapped
// before the equal sign is found.
//
// So there are a couple ways to solve this:
// 1) interate each wasy before hand looking for '=' or
// 'loop' and change the STRING into a different
// datatype so it does not get processed.
//
// This will fail if you tried to do an assignment in the middle
// of a repeat command or something else strange.
// OR
//
// 2) change the lexer so 'loop string' and 'string =' are stored
// together. This should be possible without altering the data
// structure.
//
// Seems like the more elegant way.
//
// Order-of-processing is very important both in the lexer and in
// the order in this file.
bool swapVariables(void) {
int count=0;
bool found=false;
//start from the end
struct node *ptr = elist;
// displayBackward();
while (ptr != NULL) {
count++;
if (ptr->dtype==STRING) { // detect the name of the SYMBOL
string name=string(ptr->str);
// cout << name << " --- looking at VARIABLE\n";
if (variables.find(name)!=variables.end()) {
int type=variables[name];
if (type==V_FLOAT) { // is replacement a float?
if (ptr->filled==false) {
// cout << name << " is a float\n";
found=true; // did a substitution
// replace this node with the NUMBER
// change: don't replace node, just populate
// the value of the variable
ptr->value=varValues[name]; // change datatype
// printf("DEBUG: %s was defined as %f at line %d\n",name.c_str(),varValues[name],varLines[name]);
ptr->dtype=NUMBER; // and fill in number
// flag that this variable was populated with a number
ptr->filled=true;
}
}
else {
//reconstruct line and start over
//
// monkeying around with patching over nodes was
// very error prone. We don't care about memory
// leakage.
replaceMacro(ptr,varNodes[name]);
return true;
}
}
}
//move to lft item
ptr = ptr ->lft;
}
return found;
}
//----------------------------------------------------------------------
//
// The information for the assignment is now all in the = node (start).
void doAssignment (node * ass) {
printf(" debug: assignment varname is %s\n",ass->str);
// displayForward();
string varnameStr=string(ass->str);
node *rightside=ass->rght;
if (isNumerical(rightside)) {
if (variables.find(varnameStr) != variables.end()) {
//printf("DEBUG: modifying numeric variable %s at line %d \n",varnameStr.c_str(),lineNumber);
// printf("found: modify existing %s\n",varnameStr.c_str());
variables[varnameStr]=V_FLOAT;
varValues[varnameStr]=rightside->value;
varLines[varnameStr]=lineNumber;
printf("cmd: %s=%f\n",varnameStr.c_str(),rightside->value);
}
else {
// not found: add new entry
//printf("DEBUG: storing new numeric variable %s at line %d \n",varnameStr.c_str(),lineNumber);