From bbf49400da1bd803ca54d2512e5f025ea19d834f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 18 Jan 2023 16:43:43 -0800 Subject: Use constexpr variables instead of macros --- alc/effects/pshifter.cpp | 108 +++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/alc/effects/pshifter.cpp b/alc/effects/pshifter.cpp index 8619b334..625edc92 100644 --- a/alc/effects/pshifter.cpp +++ b/alc/effects/pshifter.cpp @@ -49,27 +49,27 @@ namespace { using uint = unsigned int; using complex_d = std::complex; -#define STFT_SIZE 1024 -#define STFT_HALF_SIZE (STFT_SIZE>>1) -#define OVERSAMP (1<<2) +constexpr size_t StftSize{1024}; +constexpr size_t StftHalfSize{StftSize >> 1}; +constexpr size_t OversampleFactor{4}; -#define STFT_STEP (STFT_SIZE / OVERSAMP) -#define FIFO_LATENCY (STFT_STEP * (OVERSAMP-1)) +static_assert(StftSize%OversampleFactor == 0, "Factor must be a clean divisor of the size"); +constexpr size_t StftStep{StftSize / OversampleFactor}; /* Define a Hann window, used to filter the STFT input and output. */ -std::array InitHannWindow() +std::array InitHannWindow() { - std::array ret; - /* Create lookup table of the Hann window for the desired size, i.e. STFT_SIZE */ - for(size_t i{0};i < STFT_SIZE>>1;i++) + std::array ret; + /* Create lookup table of the Hann window for the desired size. */ + for(size_t i{0};i < StftHalfSize;i++) { - constexpr double scale{al::numbers::pi / double{STFT_SIZE}}; + constexpr double scale{al::numbers::pi / double{StftSize}}; const double val{std::sin((static_cast(i)+0.5) * scale)}; - ret[i] = ret[STFT_SIZE-1-i] = val * val; + ret[i] = ret[StftSize-1-i] = val * val; } return ret; } -alignas(16) const std::array HannWindow = InitHannWindow(); +alignas(16) const std::array HannWindow = InitHannWindow(); struct FrequencyBin { @@ -86,15 +86,15 @@ struct PshifterState final : public EffectState { double mPitchShift; /* Effects buffers */ - std::array mFIFO; - std::array mLastPhase; - std::array mSumPhase; - std::array mOutputAccum; + std::array mFIFO; + std::array mLastPhase; + std::array mSumPhase; + std::array mOutputAccum; - std::array mFftBuffer; + std::array mFftBuffer; - std::array mAnalysisBuffer; - std::array mSynthesisBuffer; + std::array mAnalysisBuffer; + std::array mSynthesisBuffer; alignas(16) FloatBufferLine mBufferOut; @@ -116,7 +116,7 @@ void PshifterState::deviceUpdate(const DeviceBase*, const Buffer&) { /* (Re-)initializing parameters and clear the buffers. */ mCount = 0; - mPos = FIFO_LATENCY; + mPos = StftSize - StftStep; mPitchShiftI = MixerFracOne; mPitchShift = 1.0; @@ -146,7 +146,8 @@ void PshifterState::update(const ContextBase*, const EffectSlot *slot, ComputePanGains(target.Main, coeffs.data(), slot->Gain, mTargetGains); } -void PshifterState::process(const size_t samplesToDo, const al::span samplesIn, const al::span samplesOut) +void PshifterState::process(const size_t samplesToDo, + const al::span samplesIn, const al::span samplesOut) { /* Pitch shifter engine based on the work of Stephan Bernsee. * http://blogs.zynaptiq.com/bernsee/pitch-shifting-using-the-ft/ @@ -155,11 +156,11 @@ void PshifterState::process(const size_t samplesToDo, const al::span(k&(OVERSAMP-1)) * expected_cycles}; - double tmp{(phase - mLastPhase[k]) - expected_diff}; - /* Store the actual phase[k] for the next update. */ + const auto bin_offset = static_cast(k % OversampleFactor); + double tmp{(phase - mLastPhase[k]) - bin_offset*expected_cycles}; + /* Store the actual phase for the next update. */ mLastPhase[k] = phase; - /* Wrap the phase delta between -pi and +pi. */ - int qpd{double2int(tmp * al::numbers::inv_pi)}; - tmp -= al::numbers::pi * (qpd + (qpd%2)); + /* Normalize from pi, and wrap the delta between -1 and +1. */ + tmp *= al::numbers::inv_pi; + int qpd{double2int(tmp)}; + tmp -= qpd + (qpd%2); - /* Get deviation from bin frequency, accounting for oversampling. */ - tmp *= OVERSAMP * al::numbers::inv_pi * 0.5; + /* Get deviation from bin frequency (-0.5 to +0.5), and account for + * oversampling. + */ + tmp *= 0.5 * OversampleFactor; /* Compute the k-th partials' frequency bin target and store the * magnitude and frequency bin in the analysis buffer. We don't @@ -227,8 +231,8 @@ void PshifterState::process(const size_t samplesToDo, const al::span> MixerFracBits}; @@ -245,7 +249,7 @@ void PshifterState::process(const size_t samplesToDo, const al::span