axmol/thirdparty/openal/alc/effects/pshifter.cpp

308 lines
11 KiB
C++
Raw Normal View History

/**
* OpenAL cross platform audio library
* Copyright (C) 2018 by Raul Herraiz.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
2021-05-14 10:15:42 +08:00
#include <algorithm>
#include <array>
2021-05-14 10:15:42 +08:00
#include <cmath>
#include <complex>
2021-05-14 10:15:42 +08:00
#include <cstdlib>
#include <iterator>
2021-05-14 10:15:42 +08:00
#include "alc/effects/base.h"
#include "alcomplex.h"
2021-05-14 10:15:42 +08:00
#include "almalloc.h"
2022-04-25 12:02:45 +08:00
#include "alnumbers.h"
#include "alnumeric.h"
2021-05-14 10:15:42 +08:00
#include "alspan.h"
#include "core/bufferline.h"
#include "core/devformat.h"
#include "core/device.h"
2022-04-25 12:02:45 +08:00
#include "core/effectslot.h"
2021-05-14 10:15:42 +08:00
#include "core/mixer.h"
#include "core/mixer/defs.h"
#include "intrusive_ptr.h"
2021-05-14 10:15:42 +08:00
struct ContextBase;
namespace {
2021-05-14 10:15:42 +08:00
using uint = unsigned int;
2023-02-04 15:03:54 +08:00
using complex_f = std::complex<float>;
2023-02-04 15:03:54 +08:00
constexpr size_t StftSize{1024};
constexpr size_t StftHalfSize{StftSize >> 1};
constexpr size_t OversampleFactor{8};
2023-02-04 15:03:54 +08:00
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. */
2023-02-04 15:03:54 +08:00
struct Windower {
alignas(16) std::array<float,StftSize> mData;
Windower()
{
2023-02-04 15:03:54 +08:00
/* 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{StftSize}};
const double val{std::sin((static_cast<double>(i)+0.5) * scale)};
mData[i] = mData[StftSize-1-i] = static_cast<float>(val * val);
}
}
2023-02-04 15:03:54 +08:00
};
const Windower gWindow{};
struct FrequencyBin {
2023-02-04 15:03:54 +08:00
float Magnitude;
float FreqBin;
};
struct PshifterState final : public EffectState {
/* Effect parameters */
size_t mCount;
size_t mPos;
uint mPitchShiftI;
2023-02-04 15:03:54 +08:00
float mPitchShift;
/* Effects buffers */
2023-02-04 15:03:54 +08:00
std::array<float,StftSize> mFIFO;
std::array<float,StftHalfSize+1> mLastPhase;
std::array<float,StftHalfSize+1> mSumPhase;
std::array<float,StftSize> mOutputAccum;
2023-02-04 15:03:54 +08:00
std::array<complex_f,StftSize> mFftBuffer;
2023-02-04 15:03:54 +08:00
std::array<FrequencyBin,StftHalfSize+1> mAnalysisBuffer;
std::array<FrequencyBin,StftHalfSize+1> mSynthesisBuffer;
alignas(16) FloatBufferLine mBufferOut;
/* Effect gains for each output channel */
2023-02-04 15:03:54 +08:00
float mCurrentGains[MaxAmbiChannels];
float mTargetGains[MaxAmbiChannels];
2021-05-14 10:15:42 +08:00
void deviceUpdate(const DeviceBase *device, const Buffer &buffer) override;
void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
const EffectTarget target) override;
void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
const al::span<FloatBufferLine> samplesOut) override;
DEF_NEWDEL(PshifterState)
};
2021-05-14 10:15:42 +08:00
void PshifterState::deviceUpdate(const DeviceBase*, const Buffer&)
{
/* (Re-)initializing parameters and clear the buffers. */
mCount = 0;
2023-02-04 15:03:54 +08:00
mPos = StftSize - StftStep;
mPitchShiftI = MixerFracOne;
2023-02-04 15:03:54 +08:00
mPitchShift = 1.0f;
2023-02-04 15:03:54 +08:00
mFIFO.fill(0.0f);
mLastPhase.fill(0.0f);
mSumPhase.fill(0.0f);
mOutputAccum.fill(0.0f);
mFftBuffer.fill(complex_f{});
mAnalysisBuffer.fill(FrequencyBin{});
mSynthesisBuffer.fill(FrequencyBin{});
std::fill(std::begin(mCurrentGains), std::end(mCurrentGains), 0.0f);
std::fill(std::begin(mTargetGains), std::end(mTargetGains), 0.0f);
}
2021-05-14 10:15:42 +08:00
void PshifterState::update(const ContextBase*, const EffectSlot *slot,
const EffectProps *props, const EffectTarget target)
{
const int tune{props->Pshifter.CoarseTune*100 + props->Pshifter.FineTune};
const float pitch{std::pow(2.0f, static_cast<float>(tune) / 1200.0f)};
2023-02-04 15:03:54 +08:00
mPitchShiftI = clampu(fastf2u(pitch*MixerFracOne), MixerFracHalf, MixerFracOne*2);
mPitchShift = static_cast<float>(mPitchShiftI) * float{1.0f/MixerFracOne};
2023-02-04 15:03:54 +08:00
static constexpr auto coeffs = CalcDirectionCoeffs({0.0f, 0.0f, -1.0f});
mOutTarget = target.Main->Buffer;
ComputePanGains(target.Main, coeffs.data(), slot->Gain, mTargetGains);
}
2023-02-04 15:03:54 +08:00
void PshifterState::process(const size_t samplesToDo,
const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
{
/* Pitch shifter engine based on the work of Stephan Bernsee.
* http://blogs.zynaptiq.com/bernsee/pitch-shifting-using-the-ft/
*/
/* Cycle offset per update expected of each frequency bin (bin 0 is none,
* bin 1 is x1, bin 2 is x2, etc).
*/
2023-02-04 15:03:54 +08:00
constexpr float expected_cycles{al::numbers::pi_v<float>*2.0f / OversampleFactor};
for(size_t base{0u};base < samplesToDo;)
{
2023-02-04 15:03:54 +08:00
const size_t todo{minz(StftStep-mCount, samplesToDo-base)};
/* Retrieve the output samples from the FIFO and fill in the new input
* samples.
*/
auto fifo_iter = mFIFO.begin()+mPos + mCount;
2023-02-04 15:03:54 +08:00
std::copy_n(fifo_iter, todo, mBufferOut.begin()+base);
std::copy_n(samplesIn[0].begin()+base, todo, fifo_iter);
mCount += todo;
base += todo;
/* Check whether FIFO buffer is filled with new samples. */
2023-02-04 15:03:54 +08:00
if(mCount < StftStep) break;
mCount = 0;
2023-02-04 15:03:54 +08:00
mPos = (mPos+StftStep) & (mFIFO.size()-1);
/* Time-domain signal windowing, store in FftBuffer, and apply a
* forward FFT to get the frequency-domain signal.
*/
2023-02-04 15:03:54 +08:00
for(size_t src{mPos}, k{0u};src < StftSize;++src,++k)
mFftBuffer[k] = mFIFO[src] * gWindow.mData[k];
for(size_t src{0u}, k{StftSize-mPos};src < mPos;++src,++k)
mFftBuffer[k] = mFIFO[src] * gWindow.mData[k];
forward_fft(al::as_span(mFftBuffer));
/* Analyze the obtained data. Since the real FFT is symmetric, only
2023-02-04 15:03:54 +08:00
* StftHalfSize+1 samples are needed.
*/
2023-02-04 15:03:54 +08:00
for(size_t k{0u};k < StftHalfSize+1;k++)
{
2023-02-04 15:03:54 +08:00
const float magnitude{std::abs(mFftBuffer[k])};
const float phase{std::arg(mFftBuffer[k])};
/* Compute the phase difference from the last update and subtract
* the expected phase difference for this bin.
*
* When oversampling, the expected per-update offset increments by
* 1/OversampleFactor for every frequency bin. So, the offset wraps
* every 'OversampleFactor' bin.
*/
const auto bin_offset = static_cast<float>(k % OversampleFactor);
float tmp{(phase - mLastPhase[k]) - bin_offset*expected_cycles};
/* Store the actual phase for the next update. */
mLastPhase[k] = phase;
2023-02-04 15:03:54 +08:00
/* Normalize from pi, and wrap the delta between -1 and +1. */
tmp *= al::numbers::inv_pi_v<float>;
int qpd{float2int(tmp)};
tmp -= static_cast<float>(qpd + (qpd%2));
2023-02-04 15:03:54 +08:00
/* Get deviation from bin frequency (-0.5 to +0.5), and account for
* oversampling.
*/
2023-02-04 15:03:54 +08:00
tmp *= 0.5f * OversampleFactor;
2023-02-04 15:03:54 +08:00
/* Compute the k-th partials' frequency bin target and store the
* magnitude and frequency bin in the analysis buffer. We don't
* need the "true frequency" since it's a linear relationship with
* the bin.
*/
mAnalysisBuffer[k].Magnitude = magnitude;
mAnalysisBuffer[k].FreqBin = static_cast<float>(k) + tmp;
}
/* Shift the frequency bins according to the pitch adjustment,
2023-02-04 15:03:54 +08:00
* accumulating the magnitudes of overlapping frequency bins.
*/
std::fill(mSynthesisBuffer.begin(), mSynthesisBuffer.end(), FrequencyBin{});
2023-02-04 15:03:54 +08:00
constexpr size_t bin_limit{((StftHalfSize+1)<<MixerFracBits) - MixerFracHalf - 1};
const size_t bin_count{minz(StftHalfSize+1, bin_limit/mPitchShiftI + 1)};
for(size_t k{0u};k < bin_count;k++)
{
2023-02-04 15:03:54 +08:00
const size_t j{(k*mPitchShiftI + MixerFracHalf) >> MixerFracBits};
/* If more than two bins end up together, use the target frequency
* bin for the one with the dominant magnitude. There might be a
* better way to handle this, but it's better than last-index-wins.
*/
if(mAnalysisBuffer[k].Magnitude > mSynthesisBuffer[j].Magnitude)
mSynthesisBuffer[j].FreqBin = mAnalysisBuffer[k].FreqBin * mPitchShift;
mSynthesisBuffer[j].Magnitude += mAnalysisBuffer[k].Magnitude;
}
/* Reconstruct the frequency-domain signal from the adjusted frequency
* bins.
*/
2023-02-04 15:03:54 +08:00
for(size_t k{0u};k < StftHalfSize+1;k++)
{
2023-02-04 15:03:54 +08:00
/* Calculate the actual delta phase for this bin's target frequency
* bin, and accumulate it to get the actual bin phase.
*/
float tmp{mSumPhase[k] + mSynthesisBuffer[k].FreqBin*expected_cycles};
/* Wrap between -pi and +pi for the sum. If mSumPhase is left to
* grow indefinitely, it will lose precision and produce less exact
* phase over time.
*/
tmp *= al::numbers::inv_pi_v<float>;
int qpd{float2int(tmp)};
tmp -= static_cast<float>(qpd + (qpd%2));
mSumPhase[k] = tmp * al::numbers::pi_v<float>;
2023-02-04 15:03:54 +08:00
mFftBuffer[k] = std::polar(mSynthesisBuffer[k].Magnitude, mSumPhase[k]);
}
2023-02-04 15:03:54 +08:00
for(size_t k{StftHalfSize+1};k < StftSize;++k)
mFftBuffer[k] = std::conj(mFftBuffer[StftSize-k]);
2023-02-04 15:03:54 +08:00
/* Apply an inverse FFT to get the time-domain signal, and accumulate
* for the output with windowing.
*/
2023-02-04 15:03:54 +08:00
inverse_fft(al::as_span(mFftBuffer));
static constexpr float scale{3.0f / OversampleFactor / StftSize};
for(size_t dst{mPos}, k{0u};dst < StftSize;++dst,++k)
mOutputAccum[dst] += gWindow.mData[k]*mFftBuffer[k].real() * scale;
for(size_t dst{0u}, k{StftSize-mPos};dst < mPos;++dst,++k)
mOutputAccum[dst] += gWindow.mData[k]*mFftBuffer[k].real() * scale;
/* Copy out the accumulated result, then clear for the next iteration. */
2023-02-04 15:03:54 +08:00
std::copy_n(mOutputAccum.begin() + mPos, StftStep, mFIFO.begin() + mPos);
std::fill_n(mOutputAccum.begin() + mPos, StftStep, 0.0f);
}
/* Now, mix the processed sound data to the output. */
MixSamples({mBufferOut.data(), samplesToDo}, samplesOut, mCurrentGains, mTargetGains,
maxz(samplesToDo, 512), 0);
}
struct PshifterStateFactory final : public EffectStateFactory {
al::intrusive_ptr<EffectState> create() override
{ return al::intrusive_ptr<EffectState>{new PshifterState{}}; }
};
} // namespace
EffectStateFactory *PshifterStateFactory_getFactory()
{
static PshifterStateFactory PshifterFactory{};
return &PshifterFactory;
}