2018-05-22 05:59:03 -07:00
|
|
|
/**
|
|
|
|
* 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"
|
|
|
|
|
2018-11-17 02:30:41 -08:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2018-11-17 06:11:55 -08:00
|
|
|
#include <array>
|
2018-11-17 04:14:57 -08:00
|
|
|
#include <complex>
|
|
|
|
#include <algorithm>
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-07-29 17:54:07 -07:00
|
|
|
#include "al/auxeffectslot.h"
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2018-11-17 23:02:27 -08:00
|
|
|
#include "alcontext.h"
|
2018-05-22 05:59:03 -07:00
|
|
|
#include "alu.h"
|
|
|
|
|
|
|
|
#include "alcomplex.h"
|
|
|
|
|
2018-11-17 02:30:41 -08:00
|
|
|
namespace {
|
|
|
|
|
2018-11-17 04:14:57 -08:00
|
|
|
using complex_d = std::complex<double>;
|
|
|
|
|
2018-05-22 05:59:03 -07:00
|
|
|
#define HIL_SIZE 1024
|
|
|
|
#define OVERSAMP (1<<2)
|
|
|
|
|
|
|
|
#define HIL_STEP (HIL_SIZE / OVERSAMP)
|
|
|
|
#define FIFO_LATENCY (HIL_STEP * (OVERSAMP-1))
|
|
|
|
|
2018-11-17 02:30:41 -08:00
|
|
|
/* Define a Hann window, used to filter the HIL input and output. */
|
2020-03-22 08:51:06 -07:00
|
|
|
std::array<double,HIL_SIZE> InitHannWindow()
|
2018-11-17 02:30:41 -08:00
|
|
|
{
|
2020-03-22 08:51:06 -07:00
|
|
|
std::array<double,HIL_SIZE> ret;
|
2018-11-17 02:30:41 -08:00
|
|
|
/* Create lookup table of the Hann window for the desired size, i.e. HIL_SIZE */
|
2019-09-14 10:59:02 -07:00
|
|
|
for(size_t i{0};i < HIL_SIZE>>1;i++)
|
2018-11-17 02:30:41 -08:00
|
|
|
{
|
2020-05-08 01:25:32 -07:00
|
|
|
constexpr double scale{al::MathDefs<double>::Pi() / double{HIL_SIZE}};
|
|
|
|
const double val{std::sin(static_cast<double>(i+1) * scale)};
|
2018-11-17 02:30:41 -08:00
|
|
|
ret[i] = ret[HIL_SIZE-1-i] = val * val;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2020-03-22 08:51:06 -07:00
|
|
|
alignas(16) const std::array<double,HIL_SIZE> HannWindow = InitHannWindow();
|
2018-11-17 02:30:41 -08:00
|
|
|
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-03-22 13:33:58 -07:00
|
|
|
struct FshifterState final : public EffectState {
|
2018-05-22 05:59:03 -07:00
|
|
|
/* Effect parameters */
|
2020-03-22 10:20:41 -07:00
|
|
|
size_t mCount{};
|
|
|
|
ALuint mPhaseStep[2]{};
|
|
|
|
ALuint mPhase[2]{};
|
|
|
|
double mSign[2]{};
|
2019-08-18 19:28:00 +02:00
|
|
|
|
2020-03-22 10:20:41 -07:00
|
|
|
/* Effects buffers */
|
2020-03-22 10:06:23 -07:00
|
|
|
double mInFIFO[HIL_SIZE]{};
|
|
|
|
complex_d mOutFIFO[HIL_STEP]{};
|
2018-11-19 21:04:50 -08:00
|
|
|
complex_d mOutputAccum[HIL_SIZE]{};
|
|
|
|
complex_d mAnalytic[HIL_SIZE]{};
|
2020-11-28 03:38:20 -08:00
|
|
|
complex_d mOutdata[BufferLineSize]{};
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2020-11-28 03:38:20 -08:00
|
|
|
alignas(16) float mBufferOut[BufferLineSize]{};
|
2018-05-22 05:59:03 -07:00
|
|
|
|
|
|
|
/* Effect gains for each output channel */
|
2019-08-18 19:28:00 +02:00
|
|
|
struct {
|
2020-04-08 07:28:07 -07:00
|
|
|
float Current[MAX_OUTPUT_CHANNELS]{};
|
|
|
|
float Target[MAX_OUTPUT_CHANNELS]{};
|
2019-08-18 15:22:10 -07:00
|
|
|
} mGains[2];
|
2018-05-22 05:59:03 -07:00
|
|
|
|
|
|
|
|
2020-04-16 17:29:32 -07:00
|
|
|
void deviceUpdate(const ALCdevice *device) override;
|
2020-11-07 08:36:49 -08:00
|
|
|
void update(const ALCcontext *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;
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-03-22 13:33:58 -07:00
|
|
|
DEF_NEWDEL(FshifterState)
|
2018-11-19 22:34:26 -08:00
|
|
|
};
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2020-04-16 17:29:32 -07:00
|
|
|
void FshifterState::deviceUpdate(const ALCdevice*)
|
2018-05-22 05:59:03 -07:00
|
|
|
{
|
|
|
|
/* (Re-)initializing parameters and clear the buffers. */
|
2019-08-18 15:22:10 -07:00
|
|
|
mCount = FIFO_LATENCY;
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2020-03-22 10:20:41 -07:00
|
|
|
std::fill(std::begin(mPhaseStep), std::end(mPhaseStep), 0u);
|
|
|
|
std::fill(std::begin(mPhase), std::end(mPhase), 0u);
|
2019-08-18 19:28:00 +02:00
|
|
|
std::fill(std::begin(mSign), std::end(mSign), 1.0);
|
2020-03-22 10:06:23 -07:00
|
|
|
std::fill(std::begin(mInFIFO), std::end(mInFIFO), 0.0);
|
2018-11-19 22:34:26 -08:00
|
|
|
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{});
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-08-18 15:22:10 -07:00
|
|
|
for(auto &gain : mGains)
|
2019-08-18 19:28:00 +02:00
|
|
|
{
|
|
|
|
std::fill(std::begin(gain.Current), std::end(gain.Current), 0.0f);
|
|
|
|
std::fill(std::begin(gain.Target), std::end(gain.Target), 0.0f);
|
|
|
|
}
|
2018-05-22 05:59:03 -07:00
|
|
|
}
|
|
|
|
|
2020-11-07 08:36:49 -08:00
|
|
|
void FshifterState::update(const ALCcontext *context, const EffectSlot *slot,
|
|
|
|
const EffectProps *props, const EffectTarget target)
|
2018-05-22 05:59:03 -07:00
|
|
|
{
|
2019-08-01 19:44:09 -07:00
|
|
|
const ALCdevice *device{context->mDevice.get()};
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2020-03-22 10:20:41 -07:00
|
|
|
const float step{props->Fshifter.Frequency / static_cast<float>(device->Frequency)};
|
2020-10-21 17:16:27 -07:00
|
|
|
mPhaseStep[0] = mPhaseStep[1] = fastf2u(minf(step, 1.0f) * MixerFracOne);
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2018-05-22 06:19:59 -07:00
|
|
|
switch(props->Fshifter.LeftDirection)
|
2018-05-22 05:59:03 -07:00
|
|
|
{
|
2019-08-18 15:22:10 -07:00
|
|
|
case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN:
|
|
|
|
mSign[0] = -1.0;
|
|
|
|
break;
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-08-18 15:22:10 -07:00
|
|
|
case AL_FREQUENCY_SHIFTER_DIRECTION_UP:
|
|
|
|
mSign[0] = 1.0;
|
|
|
|
break;
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-08-18 15:22:10 -07:00
|
|
|
case AL_FREQUENCY_SHIFTER_DIRECTION_OFF:
|
|
|
|
mPhase[0] = 0;
|
|
|
|
mPhaseStep[0] = 0;
|
|
|
|
break;
|
2018-05-22 05:59:03 -07:00
|
|
|
}
|
|
|
|
|
2020-03-22 10:20:41 -07:00
|
|
|
switch(props->Fshifter.RightDirection)
|
2019-08-18 19:28:00 +02:00
|
|
|
{
|
2019-08-18 15:22:10 -07:00
|
|
|
case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN:
|
|
|
|
mSign[1] = -1.0;
|
2019-08-18 19:28:00 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-18 15:22:10 -07:00
|
|
|
case AL_FREQUENCY_SHIFTER_DIRECTION_UP:
|
|
|
|
mSign[1] = 1.0;
|
2019-08-18 19:28:00 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-18 15:22:10 -07:00
|
|
|
case AL_FREQUENCY_SHIFTER_DIRECTION_OFF:
|
|
|
|
mPhase[1] = 0;
|
|
|
|
mPhaseStep[1] = 0;
|
|
|
|
break;
|
2019-08-18 19:28:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 23:58:53 -07:00
|
|
|
const auto lcoeffs = CalcDirectionCoeffs({-1.0f, 0.0f, 0.0f}, 0.0f);
|
|
|
|
const auto rcoeffs = CalcDirectionCoeffs({ 1.0f, 0.0f, 0.0f}, 0.0f);
|
2018-12-24 13:29:36 -08:00
|
|
|
|
2019-07-04 15:02:12 -07:00
|
|
|
mOutTarget = target.Main->Buffer;
|
2020-11-07 08:36:49 -08:00
|
|
|
ComputePanGains(target.Main, lcoeffs.data(), slot->Gain, mGains[0].Target);
|
|
|
|
ComputePanGains(target.Main, rcoeffs.data(), slot->Gain, mGains[1].Target);
|
2018-05-22 05:59:03 -07:00
|
|
|
}
|
|
|
|
|
2019-08-26 09:16:20 -07:00
|
|
|
void FshifterState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
|
2018-05-22 05:59:03 -07:00
|
|
|
{
|
2019-08-20 14:30:04 -07:00
|
|
|
for(size_t base{0u};base < samplesToDo;)
|
2018-05-22 05:59:03 -07:00
|
|
|
{
|
2020-03-22 10:06:23 -07:00
|
|
|
size_t todo{minz(HIL_SIZE-mCount, samplesToDo-base)};
|
2018-05-22 07:28:53 -07:00
|
|
|
|
2018-05-22 05:59:03 -07:00
|
|
|
/* Fill FIFO buffer with samples data */
|
2020-03-22 10:06:23 -07:00
|
|
|
size_t count{mCount};
|
|
|
|
do {
|
|
|
|
mInFIFO[count] = samplesIn[0][base];
|
|
|
|
mOutdata[base] = mOutFIFO[count-FIFO_LATENCY];
|
|
|
|
++base; ++count;
|
|
|
|
} while(--todo);
|
|
|
|
mCount = count;
|
2018-05-22 05:59:03 -07:00
|
|
|
|
|
|
|
/* Check whether FIFO buffer is filled */
|
2020-03-22 10:06:23 -07:00
|
|
|
if(mCount < HIL_SIZE) break;
|
2018-11-19 22:34:26 -08:00
|
|
|
mCount = FIFO_LATENCY;
|
2018-05-22 07:28:53 -07:00
|
|
|
|
|
|
|
/* Real signal windowing and store in Analytic buffer */
|
2020-03-22 10:06:23 -07:00
|
|
|
for(size_t k{0};k < HIL_SIZE;k++)
|
|
|
|
mAnalytic[k] = mInFIFO[k]*HannWindow[k];
|
2018-05-22 07:28:53 -07:00
|
|
|
|
|
|
|
/* Processing signal by Discrete Hilbert Transform (analytical signal). */
|
2019-06-08 16:05:18 -07:00
|
|
|
complex_hilbert(mAnalytic);
|
2018-05-22 07:28:53 -07:00
|
|
|
|
|
|
|
/* Windowing and add to output accumulator */
|
2020-03-22 10:06:23 -07:00
|
|
|
for(size_t k{0};k < HIL_SIZE;k++)
|
2018-11-19 22:34:26 -08:00
|
|
|
mOutputAccum[k] += 2.0/OVERSAMP*HannWindow[k]*mAnalytic[k];
|
2018-05-22 07:28:53 -07:00
|
|
|
|
|
|
|
/* Shift accumulator, input & output FIFO */
|
2020-03-22 10:06:23 -07:00
|
|
|
std::copy_n(mOutputAccum, HIL_STEP, mOutFIFO);
|
|
|
|
auto accum_iter = std::copy(std::begin(mOutputAccum)+HIL_STEP, std::end(mOutputAccum),
|
|
|
|
std::begin(mOutputAccum));
|
|
|
|
std::fill(accum_iter, std::end(mOutputAccum), complex_d{});
|
|
|
|
std::copy(std::begin(mInFIFO)+HIL_STEP, std::end(mInFIFO), std::begin(mInFIFO));
|
2018-05-22 05:59:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process frequency shifter using the analytic signal obtained. */
|
2020-04-08 07:28:07 -07:00
|
|
|
float *RESTRICT BufferOut{mBufferOut};
|
2019-08-18 15:22:10 -07:00
|
|
|
for(ALsizei c{0};c < 2;++c)
|
2018-05-22 05:59:03 -07:00
|
|
|
{
|
2020-03-22 10:20:41 -07:00
|
|
|
const ALuint phase_step{mPhaseStep[c]};
|
|
|
|
ALuint phase_idx{mPhase[c]};
|
2020-03-22 10:06:23 -07:00
|
|
|
for(size_t k{0};k < samplesToDo;++k)
|
2019-08-18 19:28:00 +02:00
|
|
|
{
|
2020-10-21 17:16:27 -07:00
|
|
|
const double phase{phase_idx * ((1.0/MixerFracOne) * al::MathDefs<double>::Tau())};
|
2019-08-18 19:28:00 +02:00
|
|
|
BufferOut[k] = static_cast<float>(mOutdata[k].real()*std::cos(phase) +
|
|
|
|
mOutdata[k].imag()*std::sin(phase)*mSign[c]);
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2020-03-22 10:06:23 -07:00
|
|
|
phase_idx += phase_step;
|
2020-10-21 17:16:27 -07:00
|
|
|
phase_idx &= MixerFracMask;
|
2019-08-18 19:28:00 +02:00
|
|
|
}
|
2020-03-22 10:06:23 -07:00
|
|
|
mPhase[c] = phase_idx;
|
2018-05-22 05:59:03 -07:00
|
|
|
|
2019-08-18 19:28:00 +02:00
|
|
|
/* Now, mix the processed sound data to the output. */
|
2019-08-20 14:30:04 -07:00
|
|
|
MixSamples({BufferOut, samplesToDo}, samplesOut, mGains[c].Current, mGains[c].Target,
|
|
|
|
maxz(samplesToDo, 512), 0);
|
2019-08-18 19:28:00 +02:00
|
|
|
}
|
2018-05-22 05:59:03 -07:00
|
|
|
}
|
|
|
|
|
2018-11-17 02:30:41 -08:00
|
|
|
|
2019-03-22 13:33:58 -07:00
|
|
|
struct FshifterStateFactory final : public EffectStateFactory {
|
|
|
|
EffectState *create() override { return new FshifterState{}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
EffectStateFactory *FshifterStateFactory_getFactory()
|
|
|
|
{
|
|
|
|
static FshifterStateFactory FshifterFactory{};
|
|
|
|
return &FshifterFactory;
|
|
|
|
}
|