From d6e79c9023ad66986fcfe16caef15d8e8b14a20f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 19 Jan 2023 13:30:00 -0800 Subject: Minor code refactor for the frequency shifter effect --- alc/effects/fshifter.cpp | 97 +++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 47 deletions(-) (limited to 'alc/effects/fshifter.cpp') diff --git a/alc/effects/fshifter.cpp b/alc/effects/fshifter.cpp index 5d899aed..4bdb53a7 100644 --- a/alc/effects/fshifter.cpp +++ b/alc/effects/fshifter.cpp @@ -48,44 +48,47 @@ namespace { using uint = unsigned int; using complex_d = std::complex; -#define HIL_SIZE 1024 -#define OVERSAMP (1<<2) +constexpr size_t HilSize{1024}; +constexpr size_t HilHalfSize{HilSize >> 1}; +constexpr size_t OversampleFactor{4}; -#define HIL_STEP (HIL_SIZE / OVERSAMP) -#define FIFO_LATENCY (HIL_STEP * (OVERSAMP-1)) +static_assert(HilSize%OversampleFactor == 0, "Factor must be a clean divisor of the size"); +constexpr size_t HilStep{HilSize / OversampleFactor}; /* Define a Hann window, used to filter the HIL input and output. */ -std::array InitHannWindow() -{ - std::array ret; - /* Create lookup table of the Hann window for the desired size, i.e. HIL_SIZE */ - for(size_t i{0};i < HIL_SIZE>>1;i++) +struct Windower { + alignas(16) std::array mData; + + Windower() { - constexpr double scale{al::numbers::pi / double{HIL_SIZE}}; - const double val{std::sin((static_cast(i)+0.5) * scale)}; - ret[i] = ret[HIL_SIZE-1-i] = val * val; + /* Create lookup table of the Hann window for the desired size. */ + for(size_t i{0};i < HilHalfSize;i++) + { + constexpr double scale{al::numbers::pi / double{HilSize}}; + const double val{std::sin((static_cast(i)+0.5) * scale)}; + mData[i] = mData[HilSize-1-i] = val * val; + } } - return ret; -} -alignas(16) const std::array HannWindow = InitHannWindow(); +}; +const Windower gWindow{}; struct FshifterState final : public EffectState { /* Effect parameters */ size_t mCount{}; size_t mPos{}; - uint mPhaseStep[2]{}; - uint mPhase[2]{}; - double mSign[2]{}; + std::array mPhaseStep{}; + std::array mPhase{}; + std::array mSign{}; /* Effects buffers */ - double mInFIFO[HIL_SIZE]{}; - complex_d mOutFIFO[HIL_STEP]{}; - complex_d mOutputAccum[HIL_SIZE]{}; - complex_d mAnalytic[HIL_SIZE]{}; - complex_d mOutdata[BufferLineSize]{}; + std::array mInFIFO{}; + std::array mOutFIFO{}; + std::array mOutputAccum{}; + std::array mAnalytic{}; + std::array mOutdata{}; - alignas(16) float mBufferOut[BufferLineSize]{}; + alignas(16) FloatBufferLine mBufferOut{}; /* Effect gains for each output channel */ struct { @@ -107,15 +110,15 @@ void FshifterState::deviceUpdate(const DeviceBase*, const Buffer&) { /* (Re-)initializing parameters and clear the buffers. */ mCount = 0; - mPos = FIFO_LATENCY; + mPos = HilSize - HilStep; - std::fill(std::begin(mPhaseStep), std::end(mPhaseStep), 0u); - std::fill(std::begin(mPhase), std::end(mPhase), 0u); - std::fill(std::begin(mSign), std::end(mSign), 1.0); - std::fill(std::begin(mInFIFO), std::end(mInFIFO), 0.0); - std::fill(std::begin(mOutFIFO), std::end(mOutFIFO), complex_d{}); - std::fill(std::begin(mOutputAccum), std::end(mOutputAccum), complex_d{}); - std::fill(std::begin(mAnalytic), std::end(mAnalytic), complex_d{}); + mPhaseStep.fill(0u); + mPhase.fill(0u); + mSign.fill(1.0); + mInFIFO.fill(0.0); + mOutFIFO.fill(complex_d{}); + mOutputAccum.fill(complex_d{}); + mAnalytic.fill(complex_d{}); for(auto &gain : mGains) { @@ -172,7 +175,7 @@ void FshifterState::process(const size_t samplesToDo, const al::span(mBufferOut.data())}; + for(size_t c{0};c < 2;++c) { const uint phase_step{mPhaseStep[c]}; uint phase_idx{mPhase[c]}; -- cgit v1.2.3