20 #ifndef NO_EFFECTS_SUITE
24 #include "AudioEffects/AudioEffect.h"
28 typedef float effectsuite_t;
47 virtual effectsuite_t processDouble(effectsuite_t inputSample) = 0;
54 virtual effect_t process(effect_t inputSample)
override {
55 return active_flag ? 32767.0f * processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0f) : inputSample;
82 srand(
static_cast<unsigned>(time(0)));
106 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
108 for (
int j = 0; j < 35; j += 1)
110 (sin((2. * effectsuite_t(j) + 1) * i * radPerSec)) /
111 (2. * effectsuite_t(j) + 1);
119 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
121 for (
int j = 0; j < 35; j += 1)
122 waveTable[i] += (sin((2 * j + 1) * i * radPerSec)) / (2 * j + 1);
130 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
132 for (
int j = 1; j < 11; j += 1)
133 waveTable[i] += pow(-1, j) * sin(j * radPerSec * i) / effectsuite_t(j);
140 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
148 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
150 waveTable[i] = (sin(i * radPerSec) + 1) * .5f;
178 const effectsuite_t lo = -1.;
179 const effectsuite_t hi = 1.;
180 return lo +
static_cast<effectsuite_t
>(rand()) /
181 (
static_cast<effectsuite_t
>(RAND_MAX / (hi - lo)));
217 void printInterpTable() {
218 for (
int j = 0; j < res; j++) {
219 for (
int i = 0; i < order; i++) {
220 std::cout << interpTable[i][j] <<
'\t';
230 effectsuite_t *polynomial_normaliser =
new effectsuite_t[order];
231 if (!polynomial_normaliser) {
234 std::fill(polynomial_normaliser, polynomial_normaliser + order, 1);
235 effectsuite_t *alphas =
new effectsuite_t[res];
240 for (
int i = 0; i < res; i++) {
241 alphas[i] = (i / float(res)) - 0.5;
244 effectsuite_t *anchors =
new effectsuite_t[order];
246 if ((order % 2) == 0) {
247 for (
int i = 0; i < order; i++) {
248 anchors[i] = -(effectsuite_t(order) - 1) * 0.5 + effectsuite_t(i);
249 std::fill(interpTable[i], interpTable[i] + res, 1);
252 for (
int i = 0; i < order; i++) {
253 anchors[i] = (-(effectsuite_t(order)) * 0.5) + effectsuite_t(i);
258 for (
int q = 0; q < res; q++) {
260 for (
int j = 0; j < order; j++) {
262 for (
int m = 0; m < order; m++) {
265 polynomial_normaliser[j] =
266 polynomial_normaliser[j] * (anchors[j] - anchors[m]);
268 interpTable[j][q] *= (alphas[q] - anchors[m]);
271 interpTable[j][q] /= polynomial_normaliser[j];
275 delete[] polynomial_normaliser;
303 const int orderHalf = order * .5;
305 effectsuite_t interpOut = 0;
306 int intBufferIndex = floor(bufferIndex);
307 int alphaIndex = int(floor((bufferIndex - intBufferIndex) * res));
309 for (
int i = 0; i < order; i++) {
312 interpOut += (interpTable[i][alphaIndex]) * (
waveTable[interpIndex]);
332 const int n0 = floor(bufferIndex);
334 const int n2 = (n0 + (2 * freq)) %
sampleRate;
335 const effectsuite_t alpha = bufferIndex - n0;
341 const effectsuite_t d = (-c) / 3;
342 return a + (b * alpha) + (c * alpha * alpha) + (d * alpha * alpha * alpha);
356 static const int order = 4;
357 static const int res = 100;
358 effectsuite_t interpTable[order][res];
359 bool is_noise =
false;
380 return p_mod->isNoise() ? max_value * p_mod->
readNoise() : max_value * p_mod->
readTable(freq);
386 float max_value=32767;
438 const int order = interpOrder;
439 const int res = interpResolution;
440 effectsuite_t **interpTable =
new effectsuite_t *[order];
445 for (
int i = 0; i < order; i++) {
446 interpTable[i] =
new effectsuite_t[res + 1];
447 if (!interpTable[i]) {
450 std::fill(interpTable[i], interpTable[i] + res, 1);
453 effectsuite_t *polynomial_normaliser =
new effectsuite_t[order];
454 if (!polynomial_normaliser) {
457 std::fill(polynomial_normaliser, polynomial_normaliser + order, 1);
458 effectsuite_t *alphas =
new effectsuite_t[res];
463 for (
int i = 0; i < res; i++) {
464 alphas[i] = (i / float(res)) - 0.5;
467 effectsuite_t *anchors =
new effectsuite_t[order];
469 if ((order % 2) == 0) {
470 for (
int i = 0; i < order; i++) {
471 anchors[i] = -(effectsuite_t(order) - 1) * 0.5 + effectsuite_t(i);
474 for (
int i = 0; i < order; i++) {
475 anchors[i] = (-(effectsuite_t(order)) * 0.5) + effectsuite_t(i);
480 for (
int q = 0; q < res; q++) {
482 for (
int j = 0; j < order; j++) {
484 for (
int m = 0; m < order; m++) {
487 polynomial_normaliser[j] =
488 polynomial_normaliser[j] * (anchors[j] - anchors[m]);
490 interpTable[j][q] *= (alphas[q] - anchors[m]);
493 interpTable[j][q] /= polynomial_normaliser[j];
496 delete[] polynomial_normaliser;
540 currentDelayWriteIndex++;
549 currentDelayReadIndex += indexInc;
551 currentDelayReadIndex = 0;
553 if (currentDelayReadIndex < 0) {
554 currentDelayReadIndex = 0;
563 currentDelayReadIndex = index;
565 currentDelayReadIndex = 0;
567 if (currentDelayReadIndex < 0) {
568 currentDelayReadIndex = 0;
587 const int order = interpOrder;
588 const int orderHalf = order * .5;
589 const int res = interpResolution;
590 effectsuite_t interpOut = 0;
591 int intBufferIndex = floor(bufferIndex);
592 int alphaIndex = int(floor((bufferIndex - intBufferIndex) * res));
594 for (
int i = 0; i < order; i++) {
595 int interpIndex = (i + 1 - orderHalf) + intBufferIndex;
597 if (interpIndex < 0) {
617 int currentDelayWriteIndex = 0;
618 effectsuite_t currentDelayReadIndex = 0;
619 static const int interpOrder = 4;
620 static const int interpResolution = 1000;
651 effectsuite_t outSample = 0;
671 virtual effect_t
process(effect_t inputSample)
override {
672 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
715 effectsuite_t poles = (effectsuite_t)
filterOrder - 1;
716 int order = (int)poles;
721 effectsuite_t Es, Vx, Kx;
723 Es = sqrt(pow(1 / (1 - ripple), 2) - 1);
724 Vx = (1 / poles) * log(1 / Es + sqrt(1 / (pow(Es, 2)) + 1));
725 Kx = (1 / poles) * log(1 / Es + sqrt(1 / (pow(Es, 2)) - 1));
732 const effectsuite_t T = 2 * tan(.5);
733 const effectsuite_t W = 2 * pi * cutFreq;
739 K = sin(.5 - W / 2) / sin(.5 + W / 2);
743 K = -cos(.5 + W / 2) / cos(W / 2 - .5);
747 for (
int i = 0; i < (order / 2); i++) {
749 const effectsuite_t alpha = pi / (2 * poles) + (i - 1) * (pi / poles);
751 effectsuite_t Rp, Ip;
753 Rp = -cos(alpha) * sinh(Vx) / Kx;
754 Ip = sin(alpha) * cosh(Vx) / Kx;
760 const effectsuite_t M = pow(Rp, 2) + pow(Ip, 2);
761 const effectsuite_t D = 4 - 4 * Rp * T + M * T;
763 const effectsuite_t X0 = (pow(T, 2)) / D;
764 const effectsuite_t X1 = (2 * pow(T, 2)) / D;
765 const effectsuite_t X2 = X0;
767 const effectsuite_t Y1 = (8 - (2 * M * pow(T, 2))) / D;
768 const effectsuite_t Y2 = (-4 - 4 * Rp * T - M * T) / D;
771 const effectsuite_t _D1 = 1 / (1 + Y1 * K - Y2 * pow(K, 2));
773 const effectsuite_t _A0 = (X0 - X1 * K + X2 * pow(K, 2)) * _D1;
774 effectsuite_t _A1 = (-2 * X0 * K + X1 + X1 * pow(K, 2) - 2 * X2 * K) * _D1;
775 const effectsuite_t _A2 = (X0 * pow(K, 2) - X1 * K + X2) * _D1;
777 effectsuite_t _B1 = (2 * K + Y1 + Y1 * pow(K, 2) - 2 * Y2 * K) * _D1;
778 const effectsuite_t B2 = (-(pow(K, 2)) - Y1 * K + Y2) * _D1;
780 if (shelfType == 1) {
785 for (
int j = 0; j < 22; j++) {
789 for (
int j = 2; j < 22; j++) {
803 effectsuite_t SA = 0;
804 effectsuite_t SB = 0;
805 if (shelfType == 0) {
811 for (
int j = 0; j < order; j++) {
817 const effectsuite_t gain = SA / (1 - SB);
858 effectsuite_t gain = 0;
933 firTemp =
new effectsuite_t[22];
934 iirTemp =
new effectsuite_t[22];
942 effectsuite_t
rms(effectsuite_t sample) {
944 effectsuite_t rmsValue = 0;
952 rmsValue = sqrt(rmsValue);
986 int samplingRate = 0;
995 constexpr
static const effectsuite_t pi = 3.141592653589793;
1067 const effectsuite_t delayAmount =
1070 ((currentDelayWriteIndex - waveDelay) -
1071 trunc(currentDelayWriteIndex - waveDelay));
1086 swing = readSpeed * extSampleRate * 5;
1087 base = readSpeed * extSampleRate * 20;
1121 const effectsuite_t readSpeed = ((
readNoise() + 1) * .5) * .0005;
1203 feedbackGain = gain;
1216 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
1231 }
else if (gain < -1.) {
1238 effectsuite_t delayGain = .707, feedbackGain = 0.0;
1260 currentDelaySamples = maxDelayInSamples;
1261 targetDelaySamples = maxDelayInSamples;
1291 feedbackGain = gain;
1306 effectsuite_t outSample =
getSplineOut(readHeadIndex) + (inputSample * 1);
1307 if (delayTimeChanged) {
1309 const effectsuite_t difference = (currentDelaySamples - targetDelaySamples);
1310 const effectsuite_t increment =
delayIncrement * (difference / fabs(difference));
1311 currentDelaySamples -= increment;
1312 readHeadIndex += 1 + increment;
1314 if (count > floor(delayTransitionTimeInSamples)) {
1315 currentDelaySamples = targetDelaySamples;
1316 readHeadIndex = floor(readHeadIndex);
1317 delayTimeChanged =
false;
1327 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
1342 delayTimeChanged =
true;
1343 targetDelaySamples = delayInSamples;
1344 const effectsuite_t delayTimeDifference = currentDelaySamples - targetDelaySamples;
1345 delayIncrement = delayTimeDifference / delayTransitionTimeInSamples;
1354 delayTransitionTimeInSamples = seconds * sampleRate;
1369 }
else if (gain < -1.) {
1383 const int n0 = floor(bufferIndex);
1386 const effectsuite_t alpha = bufferIndex - n0;
1393 const effectsuite_t d = (-c) * 0.33333;
1394 return a + (b * alpha) + (c * alpha * alpha) + (d * alpha * alpha * alpha);
1398 effectsuite_t delayGain = .707;
1399 effectsuite_t feedbackGain = 0.;
1400 effectsuite_t readHeadIndex;
1401 unsigned int writeHeadIndex;
1402 effectsuite_t currentDelaySamples;
1403 effectsuite_t targetDelaySamples;
1412 effectsuite_t delayTransitionTimeInSamples;
1415 bool delayTimeChanged =
false;
1458 modulationDepth = depth;
1466 modulationRate = rate;
1485 const effectsuite_t out = ((1 - fabs(effectGain * .2)) * (inputSample) +
1491 void setupSimpleFlanger(effectsuite_t extSampleRate) {
1509 }
else if (gain < -1.) {
1519 const effectsuite_t cyclesPerSample = modulationRate *
timeStep;
1528 modulationIndex = (currentDelayWriteIndex -
1529 (modulationDepth * (1 + (sin(modulationAngle))))) -
1533 (modulationIndex - floor(modulationIndex));
1541 effectsuite_t modulationDepth = 1000, modulationRate = 0, effectGain = .01;
1543 effectsuite_t modulationIndex = 0;