-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOversamplingWavetableOscillators.cpp
1060 lines (795 loc) · 32 KB
/
OversamplingWavetableOscillators.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
#include "OversamplingWavetableOscillators.hpp"
#include "SC_PlugIn.hpp"
#include "SC_PlugIn.h"
#include "sergeWavetable.h"
static InterfaceTable *ft;
namespace Extras {
SawOSNext::SawOSNext()
{
}
SawOSNext::~SawOSNext() {}
float SawOSNext::next(float freq, float phaseIn, float m_freqMul)
{
float phaseDiff = (phaseIn - m_lastPhase);
m_lastPhase = phaseIn;
m_phase += (phaseDiff);
m_phase += freq * m_freqMul;
if (m_phase >= 4.f)
m_phase -= 8.f;
else if (m_phase <= -4.f)
m_phase += 8.f;
float out = sc_wrap(m_phase, -1.f, 1.f);
return out;
}
void SawOSNext::reset(float phaseIn)
{
m_lastPhase = phaseIn;
m_phase = phaseIn;
}
ProcessFuncs::ProcessFuncs()
{
}
ProcessFuncs::~ProcessFuncs() {}
float ProcessFuncs::get_phase(const float* phase_buf_data, float phase, float phase_buf_divs, float phase_buf_loc, int phase_table_size, float phase_fmaxindex) {
int iphase_buf_divs = (int)phase_buf_divs;
float fphase_index = (phase*phase_fmaxindex);
int phase_index = (int)fphase_index;
float frac_phase_index = phase_index - phase_index;
float ramp = 0.f;
phase_buf_loc = phase_buf_loc*(float)(phase_buf_divs-1);
int iphase_buf_loc = (int)phase_buf_loc;
//if the phase_buf_loc is the last index, we only look at that index
if(iphase_buf_loc == iphase_buf_divs-1)
{
int zero_index = (iphase_buf_loc*phase_table_size);
int loc = phase_index+zero_index;
//if it is the last index, we only look at that index
if(phase_index==phase_table_size-1)
ramp = phase_buf_data[loc];
else
//otherwise interpolate between the indexa dn the next index
ramp = phase_buf_data[loc]*(1.0f-frac_phase_index) + phase_buf_data[loc+1]*frac_phase_index;
} else {
float frac_phase_loc = phase_buf_loc - iphase_buf_loc;
int final_index = phase_index + (iphase_buf_loc*phase_table_size);
int final_index2 = final_index + phase_table_size;
float ramp1, ramp2;
if (phase_index==phase_table_size-1)
{
ramp1 = phase_buf_data[final_index]*(1.0f-frac_phase_index) + phase_buf_data[iphase_buf_loc*phase_table_size]*frac_phase_index;
ramp2 = phase_buf_data[final_index2]*(1.0f-frac_phase_index) + phase_buf_data[(iphase_buf_loc+1)*phase_table_size]*frac_phase_index;
} else {
ramp1 = phase_buf_data[final_index]*(1.0f-frac_phase_index) + phase_buf_data[final_index+1]*frac_phase_index;
ramp2 = phase_buf_data[final_index2]*(1.0f-frac_phase_index) + phase_buf_data[final_index2+1]*frac_phase_index;
}
//interpolate between the phase of the two tables
ramp = ramp1*(1.0f-frac_phase_loc) + ramp2*frac_phase_loc;
}
return ramp;
};
float ProcessFuncs::get_out(const float* buf_data, float ramp, float buf_divs, float buf_loc, int each_table_size, float fmaxindex, int num_chans, int chan_loc) {
//now that we have the ramp, use it to get the value from the buffer
float findex = (ramp*fmaxindex);
int index = (int)findex;
float frac = findex - index;
int ibuf_divs = (int)buf_divs;
float out;
buf_loc = buf_loc*(float)(buf_divs-1);
int ibuf_loc = (int)buf_loc;
if(ibuf_loc == ibuf_divs-1)
{
int zero_index = (ibuf_loc*each_table_size);
int loc = (index+zero_index)*num_chans + chan_loc;
if (index==each_table_size-1)
out = buf_data[loc]*(1.0f-frac) + buf_data[zero_index*num_chans]*frac;
else
out = buf_data[loc]*(1.0f-frac) + buf_data[loc+num_chans]*frac;
} else {
float frac_loc = buf_loc - ibuf_loc;
int final_index = index + (ibuf_loc*each_table_size);
int final_index2 = final_index + each_table_size;
final_index = final_index*num_chans + chan_loc;
final_index2 = final_index2*num_chans + chan_loc;
float out1, out2;
if (index==each_table_size-1)
{
out1 = buf_data[final_index]*(1.0f-frac) + buf_data[ibuf_loc*each_table_size*num_chans + chan_loc]*frac;
out2 = buf_data[final_index2]*(1.0f-frac) + buf_data[(ibuf_loc+1)*each_table_size*num_chans + chan_loc]*frac;
} else {
out1 = buf_data[final_index]*(1.0f-frac) + buf_data[final_index+num_chans]*frac;
out2 = buf_data[final_index2]*(1.0f-frac) + buf_data[final_index2+num_chans]*frac;
}
out = out1*(1.0f-frac_loc) + out2*frac_loc;
}
return out;
}
}
namespace BuchlaFoldOS
{
BuchlaFoldOS::BuchlaFoldOS()
{
const float samplerate = (float) sampleRate();
oversample.reset(samplerate);
m_oversamplingIndex = sc_clip((int)in0(OverSample), 0, 4);
oversample.setOversamplingIndex(m_oversamplingIndex);
m_oversampling_ratio = oversample.getOversamplingRatio();
osBuffer = oversample.getOSBuffer();
mCalcFunc = make_calc_function<BuchlaFoldOS, &BuchlaFoldOS::next_aa>();
next_aa(1);
}
BuchlaFoldOS::~BuchlaFoldOS() {}
float BuchlaFoldOS::buchla_cell(float sig, float sign, float thresh, float sig_mul1, float sign_mul, float sig_mul2) {
if (std::abs(sig) > thresh) {
return (sig * sig_mul1 - (sign * sign_mul)) * sig_mul2;
} else {
return 0.0f;
}
}
float BuchlaFoldOS::next(float sig, float amp) {
float out;
if(amp>0.f){
sig = sig * amp;
float sign = (sig >= 0.0f) ? 1.0f : -1.0f;
float v1 = buchla_cell(sig, sign, 0.6f, 0.8333f, 0.5f, 12.0f);
float v2 = buchla_cell(sig, sign, 2.994f, 0.3768f, 1.1281f, 27.777f);
float v3 = buchla_cell(sig, sign, 5.46f, 0.2829f, 1.5446f, 21.428f);
float v4 = buchla_cell(sig, sign, 1.8f, 0.5743f, 1.0338f, 17.647f);
float v5 = buchla_cell(sig, sign, 4.08f, 0.2673f, 1.0907f, 36.363f);
float v6 = sig * 5.0f;
out = ((v1 + v2 + v3)*(-1.f))+ v4 + v5 + v6;
out = out / 5.0f;
} else {
out = 0.f;
}
return out;
}
float BuchlaFoldOS::next_os(float sig, float amp)
{
float out;
oversample.upsample(sig);
for (int k = 0; k < m_oversampling_ratio; k++){
osBuffer[k] = next(osBuffer[k], amp);
}
if (m_oversamplingIndex != 0)
out = oversample.downsample();
else
out = osBuffer[0];
return out;
}
void BuchlaFoldOS::next_aa(int nSamples)
{
const float *sig = in(Sig);
const float *amp = in(Amp);
float *outbuf = out(Out1);
for (int i = 0; i < nSamples; ++i)
{
outbuf[i] = next_os(sig[i], amp[i]);
}
}
} // namespace BuchlaFoldOS
namespace SergeFoldOS {
SergeFoldOS::SergeFoldOS()
{
const float samplerate = (float) sampleRate();
sergeWavetable = getSergeWavetable();
oversample.reset(samplerate);
m_oversamplingIndex = sc_clip((int)in0(OverSample), 0, 4);
oversample.setOversamplingIndex(m_oversamplingIndex);
m_oversampling_ratio = oversample.getOversamplingRatio();
osBuffer = oversample.getOSBuffer();
mCalcFunc = make_calc_function<SergeFoldOS, &SergeFoldOS::next_aa>();
next_aa(1);
}
SergeFoldOS::~SergeFoldOS() {}
float SergeFoldOS::next(float sig, float amp) {
double out = tanh(sig*amp);
float findex = ((out*0.5+0.5)*(float)sergeWavetable.size());
float frac = findex - (int)findex;
int index = (int)findex;
if (index < 0) {
index = 0;
}
else if (index > sergeWavetable.size()-2) {
index = sergeWavetable.size()-2;
}
out = sergeWavetable[index]*(1.0f-frac) + sergeWavetable[index+1]*frac;
out = tanh(out);
return out;
}
float SergeFoldOS::next_os(float sig, float amp)
{
float out;
oversample.upsample(sig);
for (int k = 0; k < m_oversampling_ratio; k++){
osBuffer[k] = next(osBuffer[k], amp);
}
if (m_oversamplingIndex != 0)
out = oversample.downsample();
else
out = osBuffer[0];
return out;
}
void SergeFoldOS::next_aa(int nSamples)
{
const float *sig = in(Sig);
const float *amp = in(Amp);
float *outbuf = out(Out1);
for (int i = 0; i < nSamples; ++i)
{
outbuf[i] = next_os(sig[i], amp[i]);
}
}
} // namespace SergeFoldOS
namespace BufUnit {
BufUnit::BufUnit()
{
m_fbufnum = std::numeric_limits<float>::quiet_NaN();
m_buf = nullptr;
}
BufUnit::~BufUnit() {}
bool BufUnit::GetTable(World* world, float fbufnum, int inNumSamples, const SndBuf*& buf, const float*& bufData,
int& tableSize) {
if (fbufnum < 0.f) { \
fbufnum = 0.f; \
}
if (fbufnum != m_fbufnum) {
uint32 bufnum = (uint32)fbufnum;
if (bufnum >= world->mNumSndBufs) {
uint32 localBufNum = bufnum - world->mNumSndBufs;
Graph* parent = mParent;
if (localBufNum <= parent->localBufNum)
m_buf = parent->mLocalSndBufs + localBufNum;
else {
bufnum = 0;
m_buf = world->mSndBufs + bufnum;
}
} else
m_buf = world->mSndBufs + bufnum;
m_fbufnum = fbufnum;
}
buf = m_buf;
if (!buf) {
ClearUnitOutputs(this, inNumSamples);
return false;
}
bufData = buf->data;
if (!bufData) {
ClearUnitOutputs(this, inNumSamples);
return false;
}
tableSize = buf->samples;
return true;
}
} // namespace BufUnit
namespace ShaperOS {
ShaperOS::ShaperOS()
{
const float samplerate = (float) sampleRate();
m_fbufnum = std::numeric_limits<float>::quiet_NaN();
m_buf = nullptr;
buf_unit = BufUnit::BufUnit();
oversample.reset(samplerate);
m_oversamplingIndex = sc_clip((int)in0(OverSample), 0, 4);
oversample.setOversamplingIndex(m_oversamplingIndex);
m_oversampling_ratio = oversample.getOversamplingRatio();
osBuffer = oversample.getOSBuffer();
mCalcFunc = make_calc_function<ShaperOS, &ShaperOS::next_aa>();
next_aa(1);
}
ShaperOS::~ShaperOS() {}
float ShaperOS::Perform(const float* table0, float in, float fmaxindex) {
float findex = ((in*0.5+0.5)*fmaxindex);
float frac = findex - (int)findex;
int index = (int)findex;
index = sc_clip(index, 0.f, fmaxindex-1.f);
float out = table0[index]*(1.0f-frac) + table0[index+1]*frac;
return out;
}
float ShaperOS::next_os(const float* table0, float in, float fmaxindex)
{
float out;
oversample.upsample(in);
for (int k = 0; k < m_oversampling_ratio; k++){
osBuffer[k] = Perform(table0, osBuffer[k], fmaxindex);
}
if (m_oversamplingIndex != 0)
out = oversample.downsample();
else
out = osBuffer[0];
return out;
}
void ShaperOS::next_aa(int nSamples)
{
const float *sig = in(Sig);
const float buf_num = in0(BufNum);
float *outbuf = out(Out1);
// get table
const SndBuf* buf; const float* bufData; int tableSize;
const bool verify_buf = buf_unit.GetTable(mWorld, buf_num, nSamples, buf, bufData, tableSize);
if (!verify_buf){
ClearUnitOutputs(this, nSamples);
return;
}
const float* table0 = bufData;
float fmaxindex = (float)(tableSize) - 1.f/(float)(tableSize);
for (int i = 0; i < nSamples; ++i)
{
outbuf[i] = ShaperOS::next_os(table0, sig[i], fmaxindex);
}
}
} // namespace ShaperOS
namespace ShaperOS2 {
ShaperOS2::ShaperOS2()
{
const float samplerate = (float) sampleRate();
m_fbufnum = std::numeric_limits<float>::quiet_NaN();
m_buf = nullptr;
m_last_phase = 0.f;
m_last_buf_loc = 0.f;
buf_unit = BufUnit::BufUnit();
oversample.reset(samplerate);
m_oversamplingIndex = sc_clip((int)in0(OverSample), 0, 4);
oversample.setOversamplingIndex(m_oversamplingIndex);
osBuffer = oversample.getOSBuffer();
upsample_buf_loc.reset(samplerate);
upsample_buf_loc.setOversamplingIndex(m_oversamplingIndex);
upsample_buf_ptr = upsample_buf_loc.getOSBuffer();
upsample_input.reset(samplerate);
upsample_input.setOversamplingIndex(m_oversamplingIndex);
upsample_input_ptr = upsample_input.getOSBuffer();
m_oversampling_ratio = oversample.getOversamplingRatio();
mCalcFunc = make_calc_function<ShaperOS2, &ShaperOS2::next_aa>();
next_aa(1);
}
ShaperOS2::~ShaperOS2() {}
// float ShaperOS2::Perform(const float* table0, float input, float buf_divs, float fbuf_loc, int table_size, float fmaxindex) {
// float findex = (input*fmaxindex);
// int index = (int)findex;
// float frac = findex - index;
// int ibuf_divs = (int)buf_divs;
// float out;
// fbuf_loc = fbuf_loc*(buf_divs-1);
// int ibuf_loc = (int)fbuf_loc;
// if(ibuf_loc == ibuf_divs-1)
// {
// int loc = index+(ibuf_loc*table_size);
// if (index==table_size-1)
// out = table0[loc];
// else
// out = table0[loc]*(1.0f-frac) + (table0[loc+1]*frac);
// } else {
// float frac_loc = fbuf_loc - ibuf_loc;
// int final_index = index + (ibuf_loc*table_size);
// int final_index2 = final_index + table_size;
// float out1, out2;
// if (index==table_size-1)
// {
// out1 = table0[final_index]*(1.0f-frac) + table0[ibuf_loc*table_size]*frac;
// out2 = table0[final_index2]*(1.0f-frac) + table0[(ibuf_loc+1)*table_size]*frac;
// } else {
// out1 = table0[final_index]*(1.0f-frac) + table0[final_index+1]*frac;
// out2 = table0[final_index2]*(1.0f-frac) + table0[final_index2+1]*frac;
// }
// out = out1*(1.0f-frac_loc) + out2*frac_loc;
// }
// return out;
// }
float ShaperOS2::next_os(const float* table0, float input, float buf_divs, float buf_loc, int each_table_size, float fmaxindex)
{
float out;
input = (input+1.f)*0.5f;
//input = sc_clip(input, 0, 1.0f);
//Print("input: %f\n", input);
float buf_loc1 = sc_clip(buf_loc, 0.f, 1.0f);
upsample_input.upsample(input);
upsample_buf_loc.upsample(buf_loc1);
for (int k = 0; k < m_oversampling_ratio; k++){
//osBuffer[k] = Perform(table0, upsample_input_ptr[k], buf_divs, upsample_buf_ptr[k], table_size, fmaxindex);
float val = sc_clip(upsample_input_ptr[k],0.0,1.0);
osBuffer[k] = process_funcs.get_out(table0, val, buf_divs, buf_loc, each_table_size, fmaxindex, 1, 0); //only one channel
}
out = oversample.downsample();
return out;
}
void ShaperOS2::next_aa(int nSamples)
{
const float *input = in(In);
float buf_divs = floor(in0(BufDivs));
const float *buf_loc = in(BufLoc);
const float buf_num = in0(BufNum);
float *outbuf = out(Out1);
if (buf_divs < 1.f)
buf_divs = 1.f;
// get table
const SndBuf* buf; const float* bufData; int tableSize;
const bool verify_buf = buf_unit.GetTable(mWorld, buf_num, nSamples, buf, bufData, tableSize);
if (!verify_buf){
ClearUnitOutputs(this, nSamples);
return;
}
const float* table0 = bufData;
int each_table_size = tableSize/buf_divs;
float fmaxindex = (float)each_table_size - 1.f;
for (int i = 0; i < nSamples; ++i)
{
outbuf[i] = ShaperOS2::next_os(table0, input[i], buf_divs, buf_loc[i], each_table_size, fmaxindex);
}
}
} // namespace ShaperOS2
namespace OscOS {
OscOS::OscOS()
{
const float samplerate = (float) sampleRate();
m_fbufnum = std::numeric_limits<float>::quiet_NaN();
m_buf = nullptr;
m_last_phase = 0.f;
m_last_buf_loc = 0.f;
buf_unit = BufUnit::BufUnit();
oversample.reset(samplerate);
m_oversamplingIndex = sc_clip((int)in0(OverSample), 0, 4);
oversample.setOversamplingIndex(m_oversamplingIndex);
osBuffer = oversample.getOSBuffer();
upsample_buf_loc.reset(samplerate);
upsample_buf_loc.setOversamplingIndex(m_oversamplingIndex);
upsample_buf = upsample_buf_loc.getOSBuffer();
m_oversampling_ratio = oversample.getOversamplingRatio();
mCalcFunc = make_calc_function<OscOS, &OscOS::next_aa>();
next_aa(1);
}
OscOS::~OscOS() {}
float OscOS::Perform(const float* table0, float phase, float buf_divs, float fbuf_loc, int table_size, float fmaxindex) {
float findex = (phase*fmaxindex);
int index = (int)findex;
float frac = findex - index;
int ibuf_divs = (int)buf_divs;
float out;
//if(ibuf_divs > 1){
fbuf_loc = fbuf_loc*(buf_divs-1);
int ibuf_loc = (int)fbuf_loc;
if(ibuf_loc == ibuf_divs-1)
{
int loc = index+(ibuf_loc*table_size);
if (index==table_size-1)
out = table0[loc]*(1.0f-frac) + table0[ibuf_loc*table_size]*frac;
else
out = table0[loc]*(1.0f-frac) + table0[loc+1]*frac;
} else {
float frac_loc = fbuf_loc - ibuf_loc;
int final_index = index + (ibuf_loc*table_size);
int final_index2 = final_index + table_size;
float out1, out2;
if (index==table_size-1)
{
out1 = table0[final_index]*(1.0f-frac) + table0[ibuf_loc*table_size]*frac;
out2 = table0[final_index2]*(1.0f-frac) + table0[(ibuf_loc+1)*table_size]*frac;
} else {
out1 = table0[final_index]*(1.0f-frac) + table0[final_index+1]*frac;
out2 = table0[final_index2]*(1.0f-frac) + table0[final_index2+1]*frac;
}
out = out1*(1.0f-frac_loc) + out2*frac_loc;
}
return out;
}
float OscOS::next_os(const float* table0, float phase, float buf_divs, float buf_loc, int table_size, float fmaxindex)
{
float out;
float phase1 = sc_clip(phase, 0.f, 1.0f);
float buf_loc1 = sc_clip(buf_loc, 0.f, 1.0f);
float phase_diff = (phase1 - m_last_phase);
upsample_buf_loc.upsample(buf_loc1);
//the phase_diff should not be more than 0.5 except when the phase crosses from 1 to 0 or vice versa
//even at the nyquist frequency, the phase_diff should not be more than 0.5
if(abs(phase_diff) > 0.5f){
if (phase1<m_last_phase)
phase_diff = (phase1 + 1.0f) - m_last_phase;
else
phase_diff = (phase1 - 1.0f) - m_last_phase;
phase_diff = phase_diff/m_oversampling_ratio;
for (int k = 0; k < m_oversampling_ratio; k++){
m_last_phase += phase_diff;
osBuffer[k] = Perform(table0, sc_wrap(m_last_phase, 0.f, 1.0f), buf_divs, upsample_buf[k], table_size, fmaxindex);
}
}
else {
phase_diff = phase_diff/m_oversampling_ratio;
for (int k = 0; k < m_oversampling_ratio; k++){
osBuffer[k] = Perform(table0, m_last_phase+(k*phase_diff), buf_divs, upsample_buf[k], table_size, fmaxindex);
}
}
m_last_phase = phase1;
m_last_buf_loc = buf_loc1;
if (m_oversamplingIndex != 0)
out = oversample.downsample();
else
out = osBuffer[0];
return out;
}
void OscOS::next_aa(int nSamples)
{
const float *phase = in(Phase);
float buf_divs = floor(in0(BufDivs));
const float *buf_loc = in(BufLoc);
const float buf_num = in0(BufNum);
float *outbuf = out(Out1);
if (buf_divs < 1.f)
buf_divs = 1.f;
// get table
const SndBuf* buf; const float* bufData; int tableSize;
const bool verify_buf = buf_unit.GetTable(mWorld, buf_num, nSamples, buf, bufData, tableSize);
if (!verify_buf){
ClearUnitOutputs(this, nSamples);
return;
}
const float* table0 = bufData;
int each_table_size = tableSize/buf_divs;
float fmaxindex = (float)each_table_size - 1.f/(float)(each_table_size);
for (int i = 0; i < nSamples; ++i)
{
outbuf[i] = OscOS::next_os(table0, phase[i], buf_divs, buf_loc[i], each_table_size, fmaxindex);
}
}
} // namespace OscOS
namespace OscOS2 {
OscOS2::OscOS2()
{
const float samplerate = (float) sampleRate();
m_fbufnum = std::numeric_limits<float>::quiet_NaN();
m_buf = nullptr;
buf_unit = BufUnit::BufUnit();
phase_buf_unit = BufUnit::BufUnit();
m_oversampling_index = sc_clip((int)in0(OverSample), 0, 4);
oversample.reset(samplerate);
oversample.setOversamplingIndex(m_oversampling_index);
os_buffer = oversample.getOSBuffer();
upsample_buf_loc.reset(samplerate);
upsample_buf_loc.setOversamplingIndex(m_oversampling_index);
os_buf_loc = upsample_buf_loc.getOSBuffer();
upsample_phase_buf_loc.reset(samplerate);
upsample_phase_buf_loc.setOversamplingIndex(m_oversampling_index);
os_phase_buf_loc = upsample_phase_buf_loc.getOSBuffer();
m_oversampling_ratio = oversample.getOversamplingRatio();
mCalcFunc = make_calc_function<OscOS2, &OscOS2::next_aa>();
next_aa(1);
}
OscOS2::~OscOS2() {}
float OscOS2::Perform(const float* buf_data, const float* phase_buf_data, float phase, float buf_divs, float buf_loc, float phase_buf_divs, float phase_buf_loc, int each_table_size, float fmaxindex, int phase_table_size, float phase_fmaxindex) {
float ramp;
//if the phase_buf is nil, just use the phase as the ramp
if(phase_buf_divs < 0.f)
ramp = phase;
else
ramp = process_funcs.get_phase(phase_buf_data, phase, phase_buf_divs, phase_buf_loc, phase_table_size, phase_fmaxindex);
float out = process_funcs.get_out(buf_data, ramp, buf_divs, buf_loc, each_table_size, fmaxindex, 1, 0); //only one channel
return out;
}
float OscOS2::next_os(const float* buf_data, const float* phase_buf_data, const float freq, const float phase, float buf_divs, float buf_loc, float phase_buf_divs, float phase_buf_loc, int each_table_size, float fmaxindex, int phase_table_size, float phase_fmaxindex)
{
float out;
buf_loc = sc_clip(buf_loc, 0.f, 1.0f);
phase_buf_loc = sc_clip(phase_buf_loc, 0.f, 1.0f);
upsample_buf_loc.upsample(buf_loc);
upsample_phase_buf_loc.upsample(phase_buf_loc);
for (int k = 0; k < m_oversampling_ratio; k++){
float saw_phase = saw.next(freq, phase, m_freqMul / m_oversampling_ratio)*0.5+0.5;
os_buffer[k] = Perform(buf_data, phase_buf_data, saw_phase, buf_divs, os_buf_loc[k], phase_buf_divs, os_phase_buf_loc[k], each_table_size, fmaxindex, phase_table_size, phase_fmaxindex);
}
if (m_oversampling_index != 0)
out = oversample.downsample();
else
out = os_buffer[0];
return out;
}
void OscOS2::next_aa(int n_samples)
{
const float buf_num = in0(BufNum);
const float phase_buf_num = in0(PhaseBuf);
const float *freq = in(Freq);
const float *phase = in(Phase);
float buf_divs = floor(in0(BufDivs));
const float *buf_loc = in(BufLoc);
float phase_buf_divs = floor(in0(PhaseBufDivs));
const float *phase_buf_loc = in(PhaseBufLoc);
float *outbuf = out(Out1);
if (buf_divs < 1.f)
buf_divs = 1.f;
if (phase_buf_divs < 1.f)
phase_buf_divs = 1.f;
// get table
const SndBuf* buf; const float* buf_data; int table_size;
const SndBuf* phase_buf; const float* phase_buf_data; int phase_table_size;
bool verify_buf = buf_unit.GetTable(mWorld, buf_num, n_samples, buf, buf_data, table_size);
if (!verify_buf){
ClearUnitOutputs(this, n_samples);
return;
}
verify_buf = phase_buf_unit.GetTable(mWorld, phase_buf_num, n_samples, phase_buf, phase_buf_data, phase_table_size);
if (!verify_buf){
ClearUnitOutputs(this, n_samples);
return;
}
int each_table_size = table_size/buf_divs;
float fmaxindex = (float)each_table_size - 1.f/(float)(each_table_size);
int each_phase_table_size = phase_table_size/phase_buf_divs;
int phase_fmaxindex = (float)each_phase_table_size - 1.f;
//Print("table_size: %i, each_table_size: %i, each_phase_table_size: %i, buf_divs: %f\n", table_size, each_table_size, each_phase_table_size, buf_divs);
for (int i = 0; i < n_samples; ++i)
{
outbuf[i] = OscOS2::next_os(buf_data, phase_buf_data, freq[i], phase[i], buf_divs, buf_loc[i], phase_buf_divs, phase_buf_loc[i], each_table_size, fmaxindex, each_phase_table_size, phase_fmaxindex);
}
}
} // namespace OscOS2
namespace OscOS3 {
OscOS3::OscOS3()
{
const float samplerate = (float) sampleRate();
m_fbufnum = std::numeric_limits<float>::quiet_NaN();
m_buf = nullptr;
buf_unit = BufUnit::BufUnit();
phase_buf_unit = BufUnit::BufUnit();
m_sync_trig = 0.f;
m_oversampling_index = sc_clip((int)in0(OverSample), 0, 4);
oversample.reset(samplerate);
oversample.setOversamplingIndex(m_oversampling_index);
os_buffer = oversample.getOSBuffer();
upsample_buf_loc.reset(samplerate);
upsample_buf_loc.setOversamplingIndex(m_oversampling_index);
os_buf_loc = upsample_buf_loc.getOSBuffer();
upsample_chan_loc.reset(samplerate);
upsample_chan_loc.setOversamplingIndex(m_oversampling_index);
os_chan_loc = upsample_chan_loc.getOSBuffer();
upsample_phase_buf_loc.reset(samplerate);
upsample_phase_buf_loc.setOversamplingIndex(m_oversampling_index);
os_phase_buf_loc = upsample_phase_buf_loc.getOSBuffer();
m_oversampling_ratio = oversample.getOversamplingRatio();
mCalcFunc = make_calc_function<OscOS3, &OscOS3::next_aa>();
next_aa(1);
}
OscOS3::~OscOS3() {}
//Perform(buf_data, phase_buf_data, saw_phase, buf_divs, upsample_buf_loc[k], phase_buf_divs, upsample_phase_buf_loc[k], table_size, fmaxindex);
float OscOS3::Perform(const float* buf_data, const float* phase_buf_data, float phase, float buf_divs, float buf_loc, float num_chans, float chan_loc, float phase_buf_divs, float phase_buf_loc, int each_table_size, float fmaxindex, int phase_table_size, float phase_fmaxindex)
{
float ramp;
//if the phase_buf is nil, just use the phase as the ramp
if(phase_buf_divs < 0.f)
ramp = phase;
else
ramp = process_funcs.get_phase(phase_buf_data, phase, phase_buf_divs, phase_buf_loc, phase_table_size, phase_fmaxindex);
float out = process_funcs.get_out(buf_data, ramp, buf_divs, buf_loc, each_table_size, fmaxindex, (int)num_chans, chan_loc);
return out;
}
float OscOS3::next_os(const float* buf_data, const float* phase_buf_data, const float freq, const float phase, float buf_divs, float buf_loc, float num_chans, float chan_loc, float phase_buf_divs, float phase_buf_loc, int each_table_size, float fmaxindex, int each_phase_table_size, float phase_fmaxindex)
{
float out;
//Print("next_os\n");
buf_loc = sc_clip(buf_loc, 0.f, 1.0f);
chan_loc = sc_clip(chan_loc, 0.f, 1.0f-1.f/num_chans);
phase_buf_loc = sc_clip(phase_buf_loc, 0.f, 1.0f);
//Print("buf_loc: %f, chan_loc: %f, phase_buf_loc: %f\n", buf_loc, chan_loc, phase_buf_loc);
//upsample the inputs
upsample_buf_loc.upsample(buf_loc);
upsample_chan_loc.upsample(chan_loc);
upsample_phase_buf_loc.upsample(phase_buf_loc);
for (int k = 0; k < m_oversampling_ratio; k++){
float saw_phase = saw.next(freq, phase, m_freqMul / m_oversampling_ratio)*0.5+0.5;
if (num_chans <= 1.f){
os_buffer[k] = Perform(buf_data, phase_buf_data, saw_phase, buf_divs, os_buf_loc[k], num_chans, 0.f, phase_buf_divs, os_phase_buf_loc[k], each_table_size, fmaxindex, each_phase_table_size, phase_fmaxindex);
} else {
float full_chan_loc = os_chan_loc[k]*(num_chans-1.f);
float low_chan_loc = floor(full_chan_loc);
float high_chan_loc = ceil(full_chan_loc);
float frac = full_chan_loc - low_chan_loc;
float chan0 = Perform(buf_data, phase_buf_data, saw_phase, buf_divs, os_buf_loc[k], num_chans, low_chan_loc, phase_buf_divs, os_phase_buf_loc[k], each_table_size, fmaxindex, each_phase_table_size, phase_fmaxindex);
float chan1 = Perform(buf_data, phase_buf_data, saw_phase, buf_divs, os_buf_loc[k], num_chans, high_chan_loc, phase_buf_divs, os_phase_buf_loc[k], each_table_size, fmaxindex, each_phase_table_size, phase_fmaxindex);
os_buffer[k] = chan0*(1.0f-frac) + chan1*frac;
}
}
out = oversample.downsample();
return out;
}
void OscOS3::next_aa(int n_samples)
{
const float buf_num = in0(BufNum);
float phase_buf_num = in0(PhaseBuf);
const float *freq = in(Freq);
const float *phase = in(Phase);
const float *sync_trig = in(SyncTrig);
float buf_divs = floor(in0(BufDivs));
const float *buf_loc = in(BufLoc);
float num_chans = floor(in0(NumChans));
const float *chan_loc = in(ChanLoc);
float phase_buf_divs = floor(in0(PhaseBufDivs));
const float *phase_buf_loc = in(PhaseBufLoc);
float *outbuf = out(Out1);
if (buf_divs < 1.f)
buf_divs = 1.f;
if (phase_buf_divs < 1.f)
phase_buf_divs = 1.f;
// get table
const SndBuf* buf; const float* buf_data; int table_size;
const SndBuf* phase_buf; const float* phase_buf_data; int phase_table_size;
bool verify_buf = buf_unit.GetTable(mWorld, buf_num, n_samples, buf, buf_data, table_size);
if (!verify_buf){
//figure out what unit is
ClearUnitOutputs(this, n_samples);
return;
}
int each_phase_table_size = 0;
float phase_fmaxindex = 0;
if(phase_buf_num < 0.f){
//Print("phase_buf_num is less than 0\n");
phase_buf_num = -1.f;
phase_buf_divs = -1.f;
} else {
//Print("phase_buf_num is greater than 0\n");
verify_buf = phase_buf_unit.GetTable(mWorld, phase_buf_num, n_samples, phase_buf, phase_buf_data, phase_table_size);
if (!verify_buf){
phase_buf_divs = -1.f;
return;
} else {
each_phase_table_size = phase_table_size/phase_buf_divs;
phase_fmaxindex = (float)each_phase_table_size - 1.f;
}
}
//Print("each_phase_table_size: %i, phase_buf_divs: %f\n", each_phase_table_size, phase_buf_divs);
//fmaxindex and each_table_size are size of a single channel of a single table
int each_table_size = table_size/(buf_divs*num_chans);
float fmaxindex = (float)each_table_size - 1.f/(float)each_table_size;
for (int i = 0; i < n_samples; ++i)
{
if((m_sync_trig <= 0.f)&&(sync_trig[i] > 0.f)){
saw.reset(phase[i]);
};
m_sync_trig = sync_trig[i];
outbuf[i] = OscOS3::next_os(buf_data, phase_buf_data, freq[i], phase[i], buf_divs, buf_loc[i], num_chans, chan_loc[i], phase_buf_divs, phase_buf_loc[i], each_table_size, fmaxindex, each_phase_table_size, phase_fmaxindex);
}