-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules_generated.cpp
1790 lines (1536 loc) · 48.6 KB
/
modules_generated.cpp
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
// Generated code! Poke around in modules.pre and moduleprocess.py, not here.
class Saw : public Module
{
public:
Saw(vector<ModuleParam *> parameters)
{
m_frequency = parameters[0]->m_module;
m_retrigger = parameters[1]->m_module;
m_position = 0;
m_last_trigger = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Saw(parameters);
}
const char *moduleName() { return "Saw"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *frequency = m_frequency->output(last_fill, samples, m_sample_rate);
const float *retrigger = m_retrigger->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = (m_position-0.5f)*2;
m_position += frequency[i] / m_sample_rate;
m_position -= int(m_position);
if(m_last_trigger < 0.9 && retrigger[i] >= 0.9)
m_position = 0;
m_last_trigger = retrigger[i];
}
g_profiler.addTime("Saw", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1;
*out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_frequency, 0, 22050);
}
private:
Module *m_frequency;
Module *m_retrigger;
float m_position;
float m_last_trigger;
};
class Pulse : public Module
{
public:
Pulse(vector<ModuleParam *> parameters)
{
m_frequency = parameters[0]->m_module;
m_pulsewidth = parameters[1]->m_module;
m_retrigger = parameters[2]->m_module;
m_position = 0;
m_last_trigger = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Pulse(parameters);
}
const char *moduleName() { return "Pulse"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *frequency = m_frequency->output(last_fill, samples, m_sample_rate);
const float *pulsewidth = m_pulsewidth->output(last_fill, samples, m_sample_rate);
const float *retrigger = m_retrigger->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = (m_position > pulsewidth[i]) ? -1.0f:1.0f;
m_position += frequency[i] / m_sample_rate;
m_position -= int(m_position);
if(m_last_trigger < 0.9 && retrigger[i] >= 0.9)
m_position = 0;
m_last_trigger = retrigger[i];
}
g_profiler.addTime("Pulse", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1;
*out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_frequency, 0, 22050);
}
private:
Module *m_frequency;
Module *m_pulsewidth;
Module *m_retrigger;
float m_position;
float m_last_trigger;
};
class Sine : public Module
{
public:
Sine(vector<ModuleParam *> parameters)
{
m_frequency = parameters[0]->m_module;
m_retrigger = parameters[1]->m_module;
m_phase_offset = parameters[2]->m_module;
m_position = 0;
m_last_trigger = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Sine(parameters);
}
const char *moduleName() { return "Sine"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *frequency = m_frequency->output(last_fill, samples, m_sample_rate);
const float *retrigger = m_retrigger->output(last_fill, samples, m_sample_rate);
const float *phase_offset = m_phase_offset->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = sin((m_position+phase_offset[i])*2*pi);
m_position += frequency[i] / m_sample_rate;
m_position -= int(m_position);
if(m_last_trigger < 0.9 && retrigger[i] >= 0.9)
m_position = 0;
m_last_trigger = retrigger[i];
}
g_profiler.addTime("Sine", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1;
*out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_frequency, 0, 22050);
}
private:
Module *m_frequency;
Module *m_retrigger;
Module *m_phase_offset;
float m_position;
float m_last_trigger;
};
class Triangle : public Module
{
public:
Triangle(vector<ModuleParam *> parameters)
{
m_frequency = parameters[0]->m_module;
m_retrigger = parameters[1]->m_module;
m_position = 0;
m_last_trigger = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Triangle(parameters);
}
const char *moduleName() { return "Triangle"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *frequency = m_frequency->output(last_fill, samples, m_sample_rate);
const float *retrigger = m_retrigger->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
if(m_position < .5f) *output++ = (m_position-0.25f) * 4;
else *output++ = (m_position-0.75f) * -4;
m_position += frequency[i] / m_sample_rate;
m_position -= int(m_position);
if(m_last_trigger < 0.9 && retrigger[i] >= 0.9)
m_position = 0;
m_last_trigger = retrigger[i];
}
g_profiler.addTime("Triangle", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1;
*out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_frequency, 0, 22050);
}
private:
Module *m_frequency;
Module *m_retrigger;
float m_position;
float m_last_trigger;
};
class Noise : public Module
{
public:
Noise(vector<ModuleParam *> parameters)
{
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Noise(parameters);
}
const char *moduleName() { return "Noise"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = 2.0f*rand()/RAND_MAX - 1.0;
}
g_profiler.addTime("Noise", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1;
*out_max = 1;
}
void validateInputRange()
{
}
private:
};
class Within : public Module
{
public:
Within(vector<ModuleParam *> parameters)
{
m_input = parameters[0]->m_module;
m_min = parameters[1]->m_float;
m_max = parameters[2]->m_float;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Within(parameters);
}
const char *moduleName() { return "Within"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *input = m_input->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = (input[i]>=m_min && input[i]<=m_max) ? 1 : 0;
}
g_profiler.addTime("Within", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = 0;
*out_max = 1;
}
void validateInputRange()
{
}
private:
Module *m_input;
float m_min;
float m_max;
};
class Sample : public Module
{
public:
Sample(vector<ModuleParam *> parameters)
{
m_sample_name = parameters[0]->m_string;
m_frequency = parameters[1]->m_module;
m_trigger = parameters[2]->m_module;
m_loop_start = parameters[3]->m_module;
m_loop_end = parameters[4]->m_module;
m_position = 0;
m_sample = 0;
m_rate_coefficient = 0;
m_last_trigger = -1;
m_sample = new WaveIn(m_sample_name);
m_rate_coefficient = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Sample(parameters);
}
const char *moduleName() { return "Sample"; }
~Sample() { delete m_sample; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *frequency = m_frequency->output(last_fill, samples, m_sample_rate);
const float *trigger = m_trigger->output(last_fill, samples, m_sample_rate);
const float *loop_start = m_loop_start->output(last_fill, samples, m_sample_rate);
const float *loop_end = m_loop_end->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = m_sample->valueAt(m_position);
if(m_last_trigger < 0.9 && trigger[i] >= 0.9)
{
m_position = 0;
m_rate_coefficient = m_sample->nativeSampleRate() / middle_c /
m_sample->length() / m_sample_rate;
}
m_last_trigger = trigger[i];
m_position += frequency[i] * m_rate_coefficient;
if(m_position > loop_end[i])
{
m_position -= (loop_end[i] - loop_start[i]);
if(m_position > loop_end[i]) m_position = loop_end[i];
}
}
g_profiler.addTime("Sample", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1;
*out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_frequency, 0, 22050);
validateWithin(*m_trigger, 0, 1);
validateWithin(*m_loop_start, 0, 1);
validateWithin(*m_loop_end, 0, 1);
}
private:
string m_sample_name;
Module *m_frequency;
Module *m_trigger;
Module *m_loop_start;
Module *m_loop_end;
float m_position;
WaveIn* m_sample;
float m_rate_coefficient;
float m_last_trigger;
};
class Rescaler : public Module
{
public:
Rescaler(vector<ModuleParam *> parameters)
{
m_input = parameters[0]->m_module;
m_to_min = parameters[1]->m_float;
m_to_max = parameters[2]->m_float;
m_from_min = 0;
m_from_range = 0;
m_to_range = 0;
float from_max;
m_input->getOutputRange(&m_from_min, &from_max);
m_to_range = m_to_max - m_to_min;
m_from_range = from_max - m_from_min;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Rescaler(parameters);
}
const char *moduleName() { return "Rescaler"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *input = m_input->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = (input[i]-m_from_min) / m_from_range * m_to_range + m_to_min;
}
g_profiler.addTime("Rescaler", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = m_to_min;
*out_max = m_to_min + m_to_range;
if(*out_max < *out_min) swap(*out_max, *out_min);
}
void validateInputRange()
{
}
private:
Module *m_input;
float m_to_min;
float m_to_max;
float m_from_min;
float m_from_range;
float m_to_range;
};
class Filter : public Module
{
public:
Filter(vector<ModuleParam *> parameters)
{
m_input = parameters[0]->m_module;
m_cutoff = parameters[1]->m_module;
m_resonance = parameters[2]->m_module;
m_oldcutoff = -1;
m_oldresonance = -1;
m_scale = 0;
m_f = 0;
m_k = 0;
m_p = 0;
m_r = 0;
m_y1 = 0;
m_y2 = 0;
m_y3 = 0;
m_y4 = 0;
m_oldx = 0;
m_oldy1 = 0;
m_oldy2 = 0;
m_oldy3 = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Filter(parameters);
}
const char *moduleName() { return "Filter"; }
inline void recalculateFilter(float cutoff, float resonance)
{
m_f = 2.0f * cutoff / m_sample_rate;
m_k = 3.6f*m_f - 1.6f*m_f*m_f -1.0f;
m_p = (m_k+1.0f)*0.5f;
m_scale = pow(e, (1.0f-m_p)*1.386249f);
m_r = resonance * m_scale;
}
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *input = m_input->output(last_fill, samples, m_sample_rate);
const float *cutoff = m_cutoff->output(last_fill, samples, m_sample_rate);
const float *resonance = m_resonance->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
if(m_oldcutoff != cutoff[i] || m_oldresonance != resonance[i])
{
recalculateFilter(cutoff[i], resonance[i]);
m_oldcutoff = cutoff[i], m_oldresonance = resonance[i];
}
m_oldcutoff = cutoff[i], m_oldresonance = resonance[i];
//Inverted feed back for corner peaking
float x = input[i] - m_r*m_y4;
//Four cascaded onepole filters (bilinear transform)
m_y1 = x*m_p + m_oldx *m_p - m_k*m_y1;
m_y2 = m_y1*m_p + m_oldy1*m_p - m_k*m_y2;
m_y3 = m_y2*m_p + m_oldy2*m_p - m_k*m_y3;
m_y4 = m_y3*m_p + m_oldy3*m_p - m_k*m_y4;
//Clipper band limited sigmoid
m_y4 -= pow(m_y4, 3)/6;
m_oldx = x;
m_oldy1 = m_y1;
m_oldy2 = m_y2;
m_oldy3 = m_y3;
*output++ = m_y4;
}
g_profiler.addTime("Filter", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
m_input->getOutputRange(out_min, out_max); // maybe? this math is hard.
}
void validateInputRange()
{
validateWithin(*m_cutoff, 0, 22050);
validateWithin(*m_resonance, 0, 1);
}
private:
Module *m_input;
Module *m_cutoff;
Module *m_resonance;
float m_oldcutoff;
float m_oldresonance;
float m_scale;
float m_f;
float m_k;
float m_p;
float m_r;
float m_y1;
float m_y2;
float m_y3;
float m_y4;
float m_oldx;
float m_oldy1;
float m_oldy2;
float m_oldy3;
};
class Multiply : public Module
{
public:
Multiply(vector<ModuleParam *> parameters)
{
m_a = parameters[0]->m_module;
m_b = parameters[1]->m_module;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Multiply(parameters);
}
const char *moduleName() { return "Multiply"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *a = m_a->output(last_fill, samples, m_sample_rate);
const float *b = m_b->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = a[i]*b[i];
}
g_profiler.addTime("Multiply", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
float a_min, a_max, b_min, b_max;
m_a->getOutputRange(&a_min, &a_max);
m_b->getOutputRange(&b_min, &b_max);
*out_min = min(min(a_min*b_max, b_min*a_max), a_min*b_min);
*out_max = a_max*b_max;
}
void validateInputRange()
{
}
private:
Module *m_a;
Module *m_b;
};
// should i bother giving this an arbitrary parameter count?
class Add : public Module
{
public:
Add(vector<ModuleParam *> parameters)
{
m_a = parameters[0]->m_module;
m_b = parameters[1]->m_module;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Add(parameters);
}
const char *moduleName() { return "Add"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *a = m_a->output(last_fill, samples, m_sample_rate);
const float *b = m_b->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = a[i] + b[i];
}
g_profiler.addTime("Add", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
float a_min, a_max, b_min, b_max;
m_a->getOutputRange(&a_min, &a_max);
m_b->getOutputRange(&b_min, &b_max);
*out_min = a_min + b_min;
*out_max = a_max + b_max;
}
void validateInputRange()
{
}
private:
Module *m_a;
Module *m_b;
};
class Quantize : public Module
{
public:
Quantize(vector<ModuleParam *> parameters)
{
m_input = parameters[0]->m_module;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Quantize(parameters);
}
const char *moduleName() { return "Quantize"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *input = m_input->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = floor(input[i]+0.5);
}
g_profiler.addTime("Quantize", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
m_input->getOutputRange(out_min, out_max);
*out_min = floor(*out_min + 0.5);
*out_max = floor(*out_max + 0.5);
}
void validateInputRange()
{
}
private:
Module *m_input;
};
class EnvelopeGenerator : public Module
{
public:
EnvelopeGenerator(vector<ModuleParam *> parameters)
{
m_gate = parameters[0]->m_module;
m_att = parameters[1]->m_float;
m_dec = parameters[2]->m_float;
m_sus = parameters[3]->m_float;
m_rel = parameters[4]->m_float;
m_held = false;
m_stage = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new EnvelopeGenerator(parameters);
}
const char *moduleName() { return "EnvelopeGenerator"; }
Curve m_curve;
enum { IDLE = 0, ATTACK, DECAY, SUSTAIN, RELEASE };
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *gate = m_gate->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
if(!m_held)
{
if(gate[i] > 0.9)
{
m_stage = ATTACK;
m_curve.start(m_curve.position(), 1, m_att, m_sample_rate);
m_held = true;
}
}
else
{
if(gate[i] < 0.1)
{
m_stage = RELEASE;
m_curve.start(m_curve.position(), 0.001, m_rel, m_sample_rate);
m_held = false;
}
}
m_curve.step();
switch(m_stage)
{
case ATTACK:
if(m_curve.finished())
{
m_stage = DECAY;
m_curve.start(m_curve.position(), m_sus, m_dec, m_sample_rate);
}
break;
case RELEASE:
if(m_curve.finished())
{
m_stage = IDLE;
m_curve.start(0, 0, 1, m_sample_rate);
}
break;
}
*output++ = m_curve.position();
}
g_profiler.addTime("EnvelopeGenerator", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = 0;
*out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_gate, -1, 1);
}
private:
Module *m_gate;
float m_att;
float m_dec;
float m_sus;
float m_rel;
bool m_held;
int m_stage;
};
/*module EnvelopeGenerator(gate, float att, float dec, float sus, float rel):
members:
float position = 0, coefficient = 0, destination = 0
bool held = false
int stage = 0
misc:
enum { IDLE = 0, ATTACK, DECAY, SUSTAIN, RELEASE };
output_range:
*out_min = 0;
*out_max = 1;
input_range:
gate: -1, 1
fill:
if(!m_held)
{
if(gate[i] > 0.9)
{
m_stage = ATTACK;
m_coefficient = onepoleCoefficient(m_att);
m_destination = 1;
m_held = true;
}
}
else
{
if(gate[i] < 0.1)
{
m_stage = RELEASE;
m_coefficient = onepoleCoefficient(m_rel);
m_destination = 0;
m_held = false;
}
}
m_position = m_position * m_coefficient + m_destination * (1-m_coefficient);
switch(m_stage)
{
case ATTACK:
if(m_position >= 0.999)
{
m_stage = DECAY;
m_coefficient = onepoleCoefficient(m_dec);
m_destination = m_sus;
}
break;
case RELEASE:
if(m_position <= 0.00001)
{
m_stage = IDLE;
m_position = 0;
m_destination = 0;
m_coefficient = 0;
}
break;
}
*output++ = m_position;
*/
class SampleAndHold : public Module
{
public:
SampleAndHold(vector<ModuleParam *> parameters)
{
m_source = parameters[0]->m_module;
m_trigger = parameters[1]->m_module;
m_waiting = true;
m_value = 0;
m_last_trigger = 0;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new SampleAndHold(parameters);
}
const char *moduleName() { return "SampleAndHold"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *source = m_source->output(last_fill, samples, m_sample_rate);
const float *trigger = m_trigger->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
if(m_last_trigger < 0.9 && trigger[i] >= 0.9)
m_value = source[i];
m_last_trigger = trigger[i];
*output++ = m_value;
}
g_profiler.addTime("SampleAndHold", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
m_source->getOutputRange(out_min, out_max);
}
void validateInputRange()
{
validateWithin(*m_trigger, -1, 1);
}
private:
Module *m_source;
Module *m_trigger;
bool m_waiting;
float m_value;
float m_last_trigger;
};
class Limiter : public Module
{
public:
Limiter(vector<ModuleParam *> parameters)
{
m_input = parameters[0]->m_module;
m_preamp = parameters[1]->m_module;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Limiter(parameters);
}
const char *moduleName() { return "Limiter"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *input = m_input->output(last_fill, samples, m_sample_rate);
const float *preamp = m_preamp->output(last_fill, samples, m_sample_rate);
double time = hires_time();
int sample_count = ::max(1.0f, samples*m_sample_rate/g_sample_rate);
for(int i=0; i<sample_count; i++)
{
*output++ = tanh(input[i] * preamp[i]);
}
g_profiler.addTime("Limiter", hires_time()-time);
}
void getOutputRange(float *out_min, float *out_max)
{
*out_min = -1, *out_max = 1;
}
void validateInputRange()
{
validateWithin(*m_preamp, 1, 10);
}
private:
Module *m_input;
Module *m_preamp;
};
class Rectifier : public Module
{
public:
Rectifier(vector<ModuleParam *> parameters)
{
m_input = parameters[0]->m_module;
}
static Module *create(vector<ModuleParam *> parameters)
{
return new Rectifier(parameters);
}
const char *moduleName() { return "Rectifier"; }
void fill(float last_fill, int samples)
{
float *output = m_output;
const float *input = m_input->output(last_fill, samples, m_sample_rate);