37 #define MAXIMILIAN_RT_AUDIO
61 #define PI 3.1415926535897932384626433832795
63 #define TWOPI 6.283185307179586476925286766559
67 #define CHEERP_EXPORT [[cheerp::jsexport]]
68 #include <cheerp/clientlib.h>
77 #define DOUBLEARRAY_REF client::Float64Array *
79 #define DOUBLEARRAY client::Float64Array
81 inline vector<double> convertArrayFromJS(DOUBLEARRAY_REF x)
83 double *__arrayStart = __builtin_cheerp_make_regular<double>(x, 0);
84 size_t __arrayLength = x->get_length();
85 return vector<double>(__arrayStart, __arrayStart + __arrayLength);
88 #define NORMALISE_ARRAY_TYPE(invar, outvar) vector<double> outvar = convertArrayFromJS(invar);
92 #define DOUBLEARRAY_REF vector<double> &
93 #define DOUBLEARRAY vector<double>
95 #define NORMALISE_ARRAY_TYPE(invar, outvar) vector<double> outvar = vector<double>(invar.begin(), invar.end());
99 const double pitchRatios[256] = {0.0006517771980725, 0.0006905338959768, 0.0007315951515920, 0.0007750981021672, 0.0008211878011934, 0.0008700182079338, 0.0009217521874234, 0.0009765623835847, 0.0010346318595111, 0.0010961542138830, 0.0011613349197432, 0.0012303915573284, 0.0013035543961450, 0.0013810677919537, 0.0014631903031841, 0.0015501962043345, 0.0016423756023869, 0.0017400364158675, 0.0018435043748468, 0.0019531247671694, 0.0020692637190223, 0.0021923084277660, 0.0023226698394865, 0.0024607831146568, 0.0026071087922901, 0.0027621355839074, 0.0029263808391988, 0.0031003924086690, 0.0032847514376044, 0.0034800728317350, 0.0036870087496936, 0.0039062497671694, 0.0041385274380445, 0.0043846168555319, 0.0046453396789730, 0.0049215662293136, 0.0052142175845802, 0.0055242711678147, 0.0058527616783977, 0.0062007848173380, 0.0065695028752089, 0.0069601456634700, 0.0073740174993873, 0.0078124995343387, 0.0082770548760891, 0.0087692337110639, 0.0092906802892685, 0.0098431324586272, 0.0104284351691604, 0.0110485423356295, 0.0117055233567953, 0.0124015696346760, 0.0131390057504177, 0.0139202913269401, 0.0147480349987745, 0.0156249990686774, 0.0165541097521782, 0.0175384692847729, 0.0185813605785370, 0.0196862649172544, 0.0208568722009659, 0.0220970865339041, 0.0234110467135906, 0.0248031392693520, 0.0262780115008354, 0.0278405826538801, 0.0294960699975491, 0.0312499981373549, 0.0331082195043564, 0.0350769385695457, 0.0371627211570740, 0.0393725298345089, 0.0417137444019318, 0.0441941730678082, 0.0468220934271812, 0.0496062822639942, 0.0525560230016708, 0.0556811690330505, 0.0589921437203884, 0.0624999962747097, 0.0662164390087128, 0.0701538771390915, 0.0743254423141479, 0.0787450596690178, 0.0834274888038635, 0.0883883461356163, 0.0936441868543625, 0.0992125645279884, 0.1051120460033417, 0.1113623380661011, 0.1179842874407768, 0.1249999925494194, 0.1324328780174255, 0.1403077542781830, 0.1486508846282959, 0.1574901193380356, 0.1668549776077271, 0.1767766922712326, 0.1872883737087250, 0.1984251290559769, 0.2102240920066833, 0.2227246761322021, 0.2359685748815536, 0.2500000000000000, 0.2648657560348511, 0.2806155085563660, 0.2973017692565918, 0.3149802684783936, 0.3337099552154541, 0.3535533845424652, 0.3745767772197723, 0.3968502581119537, 0.4204482138156891, 0.4454493522644043, 0.4719371497631073, 0.5000000000000000, 0.5297315716743469, 0.5612310171127319, 0.5946035385131836, 0.6299605369567871, 0.6674199104309082, 0.7071067690849304, 0.7491535544395447, 0.7937005162239075, 0.8408964276313782, 0.8908987045288086, 0.9438742995262146, 1.0000000000000000, 1.0594631433486938, 1.1224620342254639, 1.1892070770263672, 1.2599210739135742, 1.3348398208618164, 1.4142135381698608, 1.4983071088790894, 1.5874010324478149, 1.6817928552627563, 1.7817974090576172, 1.8877485990524292, 2.0000000000000000, 2.1189262866973877, 2.2449240684509277, 2.3784141540527344, 2.5198421478271484, 2.6696796417236328, 2.8284270763397217, 2.9966142177581787, 3.1748020648956299, 3.3635857105255127, 3.5635950565338135, 3.7754974365234375, 4.0000000000000000, 4.2378525733947754, 4.4898481369018555, 4.7568287849426270, 5.0396842956542969, 5.3393597602844238, 5.6568546295166016, 5.9932284355163574, 6.3496046066284180, 6.7271714210510254, 7.1271901130676270, 7.5509948730468750, 8.0000000000000000, 8.4757051467895508, 8.9796962738037109, 9.5136575698852539, 10.0793685913085938, 10.6787195205688477, 11.3137092590332031, 11.9864568710327148, 12.6992092132568359, 13.4543428421020508, 14.2543802261352539, 15.1019897460937500, 16.0000000000000000, 16.9514102935791016, 17.9593944549560547, 19.0273151397705078, 20.1587371826171875, 21.3574390411376953, 22.6274185180664062, 23.9729137420654297, 25.3984184265136719, 26.9086875915527344, 28.5087604522705078, 30.2039794921875000, 32.0000000000000000, 33.9028205871582031, 35.9187889099121094, 38.0546302795410156, 40.3174743652343750, 42.7148780822753906, 45.2548370361328125, 47.9458274841308594, 50.7968368530273438, 53.8173751831054688, 57.0175209045410156, 60.4079589843750000, 64.0000076293945312, 67.8056411743164062, 71.8375778198242188, 76.1092605590820312, 80.6349563598632812, 85.4297561645507812, 90.5096740722656250, 95.8916625976562500, 101.5936737060546875, 107.6347503662109375, 114.0350418090820312, 120.8159179687500000, 128.0000152587890625, 135.6112823486328125, 143.6751556396484375, 152.2185211181640625, 161.2699127197265625, 170.8595123291015625, 181.0193481445312500, 191.7833251953125000, 203.1873474121093750, 215.2695007324218750, 228.0700836181640625, 241.6318511962890625, 256.0000305175781250, 271.2225646972656250, 287.3503112792968750, 304.4370422363281250, 322.5398254394531250, 341.7190246582031250, 362.0386962890625000, 383.5666503906250000, 406.3746948242187500, 430.5390014648437500, 456.1401977539062500, 483.2637023925781250, 512.0000610351562500, 542.4451293945312500, 574.7006225585937500, 608.8740844726562500, 645.0796508789062500, 683.4380493164062500, 724.0773925781250000, 767.1333007812500000, 812.7494506835937500, 861.0780029296875000, 912.2803955078125000, 966.5274047851562500, 1024.0001220703125000, 1084.8903808593750000, 1149.4012451171875000, 1217.7481689453125000, 1290.1593017578125000, 1366.8762207031250000, 1448.1549072265625000, 1534.2666015625000000, 1625.4989013671875000};
105 static int sampleRate;
107 static int bufferSize;
108 static void setup(
int initSampleRate,
int initChannels,
int initBufferSize)
110 maxiSettings::sampleRate = initSampleRate;
111 maxiSettings::channels = initChannels;
112 maxiSettings::bufferSize = initBufferSize;
127 static int getSampleRate()
129 return maxiSettings::sampleRate;
153 double sinewave(
double frequency);
154 double coswave(
double frequency);
155 double phasor(
double frequency);
156 double phasorBetween(
double frequency,
double startphase,
double endphase);
157 double saw(
double frequency);
158 double triangle(
double frequency);
159 double square(
double frequency);
160 double pulse(
double frequency,
double duty);
161 double impulse(
double frequency);
163 double sinebuf(
double frequency);
164 double sinebuf4(
double frequency);
165 double sawn(
double frequency);
166 double rect(
double frequency,
double duty);
167 void phaseReset(
double phaseIn);
183 double line(
int numberofsegments, std::vector<double> &segments);
185 void trigger(
int index,
double amp);
191 void setValindex(
int index)
196 void setAmplitude(
double amp)
201 int getValindex()
const
206 double getAmplitude()
const
220 double memory[88200 * 2];
224 double dl(
double input,
int size,
double feedback);
225 double dlFromPosition(
double input,
int size,
double feedback,
int position);
246 double lores(
double input,
double cutoff1,
double resonance);
247 double hires(
double input,
double cutoff1,
double resonance);
248 double bandpass(
double input,
double cutoff1,
double resonance);
249 double lopass(
double input,
double cutoff);
250 double hipass(
double input,
double cutoff);
254 void setCutoff(
double cut)
259 void setResonance(
double res)
269 double getResonance()
298 void stereo(
double input, std::vector<double> &two,
double x);
299 void quad(
double input, std::vector<double> &four,
double x,
double y);
300 void ambisonic(
double input, std::vector<double> &eight,
double x,
double y,
double z);
309 T alpha, alphaReciprocal;
319 init(initAlpha, initVal);
322 void init(T initAlpha, T initVal)
325 alphaReciprocal = 1.0 - alpha;
329 inline void addSample(T newVal)
331 val = (alpha * newVal) + (alphaReciprocal * val);
335 void setAlpha(T alpha_)
340 void setAlphaReciprocal(T alphaReciprocal_)
342 alphaReciprocal = alphaReciprocal_;
355 T getAlphaReciprocal()
const
357 return alphaReciprocal;
360 inline T value()
const
371 double onZX(
double input)
374 if ((previousValue <= 0.0 || firstTrigger) && input > 0)
378 previousValue = input;
384 double onChanged(
double input,
double tolerance)
387 if (fabs(input - previousValue) > tolerance)
391 previousValue = input;
396 double previousValue = 1;
397 bool firstTrigger = 1;
441 #define DECLARE_F64_ARRAY(x) client::Float64Array *x = new client::Float64Array(1);
442 #define F64_ARRAY_SIZE(x) x->get_length()
443 #define F64_ARRAY_SETFROM(to,from) to = new client::Float64Array(from);
444 #define F64_ARRAY_CLEAR(x) x->fill(0);
445 #define F64_ARRAY_AT(x,i) (*x)[i]
447 #define DECLARE_F64_ARRAY(x) std::vector<double> x;
448 #define F64_ARRAY_SIZE(x) x.size()
449 #define F64_ARRAY_SETFROM(to,from) to = from;
450 #define F64_ARRAY_CLEAR(x) x.clear();
451 #define F64_ARRAY_AT(x,i) x[i]
459 double position, recordPosition;
469 inline long getLength() {
return F64_ARRAY_SIZE(amplitudes); };
471 short myBitsPerSample;
475 DECLARE_F64_ARRAY(amplitudes);
486 myChannels = source.myChannels;
487 mySampleRate = maxiSettings::sampleRate;
488 F64_ARRAY_SETFROM(amplitudes,source.amplitudes);
500 bool load(
string fileName,
int channel = 0);
502 bool save(
string filename);
511 bool loadOgg(
string filename,
int channel = 0);
512 int setSampleFromOggBlob(vector<unsigned char> &oggBlob,
int channel = 0);
515 bool isReady() {
return F64_ARRAY_SIZE(amplitudes) > 1;}
517 void setSample(DOUBLEARRAY_REF _sampleData)
521 F64_ARRAY_SETFROM(amplitudes, _sampleData);
523 mySampleRate = 44100;
524 position = F64_ARRAY_SIZE(amplitudes) - 1;
527 void setSampleAndRate(DOUBLEARRAY_REF _sampleData,
int sampleRate)
529 setSample(_sampleData);
530 mySampleRate = sampleRate;
533 void clear() { F64_ARRAY_CLEAR(amplitudes) }
538 void loopRecord(
double newSample,
const bool recordEnabled,
const double recordMix,
double start,
double end)
540 loopRecordLag.addSample(recordEnabled);
541 if (recordPosition < start * F64_ARRAY_SIZE(amplitudes))
542 recordPosition = start * F64_ARRAY_SIZE(amplitudes);
545 double currentSample = F64_ARRAY_AT(amplitudes,(
int)recordPosition) / 32767.0;
546 newSample = (recordMix * currentSample) + ((1.0 - recordMix) * newSample);
547 newSample *= loopRecordLag.value();
548 amplitudes[(
unsigned long)recordPosition] = newSample * 32767;
551 if (recordPosition >= end * F64_ARRAY_SIZE(amplitudes))
552 recordPosition = start * F64_ARRAY_SIZE(amplitudes);
555 void reset() {position=0;}
559 double playLoop(
double start,
double end);
562 double playOnZX(
double trigger);
563 double playOnZXAtSpeed(
double trig,
double speed);
564 double playOnZXAtSpeedFromOffset(
double trig,
double speed,
double offset);
565 double playOnZXAtSpeedBetweenPoints(
double trig,
double speed,
double offset,
double length);
567 double loopSetPosOnZX(
double trigger,
double position);
569 double playOnceAtSpeed(
double speed);
571 void setPosition(
double newPos);
573 double playUntil(
double end);
574 double playUntilAtSpeed(
double end,
double speed);
576 double playAtSpeed(
double speed);
578 double playAtSpeedBetweenPointsFromPos(
double frequency,
double start,
double end,
double pos);
580 double playAtSpeedBetweenPoints(
double frequency,
double start,
double end);
582 double play4(
double frequency,
double start,
double end);
585 void normalise(
double maxLevel);
586 void autoTrim(
float alpha,
float threshold,
bool trimStart,
bool trimEnd);
593 static double inline linlin(
double val,
double inMin,
double inMax,
double outMin,
double outMax)
595 val = max(min(val, inMax), inMin);
596 return ((val - inMin) / (inMax - inMin) * (outMax - outMin)) + outMin;
599 static double inline linexp(
double val,
double inMin,
double inMax,
double outMin,
double outMax)
602 val = max(min(val, inMax), inMin);
603 return pow((outMax / outMin), (val - inMin) / (inMax - inMin)) * outMin;
606 static double inline explin(
double val,
double inMin,
double inMax,
double outMin,
double outMax)
609 val = max(min(val, inMax), inMin);
610 return (log(val / inMin) / log(inMax / inMin) * (outMax - outMin)) + outMin;
614 static double inline clamp(
double v,
const double low,
const double high)
634 double gate(
double input,
double threshold = 0.9,
long holdtime = 1,
double attack = 1,
double release = 0.9995);
635 double compressor(
double input,
double ratio,
double threshold = 0.9,
double attack = 1,
double release = 0.9995);
636 double compress(
double input);
647 void setAttack(
double attackMS);
648 void setRelease(
double releaseMS);
649 void setThreshold(
double thresholdI);
650 void setRatio(
double ratioF);
653 int attackphase, holdphase, releasephase;
672 double ar(
double input,
double attack = 1,
double release = 0.9,
long holdtime = 1,
int trigger = 0);
673 double adsr(
double input,
double attack = 1,
double decay = 0.99,
double sustain = 0.125,
double release = 0.9,
long holdtime = 1,
int trigger = 0);
674 double adsr(
double input,
int trigger);
683 void setAttack(
double attackMS);
684 void setRelease(
double releaseMS);
685 void setDecay(
double decayMS);
686 void setSustain(
double sustainL);
691 int attackphase, decayphase, sustainphase, holdphase, releasephase;
695 int getTrigger()
const
700 void setTrigger(
int trigger)
702 this->trigger = trigger;
711 static double mtof(
int midinote);
713 static double msToSamps(
double timeMs)
715 return timeMs / 1000.0 * maxiSettings::sampleRate;
722 inline double sah(
double sigIn,
double holdTimeMs)
724 double holdTimeSamples = convert::msToSamps(holdTimeMs);
726 if (phase >= holdTimeSamples)
728 phase -= holdTimeSamples;
738 double holdValue = 0;
746 inline bool zx(
double x)
749 if (previous_x <= 0 && x > 0)
758 double previous_x = 0;
767 double atanDist(
const double in,
const double shape);
769 double fastAtanDist(
const double in,
const double shape);
770 double softclip(
double x);
771 double hardclip(
double x);
774 double asymclip(
double x,
double a,
double b);
775 double fastatan(
double x);
778 inline double maxiNonlinearity::asymclip(
double x,
double a,
double b)
800 inline double maxiNonlinearity::hardclip(
double x)
802 x = x >= 1 ? 1 : (x <= -1 ? -1 : x);
805 inline double maxiNonlinearity::softclip(
double x)
817 x = (2 / 3.0) * (x - pow(x, 3) / 3.0);
822 inline double maxiNonlinearity::fastatan(
double x)
824 return (x / (1.0 + 0.28 * (x * x)));
827 inline double maxiNonlinearity::atanDist(
const double in,
const double shape)
830 out = (1.0 / atan(shape)) * atan(in * shape);
834 inline double maxiNonlinearity::fastAtanDist(
const double in,
const double shape)
837 out = (1.0 / fastatan(shape)) * fastatan(in * shape);
850 double flange(
const double input,
const unsigned int delay,
const double feedback,
const double speed,
const double depth);
855 inline double maxiFlanger::flange(
const double input,
const unsigned int delay,
const double feedback,
const double speed,
const double depth)
859 double lfoVal = lfo.triangle(speed);
860 output = dl.dl(input, delay + (lfoVal * depth * delay) + 1, feedback);
861 double normalise = (1 - fabs(output));
863 return (output + input) / 2.0;
873 double chorus(
const double input,
const unsigned int delay,
const double feedback,
const double speed,
const double depth);
879 inline double maxiChorus::chorus(
const double input,
const unsigned int delay,
const double feedback,
const double speed,
const double depth)
882 double output1, output2;
883 double lfoVal = lfo.noise();
884 lfoVal = lopass.lores(lfoVal, speed, 1.0) * 2.0;
885 output1 = dl.dl(input, delay + (lfoVal * depth * delay) + 1, feedback);
886 output2 = dl2.dl(input, (delay + (lfoVal * depth * delay * 1.02) + 1) * 0.98, feedback * 0.99);
887 output1 *= (1.0 - fabs(output1));
888 output2 *= (1.0 - fabs(output2));
889 return (output1 + output2 + input) / 3.0;
892 template <
typename T>
902 void setAttack(T attackMS)
904 attack = pow(0.01, 1.0 / (attackMS * maxiSettings::sampleRate * 0.001));
906 void setRelease(T releaseMS)
908 release = pow(0.01, 1.0 / (releaseMS * maxiSettings::sampleRate * 0.001));
910 inline T play(T input)
914 env = attack * (env - input) + input;
916 env = release * (env - input) + input;
919 void reset() { env = 0; }
920 inline T getEnv() {
return env; }
921 inline void setEnv(T val) { env = val; }
924 T attack, release, env;
935 inline double play(
double input,
double R)
937 ym1 = input - xm1 + R * ym1;
963 maxiSVF() : v0z(0), v1(0), v2(0) { setParams(1000, 1); }
966 inline void setCutoff(
double cutoff)
968 setParams(cutoff, res);
972 inline void setResonance(
double q)
978 inline double play(
double w,
double lpmix,
double bpmix,
double hpmix,
double notchmix)
980 double low, band, high, notch;
983 double v3 = w + v0z - 2.0 * v2z;
984 v1 += g1 * v3 - g2 * v1z;
985 v2 += g3 * v3 + g4 * v1z;
989 high = w - k * v1 - v2;
991 return (low * lpmix) + (band * bpmix) + (high * hpmix) + (notch * notchmix);
995 inline void setParams(
double _freq,
double _res)
999 g = tan(PI * freq / maxiSettings::sampleRate);
1000 damping = res == 0 ? 0 : 1.0 / res;
1002 ginv = g / (1.0 + g * (g + k));
1004 g2 = 2.0 * (g + k) * ginv;
1009 double v0z, v1, v2, g, damping, k, ginv, g1, g2, g3, g4;
1028 inline double play(
double input)
1030 v[0] = input - (b1 * v[1]) - (b2 * v[2]);
1031 double y = (a0 * v[0]) + (a1 * v[1]) + (a2 * v[2]);
1036 inline void set(filterTypes filtType,
double cutoff,
double Q,
double peakGain)
1039 double V = pow(10.0, abs(peakGain) / 20.0);
1040 double K = tan(PI * cutoff / maxiSettings::sampleRate);
1044 norm = 1.0 / (1.0 + K / Q + K * K);
1048 b1 = 2.0 * (K * K - 1.0) * norm;
1049 b2 = (1.0 - K / Q + K * K) * norm;
1053 norm = 1. / (1. + K / Q + K * K);
1057 b1 = 2 * (K * K - 1) * norm;
1058 b2 = (1 - K / Q + K * K) * norm;
1062 norm = 1. / (1. + K / Q + K * K);
1066 b1 = 2. * (K * K - 1.) * norm;
1067 b2 = (1. - K / Q + K * K) * norm;
1071 norm = 1. / (1. + K / Q + K * K);
1072 a0 = (1. + K * K) * norm;
1073 a1 = 2. * (K * K - 1.) * norm;
1076 b2 = (1. - K / Q + K * K) * norm;
1080 if (peakGain >= 0.0)
1082 norm = 1. / (1. + 1. / Q * K + K * K);
1083 a0 = (1. + V / Q * K + K * K) * norm;
1084 a1 = 2. * (K * K - 1.) * norm;
1085 a2 = (1. - V / Q * K + K * K) * norm;
1087 b2 = (1. - 1. / Q * K + K * K) * norm;
1091 norm = 1. / (1. + V / Q * K + K * K);
1092 a0 = (1. + 1 / Q * K + K * K) * norm;
1093 a1 = 2. * (K * K - 1) * norm;
1094 a2 = (1. - 1. / Q * K + K * K) * norm;
1096 b2 = (1. - V / Q * K + K * K) * norm;
1102 norm = 1. / (1. + SQRT2 * K + K * K);
1103 a0 = (1. + sqrt(2. * V) * K + V * K * K) * norm;
1104 a1 = 2. * (V * K * K - 1.) * norm;
1105 a2 = (1. - sqrt(2. * V) * K + V * K * K) * norm;
1106 b1 = 2. * (K * K - 1.) * norm;
1107 b2 = (1. - SQRT2 * K + K * K) * norm;
1111 norm = 1. / (1. + sqrt(2. * V) * K + V * K * K);
1112 a0 = (1. + SQRT2 * K + K * K) * norm;
1113 a1 = 2. * (K * K - 1.) * norm;
1114 a2 = (1. - SQRT2 * K + K * K) * norm;
1115 b1 = 2. * (V * K * K - 1.) * norm;
1116 b2 = (1. - sqrt(2. * V) * K + V * K * K) * norm;
1122 norm = 1. / (1. + SQRT2 * K + K * K);
1123 a0 = (V + sqrt(2. * V) * K + K * K) * norm;
1124 a1 = 2. * (K * K - V) * norm;
1125 a2 = (V - sqrt(2. * V) * K + K * K) * norm;
1126 b1 = 2. * (K * K - 1) * norm;
1127 b2 = (1. - SQRT2 * K + K * K) * norm;
1131 norm = 1. / (V + sqrt(2. * V) * K + K * K);
1132 a0 = (1. + SQRT2 * K + K * K) * norm;
1133 a1 = 2. * (K * K - 1.) * norm;
1134 a2 = (1. - SQRT2 * K + K * K) * norm;
1135 b1 = 2. * (K * K - V) * norm;
1136 b2 = (V - sqrt(2. * V) * K + K * K) * norm;
1143 double a0 = 0, a1 = 0, a2 = 0, b1 = 0, b2 = 0;
1144 filterTypes filterType;
1145 const double SQRT2 = sqrt(2.0);
1146 double v[3] = {0, 0, 0};
1153 static vector<double> xfade(vector<double> &ch1, vector<double> &ch2,
double xfader)
1155 xfader = maxiMap::clamp(xfader, -1, 1);
1156 double xfNorm = maxiMap::linlin(xfader, -1, 1, 0, 1);
1157 double gainCh1 = sqrt(1.0 - xfNorm);
1158 double gainCh2 = sqrt(xfNorm);
1159 vector<double> output(ch1.size(), 0.0);
1160 for (
size_t i = 0; i < output.size(); i++)
1162 output[i] = (ch1[i] * gainCh1) + (ch2[i] * gainCh2);
1166 static double xfade(
double ch1,
double ch2,
double xfader)
1168 vector<double> vch1 = {ch1};
1169 vector<double> vch2 = {ch2};
1170 return maxiXFade::xfade(vch1, vch2, xfader)[0];
1178 inline double play(
double trigger)
1182 if (trigEnable && !triggered)
1184 triggered = (trigger > 0.0 && lastTrigVal <= 0.0);
1185 lineValue = lineStart;
1192 lineComplete = lineValue <= lineEnd;
1196 lineComplete = lineValue >= lineEnd;
1206 lastTrigVal = trigger;
1210 inline void prepare(
double start,
double end,
double durationMs,
bool isOneShot)
1212 lineValue = lineStart;
1215 double lineMag = end - start;
1216 double durInSamples = durationMs / 1000.0 * maxiSettings::sampleRate;
1217 inc = lineMag / durInSamples;
1218 oneShot = isOneShot;
1221 inline void triggerEnable(
double on)
1223 trigEnable = on > 0.0;
1225 inline bool isLineComplete()
1227 return lineComplete;
1232 double lineValue = 0;
1234 double lastTrigVal = -1;
1235 double trigEnable =
false;
1236 double triggered =
false;
1237 bool lineComplete =
false;
1238 double lineStart = 0;
1244 lineComplete =
false;
1251 static double add(
double x,
double y)
1255 static double div(
double x,
double y)
1259 static double mul(
double x,
double y)
1263 static double sub(
double x,
double y)
1267 static double gt(
double x,
double y)
1271 static double lt(
double x,
double y)
1275 static double gte(
double x,
double y)
1279 static double lte(
double x,
double y)
1283 static double mod(
double x,
double y)
1287 static double abs(
double x)
1291 static double xpowy(
double x,
double y)
1303 inline double play(
double freq,
double K, std::vector<double> phases)
1306 double phaseAdj = 0;
1307 for (
double v : phases)
1309 phaseAdj += sin(v - phase);
1311 phase += dt * (freq + ((K / phases.size()) * phaseAdj));
1318 inline void setPhase(
double newPhase) { phase = newPhase; }
1319 inline double getPhase() {
return phase; }
1323 double dt = TWOPI / maxiSettings::sampleRate;
1335 void setPhases(
const std::vector<double> &phases)
1338 for (
double v : phases)
1340 oscs[iOsc].setPhase(v);
1345 void setPhase(
const double phase,
const size_t oscillatorIdx)
1347 oscs[oscillatorIdx].setPhase(phase);
1350 double getPhase(
size_t i)
1352 return oscs[i].getPhase();
1360 double play(
double freq,
double K)
1364 for (
size_t i = 0; i < phases.size(); i++)
1366 phases[i] = oscs[i].getPhase();
1368 for (
auto &v : oscs)
1370 mix += v.play(freq, K, phases);
1372 return mix / phases.size();
1376 std::vector<maxiKuramotoOscillator> oscs;
1377 std::vector<double> phases;
1389 void setPhase(
const double phase,
const size_t oscillatorIdx)
1391 oscs[oscillatorIdx].setPhase(phase);
1394 void setPhases(
const std::vector<double> &phases)
1397 for (
double v : phases)
1399 oscs[iOsc].setPhase(v);
1405 double play(
double freq,
double K)
1411 for (
size_t i = 0; i < phases.size(); i++)
1413 phases[i] = oscs[i].getPhase();
1416 for (
auto &v : oscs)
1418 mix += v.play(freq, update ? K : 0, phases);
1421 return mix / phases.size();
1424 double getPhase(
size_t i)
1426 return maxiKuramotoOscillatorSet::getPhase(i);
1431 return maxiKuramotoOscillatorSet::size();
1441 typedef uint32_t bitsig;
1447 static bitsig sig(bitsig v) {
return v; }
1449 static bitsig at(
const bitsig v,
const bitsig idx)
1451 return 1 & (v >> idx);
1453 static bitsig shl(
const bitsig v,
const bitsig shift)
1457 static bitsig shr(
const bitsig v,
const bitsig shift)
1461 static bitsig r(
const bitsig v,
const bitsig offset,
const bitsig width)
1463 bitsig mask = maxiBits::l(width);
1464 bitsig shift = offset - width + 1;
1466 x = v & shl(mask, shift);
1470 static bitsig land(
const bitsig v,
const bitsig x)
1474 static bitsig lor(
const bitsig v,
const bitsig x)
1478 static bitsig lxor(
const bitsig v,
const bitsig x)
1482 static bitsig neg(
const bitsig v)
1486 static bitsig inc(
const bitsig v)
1490 static bitsig dec(
const bitsig v)
1494 static bitsig add(
const bitsig v,
const bitsig m)
1498 static bitsig sub(
const bitsig v,
const bitsig m)
1502 static bitsig mul(
const bitsig v,
const bitsig m)
1506 static bitsig div(
const bitsig v,
const bitsig m)
1510 static bitsig gt(
const bitsig v,
const bitsig m)
1514 static bitsig lt(
const bitsig v,
const bitsig m)
1518 static bitsig gte(
const bitsig v,
const bitsig m)
1522 static bitsig lte(
const bitsig v,
const bitsig m)
1526 static bitsig eq(
const bitsig v,
const bitsig m)
1530 static bitsig ct(
const bitsig v,
const bitsig width)
1533 for (
size_t i = 0; i < width; i++)
1535 x += (v & (1 << i)) > 0;
1539 static bitsig l(
const bitsig width)
1542 for (
size_t i = 0; i < width; i++)
1549 static bitsig noise()
1551 bitsig v =
static_cast<bitsig
>(rand());
1555 static double toSignal(
const bitsig t)
1557 return maxiMap::linlin(t, 0, (
double)std::numeric_limits<uint32_t>::max(), -1, 1);
1560 static double toTrigSignal(
const bitsig t)
1562 return t > 0 ? 1.0 : -1.0;
1565 static bitsig fromSignal(
const double t)
1567 const bitsig halfRange = (std::numeric_limits<uint32_t>::max() / 2);
1568 const bitsig val = halfRange + (t * (halfRange - 1));
1581 double count(
double incTrigger,
double resetTrigger)
1583 if (inctrig.onZX(incTrigger))
1587 if (rstrig.onZX(resetTrigger))
1603 double pull(
const double trigSig,
double indexSig, DOUBLEARRAY_REF _values)
1608 NORMALISE_ARRAY_TYPE(_values, values)
1609 if (trig.onZX(trigSig))
1615 size_t arrayIndex =
static_cast<size_t>(floor(indexSig * 0.99999999 * values.size()));
1616 value = values[arrayIndex];
1630 double playTrig(
double phase, DOUBLEARRAY_REF times)
1636 size_t seqlen = F64_ARRAY_SIZE(times);
1637 for(
size_t i=0; i < seqlen; i++) sum += F64_ARRAY_AT(times,i);
1638 double accumulatedTime = 0;
1639 for (
size_t i = 0; i < seqlen; i++)
1641 accumulatedTime += F64_ARRAY_AT(times,i);
1642 double normalisedTime = accumulatedTime / sum;
1643 if (normalisedTime == 1.0)
1644 normalisedTime = 0.0;
1645 if (prevPhase > phase)
1648 prevPhase = -1.0 / maxiSettings::sampleRate;
1650 if ((prevPhase <= normalisedTime && phase > normalisedTime))
1660 double playValues(
double phase, DOUBLEARRAY_REF times, DOUBLEARRAY_REF values)
1664 size_t vallen = F64_ARRAY_SIZE(values);
1665 if (lengthOfValues != vallen)
1667 lengthOfValues = vallen;
1668 counter = lengthOfValues - 1;
1670 if (playTrig(phase, times))
1673 if (counter == vallen)
1678 return F64_ARRAY_AT(values,counter);
1682 double prevPhase = 0;
1684 size_t lengthOfValues = 0;
Definition: maximilian.h:709
Definition: maximilian.h:1384
Definition: maximilian.h:1015
Definition: maximilian.h:1439
Definition: maximilian.h:867
Definition: maximilian.h:1579
Definition: maximilian.h:931
Definition: maximilian.h:214
Definition: maximilian.h:629
Definition: maximilian.h:669
Definition: maximilian.h:894
Definition: maximilian.h:171
Definition: maximilian.h:229
Definition: maximilian.h:844
Definition: maximilian.h:1600
Definition: maximilian.h:1300
Definition: maximilian.h:1328
Definition: maximilian.h:307
Definition: maximilian.h:1175
Definition: maximilian.h:590
Definition: maximilian.h:1249
Definition: maximilian.h:277
Definition: maximilian.h:763
Definition: maximilian.h:142
Definition: maximilian.h:1627
Definition: maximilian.h:961
Definition: maximilian.h:720
Definition: maximilian.h:456
Definition: maximilian.h:102
Definition: maximilian.h:367
Definition: maximilian.h:1150
Definition: maximilian.h:743