23#include "AudioTools/CoreAudio/AudioEffects/AudioEffect.h"
24#include "AudioTools/CoreAudio/AudioStreams.h"
27# define PI 3.141592653589793f
32typedef float effectsuite_t;
51 virtual effectsuite_t processDouble(effectsuite_t inputSample) = 0;
58 virtual effect_t process(effect_t inputSample)
override {
59 return active_flag ? 32767.0f * processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0f) : inputSample;
86 srand(
static_cast<unsigned>(time(0)));
110 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
112 for (
int j = 0; j < 35; j += 1)
114 (sin((2. * effectsuite_t(j) + 1) * i * radPerSec)) /
115 (2. * effectsuite_t(j) + 1);
123 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
125 for (
int j = 0; j < 35; j += 1)
126 waveTable[i] += (sin((2 * j + 1) * i * radPerSec)) / (2 * j + 1);
134 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
136 for (
int j = 1; j < 11; j += 1)
137 waveTable[i] += pow(-1, j) * sin(j * radPerSec * i) / effectsuite_t(j);
144 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
152 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
154 waveTable[i] = (sin(i * radPerSec) + 1) * .5f;
182 const effectsuite_t lo = -1.;
183 const effectsuite_t hi = 1.;
184 return lo +
static_cast<effectsuite_t
>(rand()) /
185 (
static_cast<effectsuite_t
>(RAND_MAX / (hi - lo)));
221 void printInterpTable() {
222 for (
int j = 0; j < res; j++) {
223 for (
int i = 0; i < order; i++) {
224 std::cout << interpTable[i][j] <<
'\t';
234 effectsuite_t *polynomial_normaliser =
new effectsuite_t[order];
235 if (!polynomial_normaliser) {
238 std::fill(polynomial_normaliser, polynomial_normaliser + order, 1);
239 effectsuite_t *alphas =
new effectsuite_t[res];
244 for (
int i = 0; i < res; i++) {
245 alphas[i] = (i / float(res)) - 0.5;
248 effectsuite_t *anchors =
new effectsuite_t[order];
250 if ((order % 2) == 0) {
251 for (
int i = 0; i < order; i++) {
252 anchors[i] = -(effectsuite_t(order) - 1) * 0.5 + effectsuite_t(i);
253 std::fill(interpTable[i], interpTable[i] + res, 1);
256 for (
int i = 0; i < order; i++) {
257 anchors[i] = (-(effectsuite_t(order)) * 0.5) + effectsuite_t(i);
262 for (
int q = 0; q < res; q++) {
264 for (
int j = 0; j < order; j++) {
266 for (
int m = 0; m < order; m++) {
269 polynomial_normaliser[j] =
270 polynomial_normaliser[j] * (anchors[j] - anchors[m]);
272 interpTable[j][q] *= (alphas[q] - anchors[m]);
275 interpTable[j][q] /= polynomial_normaliser[j];
279 delete[] polynomial_normaliser;
307 const int orderHalf = order * .5;
309 effectsuite_t interpOut = 0;
310 int intBufferIndex = floor(bufferIndex);
311 int alphaIndex = int(floor((bufferIndex - intBufferIndex) * res));
313 for (
int i = 0; i < order; i++) {
316 interpOut += (interpTable[i][alphaIndex]) * (
waveTable[interpIndex]);
336 const int n0 = floor(bufferIndex);
338 const int n2 = (n0 + (2 * freq)) %
sampleRate;
339 const effectsuite_t alpha = bufferIndex - n0;
345 const effectsuite_t d = (-c) / 3;
346 return a + (b * alpha) + (c * alpha * alpha) + (d * alpha * alpha * alpha);
360 static const int order = 4;
361 static const int res = 100;
362 effectsuite_t interpTable[order][res];
363 bool is_noise =
false;
384 return p_mod->isNoise() ? max_value * p_mod->
readNoise() : max_value * p_mod->
readTable(freq);
390 float max_value=32767;
442 const int order = interpOrder;
443 const int res = interpResolution;
444 effectsuite_t **interpTable =
new effectsuite_t *[order];
449 for (
int i = 0; i < order; i++) {
450 interpTable[i] =
new effectsuite_t[res + 1];
451 if (!interpTable[i]) {
454 std::fill(interpTable[i], interpTable[i] + res, 1);
457 effectsuite_t *polynomial_normaliser =
new effectsuite_t[order];
458 if (!polynomial_normaliser) {
461 std::fill(polynomial_normaliser, polynomial_normaliser + order, 1);
462 effectsuite_t *alphas =
new effectsuite_t[res];
467 for (
int i = 0; i < res; i++) {
468 alphas[i] = (i / float(res)) - 0.5;
471 effectsuite_t *anchors =
new effectsuite_t[order];
473 if ((order % 2) == 0) {
474 for (
int i = 0; i < order; i++) {
475 anchors[i] = -(effectsuite_t(order) - 1) * 0.5 + effectsuite_t(i);
478 for (
int i = 0; i < order; i++) {
479 anchors[i] = (-(effectsuite_t(order)) * 0.5) + effectsuite_t(i);
484 for (
int q = 0; q < res; q++) {
486 for (
int j = 0; j < order; j++) {
488 for (
int m = 0; m < order; m++) {
491 polynomial_normaliser[j] =
492 polynomial_normaliser[j] * (anchors[j] - anchors[m]);
494 interpTable[j][q] *= (alphas[q] - anchors[m]);
497 interpTable[j][q] /= polynomial_normaliser[j];
500 delete[] polynomial_normaliser;
544 currentDelayWriteIndex++;
553 currentDelayReadIndex += indexInc;
555 currentDelayReadIndex = 0;
557 if (currentDelayReadIndex < 0) {
558 currentDelayReadIndex = 0;
567 currentDelayReadIndex = index;
569 currentDelayReadIndex = 0;
571 if (currentDelayReadIndex < 0) {
572 currentDelayReadIndex = 0;
591 const int order = interpOrder;
592 const int orderHalf = order * .5;
593 const int res = interpResolution;
594 effectsuite_t interpOut = 0;
595 int intBufferIndex = floor(bufferIndex);
596 int alphaIndex = int(floor((bufferIndex - intBufferIndex) * res));
598 for (
int i = 0; i < order; i++) {
599 int interpIndex = (i + 1 - orderHalf) + intBufferIndex;
601 if (interpIndex < 0) {
621 int currentDelayWriteIndex = 0;
622 effectsuite_t currentDelayReadIndex = 0;
623 static const int interpOrder = 4;
624 static const int interpResolution = 1000;
655 effectsuite_t outSample = 0;
675 virtual effect_t
process(effect_t inputSample)
override {
676 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
675 virtual effect_t
process(effect_t inputSample)
override {
…}
719 effectsuite_t poles = (effectsuite_t)
filterOrder - 1;
720 int order = (int)poles;
725 effectsuite_t Es, Vx, Kx;
727 Es = sqrt(pow(1 / (1 - ripple), 2) - 1);
728 Vx = (1 / poles) * log(1 / Es + sqrt(1 / (pow(Es, 2)) + 1));
729 Kx = (1 / poles) * log(1 / Es + sqrt(1 / (pow(Es, 2)) - 1));
736 const effectsuite_t T = 2.0f * tan(.5f);
737 const effectsuite_t W = 2.0f * PI * cutFreq;
743 K = sin(.5 - W / 2) / sin(.5 + W / 2);
747 K = -cos(.5 + W / 2) / cos(W / 2 - .5);
751 for (
int i = 0; i < (order / 2); i++) {
753 const effectsuite_t alpha = PI / (2 * poles) + (i - 1) * (PI / poles);
755 effectsuite_t Rp, Ip;
757 Rp = -cos(alpha) * sinh(Vx) / Kx;
758 Ip = sin(alpha) * cosh(Vx) / Kx;
764 const effectsuite_t M = pow(Rp, 2) + pow(Ip, 2);
765 const effectsuite_t D = 4 - 4 * Rp * T + M * T;
767 const effectsuite_t X0 = (pow(T, 2)) / D;
768 const effectsuite_t X1 = (2 * pow(T, 2)) / D;
769 const effectsuite_t X2 = X0;
771 const effectsuite_t Y1 = (8 - (2 * M * pow(T, 2))) / D;
772 const effectsuite_t Y2 = (-4 - 4 * Rp * T - M * T) / D;
775 const effectsuite_t _D1 = 1 / (1 + Y1 * K - Y2 * pow(K, 2));
777 const effectsuite_t _A0 = (X0 - X1 * K + X2 * pow(K, 2)) * _D1;
778 effectsuite_t _A1 = (-2 * X0 * K + X1 + X1 * pow(K, 2) - 2 * X2 * K) * _D1;
779 const effectsuite_t _A2 = (X0 * pow(K, 2) - X1 * K + X2) * _D1;
781 effectsuite_t _B1 = (2 * K + Y1 + Y1 * pow(K, 2) - 2 * Y2 * K) * _D1;
782 const effectsuite_t B2 = (-(pow(K, 2)) - Y1 * K + Y2) * _D1;
784 if (shelfType == 1) {
789 for (
int j = 0; j < 22; j++) {
793 for (
int j = 2; j < 22; j++) {
807 effectsuite_t SA = 0;
808 effectsuite_t SB = 0;
809 if (shelfType == 0) {
815 for (
int j = 0; j < order; j++) {
821 const effectsuite_t gain = SA / (1 - SB);
862 effectsuite_t gain = 0;
937 firTemp =
new effectsuite_t[22];
938 iirTemp =
new effectsuite_t[22];
946 effectsuite_t
rms(effectsuite_t sample) {
948 effectsuite_t rmsValue = 0;
956 rmsValue = sqrt(rmsValue);
946 effectsuite_t
rms(effectsuite_t sample) {
…}
989 int samplingRate = 0;
1068 const effectsuite_t delayAmount =
1071 ((currentDelayWriteIndex - waveDelay) -
1072 trunc(currentDelayWriteIndex - waveDelay));
1087 swing = readSpeed * extSampleRate * 5;
1088 base = readSpeed * extSampleRate * 20;
1122 const effectsuite_t readSpeed = ((
readNoise() + 1) * .5) * .0005;
1204 feedbackGain = gain;
1217 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
1232 }
else if (gain < -1.) {
1239 effectsuite_t delayGain = .707, feedbackGain = 0.0;
1261 currentDelaySamples = maxDelayInSamples;
1262 targetDelaySamples = maxDelayInSamples;
1292 feedbackGain = gain;
1307 effectsuite_t outSample =
getSplineOut(readHeadIndex) + (inputSample * 1);
1308 if (delayTimeChanged) {
1310 const effectsuite_t difference = (currentDelaySamples - targetDelaySamples);
1311 const effectsuite_t increment =
delayIncrement * (difference / fabs(difference));
1312 currentDelaySamples -= increment;
1313 readHeadIndex += 1 + increment;
1315 if (count > floor(delayTransitionTimeInSamples)) {
1316 currentDelaySamples = targetDelaySamples;
1317 readHeadIndex = floor(readHeadIndex);
1318 delayTimeChanged =
false;
1328 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
1343 delayTimeChanged =
true;
1344 targetDelaySamples = delayInSamples;
1345 const effectsuite_t delayTimeDifference = currentDelaySamples - targetDelaySamples;
1346 delayIncrement = delayTimeDifference / delayTransitionTimeInSamples;
1355 delayTransitionTimeInSamples = seconds * sampleRate;
1370 }
else if (gain < -1.) {
1384 const int n0 = floor(bufferIndex);
1387 const effectsuite_t alpha = bufferIndex - n0;
1394 const effectsuite_t d = (-c) * 0.33333;
1395 return a + (b * alpha) + (c * alpha * alpha) + (d * alpha * alpha * alpha);
1399 effectsuite_t delayGain = .707;
1400 effectsuite_t feedbackGain = 0.;
1401 effectsuite_t readHeadIndex;
1402 unsigned int writeHeadIndex;
1403 effectsuite_t currentDelaySamples;
1404 effectsuite_t targetDelaySamples;
1413 effectsuite_t delayTransitionTimeInSamples;
1416 bool delayTimeChanged =
false;
1459 modulationDepth = depth;
1467 modulationRate = rate;
1486 const effectsuite_t out = ((1 - fabs(effectGain * .2)) * (inputSample) +
1492 void setupSimpleFlanger(effectsuite_t extSampleRate) {
1510 }
else if (gain < -1.) {
1520 const effectsuite_t cyclesPerSample = modulationRate *
timeStep;
1529 modulationIndex = (currentDelayWriteIndex -
1530 (modulationDepth * (1 + (sin(modulationAngle))))) -
1534 (modulationIndex - floor(modulationIndex));
1539 effectsuite_t modulationDepth = 1000, modulationRate = 0, effectGain = .01;
1541 effectsuite_t modulationIndex = 0;