23 #include "AudioTools/CoreAudio/AudioEffects/AudioEffect.h"
26 # define PI 3.141592653589793f
31 typedef float effectsuite_t;
50 virtual effectsuite_t processDouble(effectsuite_t inputSample) = 0;
57 virtual effect_t process(effect_t inputSample)
override {
58 return active_flag ? 32767.0f * processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0f) : inputSample;
85 srand(
static_cast<unsigned>(time(0)));
109 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
111 for (
int j = 0; j < 35; j += 1)
113 (sin((2. * effectsuite_t(j) + 1) * i * radPerSec)) /
114 (2. * effectsuite_t(j) + 1);
122 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
124 for (
int j = 0; j < 35; j += 1)
125 waveTable[i] += (sin((2 * j + 1) * i * radPerSec)) / (2 * j + 1);
133 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
135 for (
int j = 1; j < 11; j += 1)
136 waveTable[i] += pow(-1, j) * sin(j * radPerSec * i) / effectsuite_t(j);
143 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
151 const effectsuite_t radPerSec = 2 * 3.1415926536f *
timeStep;
153 waveTable[i] = (sin(i * radPerSec) + 1) * .5f;
181 const effectsuite_t lo = -1.;
182 const effectsuite_t hi = 1.;
183 return lo +
static_cast<effectsuite_t
>(rand()) /
184 (
static_cast<effectsuite_t
>(RAND_MAX / (hi - lo)));
220 void printInterpTable() {
221 for (
int j = 0; j < res; j++) {
222 for (
int i = 0; i < order; i++) {
223 std::cout << interpTable[i][j] <<
'\t';
233 effectsuite_t *polynomial_normaliser =
new effectsuite_t[order];
234 if (!polynomial_normaliser) {
237 std::fill(polynomial_normaliser, polynomial_normaliser + order, 1);
238 effectsuite_t *alphas =
new effectsuite_t[res];
243 for (
int i = 0; i < res; i++) {
244 alphas[i] = (i / float(res)) - 0.5;
247 effectsuite_t *anchors =
new effectsuite_t[order];
249 if ((order % 2) == 0) {
250 for (
int i = 0; i < order; i++) {
251 anchors[i] = -(effectsuite_t(order) - 1) * 0.5 + effectsuite_t(i);
252 std::fill(interpTable[i], interpTable[i] + res, 1);
255 for (
int i = 0; i < order; i++) {
256 anchors[i] = (-(effectsuite_t(order)) * 0.5) + effectsuite_t(i);
261 for (
int q = 0; q < res; q++) {
263 for (
int j = 0; j < order; j++) {
265 for (
int m = 0; m < order; m++) {
268 polynomial_normaliser[j] =
269 polynomial_normaliser[j] * (anchors[j] - anchors[m]);
271 interpTable[j][q] *= (alphas[q] - anchors[m]);
274 interpTable[j][q] /= polynomial_normaliser[j];
278 delete[] polynomial_normaliser;
306 const int orderHalf = order * .5;
308 effectsuite_t interpOut = 0;
309 int intBufferIndex = floor(bufferIndex);
310 int alphaIndex = int(floor((bufferIndex - intBufferIndex) * res));
312 for (
int i = 0; i < order; i++) {
315 interpOut += (interpTable[i][alphaIndex]) * (
waveTable[interpIndex]);
335 const int n0 = floor(bufferIndex);
337 const int n2 = (n0 + (2 * freq)) %
sampleRate;
338 const effectsuite_t alpha = bufferIndex - n0;
344 const effectsuite_t d = (-c) / 3;
345 return a + (b * alpha) + (c * alpha * alpha) + (d * alpha * alpha * alpha);
359 static const int order = 4;
360 static const int res = 100;
361 effectsuite_t interpTable[order][res];
362 bool is_noise =
false;
383 return p_mod->isNoise() ? max_value * p_mod->
readNoise() : max_value * p_mod->
readTable(freq);
389 float max_value=32767;
441 const int order = interpOrder;
442 const int res = interpResolution;
443 effectsuite_t **interpTable =
new effectsuite_t *[order];
448 for (
int i = 0; i < order; i++) {
449 interpTable[i] =
new effectsuite_t[res + 1];
450 if (!interpTable[i]) {
453 std::fill(interpTable[i], interpTable[i] + res, 1);
456 effectsuite_t *polynomial_normaliser =
new effectsuite_t[order];
457 if (!polynomial_normaliser) {
460 std::fill(polynomial_normaliser, polynomial_normaliser + order, 1);
461 effectsuite_t *alphas =
new effectsuite_t[res];
466 for (
int i = 0; i < res; i++) {
467 alphas[i] = (i / float(res)) - 0.5;
470 effectsuite_t *anchors =
new effectsuite_t[order];
472 if ((order % 2) == 0) {
473 for (
int i = 0; i < order; i++) {
474 anchors[i] = -(effectsuite_t(order) - 1) * 0.5 + effectsuite_t(i);
477 for (
int i = 0; i < order; i++) {
478 anchors[i] = (-(effectsuite_t(order)) * 0.5) + effectsuite_t(i);
483 for (
int q = 0; q < res; q++) {
485 for (
int j = 0; j < order; j++) {
487 for (
int m = 0; m < order; m++) {
490 polynomial_normaliser[j] =
491 polynomial_normaliser[j] * (anchors[j] - anchors[m]);
493 interpTable[j][q] *= (alphas[q] - anchors[m]);
496 interpTable[j][q] /= polynomial_normaliser[j];
499 delete[] polynomial_normaliser;
543 currentDelayWriteIndex++;
552 currentDelayReadIndex += indexInc;
554 currentDelayReadIndex = 0;
556 if (currentDelayReadIndex < 0) {
557 currentDelayReadIndex = 0;
566 currentDelayReadIndex = index;
568 currentDelayReadIndex = 0;
570 if (currentDelayReadIndex < 0) {
571 currentDelayReadIndex = 0;
590 const int order = interpOrder;
591 const int orderHalf = order * .5;
592 const int res = interpResolution;
593 effectsuite_t interpOut = 0;
594 int intBufferIndex = floor(bufferIndex);
595 int alphaIndex = int(floor((bufferIndex - intBufferIndex) * res));
597 for (
int i = 0; i < order; i++) {
598 int interpIndex = (i + 1 - orderHalf) + intBufferIndex;
600 if (interpIndex < 0) {
620 int currentDelayWriteIndex = 0;
621 effectsuite_t currentDelayReadIndex = 0;
622 static const int interpOrder = 4;
623 static const int interpResolution = 1000;
654 effectsuite_t outSample = 0;
674 virtual effect_t
process(effect_t inputSample)
override {
675 return active_flag ? 32767.0 *
processDouble(
static_cast<effectsuite_t
>(inputSample)/32767.0) : inputSample;
718 effectsuite_t poles = (effectsuite_t)
filterOrder - 1;
719 int order = (int)poles;
724 effectsuite_t Es, Vx, Kx;
726 Es = sqrt(pow(1 / (1 - ripple), 2) - 1);
727 Vx = (1 / poles) * log(1 / Es + sqrt(1 / (pow(Es, 2)) + 1));
728 Kx = (1 / poles) * log(1 / Es + sqrt(1 / (pow(Es, 2)) - 1));
735 const effectsuite_t T = 2.0f * tan(.5f);
736 const effectsuite_t W = 2.0f * PI * cutFreq;
742 K = sin(.5 - W / 2) / sin(.5 + W / 2);
746 K = -cos(.5 + W / 2) / cos(W / 2 - .5);
750 for (
int i = 0; i < (order / 2); i++) {
752 const effectsuite_t alpha = PI / (2 * poles) + (i - 1) * (PI / poles);
754 effectsuite_t Rp, Ip;
756 Rp = -cos(alpha) * sinh(Vx) / Kx;
757 Ip = sin(alpha) * cosh(Vx) / Kx;
763 const effectsuite_t M = pow(Rp, 2) + pow(Ip, 2);
764 const effectsuite_t D = 4 - 4 * Rp * T + M * T;
766 const effectsuite_t X0 = (pow(T, 2)) / D;
767 const effectsuite_t X1 = (2 * pow(T, 2)) / D;
768 const effectsuite_t X2 = X0;
770 const effectsuite_t Y1 = (8 - (2 * M * pow(T, 2))) / D;
771 const effectsuite_t Y2 = (-4 - 4 * Rp * T - M * T) / D;
774 const effectsuite_t _D1 = 1 / (1 + Y1 * K - Y2 * pow(K, 2));
776 const effectsuite_t _A0 = (X0 - X1 * K + X2 * pow(K, 2)) * _D1;
777 effectsuite_t _A1 = (-2 * X0 * K + X1 + X1 * pow(K, 2) - 2 * X2 * K) * _D1;
778 const effectsuite_t _A2 = (X0 * pow(K, 2) - X1 * K + X2) * _D1;
780 effectsuite_t _B1 = (2 * K + Y1 + Y1 * pow(K, 2) - 2 * Y2 * K) * _D1;
781 const effectsuite_t B2 = (-(pow(K, 2)) - Y1 * K + Y2) * _D1;
783 if (shelfType == 1) {
788 for (
int j = 0; j < 22; j++) {
792 for (
int j = 2; j < 22; j++) {
806 effectsuite_t SA = 0;
807 effectsuite_t SB = 0;
808 if (shelfType == 0) {
814 for (
int j = 0; j < order; j++) {
820 const effectsuite_t gain = SA / (1 - SB);
861 effectsuite_t gain = 0;
936 firTemp =
new effectsuite_t[22];
937 iirTemp =
new effectsuite_t[22];
945 effectsuite_t
rms(effectsuite_t sample) {
947 effectsuite_t rmsValue = 0;
955 rmsValue = sqrt(rmsValue);
988 int samplingRate = 0;
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));
1538 effectsuite_t modulationDepth = 1000, modulationRate = 0, effectGain = .01;
1540 effectsuite_t modulationIndex = 0;