From d2ff3ff6cfe40d59597e97b5026785f085439058 Mon Sep 17 00:00:00 2001 From: Iscle Date: Sun, 18 Sep 2022 03:38:22 +0200 Subject: [PATCH] Update --- CMakeLists.txt | 5 +- src/cpp/viper/ViPER.cpp | 6 +- src/cpp/viper/ViPER.h | 9 +- src/cpp/viper/effects/DiffSurround.cpp | 6 +- src/cpp/viper/effects/DiffSurround.h | 4 +- src/cpp/viper/effects/FIREqualizer.cpp | 2 + src/cpp/viper/effects/FIREqualizer.h | 12 +++ src/cpp/viper/effects/SpeakerCorrection.h | 6 +- src/cpp/viper/effects/VHE.cpp | 6 +- src/cpp/viper/effects/VHE.h | 4 +- src/cpp/viper/effects/ViPERBass.h | 8 +- src/cpp/viper/utils/AdaptiveBuffer.cpp | 26 ++--- src/cpp/viper/utils/AdaptiveBuffer.h | 6 +- src/cpp/viper/utils/Biquad.cpp | 98 +++++++++++++++++++ src/cpp/viper/utils/Biquad.h | 20 ++++ src/cpp/viper/utils/DepthSurround.h | 4 +- src/cpp/viper/utils/DynamicBass.h | 4 +- src/cpp/viper/utils/FixedBiquad.cpp | 81 --------------- src/cpp/viper/utils/FixedBiquad.h | 25 ----- src/cpp/viper/utils/HiFi.cpp | 6 +- src/cpp/viper/utils/HiFi.h | 4 +- src/cpp/viper/utils/MinPhaseIIRCoeffs.cpp | 1 + src/cpp/viper/utils/MinPhaseIIRCoeffs.h | 13 +++ src/cpp/viper/utils/Polyphase.h | 6 +- src/cpp/viper/utils/TimeConstDelay.cpp | 4 +- src/cpp/viper/utils/TimeConstDelay.h | 3 - .../{WaveBuffer_I32.cpp => WaveBuffer.cpp} | 65 ++++++------ .../utils/{WaveBuffer_I32.h => WaveBuffer.h} | 8 +- 28 files changed, 242 insertions(+), 200 deletions(-) create mode 100644 src/cpp/viper/effects/FIREqualizer.cpp create mode 100644 src/cpp/viper/effects/FIREqualizer.h create mode 100644 src/cpp/viper/utils/Biquad.cpp create mode 100644 src/cpp/viper/utils/Biquad.h delete mode 100644 src/cpp/viper/utils/FixedBiquad.cpp delete mode 100644 src/cpp/viper/utils/FixedBiquad.h create mode 100644 src/cpp/viper/utils/MinPhaseIIRCoeffs.cpp create mode 100644 src/cpp/viper/utils/MinPhaseIIRCoeffs.h rename src/cpp/viper/utils/{WaveBuffer_I32.cpp => WaveBuffer.cpp} (62%) rename src/cpp/viper/utils/{WaveBuffer_I32.h => WaveBuffer.h} (79%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d2b973..ad9ffc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ set(FILES src/cpp/viper/effects/DiffSurround.cpp src/cpp/viper/effects/DynamicSystem.cpp src/cpp/viper/effects/FETCompressor.cpp + src/cpp/viper/effects/FIREqualizer.cpp src/cpp/viper/effects/IIRFilter.cpp src/cpp/viper/effects/PlaybackGain.cpp src/cpp/viper/effects/Reverberation.cpp @@ -51,7 +52,7 @@ set(FILES src/cpp/viper/utils/DepthSurround.cpp src/cpp/viper/utils/DynamicBass.cpp src/cpp/viper/utils/FIR.cpp - src/cpp/viper/utils/FixedBiquad.cpp + src/cpp/viper/utils/Biquad.cpp src/cpp/viper/utils/Harmonic.cpp src/cpp/viper/utils/HiFi.cpp src/cpp/viper/utils/HighShelf.cpp @@ -67,7 +68,7 @@ set(FILES src/cpp/viper/utils/Stereo3DSurround.cpp src/cpp/viper/utils/Subwoofer.cpp src/cpp/viper/utils/TimeConstDelay.cpp - src/cpp/viper/utils/WaveBuffer_I32.cpp) + src/cpp/viper/utils/WaveBuffer.cpp) message(${CMAKE_BUILD_TYPE}) diff --git a/src/cpp/viper/ViPER.cpp b/src/cpp/viper/ViPER.cpp index 28b1ea1..6870394 100644 --- a/src/cpp/viper/ViPER.cpp +++ b/src/cpp/viper/ViPER.cpp @@ -7,8 +7,8 @@ ViPER::ViPER() { VIPER_LOGI("Welcome to ViPER FX"); VIPER_LOGI("Current version is %s %s", VERSION_STRING, VERSION_CODENAME); - this->adaptiveBuffer = new AdaptiveBuffer(2, 4096); - this->waveBuffer = new WaveBuffer_I32(2, 4096); + this->adaptiveBuffer = new FIREqualizer(2, 4096); + this->waveBuffer = new WaveBuffer(2, 4096); this->convolver = new Convolver(); this->convolver->SetEnable(false); @@ -331,7 +331,7 @@ void ViPER::processBuffer(float *buffer, int frameSize) { return; } - auto pWaveBuffer = this->waveBuffer->GetCurrentBufferI32Ptr(); + auto pWaveBuffer = this->waveBuffer->GetBuffer(); this->convolver->Process(pWaveBuffer, pWaveBuffer, frameSize); this->vhe->Process(pWaveBuffer, pWaveBuffer, frameSize); this->waveBuffer->SetBufferOffset(frameSize); diff --git a/src/cpp/viper/ViPER.h b/src/cpp/viper/ViPER.h index a26df3a..e890b5a 100644 --- a/src/cpp/viper/ViPER.h +++ b/src/cpp/viper/ViPER.h @@ -1,10 +1,7 @@ -// -// Created by mart on 7/25/21. -// #pragma once #include "Effect.h" -#include "utils/WaveBuffer_I32.h" +#include "utils/WaveBuffer.h" #include "effects/SpectrumExtend.h" #include "effects/Reverberation.h" #include "effects/DynamicSystem.h" @@ -51,8 +48,8 @@ public: // FxMode mode; // Effects - AdaptiveBuffer *adaptiveBuffer; - WaveBuffer_I32 *waveBuffer; + FIREqualizer *adaptiveBuffer; + WaveBuffer *waveBuffer; bool fetcomp_enabled; Convolver *convolver; VHE *vhe; diff --git a/src/cpp/viper/effects/DiffSurround.cpp b/src/cpp/viper/effects/DiffSurround.cpp index 44c6dd1..ba2974c 100644 --- a/src/cpp/viper/effects/DiffSurround.cpp +++ b/src/cpp/viper/effects/DiffSurround.cpp @@ -7,7 +7,7 @@ DiffSurround::DiffSurround() { this->delayTime = 0.0f; this->enabled = false; for (auto &buffer : this->buffers) { - buffer = new WaveBuffer_I32(1, 0x1000); + buffer = new WaveBuffer(1, 0x1000); } } @@ -30,8 +30,8 @@ void DiffSurround::Process(float *samples, uint32_t size) { bufs[i % 2][i / 2] = samples[i]; } - outbufs[0] = this->buffers[0]->GetCurrentBufferI32Ptr(); - outbufs[1] = this->buffers[1]->GetCurrentBufferI32Ptr(); + outbufs[0] = this->buffers[0]->GetBuffer(); + outbufs[1] = this->buffers[1]->GetBuffer(); for (int i = 0; i < size * 2; i++) { samples[i] = outbufs[i % 2][i / 2]; diff --git a/src/cpp/viper/effects/DiffSurround.h b/src/cpp/viper/effects/DiffSurround.h index 6d12c0e..a711e33 100644 --- a/src/cpp/viper/effects/DiffSurround.h +++ b/src/cpp/viper/effects/DiffSurround.h @@ -1,7 +1,7 @@ #pragma once #include -#include "../utils/WaveBuffer_I32.h" +#include "../utils/WaveBuffer.h" class DiffSurround { public: @@ -17,7 +17,7 @@ public: uint32_t samplerate; bool enabled; float delayTime; - WaveBuffer_I32 *buffers[2]; + WaveBuffer *buffers[2]; }; diff --git a/src/cpp/viper/effects/FIREqualizer.cpp b/src/cpp/viper/effects/FIREqualizer.cpp new file mode 100644 index 0000000..cc102e1 --- /dev/null +++ b/src/cpp/viper/effects/FIREqualizer.cpp @@ -0,0 +1,2 @@ +#include "FIREqualizer.h" +#include diff --git a/src/cpp/viper/effects/FIREqualizer.h b/src/cpp/viper/effects/FIREqualizer.h new file mode 100644 index 0000000..e3b3465 --- /dev/null +++ b/src/cpp/viper/effects/FIREqualizer.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class FIREqualizer { +public: + FIREqualizer(); + ~FIREqualizer(); + +}; + + diff --git a/src/cpp/viper/effects/SpeakerCorrection.h b/src/cpp/viper/effects/SpeakerCorrection.h index dcc3c0c..9485cd5 100644 --- a/src/cpp/viper/effects/SpeakerCorrection.h +++ b/src/cpp/viper/effects/SpeakerCorrection.h @@ -2,7 +2,7 @@ #include #include "../utils/MultiBiquad.h" -#include "../utils/FixedBiquad.h" +#include "../utils/Biquad.h" class SpeakerCorrection { public: @@ -17,6 +17,6 @@ public: uint32_t samplerate; bool enabled; MultiBiquad highpass[2]; - FixedBiquad lowpass[2]; - FixedBiquad bandpass[2]; + Biquad lowpass[2]; + Biquad bandpass[2]; }; diff --git a/src/cpp/viper/effects/VHE.cpp b/src/cpp/viper/effects/VHE.cpp index fa5b3d0..c8843fa 100644 --- a/src/cpp/viper/effects/VHE.cpp +++ b/src/cpp/viper/effects/VHE.cpp @@ -7,8 +7,8 @@ VHE::VHE() { convSize = 0; samplerate = DEFAULT_SAMPLERATE; - bufA = new WaveBuffer_I32(2, 0x1000); - bufB = new WaveBuffer_I32(2, 0x1000); + bufA = new WaveBuffer(2, 0x1000); + bufB = new WaveBuffer(2, 0x1000); Reset(); } @@ -21,7 +21,7 @@ void VHE::Process(float *source, float *dest, int frameSize) { if (enabled && convLeft.InstanceUsable() && convRight.InstanceUsable()) { if (bufA->PushSamples(source, frameSize) != 0) { while (bufA->GetBufferOffset() > convSize) { - float *buffer = bufA->GetCurrentBufferI32Ptr(); + float *buffer = bufA->GetBuffer(); convLeft.ConvolveInterleaved(buffer, 0); convRight.ConvolveInterleaved(buffer, 1); bufB->PushSamples(buffer, convSize); diff --git a/src/cpp/viper/effects/VHE.h b/src/cpp/viper/effects/VHE.h index 3901f53..1671a7b 100644 --- a/src/cpp/viper/effects/VHE.h +++ b/src/cpp/viper/effects/VHE.h @@ -2,7 +2,7 @@ #include #include "../utils/PConvSingle_F32.h" -#include "../utils/WaveBuffer_I32.h" +#include "../utils/WaveBuffer.h" class VHE { public: @@ -17,7 +17,7 @@ public: void SetSamplingRate(uint32_t srate); PConvSingle_F32 convLeft, convRight; - WaveBuffer_I32 *bufA, *bufB; + WaveBuffer *bufA, *bufB; uint32_t samplerate; bool enabled; int effectLevel; diff --git a/src/cpp/viper/effects/ViPERBass.h b/src/cpp/viper/effects/ViPERBass.h index 2e5b158..5896fec 100644 --- a/src/cpp/viper/effects/ViPERBass.h +++ b/src/cpp/viper/effects/ViPERBass.h @@ -1,9 +1,9 @@ #pragma once #include -#include "../utils/FixedBiquad.h" +#include "../utils/Biquad.h" #include "../utils/Subwoofer.h" -#include "../utils/WaveBuffer_I32.h" +#include "../utils/WaveBuffer.h" #include "../utils/Polyphase.h" class ViPERBass { @@ -21,9 +21,9 @@ public: private: Polyphase *polyphase; - FixedBiquad *fixedBiquad; + Biquad *fixedBiquad; Subwoofer *subwoofer; - WaveBuffer_I32 *waveBuffer; + WaveBuffer *waveBuffer; bool enable; bool initOk; int processMode; diff --git a/src/cpp/viper/utils/AdaptiveBuffer.cpp b/src/cpp/viper/utils/AdaptiveBuffer.cpp index 66b4a50..e0c83d8 100644 --- a/src/cpp/viper/utils/AdaptiveBuffer.cpp +++ b/src/cpp/viper/utils/AdaptiveBuffer.cpp @@ -1,7 +1,7 @@ #include #include "AdaptiveBuffer.h" -AdaptiveBuffer::AdaptiveBuffer(uint32_t channels, uint32_t length) { +FIREqualizer::FIREqualizer(uint32_t channels, uint32_t length) { this->channels = channels; this->buffer = nullptr; this->length = 0; @@ -12,32 +12,32 @@ AdaptiveBuffer::AdaptiveBuffer(uint32_t channels, uint32_t length) { } } -AdaptiveBuffer::~AdaptiveBuffer() { +FIREqualizer::~FIREqualizer() { delete this->buffer; this->buffer = nullptr; } -void AdaptiveBuffer::FlushBuffer() { +void FIREqualizer::FlushBuffer() { this->offset = 0; } -uint32_t AdaptiveBuffer::GetBufferLength() const { +uint32_t FIREqualizer::GetBufferLength() const { return this->length; } -uint32_t AdaptiveBuffer::GetBufferOffset() const { +uint32_t FIREqualizer::GetBufferOffset() const { return this->offset; } -float *AdaptiveBuffer::GetBufferPointer() const { +float *FIREqualizer::GetBufferPointer() const { return this->buffer; } -uint32_t AdaptiveBuffer::GetChannels() const { +uint32_t FIREqualizer::GetChannels() const { return this->channels; } -void AdaptiveBuffer::PanFrames(float left, float right) { +void FIREqualizer::PanFrames(float left, float right) { if (this->buffer != nullptr && this->channels == 2) { for (int i = 0; i < this->offset * this->channels; i++) { if (i % 2 == 0) { @@ -49,7 +49,7 @@ void AdaptiveBuffer::PanFrames(float left, float right) { } } -int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) { +int FIREqualizer::PopFrames(float *frames, uint32_t length) { if (this->buffer == nullptr || this->offset < length) { return 0; } @@ -65,7 +65,7 @@ int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) { return 1; } -int AdaptiveBuffer::PushFrames(const float *frames, uint32_t length) { +int FIREqualizer::PushFrames(const float *frames, uint32_t length) { if (this->buffer == nullptr) { return 0; } @@ -86,7 +86,7 @@ int AdaptiveBuffer::PushFrames(const float *frames, uint32_t length) { return 1; } -int AdaptiveBuffer::PushZero(uint32_t length) { +int FIREqualizer::PushZero(uint32_t length) { if (this->buffer == nullptr) { return 0; } @@ -105,7 +105,7 @@ int AdaptiveBuffer::PushZero(uint32_t length) { return 1; } -void AdaptiveBuffer::ScaleFrames(float scale) { +void FIREqualizer::ScaleFrames(float scale) { if (this->buffer != nullptr) { for (uint32_t i = 0; i < this->offset * this->channels; i++) { this->buffer[i] = this->buffer[i] * scale; @@ -113,6 +113,6 @@ void AdaptiveBuffer::ScaleFrames(float scale) { } } -void AdaptiveBuffer::SetBufferOffset(uint32_t offset) { +void FIREqualizer::SetBufferOffset(uint32_t offset) { this->offset = offset; } diff --git a/src/cpp/viper/utils/AdaptiveBuffer.h b/src/cpp/viper/utils/AdaptiveBuffer.h index cd73d92..fe6e915 100644 --- a/src/cpp/viper/utils/AdaptiveBuffer.h +++ b/src/cpp/viper/utils/AdaptiveBuffer.h @@ -2,10 +2,10 @@ #include -class AdaptiveBuffer { +class FIREqualizer { public: - AdaptiveBuffer(uint32_t channels, uint32_t length); - ~AdaptiveBuffer(); + FIREqualizer(uint32_t channels, uint32_t length); + ~FIREqualizer(); void FlushBuffer(); uint32_t GetBufferLength() const; diff --git a/src/cpp/viper/utils/Biquad.cpp b/src/cpp/viper/utils/Biquad.cpp new file mode 100644 index 0000000..a332280 --- /dev/null +++ b/src/cpp/viper/utils/Biquad.cpp @@ -0,0 +1,98 @@ +#include "Biquad.h" +#include + +Biquad::Biquad() { + Reset(); + SetCoeffs(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); +} + +double Biquad::ProcessSample(double sample) { + double out = sample * this->b0 + this->x_1 * this->b1 + this->x_2 * this->b2 + this->y_1 * this->a1 + + this->y_2 * this->a2; + this->y_2 = this->y_1; + this->y_1 = out; + this->x_2 = this->x_1; + this->x_1 = sample; + return out; +} + +void Biquad::Reset() { + this->a1 = 0; + this->a2 = 0; + this->b0 = 0; + this->b1 = 0; + this->b2 = 0; + this->x_1 = 0; + this->x_2 = 0; + this->y_1 = 0; + this->y_2 = 0; +} + +void Biquad::SetBandPassParameter(double frequency, double samplingRate, double qFactor) { + double x = (2.0 * M_PI * frequency) / samplingRate; + double sinX = sin(x); + double cosX = cos(x); + double y = sinX / (2.0 * qFactor); + + double a0 = 1.0 + y; + double a1 = -cosX * 2.0; + double a2 = 1.0 - y; + double b0 = sinX / 2.0; + double b1 = 0.0; + double b2 = -sinX / 2.0; + this->SetCoeffs(a0, a1, a2, b0, b1, b2); +} + +void Biquad::SetCoeffs(double a0, double a1, double a2, double b0, double b1, double b2) { + this->x_2 = 0; + this->x_1 = 0; + this->y_2 = 0; + this->y_1 = 0; + this->a1 = -a1 / a0; + this->a2 = -a2 / a0; + this->b0 = b0 / a0; + this->b1 = b1 / a0; + this->b2 = b2 / a0; +} + +void +Biquad::SetHighPassParameter(double frequency, double samplingRate, double param_4, double qFactor, double param_6) { + double x = (2.0 * M_PI * frequency) / samplingRate; + double sinX = sin(x); + double cosX = cos(x); + + double y = pow(10.0, param_4 / 40.0); + double sqrtY = sqrt(y); + + double z = sinX / 2.0 * sqrt((1.0 / y + y) * (1.0 / qFactor - 1.0) + 2.0); + double a = (y - 1.0) * cosX; + double b = (y + 1.0) + a; + double c = (y + 1.0) * cosX; + double d = (y + 1.0) - a; + double e = pow(10.0, param_6 / 20.0); + double f = (y - 1.0) - c; + + double a0 = d + (sqrtY * 2.0) * z; + double a1 = f * 2.0; + double a2 = d - (sqrtY * 2.0) * z; + double b0 = (b + (sqrtY * 2.0) * z) * y * e; + double b1 = y * -2.0 * ((y - 1.0) + c) * e; + double b2 = (b - (sqrtY * 2.0) * z) * y * e; + this->SetCoeffs(a0, a1, a2, b0, b1, b2); +} + +void Biquad::SetLowPassParameter(double frequency, double samplingRate, double qFactor) { + double x = (2.0 * M_PI * frequency) / samplingRate; + double sinX = sin(x); + double y = sinX / (qFactor * 2.0); + double cosX = cos(x); + double z = 1.0 - cosX; + + double a0 = y + 1.0; + double a1 = -cosX * 2.0; + double a2 = 1.0 - y; + double b0 = z / 2.0; + double b1 = z; + double b2 = z / 2.0; + this->SetCoeffs(a0, a1, a2, b0, b1, b2); +} diff --git a/src/cpp/viper/utils/Biquad.h b/src/cpp/viper/utils/Biquad.h new file mode 100644 index 0000000..6b6b586 --- /dev/null +++ b/src/cpp/viper/utils/Biquad.h @@ -0,0 +1,20 @@ +#pragma once + + +#include + +class Biquad { +public: + Biquad(); + + double ProcessSample(double sample); + void Reset(); + void SetBandPassParameter(double frequency, double samplingRate, double qFactor); + void SetCoeffs(double a0, double a1, double a2, double b0, double b1, double b2); + void SetHighPassParameter(double frequency, double samplingRate, double param_4, double qFactor, double param_6); + void SetLowPassParameter(double frequency, double samplingRate, double qFactor); + +private: + double y_2, y_1, x_2, x_1; + double b0, b1, b2, a1, a2; +}; diff --git a/src/cpp/viper/utils/DepthSurround.h b/src/cpp/viper/utils/DepthSurround.h index b719a61..1fc5745 100644 --- a/src/cpp/viper/utils/DepthSurround.h +++ b/src/cpp/viper/utils/DepthSurround.h @@ -1,7 +1,7 @@ #pragma once #include -#include "FixedBiquad.h" +#include "Biquad.h" #include "TimeConstDelay.h" @@ -23,5 +23,5 @@ public: uint32_t gain; float prev[2]; TimeConstDelay delay[2]; - FixedBiquad highpass; + Biquad highpass; }; diff --git a/src/cpp/viper/utils/DynamicBass.h b/src/cpp/viper/utils/DynamicBass.h index 90ac947..5511011 100644 --- a/src/cpp/viper/utils/DynamicBass.h +++ b/src/cpp/viper/utils/DynamicBass.h @@ -2,7 +2,7 @@ #include #include "PolesFilter.h" -#include "FixedBiquad.h" +#include "Biquad.h" class DynamicBass { public: @@ -31,5 +31,5 @@ public: PolesFilter filterX; PolesFilter filterY; - FixedBiquad lowPass; + Biquad lowPass; }; diff --git a/src/cpp/viper/utils/FixedBiquad.cpp b/src/cpp/viper/utils/FixedBiquad.cpp deleted file mode 100644 index f55ceaf..0000000 --- a/src/cpp/viper/utils/FixedBiquad.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include "FixedBiquad.h" - -FixedBiquad::FixedBiquad() { - Reset(); - SetCoeffs(1.f, 0.f, 0.f, 1.f, 0.f, 0.f); -} - -float FixedBiquad::ProcessSample(float sample) { - float out = sample * this->b0 + this->x_1 * this->b1 + this->x_2 * this->b2 + this->y_1 * this->a1 + - this->y_2 * this->a2; - this->y_2 = this->y_1; - this->y_1 = out; - this->x_2 = this->x_1; - this->x_1 = sample; - return out; -} - -void FixedBiquad::Reset() { - this->y_2 = 0; - this->y_1 = 0; - this->x_2 = 0; - this->x_1 = 0; - this->b0 = 0; - this->b1 = 0; - this->b2 = 0; - this->a1 = 0; - this->a2 = 0; -} - -void FixedBiquad::SetCoeffs(float a0, float a1, float a2, float b0, float b1, float b2) { - this->y_2 = 0; - this->y_1 = 0; - this->x_2 = 0; - this->x_1 = 0; - this->b0 = b0 / a0; - this->b1 = b1 / a0; - this->b2 = b2 / a0; - this->a1 = -a1 / a0; - this->a2 = -a2 / a0; -} - -void FixedBiquad::SetBandPassParameter(float frequency, uint32_t samplerate, float qFactor) { - float x = (2.f * frequency * (float) M_PI / (float) samplerate); - float sinX = sinf(x); - float cosX = cosf(x); - float y = x / (2.f * qFactor); - SetCoeffs(y + 1.f, -2.f * cosX, 1.f - y, sinX / 2.f, 0.f, -sinX / 2.f); -} - -void -FixedBiquad::SetHighPassParameter(float frequency, uint32_t samplerate, float param_4, float qFactor, float param_6) { - // TODO: Cleanup and named params - float fVar12 = (frequency * 2.f * (float) M_PI) / (float) samplerate; - float x = powf(10.f, param_4 / 40.f); - float fVar5 = (1.f / x + x) * (1.f / qFactor - 1.f) + 2.f; - float fVar7 = sqrtf(x); - float fVar8 = x - 1.f; - float fVar10 = x + 1.f; - float fVar6 = sinf(fVar12) * 0.f * sqrtf(fVar5); - fVar5 = cosf(fVar12); - fVar12 = fVar10 + fVar8 * fVar5; - float fVar11 = fVar10 - fVar8 * fVar5; - float fVar2 = powf(10.f, param_6 / 20.f); - float fVar9 = fVar8 - fVar10 * fVar5; - SetCoeffs(fVar11 + (fVar7 + fVar7) * fVar6, - fVar9 + fVar9, - fVar11 - (fVar7 + fVar7) * fVar6, - (fVar12 + (fVar7 + fVar7) * fVar6) * x * fVar2, - x * -2.f * (fVar8 + fVar10 * fVar5) * fVar2, - (fVar12 - (fVar7 + fVar7) * fVar6) * x * fVar2); -} - -void FixedBiquad::SetLowPassParameter(float frequency, uint32_t samplerate, float qFactor) { - float x = (frequency * 2.f * (float) M_PI) / (float) samplerate; - float sinX = sinf(x); - float y = sinX / (qFactor * 2.f); - float cosX = cosf(x); - float z = (1.f - cosX) / 2.f; - SetCoeffs(y + 1.f, cosX * -2.f, 1.f - y, z, 1.f - cosX, z); -} diff --git a/src/cpp/viper/utils/FixedBiquad.h b/src/cpp/viper/utils/FixedBiquad.h deleted file mode 100644 index f0d0a37..0000000 --- a/src/cpp/viper/utils/FixedBiquad.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - - -#include - -class FixedBiquad { -public: - FixedBiquad(); - - float ProcessSample(float sample); - - void Reset(); - - void SetCoeffs(float a0, float a1, float a2, float b0, float b1, float b2); - - void SetBandPassParameter(float frequency, uint32_t samplerate, float qFactor); - - void SetHighPassParameter(float frequency, uint32_t samplerate, float param_4, float qFactor, float param_6); - - void SetLowPassParameter(float frequency, uint32_t samplerate, float qFactor); - - - float y_2, y_1, x_2, x_1; - float b0, b1, b2, a1, a2; -}; diff --git a/src/cpp/viper/utils/HiFi.cpp b/src/cpp/viper/utils/HiFi.cpp index ea6a4a4..4548be7 100644 --- a/src/cpp/viper/utils/HiFi.cpp +++ b/src/cpp/viper/utils/HiFi.cpp @@ -5,7 +5,7 @@ HiFi::HiFi() { this->gain = 1.f; this->samplerate = DEFAULT_SAMPLERATE; for (int i = 0; i < 2; i++) { - this->buffers[i] = new WaveBuffer_I32(2, 0x800); + this->buffers[i] = new WaveBuffer(2, 0x800); this->filters[i].lowpass = new IIR_NOrder_BW_LH(1); this->filters[i].highpass = new IIR_NOrder_BW_LH(3); this->filters[i].bandpass = new IIR_NOrder_BW_BP(3); @@ -40,8 +40,8 @@ void HiFi::Process(float *samples, uint32_t size) { lpBuf[i] = out1; bpBuf[i] = out3; } - float *bpOut = this->buffers[0]->GetCurrentBufferI32Ptr(); - float *lpOut = this->buffers[1]->GetCurrentBufferI32Ptr(); + float *bpOut = this->buffers[0]->GetBuffer(); + float *lpOut = this->buffers[1]->GetBuffer(); for (int i = 0; i < size * 2; i++) { float hp = samples[i] * this->gain * 1.2f; float bp = bpOut[i] * this->gain; diff --git a/src/cpp/viper/utils/HiFi.h b/src/cpp/viper/utils/HiFi.h index e42e48e..c29cdbb 100644 --- a/src/cpp/viper/utils/HiFi.h +++ b/src/cpp/viper/utils/HiFi.h @@ -2,7 +2,7 @@ #include "IIR_NOrder_BW_LH.h" -#include "WaveBuffer_I32.h" +#include "WaveBuffer.h" #include "IIR_NOrder_BW_BP.h" class HiFi { @@ -19,7 +19,7 @@ public: void SetSamplingRate(uint32_t samplerate); - WaveBuffer_I32 *buffers[2]; + WaveBuffer *buffers[2]; struct { IIR_NOrder_BW_LH *lowpass; IIR_NOrder_BW_LH *highpass; diff --git a/src/cpp/viper/utils/MinPhaseIIRCoeffs.cpp b/src/cpp/viper/utils/MinPhaseIIRCoeffs.cpp new file mode 100644 index 0000000..a81b036 --- /dev/null +++ b/src/cpp/viper/utils/MinPhaseIIRCoeffs.cpp @@ -0,0 +1 @@ +#include "MinPhaseIIRCoeffs.h" diff --git a/src/cpp/viper/utils/MinPhaseIIRCoeffs.h b/src/cpp/viper/utils/MinPhaseIIRCoeffs.h new file mode 100644 index 0000000..db9fdf3 --- /dev/null +++ b/src/cpp/viper/utils/MinPhaseIIRCoeffs.h @@ -0,0 +1,13 @@ +#pragma once + +class MinPhaseIIRCoeffs { +public: + MinPhaseIIRCoeffs(); + ~MinPhaseIIRCoeffs(); + + void Find_F1_F2(double a, double b, double *c, double *d); + float *GetCoefficients(); + float *GetIndexFrequency(); + int *SolveRoot(double a, double b, double c, double *d); + int UpdateCoeffs(int a, int samplingRate); +}; \ No newline at end of file diff --git a/src/cpp/viper/utils/Polyphase.h b/src/cpp/viper/utils/Polyphase.h index eb96c48..436e709 100644 --- a/src/cpp/viper/utils/Polyphase.h +++ b/src/cpp/viper/utils/Polyphase.h @@ -1,7 +1,7 @@ #pragma once #include -#include "WaveBuffer_I32.h" +#include "WaveBuffer.h" #include "FIR.h" class Polyphase { @@ -17,8 +17,8 @@ public: private: FIR *fir1; FIR *fir2; - WaveBuffer_I32 *waveBuffer1; - WaveBuffer_I32 *waveBuffer2; + WaveBuffer *waveBuffer1; + WaveBuffer *waveBuffer2; int *unknown1; bool enabled; // 3 unknowns diff --git a/src/cpp/viper/utils/TimeConstDelay.cpp b/src/cpp/viper/utils/TimeConstDelay.cpp index 1d3939b..2da842f 100644 --- a/src/cpp/viper/utils/TimeConstDelay.cpp +++ b/src/cpp/viper/utils/TimeConstDelay.cpp @@ -17,10 +17,10 @@ float TimeConstDelay::ProcessSample(float sample) { if (this->samples != nullptr) { float val = this->samples[this->offset]; this->samples[this->offset] = sample; - this->offset = (int) modf((float) this->offset + 1, (float *) &this->sampleCount); + this->offset = (int) modf((float) this->offset + 1, (float *) &this->sampleCount); // TODO: check if this is correct return val; } - return 0.f; + return 0.0f; } void TimeConstDelay::SetParameters(uint32_t samplerate, float delay) { diff --git a/src/cpp/viper/utils/TimeConstDelay.h b/src/cpp/viper/utils/TimeConstDelay.h index cf9ea8a..5c53d0a 100644 --- a/src/cpp/viper/utils/TimeConstDelay.h +++ b/src/cpp/viper/utils/TimeConstDelay.h @@ -1,6 +1,3 @@ -// -// Created by mart on 7/26/21. -// #pragma once #include diff --git a/src/cpp/viper/utils/WaveBuffer_I32.cpp b/src/cpp/viper/utils/WaveBuffer.cpp similarity index 62% rename from src/cpp/viper/utils/WaveBuffer_I32.cpp rename to src/cpp/viper/utils/WaveBuffer.cpp index 9b08da1..4c2f503 100644 --- a/src/cpp/viper/utils/WaveBuffer_I32.cpp +++ b/src/cpp/viper/utils/WaveBuffer.cpp @@ -1,36 +1,31 @@ -#include +#include "WaveBuffer.h" #include -#include "WaveBuffer_I32.h" -WaveBuffer_I32::WaveBuffer_I32(int channels, uint32_t size) { +WaveBuffer::WaveBuffer(int channels, uint32_t size) { this->channels = channels; this->size = size * channels; this->index = 0; this->buffer = new float[this->size]; } -WaveBuffer_I32::~WaveBuffer_I32() { +WaveBuffer::~WaveBuffer() { delete this->buffer; this->buffer = nullptr; } -void WaveBuffer_I32::Reset() { - this->index = 0; -} - -uint32_t WaveBuffer_I32::GetBufferOffset() { +uint32_t WaveBuffer::GetBufferOffset() { return this->index / this->channels; } -uint32_t WaveBuffer_I32::GetBufferSize() { +uint32_t WaveBuffer::GetBufferSize() { return this->size / this->channels; } -float *WaveBuffer_I32::GetCurrentBufferI32Ptr() { +float *WaveBuffer::GetBuffer() { return this->buffer; } -uint32_t WaveBuffer_I32::PopSamples(uint32_t size, bool resetIndex) { +uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) { if (this->buffer == nullptr || this->size == 0) { return 0; } @@ -50,13 +45,13 @@ uint32_t WaveBuffer_I32::PopSamples(uint32_t size, bool resetIndex) { return 0; } -uint32_t WaveBuffer_I32::PopSamples(float *dest, uint32_t size, bool resetIndex) { +uint32_t WaveBuffer::PopSamples(float *dest, uint32_t size, bool resetIndex) { if (this->buffer == nullptr || this->size == 0 || dest == nullptr) { return 0; } if (this->channels * size <= this->index) { - memcpy(dest, this->buffer, this->index * sizeof(float)); + memcpy(dest, this->buffer, this->channels * size * sizeof(float)); this->index -= this->channels * size; memmove(this->buffer, &this->buffer[this->channels * size], this->index * sizeof(float)); return size; @@ -72,18 +67,19 @@ uint32_t WaveBuffer_I32::PopSamples(float *dest, uint32_t size, bool resetIndex) return 0; } -int WaveBuffer_I32::PushSamples(float *source, uint32_t size) { +int WaveBuffer::PushSamples(float *source, uint32_t size) { if (this->buffer == nullptr) { return 0; } if (size > 0) { - if (this->size < this->channels * size + this->index) { - auto *buf = new float[this->channels * size + this->index]; + uint32_t requiredSize = this->channels * size + this->index; + if (this->size < requiredSize) { + auto *buf = new float[requiredSize]; memcpy(buf, this->buffer, this->index * sizeof(float)); delete this->buffer; this->buffer = buf; - this->size = this->channels * size + this->index; + this->size = requiredSize; } memcpy(&this->buffer[this->index], source, this->channels * size * sizeof(float)); this->index += this->channels * size; @@ -92,18 +88,19 @@ int WaveBuffer_I32::PushSamples(float *source, uint32_t size) { return 1; } -int WaveBuffer_I32::PushZeros(uint32_t size) { +int WaveBuffer::PushZeros(uint32_t size) { if (this->buffer == nullptr) { return 0; } if (size > 0) { - if (this->size < this->channels * size + this->index) { - auto *buf = new float[this->channels * size + this->index]; + uint32_t requiredSize = this->channels * size + this->index; + if (this->size < requiredSize) { + auto *buf = new float[requiredSize]; memcpy(buf, this->buffer, this->index * sizeof(float)); delete this->buffer; this->buffer = buf; - this->size = this->channels * size + this->index; + this->size = requiredSize; } memset(&this->buffer[this->index], 0, this->channels * size * sizeof(float)); this->index += this->channels * size; @@ -112,26 +109,36 @@ int WaveBuffer_I32::PushZeros(uint32_t size) { return 1; } -float *WaveBuffer_I32::PushZerosGetBuffer(uint32_t size) { +float *WaveBuffer::PushZerosGetBuffer(uint32_t size) { + uint32_t oldIndex = this->index; + if (this->buffer == nullptr) { return nullptr; } if (size > 0) { - if (this->size < this->channels * size + this->index) { - auto *buf = new float[this->channels * size + this->index]; + uint32_t requiredSize = this->channels * size + this->index; + if (this->size < requiredSize) { + auto *buf = new float[requiredSize]; memcpy(buf, this->buffer, this->index * sizeof(float)); delete this->buffer; this->buffer = buf; - this->size = this->channels * size + this->index; + this->size = requiredSize; } memset(&this->buffer[this->index], 0, this->channels * size * sizeof(float)); this->index += this->channels * size; } - return &this->buffer[this->index]; + return &this->buffer[oldIndex]; } -void WaveBuffer_I32::SetBufferOffset(uint32_t offset) { - this->index = offset; +void WaveBuffer::Reset() { + this->index = 0; +} + +void WaveBuffer::SetBufferOffset(uint32_t offset) { + uint32_t maxOffset = this->size / this->channels; + if (offset <= maxOffset) { + this->index = offset * this->channels; + } } diff --git a/src/cpp/viper/utils/WaveBuffer_I32.h b/src/cpp/viper/utils/WaveBuffer.h similarity index 79% rename from src/cpp/viper/utils/WaveBuffer_I32.h rename to src/cpp/viper/utils/WaveBuffer.h index 243dcfd..4a24bc0 100644 --- a/src/cpp/viper/utils/WaveBuffer_I32.h +++ b/src/cpp/viper/utils/WaveBuffer.h @@ -3,11 +3,11 @@ #include -class WaveBuffer_I32 { +class WaveBuffer { public: - WaveBuffer_I32(int channels, uint32_t size); + WaveBuffer(int channels, uint32_t size); - ~WaveBuffer_I32(); + ~WaveBuffer(); void Reset(); @@ -15,7 +15,7 @@ public: uint32_t GetBufferSize(); - float *GetCurrentBufferI32Ptr(); + float *GetBuffer(); uint32_t PopSamples(uint32_t size, bool resetIndex);