diff --git a/DSP/AllpassDiffuser.h b/DSP/AllpassDiffuser.h index 7702b3e..03d9b74 100644 --- a/DSP/AllpassDiffuser.h +++ b/DSP/AllpassDiffuser.h @@ -22,7 +22,6 @@ THE SOFTWARE. #pragma once -#include #include "ModulatedAllpass.h" #include "RandomBuffer.h" @@ -31,7 +30,7 @@ namespace Cloudseed class AllpassDiffuser { public: - static const int MaxStageCount = 12; + static constexpr int MaxStageCount = 12; private: int samplerate; @@ -39,7 +38,7 @@ namespace Cloudseed ModulatedAllpass filters[MaxStageCount]; int delay; float modRate; - std::vector seedValues; + float seedValues[MaxStageCount * 3]; int seed; float crossSeed; @@ -126,14 +125,10 @@ namespace Cloudseed void Process(float* input, float* output, int bufSize) { - float tempBuffer[BUFFER_SIZE]; - - filters[0].Process(input, tempBuffer, bufSize); + filters[0].Process(input, output, bufSize); for (int i = 1; i < Stages; i++) - filters[i].Process(tempBuffer, tempBuffer, bufSize); - - Utils::Copy(output, tempBuffer, bufSize); + filters[i].Process(output, output, bufSize); } void ClearBuffers() @@ -155,7 +150,7 @@ namespace Cloudseed void UpdateSeeds() { - this->seedValues = RandomBuffer::Generate(seed, MaxStageCount * 3, crossSeed); + RandomBuffer::Generate(seed, seedValues, MaxStageCount * 3, crossSeed); Update(); } diff --git a/DSP/DelayLine.h b/DSP/DelayLine.h index e6b3ba5..92e07cd 100644 --- a/DSP/DelayLine.h +++ b/DSP/DelayLine.h @@ -44,8 +44,8 @@ namespace Cloudseed void Reset() { - for (int i = 0; i < N; i++) - buffer[i] = 0.0f; + memset(buffer, 0, sizeof buffer); + idxRead = 0; idxWrite = 0; count = 0; diff --git a/DSP/LcgRandom.h b/DSP/LcgRandom.h index f19d68a..c9c8c05 100644 --- a/DSP/LcgRandom.h +++ b/DSP/LcgRandom.h @@ -33,9 +33,9 @@ namespace Cloudseed uint64_t a; uint64_t c; - double doubleInv; - float floatUintInv; - float floatIntInv; + static constexpr double doubleInv = 1.0 / UINT32_MAX; + static constexpr float floatUintInv = 1.0 / UINT32_MAX; + static constexpr float floatIntInv = 1.0 / INT32_MAX; public: inline LcgRandom(uint64_t seed = 0) @@ -43,10 +43,6 @@ namespace Cloudseed x = seed; a = 22695477; c = 1; - - doubleInv = 1.0 / (double)UINT32_MAX; - floatUintInv = 1.0 / (float)UINT32_MAX; - floatIntInv = 1.0 / (float)INT32_MAX; } inline void SetSeed(uint64_t seed) diff --git a/DSP/ModulatedAllpass.h b/DSP/ModulatedAllpass.h index a661fa7..d5b3969 100644 --- a/DSP/ModulatedAllpass.h +++ b/DSP/ModulatedAllpass.h @@ -31,8 +31,8 @@ namespace Cloudseed class ModulatedAllpass { public: - static const int DelayBufferSize = 19200; // 100ms at 192Khz - static const int ModulationUpdateRate = 8; + static constexpr int DelayBufferSize = 19200; // 100ms at 192Khz + static constexpr int ModulationUpdateRate = 8; private: float delayBuffer[DelayBufferSize] = { 0 }; @@ -161,20 +161,16 @@ namespace Cloudseed void Update() { modPhase += ModRate * ModulationUpdateRate; - if (modPhase > 1) - modPhase = std::fmod(modPhase, 1.0); + if (modPhase > 1.0f) + modPhase -= (int)modPhase; auto mod = std::sinf(modPhase * 2 * M_PI); if (ModAmount >= SampleDelay) // don't modulate to negative value ModAmount = SampleDelay - 1; - auto totalDelay = SampleDelay + ModAmount * mod; - if (totalDelay <= 0) // should no longer be required - totalDelay = 1; - delayA = (int)totalDelay; delayB = (int)totalDelay + 1; diff --git a/DSP/ModulatedDelay.h b/DSP/ModulatedDelay.h index 9e26984..911eb59 100644 --- a/DSP/ModulatedDelay.h +++ b/DSP/ModulatedDelay.h @@ -32,8 +32,8 @@ namespace Cloudseed { private: - static const int ModulationUpdateRate = 8; - static const int DelayBufferSize = 192000 * 2; + static constexpr int ModulationUpdateRate = 8; + static constexpr int DelayBufferSize = 192000 * 2; float delayBuffer[DelayBufferSize] = { 0 }; int writeIndex; @@ -102,8 +102,8 @@ namespace Cloudseed void Update() { modPhase += ModRate * ModulationUpdateRate; - if (modPhase > 1) - modPhase = std::fmod(modPhase, 1.0); + if (modPhase > 1.0f) + modPhase -= (int)modPhase; auto mod = std::sinf(modPhase * 2 * M_PI); auto totalDelay = SampleDelay + ModAmount * mod; diff --git a/DSP/MultitapDelay.h b/DSP/MultitapDelay.h index 982bc6b..a8e6c70 100644 --- a/DSP/MultitapDelay.h +++ b/DSP/MultitapDelay.h @@ -22,9 +22,6 @@ THE SOFTWARE. #pragma once -#include -#include -#include #include #include "Utils.h" #include "RandomBuffer.h" @@ -34,8 +31,8 @@ namespace Cloudseed class MultitapDelay { public: - static const int MaxTaps = 256; - static const int DelayBufferSize = 192000 * 2; + static constexpr int MaxTaps = 256; + static constexpr int DelayBufferSize = 192000 * 2; private: float delayBuffer[DelayBufferSize] = { 0 }; @@ -43,7 +40,7 @@ namespace Cloudseed float tapGains[MaxTaps] = { 0 }; float tapPosition[MaxTaps] = { 0 }; - std::vector seedValues; + float seedValues[MaxTaps * 3]; int writeIdx; int seed; @@ -143,7 +140,7 @@ namespace Cloudseed void UpdateSeeds() { - this->seedValues = RandomBuffer::Generate(seed, MaxTaps * 3, crossSeed); + RandomBuffer::Generate(seed, seedValues, MaxTaps * 3, crossSeed); Update(); } }; diff --git a/DSP/RandomBuffer.cpp b/DSP/RandomBuffer.cpp index d43bdef..19df406 100644 --- a/DSP/RandomBuffer.cpp +++ b/DSP/RandomBuffer.cpp @@ -26,32 +26,29 @@ THE SOFTWARE. namespace Cloudseed { - std::vector RandomBuffer::Generate(uint64_t seed, int count) + void RandomBuffer::Generate(uint64_t seed, float* output, int count) { LcgRandom rand(seed); - std::vector output; for (int i = 0; i < count; i++) { unsigned int val = rand.NextUInt(); float fVal = val / (float)UINT_MAX; - output.push_back(fVal); + output[i] = fVal; } - - return output; } - std::vector RandomBuffer::Generate(uint64_t seed, int count, float crossSeed) + void RandomBuffer::Generate(uint64_t seed, float* output, int count, float crossSeed) { - auto seedA = seed; - auto seedB = ~seed; - auto seriesA = Generate(seedA, count); - auto seriesB = Generate(seedB, count); + uint64_t seedA = seed; + uint64_t seedB = ~seed; + float seriesA[count]; + float seriesB[count]; - std::vector output; - for (int i = 0; i < count; i++) - output.push_back(seriesA[i] * (1 - crossSeed) + seriesB[i] * crossSeed); + Generate(seedA, seriesA, count); + Generate(seedB, seriesB, count); - return output; + for (int i = 0; i < count; i++) + output[i] = seriesA[i] * (1 - crossSeed) + seriesB[i] * crossSeed; } } diff --git a/DSP/RandomBuffer.h b/DSP/RandomBuffer.h index b792773..54ab785 100644 --- a/DSP/RandomBuffer.h +++ b/DSP/RandomBuffer.h @@ -22,7 +22,6 @@ THE SOFTWARE. #pragma once -#include #include namespace Cloudseed @@ -30,7 +29,7 @@ namespace Cloudseed class RandomBuffer { public: - static std::vector Generate(uint64_t seed, int count); - static std::vector Generate(uint64_t seed, int count, float crossSeed); + static void Generate(uint64_t seed, float* output, int count); + static void Generate(uint64_t seed, float* output, int count, float crossSeed); }; } diff --git a/DSP/ReverbChannel.h b/DSP/ReverbChannel.h index 570d78b..4526888 100644 --- a/DSP/ReverbChannel.h +++ b/DSP/ReverbChannel.h @@ -22,8 +22,6 @@ THE SOFTWARE. #pragma once -#include -#include #include "../Parameters.h" #include "ModulatedDelay.h" #include "MultitapDelay.h" @@ -47,7 +45,7 @@ namespace Cloudseed class ReverbChannel { private: - static const int TotalLineCount = 12; + static constexpr int TotalLineCount = 12; double paramsScaled[Parameter::COUNT] = { 0.0 }; int samplerate; @@ -56,7 +54,6 @@ namespace Cloudseed MultitapDelay multitap; AllpassDiffuser diffuser; DelayLine lines[TotalLineCount]; - RandomBuffer rand; Hp1 highPass; Lp1 lowPass; @@ -390,7 +387,8 @@ namespace Cloudseed auto lateDiffusionModAmount = Ms2Samples(paramsScaled[Parameter::LateDiffuseModAmount]); auto lateDiffusionModRate = paramsScaled[Parameter::LateDiffuseModRate]; - auto delayLineSeeds = RandomBuffer::Generate(delayLineSeed, TotalLineCount * 3, crossSeed); + float delayLineSeeds[TotalLineCount * 3]; + RandomBuffer::Generate(delayLineSeed, delayLineSeeds, TotalLineCount * 3, crossSeed); for (int i = 0; i < TotalLineCount; i++) { diff --git a/DSP/ReverbController.h b/DSP/ReverbController.h index 3601e09..b5d5c9a 100644 --- a/DSP/ReverbController.h +++ b/DSP/ReverbController.h @@ -22,7 +22,6 @@ THE SOFTWARE. #pragma once -#include #include "../Parameters.h" #include "ReverbChannel.h" #include "AllpassDiffuser.h" @@ -86,19 +85,14 @@ namespace Cloudseed void Process(float* inL, float* inR, float* outL, float* outR, int bufSize) { - float outLTemp[BUFFER_SIZE]; - float outRTemp[BUFFER_SIZE]; - while (bufSize > 0) { int subBufSize = bufSize > BUFFER_SIZE ? BUFFER_SIZE : bufSize; - ProcessChunk(inL, inR, outLTemp, outRTemp, subBufSize); - Utils::Copy(outL, outLTemp, subBufSize); - Utils::Copy(outR, outRTemp, subBufSize); - inL = &inL[subBufSize]; - inR = &inR[subBufSize]; - outL = &outL[subBufSize]; - outR = &outR[subBufSize]; + ProcessChunk(inL, inR, outL, outR, subBufSize); + inL += subBufSize; + inR += subBufSize; + outL += subBufSize; + outR += subBufSize; bufSize -= subBufSize; } } diff --git a/DSP/Utils.h b/DSP/Utils.h index 97ad74e..b4e9671 100644 --- a/DSP/Utils.h +++ b/DSP/Utils.h @@ -59,20 +59,20 @@ namespace Cloudseed target[i] += source[i] * gain; } - inline float DB2Gainf(float input) + inline constexpr float DB2Gainf(float input) { //return std::pow(10.0f, input / 20.0f); return powf(10, input * 0.05f); } template - inline double DB2Gain(T input) + inline constexpr double DB2Gain(T input) { return pow10f(input / 20.0); } template - inline double Gain2DB(T input) + inline constexpr double Gain2DB(T input) { //if (input < 0.0000001) // return -100000; @@ -80,33 +80,33 @@ namespace Cloudseed return 20.0f * log10f(input); } - const float dec1Mult = (10 / 9.0) * 0.1; - const float dec2Mult = (100 / 99.0) * 0.01; - const float dec3Mult = (1000 / 999.0) * 0.001; - const float dec4Mult = (10000 / 9999.0) * 0.0001; - - const float oct1Mult = (2 / 1.0) * 0.5; - const float oct2Mult = (4 / 3.0) * 0.25; - const float oct3Mult = (8 / 7.0) * 0.125; - const float oct4Mult = (16 / 15.0) * 0.0625; - const float oct5Mult = (32 / 31.0) * 0.03125; - const float oct6Mult = (64 / 63.0) * 0.015625; - const float oct7Mult = (128 / 127.0) * 0.0078125; - const float oct8Mult = (256 / 255.0) * 0.00390625; - - inline float Resp1dec(float x) { return (powf(10, x) - 1) * dec1Mult; } - inline float Resp2dec(float x) { return (powf(10, 2 * x) - 1) * dec2Mult; } - inline float Resp3dec(float x) { return (powf(10, 3 * x) - 1) * dec3Mult; } - inline float Resp4dec(float x) { return (powf(10, 4 * x) - 1) * dec4Mult; } - - inline float Resp1oct(float x) { return (powf(2, x) - 1) * oct1Mult; } - inline float Resp2oct(float x) { return (powf(2, 2 * x) - 1) * oct2Mult; } - inline float Resp3oct(float x) { return (powf(2, 3 * x) - 1) * oct3Mult; } - inline float Resp4oct(float x) { return (powf(2, 4 * x) - 1) * oct4Mult; } - inline float Resp5oct(float x) { return (powf(2, 5 * x) - 1) * oct5Mult; } - inline float Resp6oct(float x) { return (powf(2, 6 * x) - 1) * oct6Mult; } - inline float Resp7oct(float x) { return (powf(2, 7 * x) - 1) * oct7Mult; } - inline float Resp8oct(float x) { return (powf(2, 8 * x) - 1) * oct8Mult; } + constexpr float dec1Mult = (10 / 9.0) * 0.1; + constexpr float dec2Mult = (100 / 99.0) * 0.01; + constexpr float dec3Mult = (1000 / 999.0) * 0.001; + constexpr float dec4Mult = (10000 / 9999.0) * 0.0001; + + constexpr float oct1Mult = (2 / 1.0) * 0.5; + constexpr float oct2Mult = (4 / 3.0) * 0.25; + constexpr float oct3Mult = (8 / 7.0) * 0.125; + constexpr float oct4Mult = (16 / 15.0) * 0.0625; + constexpr float oct5Mult = (32 / 31.0) * 0.03125; + constexpr float oct6Mult = (64 / 63.0) * 0.015625; + constexpr float oct7Mult = (128 / 127.0) * 0.0078125; + constexpr float oct8Mult = (256 / 255.0) * 0.00390625; + + inline constexpr float Resp1dec(float x) { return (powf(10, x) - 1) * dec1Mult; } + inline constexpr float Resp2dec(float x) { return (powf(10, 2 * x) - 1) * dec2Mult; } + inline constexpr float Resp3dec(float x) { return (powf(10, 3 * x) - 1) * dec3Mult; } + inline constexpr float Resp4dec(float x) { return (powf(10, 4 * x) - 1) * dec4Mult; } + + inline constexpr float Resp1oct(float x) { return (powf(2, x) - 1) * oct1Mult; } + inline constexpr float Resp2oct(float x) { return (powf(2, 2 * x) - 1) * oct2Mult; } + inline constexpr float Resp3oct(float x) { return (powf(2, 3 * x) - 1) * oct3Mult; } + inline constexpr float Resp4oct(float x) { return (powf(2, 4 * x) - 1) * oct4Mult; } + inline constexpr float Resp5oct(float x) { return (powf(2, 5 * x) - 1) * oct5Mult; } + inline constexpr float Resp6oct(float x) { return (powf(2, 6 * x) - 1) * oct6Mult; } + inline constexpr float Resp7oct(float x) { return (powf(2, 7 * x) - 1) * oct7Mult; } + inline constexpr float Resp8oct(float x) { return (powf(2, 8 * x) - 1) * oct8Mult; } } }