mirror of
https://github.com/love2d/megasource.git
synced 2026-08-19 20:20:11 +02:00
update OpenAL-Soft to 1.24.3.
This commit is contained in:
@@ -30,13 +30,16 @@ inline constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */
|
||||
enum class Resampler : std::uint8_t {
|
||||
Point,
|
||||
Linear,
|
||||
Cubic,
|
||||
Spline,
|
||||
Gaussian,
|
||||
FastBSinc12,
|
||||
BSinc12,
|
||||
FastBSinc24,
|
||||
BSinc24,
|
||||
FastBSinc48,
|
||||
BSinc48,
|
||||
|
||||
Max = BSinc24
|
||||
Max = BSinc48
|
||||
};
|
||||
|
||||
/* Interpolator state. Kind of a misnomer since the interpolator itself is
|
||||
@@ -51,7 +54,7 @@ struct BsincState {
|
||||
* delta coefficients. Starting at phase index 0, each subsequent phase
|
||||
* index follows contiguously.
|
||||
*/
|
||||
const float *filter;
|
||||
al::span<const float> filter;
|
||||
};
|
||||
|
||||
struct CubicState {
|
||||
@@ -59,49 +62,51 @@ struct CubicState {
|
||||
* each subsequent phase index follows contiguously.
|
||||
*/
|
||||
al::span<const CubicCoefficients,CubicPhaseCount> filter;
|
||||
CubicState(al::span<const CubicCoefficients,CubicPhaseCount> f) : filter{f} { }
|
||||
explicit CubicState(al::span<const CubicCoefficients,CubicPhaseCount> f) : filter{f} { }
|
||||
};
|
||||
|
||||
using InterpState = std::variant<std::monostate,CubicState,BsincState>;
|
||||
|
||||
using ResamplerFunc = void(*)(const InterpState *state, const float *src, uint frac,
|
||||
using ResamplerFunc = void(*)(const InterpState *state, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst);
|
||||
|
||||
ResamplerFunc PrepareResampler(Resampler resampler, uint increment, InterpState *state);
|
||||
|
||||
|
||||
template<typename TypeTag, typename InstTag>
|
||||
void Resample_(const InterpState *state, const float *src, uint frac, const uint increment,
|
||||
const al::span<float> dst);
|
||||
void Resample_(const InterpState *state, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst);
|
||||
|
||||
template<typename InstTag>
|
||||
void Mix_(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
|
||||
float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos);
|
||||
const al::span<float> CurrentGains, const al::span<const float> TargetGains,
|
||||
const size_t Counter, const size_t OutPos);
|
||||
template<typename InstTag>
|
||||
void Mix_(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain,
|
||||
const float TargetGain, const size_t Counter);
|
||||
void Mix_(const al::span<const float> InSamples, const al::span<float> OutBuffer,
|
||||
float &CurrentGain, const float TargetGain, const size_t Counter);
|
||||
|
||||
template<typename InstTag>
|
||||
void MixHrtf_(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const MixHrtfFilter *hrtfparams, const size_t BufferSize);
|
||||
void MixHrtf_(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
|
||||
const uint IrSize, const MixHrtfFilter *hrtfparams, const size_t SamplesToDo);
|
||||
template<typename InstTag>
|
||||
void MixHrtfBlend_(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize);
|
||||
void MixHrtfBlend_(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
|
||||
const uint IrSize, const HrtfFilter *oldparams, const MixHrtfFilter *newparams,
|
||||
const size_t SamplesToDo);
|
||||
template<typename InstTag>
|
||||
void MixDirectHrtf_(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
|
||||
const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, HrtfChannelState *ChanState, const size_t IrSize,
|
||||
const size_t BufferSize);
|
||||
const al::span<const FloatBufferLine> InSamples, const al::span<float2> AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, const al::span<HrtfChannelState> ChanState,
|
||||
const size_t IrSize, const size_t SamplesToDo);
|
||||
|
||||
/* Vectorized resampler helpers */
|
||||
template<size_t N>
|
||||
constexpr void InitPosArrays(uint frac, const uint increment, const al::span<uint,N> frac_arr,
|
||||
const al::span<uint,N> pos_arr)
|
||||
constexpr void InitPosArrays(uint pos, uint frac, const uint increment,
|
||||
const al::span<uint,N> frac_arr, const al::span<uint,N> pos_arr)
|
||||
{
|
||||
static_assert(pos_arr.size() == frac_arr.size());
|
||||
pos_arr[0] = 0;
|
||||
pos_arr[0] = pos;
|
||||
frac_arr[0] = frac;
|
||||
for(size_t i{1};i < pos_arr.size();i++)
|
||||
for(size_t i{1};i < pos_arr.size();++i)
|
||||
{
|
||||
const uint frac_tmp{frac_arr[i-1] + increment};
|
||||
pos_arr[i] = pos_arr[i-1] + (frac_tmp>>MixerFracBits);
|
||||
|
||||
@@ -4,21 +4,23 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "almalloc.h"
|
||||
#include "defs.h"
|
||||
#include "hrtfdefs.h"
|
||||
#include "opthelpers.h"
|
||||
|
||||
|
||||
using uint = unsigned int;
|
||||
|
||||
using ApplyCoeffsT = void(&)(float2 *RESTRICT Values, const size_t irSize,
|
||||
using ApplyCoeffsT = void(const al::span<float2> Values, const size_t irSize,
|
||||
const ConstHrirSpan Coeffs, const float left, const float right);
|
||||
|
||||
template<ApplyCoeffsT ApplyCoeffs>
|
||||
inline void MixHrtfBase(const float *InSamples, float2 *RESTRICT AccumSamples, const size_t IrSize,
|
||||
const MixHrtfFilter *hrtfparams, const size_t BufferSize)
|
||||
inline void MixHrtfBase(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
|
||||
const size_t IrSize, const MixHrtfFilter *hrtfparams, const size_t SamplesToDo)
|
||||
{
|
||||
ASSUME(BufferSize > 0);
|
||||
ASSUME(SamplesToDo > 0);
|
||||
ASSUME(SamplesToDo <= BufferLineSize);
|
||||
ASSUME(IrSize <= HrirLength);
|
||||
|
||||
const ConstHrirSpan Coeffs{hrtfparams->Coeffs};
|
||||
const float gainstep{hrtfparams->GainStep};
|
||||
@@ -27,26 +29,28 @@ inline void MixHrtfBase(const float *InSamples, float2 *RESTRICT AccumSamples, c
|
||||
size_t ldelay{HrtfHistoryLength - hrtfparams->Delay[0]};
|
||||
size_t rdelay{HrtfHistoryLength - hrtfparams->Delay[1]};
|
||||
float stepcount{0.0f};
|
||||
for(size_t i{0u};i < BufferSize;++i)
|
||||
for(size_t i{0u};i < SamplesToDo;++i)
|
||||
{
|
||||
const float g{gain + gainstep*stepcount};
|
||||
const float left{InSamples[ldelay++] * g};
|
||||
const float right{InSamples[rdelay++] * g};
|
||||
ApplyCoeffs(AccumSamples+i, IrSize, Coeffs, left, right);
|
||||
ApplyCoeffs(AccumSamples.subspan(i), IrSize, Coeffs, left, right);
|
||||
|
||||
stepcount += 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
template<ApplyCoeffsT ApplyCoeffs>
|
||||
inline void MixHrtfBlendBase(const float *InSamples, float2 *RESTRICT AccumSamples,
|
||||
const size_t IrSize, const HrtfFilter *oldparams, const MixHrtfFilter *newparams,
|
||||
const size_t BufferSize)
|
||||
inline void MixHrtfBlendBase(const al::span<const float> InSamples,
|
||||
const al::span<float2> AccumSamples, const size_t IrSize, const HrtfFilter *oldparams,
|
||||
const MixHrtfFilter *newparams, const size_t SamplesToDo)
|
||||
{
|
||||
ASSUME(BufferSize > 0);
|
||||
ASSUME(SamplesToDo > 0);
|
||||
ASSUME(SamplesToDo <= BufferLineSize);
|
||||
ASSUME(IrSize <= HrirLength);
|
||||
|
||||
const ConstHrirSpan OldCoeffs{oldparams->Coeffs};
|
||||
const float oldGainStep{oldparams->Gain / static_cast<float>(BufferSize)};
|
||||
const float oldGainStep{oldparams->Gain / static_cast<float>(SamplesToDo)};
|
||||
const ConstHrirSpan NewCoeffs{newparams->Coeffs};
|
||||
const float newGainStep{newparams->GainStep};
|
||||
|
||||
@@ -54,29 +58,29 @@ inline void MixHrtfBlendBase(const float *InSamples, float2 *RESTRICT AccumSampl
|
||||
{
|
||||
size_t ldelay{HrtfHistoryLength - oldparams->Delay[0]};
|
||||
size_t rdelay{HrtfHistoryLength - oldparams->Delay[1]};
|
||||
auto stepcount = static_cast<float>(BufferSize);
|
||||
for(size_t i{0u};i < BufferSize;++i)
|
||||
auto stepcount = static_cast<float>(SamplesToDo);
|
||||
for(size_t i{0u};i < SamplesToDo;++i)
|
||||
{
|
||||
const float g{oldGainStep*stepcount};
|
||||
const float left{InSamples[ldelay++] * g};
|
||||
const float right{InSamples[rdelay++] * g};
|
||||
ApplyCoeffs(AccumSamples+i, IrSize, OldCoeffs, left, right);
|
||||
ApplyCoeffs(AccumSamples.subspan(i), IrSize, OldCoeffs, left, right);
|
||||
|
||||
stepcount -= 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if(newGainStep*static_cast<float>(BufferSize) > GainSilenceThreshold) LIKELY
|
||||
if(newGainStep*static_cast<float>(SamplesToDo) > GainSilenceThreshold) LIKELY
|
||||
{
|
||||
size_t ldelay{HrtfHistoryLength+1 - newparams->Delay[0]};
|
||||
size_t rdelay{HrtfHistoryLength+1 - newparams->Delay[1]};
|
||||
float stepcount{1.0f};
|
||||
for(size_t i{1u};i < BufferSize;++i)
|
||||
for(size_t i{1u};i < SamplesToDo;++i)
|
||||
{
|
||||
const float g{newGainStep*stepcount};
|
||||
const float left{InSamples[ldelay++] * g};
|
||||
const float right{InSamples[rdelay++] * g};
|
||||
ApplyCoeffs(AccumSamples+i, IrSize, NewCoeffs, left, right);
|
||||
ApplyCoeffs(AccumSamples.subspan(i), IrSize, NewCoeffs, left, right);
|
||||
|
||||
stepcount += 1.0f;
|
||||
}
|
||||
@@ -85,46 +89,52 @@ inline void MixHrtfBlendBase(const float *InSamples, float2 *RESTRICT AccumSampl
|
||||
|
||||
template<ApplyCoeffsT ApplyCoeffs>
|
||||
inline void MixDirectHrtfBase(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
|
||||
const al::span<const FloatBufferLine> InSamples, float2 *RESTRICT AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, HrtfChannelState *ChanState, const size_t IrSize,
|
||||
const size_t BufferSize)
|
||||
const al::span<const FloatBufferLine> InSamples, const al::span<float2> AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, const al::span<HrtfChannelState> ChannelState,
|
||||
const size_t IrSize, const size_t SamplesToDo)
|
||||
{
|
||||
ASSUME(BufferSize > 0);
|
||||
ASSUME(SamplesToDo > 0);
|
||||
ASSUME(SamplesToDo <= BufferLineSize);
|
||||
ASSUME(IrSize <= HrirLength);
|
||||
assert(ChannelState.size() == InSamples.size());
|
||||
|
||||
auto ChanState = ChannelState.begin();
|
||||
for(const FloatBufferLine &input : InSamples)
|
||||
{
|
||||
/* For dual-band processing, the signal needs extra scaling applied to
|
||||
* the high frequency response. The band-splitter applies this scaling
|
||||
* with a consistent phase shift regardless of the scale amount.
|
||||
*/
|
||||
ChanState->mSplitter.processHfScale({input.data(), BufferSize}, TempBuf,
|
||||
ChanState->mSplitter.processHfScale(al::span{input}.first(SamplesToDo), TempBuf,
|
||||
ChanState->mHfScale);
|
||||
|
||||
/* Now apply the HRIR coefficients to this channel. */
|
||||
const float *RESTRICT tempbuf{al::assume_aligned<16>(TempBuf.data())};
|
||||
const ConstHrirSpan Coeffs{ChanState->mCoeffs};
|
||||
for(size_t i{0u};i < BufferSize;++i)
|
||||
for(size_t i{0u};i < SamplesToDo;++i)
|
||||
{
|
||||
const float insample{tempbuf[i]};
|
||||
ApplyCoeffs(AccumSamples+i, IrSize, Coeffs, insample, insample);
|
||||
const float insample{TempBuf[i]};
|
||||
ApplyCoeffs(AccumSamples.subspan(i), IrSize, Coeffs, insample, insample);
|
||||
}
|
||||
|
||||
++ChanState;
|
||||
}
|
||||
|
||||
/* Add the HRTF signal to the existing "direct" signal. */
|
||||
float *RESTRICT left{al::assume_aligned<16>(LeftOut.data())};
|
||||
float *RESTRICT right{al::assume_aligned<16>(RightOut.data())};
|
||||
for(size_t i{0u};i < BufferSize;++i)
|
||||
left[i] += AccumSamples[i][0];
|
||||
for(size_t i{0u};i < BufferSize;++i)
|
||||
right[i] += AccumSamples[i][1];
|
||||
const auto left = al::span{al::assume_aligned<16>(LeftOut.data()), SamplesToDo};
|
||||
std::transform(left.cbegin(), left.cend(), AccumSamples.cbegin(), left.begin(),
|
||||
[](const float sample, const float2 &accum) noexcept -> float
|
||||
{ return sample + accum[0]; });
|
||||
const auto right = al::span{al::assume_aligned<16>(RightOut.data()), SamplesToDo};
|
||||
std::transform(right.cbegin(), right.cend(), AccumSamples.cbegin(), right.begin(),
|
||||
[](const float sample, const float2 &accum) noexcept -> float
|
||||
{ return sample + accum[1]; });
|
||||
|
||||
/* Copy the new in-progress accumulation values to the front and clear the
|
||||
* following samples for the next mix.
|
||||
*/
|
||||
auto accum_iter = std::copy_n(AccumSamples+BufferSize, HrirLength, AccumSamples);
|
||||
std::fill_n(accum_iter, BufferSize, float2{});
|
||||
const auto accum_inprog = AccumSamples.subspan(SamplesToDo, HrirLength);
|
||||
auto accum_iter = std::copy(accum_inprog.cbegin(), accum_inprog.cend(), AccumSamples.begin());
|
||||
std::fill_n(accum_iter, SamplesToDo, float2{});
|
||||
}
|
||||
|
||||
#endif /* CORE_MIXER_HRTFBASE_H */
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "core/bufferline.h"
|
||||
#include "core/cubic_defs.h"
|
||||
#include "core/mixer/hrtfdefs.h"
|
||||
#include "core/resampler_limits.h"
|
||||
#include "defs.h"
|
||||
#include "hrtfbase.h"
|
||||
#include "opthelpers.h"
|
||||
@@ -34,214 +35,246 @@ constexpr uint CubicPhaseDiffBits{MixerFracBits - CubicPhaseBits};
|
||||
constexpr uint CubicPhaseDiffOne{1 << CubicPhaseDiffBits};
|
||||
constexpr uint CubicPhaseDiffMask{CubicPhaseDiffOne - 1u};
|
||||
|
||||
constexpr
|
||||
auto do_point(const float *vals, const uint) noexcept -> float { return vals[0]; }
|
||||
constexpr
|
||||
auto do_lerp(const float *vals, const uint frac) noexcept -> float
|
||||
{ return lerpf(vals[0], vals[1], static_cast<float>(frac)*(1.0f/MixerFracOne)); }
|
||||
constexpr
|
||||
auto do_cubic(const CubicState &istate, const float *vals, const uint frac) noexcept -> float
|
||||
using SamplerNST = float(const al::span<const float>, const size_t, const uint) noexcept;
|
||||
|
||||
template<typename T>
|
||||
using SamplerT = float(const T&,const al::span<const float>,const size_t,const uint) noexcept;
|
||||
|
||||
[[nodiscard]] constexpr
|
||||
auto do_point(const al::span<const float> vals, const size_t pos, const uint) noexcept -> float
|
||||
{ return vals[pos]; }
|
||||
[[nodiscard]] constexpr
|
||||
auto do_lerp(const al::span<const float> vals, const size_t pos, const uint frac) noexcept -> float
|
||||
{ return lerpf(vals[pos+0], vals[pos+1], static_cast<float>(frac)*(1.0f/MixerFracOne)); }
|
||||
[[nodiscard]] constexpr
|
||||
auto do_cubic(const CubicState &istate, const al::span<const float> vals, const size_t pos,
|
||||
const uint frac) noexcept -> float
|
||||
{
|
||||
/* Calculate the phase index and factor. */
|
||||
const uint pi{frac >> CubicPhaseDiffBits};
|
||||
const uint pi{frac >> CubicPhaseDiffBits}; ASSUME(pi < CubicPhaseCount);
|
||||
const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)};
|
||||
|
||||
const auto fil = al::span{istate.filter[pi].mCoeffs};
|
||||
const auto phd = al::span{istate.filter[pi].mDeltas};
|
||||
|
||||
/* Apply the phase interpolated filter. */
|
||||
return (fil[0] + pf*phd[0])*vals[0] + (fil[1] + pf*phd[1])*vals[1]
|
||||
+ (fil[2] + pf*phd[2])*vals[2] + (fil[3] + pf*phd[3])*vals[3];
|
||||
return (fil[0] + pf*phd[0])*vals[pos+0] + (fil[1] + pf*phd[1])*vals[pos+1]
|
||||
+ (fil[2] + pf*phd[2])*vals[pos+2] + (fil[3] + pf*phd[3])*vals[pos+3];
|
||||
}
|
||||
constexpr
|
||||
auto do_bsinc(const BsincState &istate, const float *vals, const uint frac) noexcept -> float
|
||||
[[nodiscard]] constexpr
|
||||
auto do_fastbsinc(const BsincState &bsinc, const al::span<const float> vals, const size_t pos,
|
||||
const uint frac) noexcept -> float
|
||||
{
|
||||
const size_t m{istate.m};
|
||||
const size_t m{bsinc.m};
|
||||
ASSUME(m > 0);
|
||||
ASSUME(m <= MaxResamplerPadding);
|
||||
|
||||
/* Calculate the phase index and factor. */
|
||||
const uint pi{frac >> BsincPhaseDiffBits};
|
||||
const uint pi{frac >> BsincPhaseDiffBits}; ASSUME(pi < BSincPhaseCount);
|
||||
const float pf{static_cast<float>(frac&BsincPhaseDiffMask) * (1.0f/BsincPhaseDiffOne)};
|
||||
|
||||
const float *fil{istate.filter + m*pi*2_uz};
|
||||
const float *phd{fil + m};
|
||||
const float *scd{fil + BSincPhaseCount*2_uz*m};
|
||||
const float *spd{scd + m};
|
||||
|
||||
/* Apply the scale and phase interpolated filter. */
|
||||
float r{0.0f};
|
||||
for(size_t j_f{0};j_f < m;j_f++)
|
||||
r += (fil[j_f] + istate.sf*scd[j_f] + pf*(phd[j_f] + istate.sf*spd[j_f])) * vals[j_f];
|
||||
return r;
|
||||
}
|
||||
constexpr
|
||||
auto do_fastbsinc(const BsincState &istate, const float *vals, const uint frac) noexcept -> float
|
||||
{
|
||||
const size_t m{istate.m};
|
||||
ASSUME(m > 0);
|
||||
|
||||
/* Calculate the phase index and factor. */
|
||||
const uint pi{frac >> BsincPhaseDiffBits};
|
||||
const float pf{static_cast<float>(frac&BsincPhaseDiffMask) * (1.0f/BsincPhaseDiffOne)};
|
||||
|
||||
const float *fil{istate.filter + m*pi*2_uz};
|
||||
const float *phd{fil + m};
|
||||
const auto fil = bsinc.filter.subspan(2_uz*pi*m);
|
||||
const auto phd = fil.subspan(m);
|
||||
|
||||
/* Apply the phase interpolated filter. */
|
||||
float r{0.0f};
|
||||
for(size_t j_f{0};j_f < m;j_f++)
|
||||
r += (fil[j_f] + pf*phd[j_f]) * vals[j_f];
|
||||
for(size_t j_f{0};j_f < m;++j_f)
|
||||
r += (fil[j_f] + pf*phd[j_f]) * vals[pos+j_f];
|
||||
return r;
|
||||
}
|
||||
[[nodiscard]] constexpr
|
||||
auto do_bsinc(const BsincState &bsinc, const al::span<const float> vals, const size_t pos,
|
||||
const uint frac) noexcept -> float
|
||||
{
|
||||
const size_t m{bsinc.m};
|
||||
ASSUME(m > 0);
|
||||
ASSUME(m <= MaxResamplerPadding);
|
||||
|
||||
/* Calculate the phase index and factor. */
|
||||
const uint pi{frac >> BsincPhaseDiffBits}; ASSUME(pi < BSincPhaseCount);
|
||||
const float pf{static_cast<float>(frac&BsincPhaseDiffMask) * (1.0f/BsincPhaseDiffOne)};
|
||||
|
||||
const auto fil = bsinc.filter.subspan(2_uz*pi*m);
|
||||
const auto phd = fil.subspan(m);
|
||||
const auto scd = fil.subspan(BSincPhaseCount*2_uz*m);
|
||||
const auto spd = scd.subspan(m);
|
||||
|
||||
/* Apply the scale and phase interpolated filter. */
|
||||
float r{0.0f};
|
||||
for(size_t j_f{0};j_f < m;++j_f)
|
||||
r += (fil[j_f] + bsinc.sf*scd[j_f] + pf*(phd[j_f] + bsinc.sf*spd[j_f])) * vals[pos+j_f];
|
||||
return r;
|
||||
}
|
||||
|
||||
template<float(&Sampler)(const float*, const uint)noexcept>
|
||||
void DoResample(const float *src, uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
std::generate(dst.begin(), dst.end(), [&src,&frac,increment]() -> float
|
||||
{
|
||||
const float output{Sampler(src, frac)};
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
template<typename U, float(&Sampler)(const U&, const float*,const uint)noexcept>
|
||||
void DoResample(const U istate, const float *src, uint frac, const uint increment,
|
||||
template<SamplerNST Sampler>
|
||||
void DoResample(const al::span<const float> src, uint frac, const uint increment,
|
||||
const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
std::generate(dst.begin(), dst.end(), [istate,&src,&frac,increment]() -> float
|
||||
size_t pos{0};
|
||||
std::generate(dst.begin(), dst.end(), [&pos,&frac,src,increment]() -> float
|
||||
{
|
||||
const float output{Sampler(istate, src, frac)};
|
||||
const float output{Sampler(src, pos, frac)};
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
constexpr void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize,
|
||||
template<typename U, SamplerT<U> Sampler>
|
||||
void DoResample(const U istate, const al::span<const float> src, uint frac, const uint increment,
|
||||
const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
size_t pos{0};
|
||||
std::generate(dst.begin(), dst.end(), [istate,src,&pos,&frac,increment]() -> float
|
||||
{
|
||||
const float output{Sampler(istate, src, pos, frac)};
|
||||
frac += increment;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
inline void ApplyCoeffs(const al::span<float2> Values, const size_t IrSize,
|
||||
const ConstHrirSpan Coeffs, const float left, const float right) noexcept
|
||||
{
|
||||
ASSUME(IrSize >= MinIrLength);
|
||||
for(size_t c{0};c < IrSize;++c)
|
||||
{
|
||||
Values[c][0] += Coeffs[c][0] * left;
|
||||
Values[c][1] += Coeffs[c][1] * right;
|
||||
}
|
||||
ASSUME(IrSize <= HrirLength);
|
||||
|
||||
auto mix_impulse = [left,right](const float2 &value, const float2 &coeff) noexcept -> float2
|
||||
{ return float2{{value[0] + coeff[0]*left, value[1] + coeff[1]*right}}; };
|
||||
std::transform(Values.cbegin(), Values.cbegin()+ptrdiff_t(IrSize), Coeffs.cbegin(),
|
||||
Values.begin(), mix_impulse);
|
||||
}
|
||||
|
||||
force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst,
|
||||
float &CurrentGain, const float TargetGain, const float delta, const size_t min_len,
|
||||
force_inline void MixLine(al::span<const float> InSamples, const al::span<float> dst,
|
||||
float &CurrentGain, const float TargetGain, const float delta, const size_t fade_len,
|
||||
size_t Counter)
|
||||
{
|
||||
float gain{CurrentGain};
|
||||
const float step{(TargetGain-gain) * delta};
|
||||
const float step{(TargetGain-CurrentGain) * delta};
|
||||
|
||||
size_t pos{0};
|
||||
if(!(std::abs(step) > std::numeric_limits<float>::epsilon()))
|
||||
gain = TargetGain;
|
||||
else
|
||||
auto output = dst.begin();
|
||||
if(std::abs(step) > std::numeric_limits<float>::epsilon())
|
||||
{
|
||||
float step_count{0.0f};
|
||||
for(;pos != min_len;++pos)
|
||||
{
|
||||
dst[pos] += InSamples[pos] * (gain + step*step_count);
|
||||
step_count += 1.0f;
|
||||
}
|
||||
if(pos == Counter)
|
||||
gain = TargetGain;
|
||||
else
|
||||
gain += step*step_count;
|
||||
}
|
||||
CurrentGain = gain;
|
||||
auto input = InSamples.first(fade_len);
|
||||
InSamples = InSamples.subspan(fade_len);
|
||||
|
||||
if(!(std::abs(gain) > GainSilenceThreshold))
|
||||
const float gain{CurrentGain};
|
||||
float step_count{0.0f};
|
||||
output = std::transform(input.begin(), input.end(), output, output,
|
||||
[gain,step,&step_count](const float in, float out) noexcept -> float
|
||||
{
|
||||
out += in * (gain + step*step_count);
|
||||
step_count += 1.0f;
|
||||
return out;
|
||||
});
|
||||
|
||||
if(fade_len < Counter)
|
||||
{
|
||||
CurrentGain = gain + step*step_count;
|
||||
return;
|
||||
}
|
||||
}
|
||||
CurrentGain = TargetGain;
|
||||
|
||||
if(!(std::abs(TargetGain) > GainSilenceThreshold))
|
||||
return;
|
||||
for(;pos != InSamples.size();++pos)
|
||||
dst[pos] += InSamples[pos] * gain;
|
||||
|
||||
std::transform(InSamples.begin(), InSamples.end(), output, output,
|
||||
[TargetGain](const float in, const float out) noexcept -> float
|
||||
{ return out + in*TargetGain; });
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
template<>
|
||||
void Resample_<PointTag,CTag>(const InterpState*, const float *src, uint frac,
|
||||
void Resample_<PointTag,CTag>(const InterpState*, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{ DoResample<do_point>(src, frac, increment, dst); }
|
||||
{ DoResample<do_point>(src.subspan(MaxResamplerEdge), frac, increment, dst); }
|
||||
|
||||
template<>
|
||||
void Resample_<LerpTag,CTag>(const InterpState*, const float *src, uint frac, const uint increment,
|
||||
const al::span<float> dst)
|
||||
{ DoResample<do_lerp>(src, frac, increment, dst); }
|
||||
|
||||
template<>
|
||||
void Resample_<CubicTag,CTag>(const InterpState *state, const float *src, uint frac,
|
||||
void Resample_<LerpTag,CTag>(const InterpState*, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{ DoResample<CubicState,do_cubic>(std::get<CubicState>(*state), src-1, frac, increment, dst); }
|
||||
{ DoResample<do_lerp>(src.subspan(MaxResamplerEdge), frac, increment, dst); }
|
||||
|
||||
template<>
|
||||
void Resample_<BSincTag,CTag>(const InterpState *state, const float *src, uint frac,
|
||||
void Resample_<CubicTag,CTag>(const InterpState *state, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto istate = std::get<BsincState>(*state);
|
||||
DoResample<BsincState,do_bsinc>(istate, src-istate.l, frac, increment, dst);
|
||||
DoResample<CubicState,do_cubic>(std::get<CubicState>(*state), src.subspan(MaxResamplerEdge-1),
|
||||
frac, increment, dst);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<FastBSincTag,CTag>(const InterpState *state, const float *src, uint frac,
|
||||
void Resample_<FastBSincTag,CTag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto istate = std::get<BsincState>(*state);
|
||||
ASSUME(istate.l <= MaxResamplerEdge);
|
||||
DoResample<BsincState,do_fastbsinc>(istate, src.subspan(MaxResamplerEdge-istate.l), frac,
|
||||
increment, dst);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<BSincTag,CTag>(const InterpState *state, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto istate = std::get<BsincState>(*state);
|
||||
DoResample<BsincState,do_fastbsinc>(istate, src-istate.l, frac, increment, dst);
|
||||
ASSUME(istate.l <= MaxResamplerEdge);
|
||||
DoResample<BsincState,do_bsinc>(istate, src.subspan(MaxResamplerEdge-istate.l), frac,
|
||||
increment, dst);
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
void MixHrtf_<CTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const MixHrtfFilter *hrtfparams, const size_t BufferSize)
|
||||
{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); }
|
||||
void MixHrtf_<CTag>(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
|
||||
const uint IrSize, const MixHrtfFilter *hrtfparams, const size_t SamplesToDo)
|
||||
{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, SamplesToDo); }
|
||||
|
||||
template<>
|
||||
void MixHrtfBlend_<CTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize)
|
||||
void MixHrtfBlend_<CTag>(const al::span<const float> InSamples,const al::span<float2> AccumSamples,
|
||||
const uint IrSize, const HrtfFilter *oldparams, const MixHrtfFilter *newparams,
|
||||
const size_t SamplesToDo)
|
||||
{
|
||||
MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams,
|
||||
BufferSize);
|
||||
SamplesToDo);
|
||||
}
|
||||
|
||||
template<>
|
||||
void MixDirectHrtf_<CTag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
|
||||
const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, HrtfChannelState *ChanState, const size_t IrSize,
|
||||
const size_t BufferSize)
|
||||
const al::span<const FloatBufferLine> InSamples, const al::span<float2> AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, const al::span<HrtfChannelState> ChanState,
|
||||
const size_t IrSize, const size_t SamplesToDo)
|
||||
{
|
||||
MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState,
|
||||
IrSize, BufferSize);
|
||||
IrSize, SamplesToDo);
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
void Mix_<CTag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
|
||||
float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos)
|
||||
const al::span<float> CurrentGains, const al::span<const float> TargetGains,
|
||||
const size_t Counter, const size_t OutPos)
|
||||
{
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto min_len = std::min(Counter, InSamples.size());
|
||||
const auto fade_len = std::min(Counter, InSamples.size());
|
||||
|
||||
auto curgains = CurrentGains.begin();
|
||||
auto targetgains = TargetGains.cbegin();
|
||||
for(FloatBufferLine &output : OutBuffer)
|
||||
MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++,
|
||||
*TargetGains++, delta, min_len, Counter);
|
||||
MixLine(InSamples, al::span{output}.subspan(OutPos), *curgains++, *targetgains++, delta,
|
||||
fade_len, Counter);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Mix_<CTag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain,
|
||||
const float TargetGain, const size_t Counter)
|
||||
void Mix_<CTag>(const al::span<const float> InSamples, const al::span<float> OutBuffer,
|
||||
float &CurrentGain, const float TargetGain, const size_t Counter)
|
||||
{
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto min_len = std::min(Counter, InSamples.size());
|
||||
const auto fade_len = std::min(Counter, InSamples.size());
|
||||
|
||||
MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain,
|
||||
TargetGain, delta, min_len, Counter);
|
||||
MixLine(InSamples, OutBuffer, CurrentGain, TargetGain, delta, fade_len, Counter);
|
||||
}
|
||||
|
||||
@@ -14,10 +14,12 @@
|
||||
#include "core/bufferline.h"
|
||||
#include "core/cubic_defs.h"
|
||||
#include "core/mixer/hrtfdefs.h"
|
||||
#include "core/resampler_limits.h"
|
||||
#include "defs.h"
|
||||
#include "hrtfbase.h"
|
||||
#include "opthelpers.h"
|
||||
|
||||
struct CTag;
|
||||
struct NEONTag;
|
||||
struct LerpTag;
|
||||
struct CubicTag;
|
||||
@@ -63,60 +65,62 @@ inline float32x4_t set_f4(float l0, float l1, float l2, float l3)
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs,
|
||||
const float left, const float right)
|
||||
inline void ApplyCoeffs(const al::span<float2> Values, const size_t IrSize,
|
||||
const ConstHrirSpan Coeffs, const float left, const float right)
|
||||
{
|
||||
auto dup_samples = [left,right]
|
||||
ASSUME(IrSize >= MinIrLength);
|
||||
ASSUME(IrSize <= HrirLength);
|
||||
|
||||
auto dup_samples = [left,right]() -> float32x4_t
|
||||
{
|
||||
float32x2_t leftright2{vset_lane_f32(right, vmov_n_f32(left), 1)};
|
||||
return vcombine_f32(leftright2, leftright2);
|
||||
};
|
||||
const float32x4_t leftright4{dup_samples()};
|
||||
const auto leftright4 = dup_samples();
|
||||
|
||||
ASSUME(IrSize >= MinIrLength);
|
||||
/* Using a loop here instead of std::transform since some builds seem to
|
||||
* have an issue with accessing an array/span of float32x4_t.
|
||||
*/
|
||||
for(size_t c{0};c < IrSize;c += 2)
|
||||
{
|
||||
float32x4_t vals = vld1q_f32(&Values[c][0]);
|
||||
float32x4_t coefs = vld1q_f32(&Coeffs[c][0]);
|
||||
|
||||
vals = vmlaq_f32(vals, coefs, leftright4);
|
||||
|
||||
auto vals = vld1q_f32(&Values[c][0]);
|
||||
vals = vmlaq_f32(vals, vld1q_f32(&Coeffs[c][0]), leftright4);
|
||||
vst1q_f32(&Values[c][0], vals);
|
||||
}
|
||||
}
|
||||
|
||||
force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst,
|
||||
float &CurrentGain, const float TargetGain, const float delta, const size_t min_len,
|
||||
const size_t aligned_len, size_t Counter)
|
||||
force_inline void MixLine(const al::span<const float> InSamples, const al::span<float> dst,
|
||||
float &CurrentGain, const float TargetGain, const float delta, const size_t fade_len,
|
||||
const size_t realign_len, size_t Counter)
|
||||
{
|
||||
float gain{CurrentGain};
|
||||
const float step{(TargetGain-gain) * delta};
|
||||
const auto step = float{(TargetGain-CurrentGain) * delta};
|
||||
|
||||
size_t pos{0};
|
||||
if(!(std::abs(step) > std::numeric_limits<float>::epsilon()))
|
||||
gain = TargetGain;
|
||||
else
|
||||
auto pos = size_t{0};
|
||||
if(std::abs(step) > std::numeric_limits<float>::epsilon())
|
||||
{
|
||||
float step_count{0.0f};
|
||||
const auto gain = float{CurrentGain};
|
||||
auto step_count = float{0.0f};
|
||||
/* Mix with applying gain steps in aligned multiples of 4. */
|
||||
if(size_t todo{min_len >> 2})
|
||||
if(const size_t todo{fade_len >> 2})
|
||||
{
|
||||
const float32x4_t four4{vdupq_n_f32(4.0f)};
|
||||
const float32x4_t step4{vdupq_n_f32(step)};
|
||||
const float32x4_t gain4{vdupq_n_f32(gain)};
|
||||
float32x4_t step_count4{vdupq_n_f32(0.0f)};
|
||||
step_count4 = vsetq_lane_f32(1.0f, step_count4, 1);
|
||||
step_count4 = vsetq_lane_f32(2.0f, step_count4, 2);
|
||||
step_count4 = vsetq_lane_f32(3.0f, step_count4, 3);
|
||||
const auto four4 = vdupq_n_f32(4.0f);
|
||||
const auto step4 = vdupq_n_f32(step);
|
||||
const auto gain4 = vdupq_n_f32(gain);
|
||||
auto step_count4 = set_f4(0.0f, 1.0f, 2.0f, 3.0f);
|
||||
|
||||
const auto in4 = al::span{reinterpret_cast<const float32x4_t*>(InSamples.data()),
|
||||
InSamples.size()/4}.first(todo);
|
||||
const auto out4 = al::span{reinterpret_cast<float32x4_t*>(dst.data()), dst.size()/4};
|
||||
std::transform(in4.begin(), in4.end(), out4.begin(), out4.begin(),
|
||||
[gain4,step4,four4,&step_count4](const float32x4_t val4, float32x4_t dry4)
|
||||
{
|
||||
/* dry += val * (gain + step*step_count) */
|
||||
dry4 = vmlaq_f32(dry4, val4, vmlaq_f32(gain4, step4, step_count4));
|
||||
step_count4 = vaddq_f32(step_count4, four4);
|
||||
return dry4;
|
||||
});
|
||||
pos += in4.size()*4;
|
||||
|
||||
do {
|
||||
const float32x4_t val4 = vld1q_f32(&InSamples[pos]);
|
||||
float32x4_t dry4 = vld1q_f32(&dst[pos]);
|
||||
dry4 = vmlaq_f32(dry4, val4, vmlaq_f32(gain4, step4, step_count4));
|
||||
step_count4 = vaddq_f32(step_count4, four4);
|
||||
vst1q_f32(&dst[pos], dry4);
|
||||
pos += 4;
|
||||
} while(--todo);
|
||||
/* NOTE: step_count4 now represents the next four counts after the
|
||||
* last four mixed samples, so the lowest element represents the
|
||||
* next step count to apply.
|
||||
@@ -124,43 +128,70 @@ force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT
|
||||
step_count = vgetq_lane_f32(step_count4, 0);
|
||||
}
|
||||
/* Mix with applying left over gain steps that aren't aligned multiples of 4. */
|
||||
for(size_t leftover{min_len&3};leftover;++pos,--leftover)
|
||||
if(const size_t leftover{fade_len&3})
|
||||
{
|
||||
dst[pos] += InSamples[pos] * (gain + step*step_count);
|
||||
step_count += 1.0f;
|
||||
const auto in = InSamples.subspan(pos, leftover);
|
||||
const auto out = dst.subspan(pos);
|
||||
|
||||
std::transform(in.begin(), in.end(), out.begin(), out.begin(),
|
||||
[gain,step,&step_count](const float val, float dry) noexcept -> float
|
||||
{
|
||||
dry += val * (gain + step*step_count);
|
||||
step_count += 1.0f;
|
||||
return dry;
|
||||
});
|
||||
pos += leftover;
|
||||
}
|
||||
if(pos < Counter)
|
||||
{
|
||||
CurrentGain = gain + step*step_count;
|
||||
return;
|
||||
}
|
||||
if(pos == Counter)
|
||||
gain = TargetGain;
|
||||
else
|
||||
gain += step*step_count;
|
||||
|
||||
/* Mix until pos is aligned with 4 or the mix is done. */
|
||||
for(size_t leftover{aligned_len&3};leftover;++pos,--leftover)
|
||||
dst[pos] += InSamples[pos] * gain;
|
||||
}
|
||||
CurrentGain = gain;
|
||||
if(const size_t leftover{realign_len&3})
|
||||
{
|
||||
const auto in = InSamples.subspan(pos, leftover);
|
||||
const auto out = dst.subspan(pos);
|
||||
|
||||
if(!(std::abs(gain) > GainSilenceThreshold))
|
||||
return;
|
||||
if(size_t todo{(InSamples.size()-pos) >> 2})
|
||||
{
|
||||
const float32x4_t gain4 = vdupq_n_f32(gain);
|
||||
do {
|
||||
const float32x4_t val4 = vld1q_f32(&InSamples[pos]);
|
||||
float32x4_t dry4 = vld1q_f32(&dst[pos]);
|
||||
dry4 = vmlaq_f32(dry4, val4, gain4);
|
||||
vst1q_f32(&dst[pos], dry4);
|
||||
pos += 4;
|
||||
} while(--todo);
|
||||
std::transform(in.begin(), in.end(), out.begin(), out.begin(),
|
||||
[TargetGain](const float val, const float dry) noexcept -> float
|
||||
{ return dry + val*TargetGain; });
|
||||
pos += leftover;
|
||||
}
|
||||
}
|
||||
CurrentGain = TargetGain;
|
||||
|
||||
if(!(std::abs(TargetGain) > GainSilenceThreshold))
|
||||
return;
|
||||
if(const size_t todo{(InSamples.size()-pos) >> 2})
|
||||
{
|
||||
const auto in4 = al::span{reinterpret_cast<const float32x4_t*>(InSamples.data()),
|
||||
InSamples.size()/4}.last(todo);
|
||||
const auto out = dst.subspan(pos);
|
||||
const auto out4 = al::span{reinterpret_cast<float32x4_t*>(out.data()), out.size()/4};
|
||||
|
||||
const auto gain4 = vdupq_n_f32(TargetGain);
|
||||
std::transform(in4.begin(), in4.end(), out4.begin(), out4.begin(),
|
||||
[gain4](const float32x4_t val4, const float32x4_t dry4) -> float32x4_t
|
||||
{ return vmlaq_f32(dry4, val4, gain4); });
|
||||
pos += in4.size()*4;
|
||||
}
|
||||
if(const size_t leftover{(InSamples.size()-pos)&3})
|
||||
{
|
||||
const auto in = InSamples.last(leftover);
|
||||
const auto out = dst.subspan(pos);
|
||||
|
||||
std::transform(in.begin(), in.end(), out.begin(), out.begin(),
|
||||
[TargetGain](const float val, const float dry) noexcept -> float
|
||||
{ return dry + val*TargetGain; });
|
||||
}
|
||||
for(size_t leftover{(InSamples.size()-pos)&3};leftover;++pos,--leftover)
|
||||
dst[pos] += InSamples[pos] * gain;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
template<>
|
||||
void Resample_<LerpTag,NEONTag>(const InterpState*, const float *src, uint frac,
|
||||
void Resample_<LerpTag,NEONTag>(const InterpState*, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
@@ -169,18 +200,19 @@ void Resample_<LerpTag,NEONTag>(const InterpState*, const float *src, uint frac,
|
||||
const float32x4_t fracOne4 = vdupq_n_f32(1.0f/MixerFracOne);
|
||||
const uint32x4_t fracMask4 = vdupq_n_u32(MixerFracMask);
|
||||
|
||||
alignas(16) std::array<uint,4> pos_, frac_;
|
||||
InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_});
|
||||
alignas(16) std::array<uint,4> pos_{}, frac_{};
|
||||
InitPosArrays(MaxResamplerEdge, frac, increment, al::span{frac_}, al::span{pos_});
|
||||
uint32x4_t frac4 = vld1q_u32(frac_.data());
|
||||
uint32x4_t pos4 = vld1q_u32(pos_.data());
|
||||
|
||||
auto vecout = al::span<float32x4_t>{reinterpret_cast<float32x4_t*>(dst.data()), dst.size()/4};
|
||||
auto vecout = al::span{reinterpret_cast<float32x4_t*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]() -> float32x4_t
|
||||
{
|
||||
const uint pos0{vgetq_lane_u32(pos4, 0)};
|
||||
const uint pos1{vgetq_lane_u32(pos4, 1)};
|
||||
const uint pos2{vgetq_lane_u32(pos4, 2)};
|
||||
const uint pos3{vgetq_lane_u32(pos4, 3)};
|
||||
ASSUME(pos0 <= pos1); ASSUME(pos1 <= pos2); ASSUME(pos2 <= pos3);
|
||||
const float32x4_t val1{set_f4(src[pos0], src[pos1], src[pos2], src[pos3])};
|
||||
const float32x4_t val2{set_f4(src[pos0+1_uz], src[pos1+1_uz], src[pos2+1_uz], src[pos3+1_uz])};
|
||||
|
||||
@@ -197,24 +229,26 @@ void Resample_<LerpTag,NEONTag>(const InterpState*, const float *src, uint frac,
|
||||
|
||||
if(size_t todo{dst.size()&3})
|
||||
{
|
||||
src += vgetq_lane_u32(pos4, 0);
|
||||
auto pos = size_t{vgetq_lane_u32(pos4, 0)};
|
||||
frac = vgetq_lane_u32(frac4, 0);
|
||||
|
||||
std::generate(dst.end()-ptrdiff_t(todo), dst.end(), [&src,&frac,increment]
|
||||
const auto out = dst.last(todo);
|
||||
std::generate(out.begin(), out.end(), [&pos,&frac,src,increment]
|
||||
{
|
||||
const float out{lerpf(src[0], src[1], static_cast<float>(frac) * (1.0f/MixerFracOne))};
|
||||
const float output{lerpf(src[pos+0], src[pos+1],
|
||||
static_cast<float>(frac) * (1.0f/MixerFracOne))};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return out;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<CubicTag,NEONTag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
@@ -225,23 +259,23 @@ void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *src, uin
|
||||
const float32x4_t fracDiffOne4{vdupq_n_f32(1.0f/CubicPhaseDiffOne)};
|
||||
const uint32x4_t fracDiffMask4{vdupq_n_u32(CubicPhaseDiffMask)};
|
||||
|
||||
alignas(16) std::array<uint,4> pos_, frac_;
|
||||
InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_});
|
||||
alignas(16) std::array<uint,4> pos_{}, frac_{};
|
||||
InitPosArrays(MaxResamplerEdge-1, frac, increment, al::span{frac_}, al::span{pos_});
|
||||
uint32x4_t frac4{vld1q_u32(frac_.data())};
|
||||
uint32x4_t pos4{vld1q_u32(pos_.data())};
|
||||
|
||||
src -= 1;
|
||||
auto vecout = al::span<float32x4_t>{reinterpret_cast<float32x4_t*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]() -> float32x4_t
|
||||
auto vecout = al::span{reinterpret_cast<float32x4_t*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]
|
||||
{
|
||||
const uint pos0{vgetq_lane_u32(pos4, 0)};
|
||||
const uint pos1{vgetq_lane_u32(pos4, 1)};
|
||||
const uint pos2{vgetq_lane_u32(pos4, 2)};
|
||||
const uint pos3{vgetq_lane_u32(pos4, 3)};
|
||||
const float32x4_t val0{vld1q_f32(src+pos0)};
|
||||
const float32x4_t val1{vld1q_f32(src+pos1)};
|
||||
const float32x4_t val2{vld1q_f32(src+pos2)};
|
||||
const float32x4_t val3{vld1q_f32(src+pos3)};
|
||||
ASSUME(pos0 <= pos1); ASSUME(pos1 <= pos2); ASSUME(pos2 <= pos3);
|
||||
const float32x4_t val0{vld1q_f32(&src[pos0])};
|
||||
const float32x4_t val1{vld1q_f32(&src[pos1])};
|
||||
const float32x4_t val2{vld1q_f32(&src[pos2])};
|
||||
const float32x4_t val3{vld1q_f32(&src[pos3])};
|
||||
|
||||
const uint32x4_t pi4{vshrq_n_u32(frac4, CubicPhaseDiffBits)};
|
||||
const uint pi0{vgetq_lane_u32(pi4, 0)}; ASSUME(pi0 < CubicPhaseCount);
|
||||
@@ -276,10 +310,11 @@ void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *src, uin
|
||||
|
||||
if(const size_t todo{dst.size()&3})
|
||||
{
|
||||
src += vgetq_lane_u32(pos4, 0);
|
||||
auto pos = size_t{vgetq_lane_u32(pos4, 0)};
|
||||
frac = vgetq_lane_u32(frac4, 0);
|
||||
|
||||
std::generate(dst.end()-ptrdiff_t(todo), dst.end(), [&src,&frac,increment,filter]
|
||||
auto out = dst.last(todo);
|
||||
std::generate(out.begin(), out.end(), [&pos,&frac,src,increment,filter]
|
||||
{
|
||||
const uint pi{frac >> CubicPhaseDiffBits}; ASSUME(pi < CubicPhaseCount);
|
||||
const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)};
|
||||
@@ -287,13 +322,13 @@ void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *src, uin
|
||||
|
||||
const float32x4_t f4{vmlaq_f32(vld1q_f32(filter[pi].mCoeffs.data()), pf4,
|
||||
vld1q_f32(filter[pi].mDeltas.data()))};
|
||||
float32x4_t r4{vmulq_f32(f4, vld1q_f32(src))};
|
||||
float32x4_t r4{vmulq_f32(f4, vld1q_f32(&src[pos]))};
|
||||
|
||||
r4 = vaddq_f32(r4, vrev64q_f32(r4));
|
||||
const float output{vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0)};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
@@ -301,31 +336,34 @@ void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *src, uin
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<BSincTag,NEONTag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<BSincTag,NEONTag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto &bsinc = std::get<BsincState>(*state);
|
||||
const float *const filter{bsinc.filter};
|
||||
const float32x4_t sf4{vdupq_n_f32(bsinc.sf)};
|
||||
const size_t m{bsinc.m};
|
||||
const auto sf4 = vdupq_n_f32(bsinc.sf);
|
||||
const auto m = size_t{bsinc.m};
|
||||
ASSUME(m > 0);
|
||||
ASSUME(m <= MaxResamplerPadding);
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
src -= bsinc.l;
|
||||
std::generate(dst.begin(), dst.end(), [&src,&frac,increment,filter,sf4,m]() -> float
|
||||
const auto filter = bsinc.filter.first(4_uz*BSincPhaseCount*m);
|
||||
|
||||
ASSUME(bsinc.l <= MaxResamplerEdge);
|
||||
auto pos = size_t{MaxResamplerEdge-bsinc.l};
|
||||
std::generate(dst.begin(), dst.end(), [&pos,&frac,src,increment,sf4,m,filter]() -> float
|
||||
{
|
||||
// Calculate the phase index and factor.
|
||||
const uint pi{frac >> BSincPhaseDiffBits};
|
||||
const uint pi{frac >> BSincPhaseDiffBits}; ASSUME(pi < BSincPhaseCount);
|
||||
const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)};
|
||||
|
||||
// Apply the scale and phase interpolated filter.
|
||||
float32x4_t r4{vdupq_n_f32(0.0f)};
|
||||
{
|
||||
const float32x4_t pf4{vdupq_n_f32(pf)};
|
||||
const float *fil{filter + m*pi*2_uz};
|
||||
const float *phd{fil + m};
|
||||
const float *scd{fil + BSincPhaseCount*2_uz*m};
|
||||
const float *spd{scd + m};
|
||||
const auto fil = filter.subspan(2_uz*pi*m);
|
||||
const auto phd = fil.subspan(m);
|
||||
const auto scd = fil.subspan(2_uz*BSincPhaseCount*m);
|
||||
const auto spd = scd.subspan(m);
|
||||
size_t td{m >> 2};
|
||||
size_t j{0u};
|
||||
|
||||
@@ -335,7 +373,7 @@ void Resample_<BSincTag,NEONTag>(const InterpState *state, const float *src, uin
|
||||
vmlaq_f32(vld1q_f32(&fil[j]), sf4, vld1q_f32(&scd[j])),
|
||||
pf4, vmlaq_f32(vld1q_f32(&phd[j]), sf4, vld1q_f32(&spd[j])));
|
||||
/* r += f*src */
|
||||
r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j]));
|
||||
r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[pos+j]));
|
||||
j += 4;
|
||||
} while(--td);
|
||||
}
|
||||
@@ -343,35 +381,38 @@ void Resample_<BSincTag,NEONTag>(const InterpState *state, const float *src, uin
|
||||
const float output{vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0)};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const float *src,
|
||||
void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto &bsinc = std::get<BsincState>(*state);
|
||||
const float *const filter{bsinc.filter};
|
||||
const size_t m{bsinc.m};
|
||||
const auto m = size_t{bsinc.m};
|
||||
ASSUME(m > 0);
|
||||
ASSUME(m <= MaxResamplerPadding);
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
src -= bsinc.l;
|
||||
std::generate(dst.begin(), dst.end(), [&src,&frac,increment,filter,m]() -> float
|
||||
const auto filter = bsinc.filter.first(2_uz*BSincPhaseCount*m);
|
||||
|
||||
ASSUME(bsinc.l <= MaxResamplerEdge);
|
||||
auto pos = size_t{MaxResamplerEdge-bsinc.l};
|
||||
std::generate(dst.begin(), dst.end(), [&pos,&frac,src,increment,m,filter]() -> float
|
||||
{
|
||||
// Calculate the phase index and factor.
|
||||
const uint pi{frac >> BSincPhaseDiffBits};
|
||||
const uint pi{frac >> BSincPhaseDiffBits}; ASSUME(pi < BSincPhaseCount);
|
||||
const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)};
|
||||
|
||||
// Apply the phase interpolated filter.
|
||||
float32x4_t r4{vdupq_n_f32(0.0f)};
|
||||
{
|
||||
const float32x4_t pf4{vdupq_n_f32(pf)};
|
||||
const float *fil{filter + m*pi*2_uz};
|
||||
const float *phd{fil + m};
|
||||
const auto fil = filter.subspan(2_uz*pi*m);
|
||||
const auto phd = fil.subspan(m);
|
||||
size_t td{m >> 2};
|
||||
size_t j{0u};
|
||||
|
||||
@@ -379,7 +420,7 @@ void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const float *src,
|
||||
/* f = fil + pf*phd */
|
||||
const float32x4_t f4 = vmlaq_f32(vld1q_f32(&fil[j]), pf4, vld1q_f32(&phd[j]));
|
||||
/* r += f*src */
|
||||
r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j]));
|
||||
r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[pos+j]));
|
||||
j += 4;
|
||||
} while(--td);
|
||||
}
|
||||
@@ -387,7 +428,7 @@ void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const float *src,
|
||||
const float output{vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0)};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
@@ -395,50 +436,59 @@ void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const float *src,
|
||||
|
||||
|
||||
template<>
|
||||
void MixHrtf_<NEONTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const MixHrtfFilter *hrtfparams, const size_t BufferSize)
|
||||
{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); }
|
||||
void MixHrtf_<NEONTag>(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
|
||||
const uint IrSize, const MixHrtfFilter *hrtfparams, const size_t SamplesToDo)
|
||||
{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, SamplesToDo); }
|
||||
|
||||
template<>
|
||||
void MixHrtfBlend_<NEONTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize)
|
||||
void MixHrtfBlend_<NEONTag>(const al::span<const float> InSamples,
|
||||
const al::span<float2> AccumSamples, const uint IrSize, const HrtfFilter *oldparams,
|
||||
const MixHrtfFilter *newparams, const size_t SamplesToDo)
|
||||
{
|
||||
MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams,
|
||||
BufferSize);
|
||||
SamplesToDo);
|
||||
}
|
||||
|
||||
template<>
|
||||
void MixDirectHrtf_<NEONTag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
|
||||
const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, HrtfChannelState *ChanState, const size_t IrSize,
|
||||
const size_t BufferSize)
|
||||
const al::span<const FloatBufferLine> InSamples, const al::span<float2> AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, const al::span<HrtfChannelState> ChanState,
|
||||
const size_t IrSize, const size_t SamplesToDo)
|
||||
{
|
||||
MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState,
|
||||
IrSize, BufferSize);
|
||||
IrSize, SamplesToDo);
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
void Mix_<NEONTag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
|
||||
float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos)
|
||||
void Mix_<NEONTag>(const al::span<const float> InSamples,const al::span<FloatBufferLine> OutBuffer,
|
||||
const al::span<float> CurrentGains, const al::span<const float> TargetGains,
|
||||
const size_t Counter, const size_t OutPos)
|
||||
{
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto min_len = std::min(Counter, InSamples.size());
|
||||
const auto aligned_len = std::min((min_len+3_uz) & ~3_uz, InSamples.size()) - min_len;
|
||||
if((OutPos&3) != 0) UNLIKELY
|
||||
return Mix_<CTag>(InSamples, OutBuffer, CurrentGains, TargetGains, Counter, OutPos);
|
||||
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto fade_len = std::min(Counter, InSamples.size());
|
||||
const auto realign_len = std::min((fade_len+3_uz) & ~3_uz, InSamples.size()) - fade_len;
|
||||
|
||||
auto curgains = CurrentGains.begin();
|
||||
auto targetgains = TargetGains.cbegin();
|
||||
for(FloatBufferLine &output : OutBuffer)
|
||||
MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++,
|
||||
*TargetGains++, delta, min_len, aligned_len, Counter);
|
||||
MixLine(InSamples, al::span{output}.subspan(OutPos), *curgains++, *targetgains++, delta,
|
||||
fade_len, realign_len, Counter);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Mix_<NEONTag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain,
|
||||
const float TargetGain, const size_t Counter)
|
||||
void Mix_<NEONTag>(const al::span<const float> InSamples, const al::span<float> OutBuffer,
|
||||
float &CurrentGain, const float TargetGain, const size_t Counter)
|
||||
{
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto min_len = std::min(Counter, InSamples.size());
|
||||
const auto aligned_len = std::min((min_len+3_uz) & ~3_uz, InSamples.size()) - min_len;
|
||||
if((reinterpret_cast<uintptr_t>(OutBuffer.data())&15) != 0) UNLIKELY
|
||||
return Mix_<CTag>(InSamples, OutBuffer, CurrentGain, TargetGain, Counter);
|
||||
|
||||
MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, TargetGain, delta, min_len,
|
||||
aligned_len, Counter);
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto fade_len = std::min(Counter, InSamples.size());
|
||||
const auto realign_len = std::min((fade_len+3_uz) & ~3_uz, InSamples.size()) - fade_len;
|
||||
|
||||
MixLine(InSamples, OutBuffer, CurrentGain, TargetGain, delta, fade_len, realign_len, Counter);
|
||||
}
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
#include "core/bufferline.h"
|
||||
#include "core/cubic_defs.h"
|
||||
#include "core/mixer/hrtfdefs.h"
|
||||
#include "core/resampler_limits.h"
|
||||
#include "defs.h"
|
||||
#include "hrtfbase.h"
|
||||
#include "opthelpers.h"
|
||||
|
||||
struct CTag;
|
||||
struct SSETag;
|
||||
struct CubicTag;
|
||||
struct BSincTag;
|
||||
@@ -43,40 +45,45 @@ constexpr uint CubicPhaseDiffMask{CubicPhaseDiffOne - 1u};
|
||||
force_inline __m128 vmadd(const __m128 x, const __m128 y, const __m128 z) noexcept
|
||||
{ return _mm_add_ps(x, _mm_mul_ps(y, z)); }
|
||||
|
||||
inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs,
|
||||
const float left, const float right)
|
||||
inline void ApplyCoeffs(const al::span<float2> Values, const size_t IrSize,
|
||||
const ConstHrirSpan Coeffs, const float left, const float right)
|
||||
{
|
||||
const __m128 lrlr{_mm_setr_ps(left, right, left, right)};
|
||||
|
||||
ASSUME(IrSize >= MinIrLength);
|
||||
ASSUME(IrSize <= HrirLength);
|
||||
const auto lrlr = _mm_setr_ps(left, right, left, right);
|
||||
/* Round up the IR size to a multiple of 2 for SIMD (2 IRs for 2 channels
|
||||
* is 4 floats), to avoid cutting the last sample for odd IR counts. The
|
||||
* underlying HRIR is a fixed-size multiple of 2, any extra samples are
|
||||
* either 0 (silence) or more IR samples that get applied for "free".
|
||||
*/
|
||||
const auto count4 = size_t{(IrSize+1) >> 1};
|
||||
|
||||
/* This isn't technically correct to test alignment, but it's true for
|
||||
* systems that support SSE, which is the only one that needs to know the
|
||||
* alignment of Values (which alternates between 8- and 16-byte aligned).
|
||||
*/
|
||||
if(!(reinterpret_cast<uintptr_t>(Values)&15))
|
||||
if(!(reinterpret_cast<uintptr_t>(Values.data())&15))
|
||||
{
|
||||
for(size_t i{0};i < IrSize;i += 2)
|
||||
{
|
||||
const __m128 coeffs{_mm_load_ps(Coeffs[i].data())};
|
||||
__m128 vals{_mm_load_ps(Values[i].data())};
|
||||
vals = vmadd(vals, lrlr, coeffs);
|
||||
_mm_store_ps(Values[i].data(), vals);
|
||||
}
|
||||
const auto vals4 = al::span{reinterpret_cast<__m128*>(Values[0].data()), count4};
|
||||
const auto coeffs4 = al::span{reinterpret_cast<const __m128*>(Coeffs[0].data()), count4};
|
||||
|
||||
std::transform(vals4.cbegin(), vals4.cend(), coeffs4.cbegin(), vals4.begin(),
|
||||
[lrlr](const __m128 &val, const __m128 &coeff) -> __m128
|
||||
{ return vmadd(val, coeff, lrlr); });
|
||||
}
|
||||
else
|
||||
{
|
||||
__m128 imp0, imp1;
|
||||
__m128 coeffs{_mm_load_ps(Coeffs[0].data())};
|
||||
__m128 vals{_mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(Values[0].data()))};
|
||||
imp0 = _mm_mul_ps(lrlr, coeffs);
|
||||
auto coeffs = _mm_load_ps(Coeffs[0].data());
|
||||
auto vals = _mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(Values[0].data()));
|
||||
auto imp0 = _mm_mul_ps(lrlr, coeffs);
|
||||
vals = _mm_add_ps(imp0, vals);
|
||||
_mm_storel_pi(reinterpret_cast<__m64*>(Values[0].data()), vals);
|
||||
size_t td{((IrSize+1)>>1) - 1};
|
||||
size_t td{count4 - 1};
|
||||
size_t i{1};
|
||||
do {
|
||||
coeffs = _mm_load_ps(Coeffs[i+1].data());
|
||||
vals = _mm_load_ps(Values[i].data());
|
||||
imp1 = _mm_mul_ps(lrlr, coeffs);
|
||||
const auto imp1 = _mm_mul_ps(lrlr, coeffs);
|
||||
imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2));
|
||||
vals = _mm_add_ps(imp0, vals);
|
||||
_mm_store_ps(Values[i].data(), vals);
|
||||
@@ -90,37 +97,38 @@ inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const Cons
|
||||
}
|
||||
}
|
||||
|
||||
force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst,
|
||||
float &CurrentGain, const float TargetGain, const float delta, const size_t min_len,
|
||||
const size_t aligned_len, size_t Counter)
|
||||
force_inline void MixLine(const al::span<const float> InSamples, const al::span<float> dst,
|
||||
float &CurrentGain, const float TargetGain, const float delta, const size_t fade_len,
|
||||
const size_t realign_len, size_t Counter)
|
||||
{
|
||||
float gain{CurrentGain};
|
||||
const float step{(TargetGain-gain) * delta};
|
||||
const auto step = float{(TargetGain-CurrentGain) * delta};
|
||||
|
||||
size_t pos{0};
|
||||
if(!(std::abs(step) > std::numeric_limits<float>::epsilon()))
|
||||
gain = TargetGain;
|
||||
else
|
||||
if(std::abs(step) > std::numeric_limits<float>::epsilon())
|
||||
{
|
||||
float step_count{0.0f};
|
||||
const auto gain = CurrentGain;
|
||||
auto step_count = 0.0f;
|
||||
/* Mix with applying gain steps in aligned multiples of 4. */
|
||||
if(size_t todo{min_len >> 2})
|
||||
if(const size_t todo{fade_len >> 2})
|
||||
{
|
||||
const __m128 four4{_mm_set1_ps(4.0f)};
|
||||
const __m128 step4{_mm_set1_ps(step)};
|
||||
const __m128 gain4{_mm_set1_ps(gain)};
|
||||
__m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)};
|
||||
do {
|
||||
const __m128 val4{_mm_load_ps(&InSamples[pos])};
|
||||
__m128 dry4{_mm_load_ps(&dst[pos])};
|
||||
const auto four4 = _mm_set1_ps(4.0f);
|
||||
const auto step4 = _mm_set1_ps(step);
|
||||
const auto gain4 = _mm_set1_ps(gain);
|
||||
auto step_count4 = _mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f);
|
||||
|
||||
/* dry += val * (gain + step*step_count) */
|
||||
dry4 = vmadd(dry4, val4, vmadd(gain4, step4, step_count4));
|
||||
const auto in4 = al::span{reinterpret_cast<const __m128*>(InSamples.data()),
|
||||
InSamples.size()/4}.first(todo);
|
||||
const auto out4 = al::span{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::transform(in4.begin(), in4.end(), out4.begin(), out4.begin(),
|
||||
[gain4,step4,four4,&step_count4](const __m128 val4, __m128 dry4) -> __m128
|
||||
{
|
||||
/* dry += val * (gain + step*step_count) */
|
||||
dry4 = vmadd(dry4, val4, vmadd(gain4, step4, step_count4));
|
||||
step_count4 = _mm_add_ps(step_count4, four4);
|
||||
return dry4;
|
||||
});
|
||||
pos += in4.size()*4;
|
||||
|
||||
_mm_store_ps(&dst[pos], dry4);
|
||||
step_count4 = _mm_add_ps(step_count4, four4);
|
||||
pos += 4;
|
||||
} while(--todo);
|
||||
/* NOTE: step_count4 now represents the next four counts after the
|
||||
* last four mixed samples, so the lowest element represents the
|
||||
* next step count to apply.
|
||||
@@ -128,51 +136,78 @@ force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT
|
||||
step_count = _mm_cvtss_f32(step_count4);
|
||||
}
|
||||
/* Mix with applying left over gain steps that aren't aligned multiples of 4. */
|
||||
for(size_t leftover{min_len&3};leftover;++pos,--leftover)
|
||||
if(const size_t leftover{fade_len&3})
|
||||
{
|
||||
dst[pos] += InSamples[pos] * (gain + step*step_count);
|
||||
step_count += 1.0f;
|
||||
const auto in = InSamples.subspan(pos, leftover);
|
||||
const auto out = dst.subspan(pos);
|
||||
|
||||
std::transform(in.begin(), in.end(), out.begin(), out.begin(),
|
||||
[gain,step,&step_count](const float val, float dry) noexcept -> float
|
||||
{
|
||||
dry += val * (gain + step*step_count);
|
||||
step_count += 1.0f;
|
||||
return dry;
|
||||
});
|
||||
pos += leftover;
|
||||
}
|
||||
if(pos < Counter)
|
||||
{
|
||||
CurrentGain = gain + step*step_count;
|
||||
return;
|
||||
}
|
||||
if(pos == Counter)
|
||||
gain = TargetGain;
|
||||
else
|
||||
gain += step*step_count;
|
||||
|
||||
/* Mix until pos is aligned with 4 or the mix is done. */
|
||||
for(size_t leftover{aligned_len&3};leftover;++pos,--leftover)
|
||||
dst[pos] += InSamples[pos] * gain;
|
||||
}
|
||||
CurrentGain = gain;
|
||||
if(const size_t leftover{realign_len&3})
|
||||
{
|
||||
const auto in = InSamples.subspan(pos, leftover);
|
||||
const auto out = dst.subspan(pos);
|
||||
|
||||
if(!(std::abs(gain) > GainSilenceThreshold))
|
||||
std::transform(in.begin(), in.end(), out.begin(), out.begin(),
|
||||
[TargetGain](const float val, const float dry) noexcept -> float
|
||||
{ return dry + val*TargetGain; });
|
||||
pos += leftover;
|
||||
}
|
||||
}
|
||||
CurrentGain = TargetGain;
|
||||
|
||||
if(!(std::abs(TargetGain) > GainSilenceThreshold))
|
||||
return;
|
||||
if(size_t todo{(InSamples.size()-pos) >> 2})
|
||||
{
|
||||
const __m128 gain4{_mm_set1_ps(gain)};
|
||||
do {
|
||||
const __m128 val4{_mm_load_ps(&InSamples[pos])};
|
||||
__m128 dry4{_mm_load_ps(&dst[pos])};
|
||||
dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
|
||||
_mm_store_ps(&dst[pos], dry4);
|
||||
pos += 4;
|
||||
} while(--todo);
|
||||
const auto in4 = al::span{reinterpret_cast<const __m128*>(InSamples.data()),
|
||||
InSamples.size()/4}.last(todo);
|
||||
const auto out = dst.subspan(pos);
|
||||
const auto out4 = al::span{reinterpret_cast<__m128*>(out.data()), out.size()/4};
|
||||
|
||||
const auto gain4 = _mm_set1_ps(TargetGain);
|
||||
std::transform(in4.begin(), in4.end(), out4.begin(), out4.begin(),
|
||||
[gain4](const __m128 val4, const __m128 dry4) -> __m128
|
||||
{ return vmadd(dry4, val4, gain4); });
|
||||
pos += in4.size()*4;
|
||||
}
|
||||
if(const size_t leftover{(InSamples.size()-pos)&3})
|
||||
{
|
||||
const auto in = InSamples.last(leftover);
|
||||
const auto out = dst.subspan(pos);
|
||||
|
||||
std::transform(in.begin(), in.end(), out.begin(), out.begin(),
|
||||
[TargetGain](const float val, const float dry) noexcept -> float
|
||||
{ return dry + val*TargetGain; });
|
||||
}
|
||||
for(size_t leftover{(InSamples.size()-pos)&3};leftover;++pos,--leftover)
|
||||
dst[pos] += InSamples[pos] * gain;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
template<>
|
||||
void Resample_<CubicTag,SSETag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<CubicTag,SSETag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
const auto filter = std::get<CubicState>(*state).filter;
|
||||
|
||||
src -= 1;
|
||||
std::generate(dst.begin(), dst.end(), [&src,&frac,increment,filter]() -> float
|
||||
size_t pos{MaxResamplerEdge-1};
|
||||
std::generate(dst.begin(), dst.end(), [&pos,&frac,src,increment,filter]() -> float
|
||||
{
|
||||
const uint pi{frac >> CubicPhaseDiffBits}; ASSUME(pi < CubicPhaseCount);
|
||||
const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)};
|
||||
@@ -184,47 +219,50 @@ void Resample_<CubicTag,SSETag>(const InterpState *state, const float *src, uint
|
||||
const __m128 f4 = vmadd(_mm_load_ps(filter[pi].mCoeffs.data()), pf4,
|
||||
_mm_load_ps(filter[pi].mDeltas.data()));
|
||||
/* r = f*src */
|
||||
__m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(src))};
|
||||
__m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(&src[pos]))};
|
||||
|
||||
r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
|
||||
r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
|
||||
const float output{_mm_cvtss_f32(r4)};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<BSincTag,SSETag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<BSincTag,SSETag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto &bsinc = std::get<BsincState>(*state);
|
||||
const float *const filter{bsinc.filter};
|
||||
const __m128 sf4{_mm_set1_ps(bsinc.sf)};
|
||||
const size_t m{bsinc.m};
|
||||
const auto sf4 = _mm_set1_ps(bsinc.sf);
|
||||
const auto m = size_t{bsinc.m};
|
||||
ASSUME(m > 0);
|
||||
ASSUME(m <= MaxResamplerPadding);
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
src -= bsinc.l;
|
||||
std::generate(dst.begin(), dst.end(), [&src,&frac,increment,filter,sf4,m]() -> float
|
||||
const auto filter = bsinc.filter.first(4_uz*BSincPhaseCount*m);
|
||||
|
||||
ASSUME(bsinc.l <= MaxResamplerEdge);
|
||||
auto pos = size_t{MaxResamplerEdge-bsinc.l};
|
||||
std::generate(dst.begin(), dst.end(), [&pos,&frac,src,increment,sf4,m,filter]() -> float
|
||||
{
|
||||
// Calculate the phase index and factor.
|
||||
const uint pi{frac >> BSincPhaseDiffBits};
|
||||
const size_t pi{frac >> BSincPhaseDiffBits}; ASSUME(pi < BSincPhaseCount);
|
||||
const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)};
|
||||
|
||||
// Apply the scale and phase interpolated filter.
|
||||
__m128 r4{_mm_setzero_ps()};
|
||||
auto r4 = _mm_setzero_ps();
|
||||
{
|
||||
const __m128 pf4{_mm_set1_ps(pf)};
|
||||
const float *fil{filter + m*pi*2_uz};
|
||||
const float *phd{fil + m};
|
||||
const float *scd{fil + BSincPhaseCount*2_uz*m};
|
||||
const float *spd{scd + m};
|
||||
size_t td{m >> 2};
|
||||
size_t j{0u};
|
||||
const auto pf4 = _mm_set1_ps(pf);
|
||||
const auto fil = filter.subspan(2_uz*pi*m);
|
||||
const auto phd = fil.subspan(m);
|
||||
const auto scd = fil.subspan(2_uz*BSincPhaseCount*m);
|
||||
const auto spd = scd.subspan(m);
|
||||
auto td = size_t{m >> 2};
|
||||
auto j = size_t{0};
|
||||
|
||||
do {
|
||||
/* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
|
||||
@@ -232,61 +270,64 @@ void Resample_<BSincTag,SSETag>(const InterpState *state, const float *src, uint
|
||||
vmadd(_mm_load_ps(&fil[j]), sf4, _mm_load_ps(&scd[j])),
|
||||
pf4, vmadd(_mm_load_ps(&phd[j]), sf4, _mm_load_ps(&spd[j])));
|
||||
/* r += f*src */
|
||||
r4 = vmadd(r4, f4, _mm_loadu_ps(&src[j]));
|
||||
r4 = vmadd(r4, f4, _mm_loadu_ps(&src[pos+j]));
|
||||
j += 4;
|
||||
} while(--td);
|
||||
}
|
||||
r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
|
||||
r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
|
||||
const float output{_mm_cvtss_f32(r4)};
|
||||
const auto output = _mm_cvtss_f32(r4);
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<FastBSincTag,SSETag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<FastBSincTag,SSETag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
const auto &bsinc = std::get<BsincState>(*state);
|
||||
const float *const filter{bsinc.filter};
|
||||
const size_t m{bsinc.m};
|
||||
const auto m = size_t{bsinc.m};
|
||||
ASSUME(m > 0);
|
||||
ASSUME(m <= MaxResamplerPadding);
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
src -= bsinc.l;
|
||||
std::generate(dst.begin(), dst.end(), [&src,&frac,increment,filter,m]() -> float
|
||||
const auto filter = bsinc.filter.first(2_uz*m*BSincPhaseCount);
|
||||
|
||||
ASSUME(bsinc.l <= MaxResamplerEdge);
|
||||
size_t pos{MaxResamplerEdge-bsinc.l};
|
||||
std::generate(dst.begin(), dst.end(), [&pos,&frac,src,increment,filter,m]() -> float
|
||||
{
|
||||
// Calculate the phase index and factor.
|
||||
const uint pi{frac >> BSincPhaseDiffBits};
|
||||
const size_t pi{frac >> BSincPhaseDiffBits}; ASSUME(pi < BSincPhaseCount);
|
||||
const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)};
|
||||
|
||||
// Apply the phase interpolated filter.
|
||||
__m128 r4{_mm_setzero_ps()};
|
||||
auto r4 = _mm_setzero_ps();
|
||||
{
|
||||
const __m128 pf4{_mm_set1_ps(pf)};
|
||||
const float *fil{filter + m*pi*2_uz};
|
||||
const float *phd{fil + m};
|
||||
size_t td{m >> 2};
|
||||
size_t j{0u};
|
||||
const auto pf4 = _mm_set1_ps(pf);
|
||||
const auto fil = filter.subspan(2_uz*m*pi);
|
||||
const auto phd = fil.subspan(m);
|
||||
auto td = size_t{m >> 2};
|
||||
auto j = size_t{0};
|
||||
|
||||
do {
|
||||
/* f = fil + pf*phd */
|
||||
const __m128 f4 = vmadd(_mm_load_ps(&fil[j]), pf4, _mm_load_ps(&phd[j]));
|
||||
const auto f4 = vmadd(_mm_load_ps(&fil[j]), pf4, _mm_load_ps(&phd[j]));
|
||||
/* r += f*src */
|
||||
r4 = vmadd(r4, f4, _mm_loadu_ps(&src[j]));
|
||||
r4 = vmadd(r4, f4, _mm_loadu_ps(&src[pos+j]));
|
||||
j += 4;
|
||||
} while(--td);
|
||||
}
|
||||
r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
|
||||
r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
|
||||
const float output{_mm_cvtss_f32(r4)};
|
||||
const auto output = _mm_cvtss_f32(r4);
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
@@ -294,50 +335,59 @@ void Resample_<FastBSincTag,SSETag>(const InterpState *state, const float *src,
|
||||
|
||||
|
||||
template<>
|
||||
void MixHrtf_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const MixHrtfFilter *hrtfparams, const size_t BufferSize)
|
||||
{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); }
|
||||
void MixHrtf_<SSETag>(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
|
||||
const uint IrSize, const MixHrtfFilter *hrtfparams, const size_t SamplesToDo)
|
||||
{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, SamplesToDo); }
|
||||
|
||||
template<>
|
||||
void MixHrtfBlend_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
||||
const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize)
|
||||
void MixHrtfBlend_<SSETag>(const al::span<const float> InSamples,
|
||||
const al::span<float2> AccumSamples, const uint IrSize, const HrtfFilter *oldparams,
|
||||
const MixHrtfFilter *newparams, const size_t SamplesToDo)
|
||||
{
|
||||
MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams,
|
||||
BufferSize);
|
||||
SamplesToDo);
|
||||
}
|
||||
|
||||
template<>
|
||||
void MixDirectHrtf_<SSETag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
|
||||
const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, HrtfChannelState *ChanState, const size_t IrSize,
|
||||
const size_t BufferSize)
|
||||
const al::span<const FloatBufferLine> InSamples, const al::span<float2> AccumSamples,
|
||||
const al::span<float,BufferLineSize> TempBuf, const al::span<HrtfChannelState> ChanState,
|
||||
const size_t IrSize, const size_t SamplesToDo)
|
||||
{
|
||||
MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState,
|
||||
IrSize, BufferSize);
|
||||
IrSize, SamplesToDo);
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
|
||||
float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos)
|
||||
const al::span<float> CurrentGains, const al::span<const float> TargetGains,
|
||||
const size_t Counter, const size_t OutPos)
|
||||
{
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto min_len = std::min(Counter, InSamples.size());
|
||||
const auto aligned_len = std::min((min_len+3_uz) & ~3_uz, InSamples.size()) - min_len;
|
||||
if((OutPos&3) != 0) UNLIKELY
|
||||
return Mix_<CTag>(InSamples, OutBuffer, CurrentGains, TargetGains, Counter, OutPos);
|
||||
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto fade_len = std::min(Counter, InSamples.size());
|
||||
const auto realign_len = std::min((fade_len+3_uz) & ~3_uz, InSamples.size()) - fade_len;
|
||||
|
||||
auto curgains = CurrentGains.begin();
|
||||
auto targetgains = TargetGains.cbegin();
|
||||
for(FloatBufferLine &output : OutBuffer)
|
||||
MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++,
|
||||
*TargetGains++, delta, min_len, aligned_len, Counter);
|
||||
MixLine(InSamples, al::span{output}.subspan(OutPos), *curgains++, *targetgains++, delta,
|
||||
fade_len, realign_len, Counter);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Mix_<SSETag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain,
|
||||
const float TargetGain, const size_t Counter)
|
||||
void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<float> OutBuffer,
|
||||
float &CurrentGain, const float TargetGain, const size_t Counter)
|
||||
{
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto min_len = std::min(Counter, InSamples.size());
|
||||
const auto aligned_len = std::min((min_len+3_uz) & ~3_uz, InSamples.size()) - min_len;
|
||||
if((reinterpret_cast<uintptr_t>(OutBuffer.data())&15) != 0) UNLIKELY
|
||||
return Mix_<CTag>(InSamples, OutBuffer, CurrentGain, TargetGain, Counter);
|
||||
|
||||
MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, TargetGain, delta, min_len,
|
||||
aligned_len, Counter);
|
||||
const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
|
||||
const auto fade_len = std::min(Counter, InSamples.size());
|
||||
const auto realign_len = std::min((fade_len+3_uz) & ~3_uz, InSamples.size()) - fade_len;
|
||||
|
||||
MixLine(InSamples, OutBuffer, CurrentGain, TargetGain, delta, fade_len, realign_len, Counter);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "alnumeric.h"
|
||||
#include "alspan.h"
|
||||
#include "core/cubic_defs.h"
|
||||
#include "core/resampler_limits.h"
|
||||
#include "defs.h"
|
||||
#include "opthelpers.h"
|
||||
|
||||
@@ -57,7 +58,7 @@ force_inline __m128 vmadd(const __m128 x, const __m128 y, const __m128 z) noexce
|
||||
} // namespace
|
||||
|
||||
template<>
|
||||
void Resample_<LerpTag,SSE2Tag>(const InterpState*, const float *src, uint frac,
|
||||
void Resample_<LerpTag,SSE2Tag>(const InterpState*, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
@@ -66,20 +67,21 @@ void Resample_<LerpTag,SSE2Tag>(const InterpState*, const float *src, uint frac,
|
||||
const __m128 fracOne4{_mm_set1_ps(1.0f/MixerFracOne)};
|
||||
const __m128i fracMask4{_mm_set1_epi32(MixerFracMask)};
|
||||
|
||||
alignas(16) std::array<uint,4> pos_, frac_;
|
||||
InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_});
|
||||
std::array<uint,4> pos_{}, frac_{};
|
||||
InitPosArrays(MaxResamplerEdge, frac, increment, al::span{frac_}, al::span{pos_});
|
||||
__m128i frac4{_mm_setr_epi32(static_cast<int>(frac_[0]), static_cast<int>(frac_[1]),
|
||||
static_cast<int>(frac_[2]), static_cast<int>(frac_[3]))};
|
||||
__m128i pos4{_mm_setr_epi32(static_cast<int>(pos_[0]), static_cast<int>(pos_[1]),
|
||||
static_cast<int>(pos_[2]), static_cast<int>(pos_[3]))};
|
||||
|
||||
auto vecout = al::span<__m128>{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
auto vecout = al::span{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]() -> __m128
|
||||
{
|
||||
const auto pos0 = static_cast<uint>(_mm_cvtsi128_si32(pos4));
|
||||
const auto pos1 = static_cast<uint>(_mm_cvtsi128_si32(_mm_srli_si128(pos4, 4)));
|
||||
const auto pos2 = static_cast<uint>(_mm_cvtsi128_si32(_mm_srli_si128(pos4, 8)));
|
||||
const auto pos3 = static_cast<uint>(_mm_cvtsi128_si32(_mm_srli_si128(pos4, 12)));
|
||||
ASSUME(pos0 <= pos1); ASSUME(pos1 <= pos2); ASSUME(pos2 <= pos3);
|
||||
const __m128 val1{_mm_setr_ps(src[pos0], src[pos1], src[pos2], src[pos3])};
|
||||
const __m128 val2{_mm_setr_ps(src[pos0+1_uz], src[pos1+1_uz], src[pos2+1_uz], src[pos3+1_uz])};
|
||||
|
||||
@@ -96,24 +98,26 @@ void Resample_<LerpTag,SSE2Tag>(const InterpState*, const float *src, uint frac,
|
||||
|
||||
if(size_t todo{dst.size()&3})
|
||||
{
|
||||
src += static_cast<uint>(_mm_cvtsi128_si32(pos4));
|
||||
auto pos = size_t{static_cast<uint>(_mm_cvtsi128_si32(pos4))};
|
||||
frac = static_cast<uint>(_mm_cvtsi128_si32(frac4));
|
||||
|
||||
std::generate(dst.end()-ptrdiff_t(todo), dst.end(), [&src,&frac,increment]()
|
||||
const auto out = dst.last(todo);
|
||||
std::generate(out.begin(), out.end(), [&pos,&frac,src,increment]()
|
||||
{
|
||||
const float out{lerpf(src[0], src[1], static_cast<float>(frac) * (1.0f/MixerFracOne))};
|
||||
const float smp{lerpf(src[pos+0], src[pos+1],
|
||||
static_cast<float>(frac) * (1.0f/MixerFracOne))};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return out;
|
||||
return smp;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<CubicTag,SSE2Tag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<CubicTag,SSE2Tag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
@@ -124,25 +128,25 @@ void Resample_<CubicTag,SSE2Tag>(const InterpState *state, const float *src, uin
|
||||
const __m128 fracDiffOne4{_mm_set1_ps(1.0f/CubicPhaseDiffOne)};
|
||||
const __m128i fracDiffMask4{_mm_set1_epi32(CubicPhaseDiffMask)};
|
||||
|
||||
alignas(16) std::array<uint,4> pos_, frac_;
|
||||
InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_});
|
||||
std::array<uint,4> pos_{}, frac_{};
|
||||
InitPosArrays(MaxResamplerEdge-1, frac, increment, al::span{frac_}, al::span{pos_});
|
||||
__m128i frac4{_mm_setr_epi32(static_cast<int>(frac_[0]), static_cast<int>(frac_[1]),
|
||||
static_cast<int>(frac_[2]), static_cast<int>(frac_[3]))};
|
||||
__m128i pos4{_mm_setr_epi32(static_cast<int>(pos_[0]), static_cast<int>(pos_[1]),
|
||||
static_cast<int>(pos_[2]), static_cast<int>(pos_[3]))};
|
||||
|
||||
src -= 1;
|
||||
auto vecout = al::span<__m128>{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]() -> __m128
|
||||
auto vecout = al::span{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]
|
||||
{
|
||||
const auto pos0 = static_cast<uint>(_mm_cvtsi128_si32(pos4));
|
||||
const auto pos1 = static_cast<uint>(_mm_cvtsi128_si32(_mm_srli_si128(pos4, 4)));
|
||||
const auto pos2 = static_cast<uint>(_mm_cvtsi128_si32(_mm_srli_si128(pos4, 8)));
|
||||
const auto pos3 = static_cast<uint>(_mm_cvtsi128_si32(_mm_srli_si128(pos4, 12)));
|
||||
const __m128 val0{_mm_loadu_ps(src+pos0)};
|
||||
const __m128 val1{_mm_loadu_ps(src+pos1)};
|
||||
const __m128 val2{_mm_loadu_ps(src+pos2)};
|
||||
const __m128 val3{_mm_loadu_ps(src+pos3)};
|
||||
ASSUME(pos0 <= pos1); ASSUME(pos1 <= pos2); ASSUME(pos2 <= pos3);
|
||||
const __m128 val0{_mm_loadu_ps(&src[pos0])};
|
||||
const __m128 val1{_mm_loadu_ps(&src[pos1])};
|
||||
const __m128 val2{_mm_loadu_ps(&src[pos2])};
|
||||
const __m128 val3{_mm_loadu_ps(&src[pos3])};
|
||||
|
||||
const __m128i pi4{_mm_srli_epi32(frac4, CubicPhaseDiffBits)};
|
||||
const auto pi0 = static_cast<uint>(_mm_cvtsi128_si32(pi4));
|
||||
@@ -183,10 +187,11 @@ void Resample_<CubicTag,SSE2Tag>(const InterpState *state, const float *src, uin
|
||||
|
||||
if(const size_t todo{dst.size()&3})
|
||||
{
|
||||
src += static_cast<uint>(_mm_cvtsi128_si32(pos4));
|
||||
auto pos = size_t{static_cast<uint>(_mm_cvtsi128_si32(pos4))};
|
||||
frac = static_cast<uint>(_mm_cvtsi128_si32(frac4));
|
||||
|
||||
std::generate(dst.end()-ptrdiff_t(todo), dst.end(), [&src,&frac,increment,filter]
|
||||
auto out = dst.last(todo);
|
||||
std::generate(out.begin(), out.end(), [&pos,&frac,src,increment,filter]
|
||||
{
|
||||
const uint pi{frac >> CubicPhaseDiffBits}; ASSUME(pi < CubicPhaseCount);
|
||||
const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)};
|
||||
@@ -194,14 +199,14 @@ void Resample_<CubicTag,SSE2Tag>(const InterpState *state, const float *src, uin
|
||||
|
||||
const __m128 f4 = vmadd(_mm_load_ps(filter[pi].mCoeffs.data()), pf4,
|
||||
_mm_load_ps(filter[pi].mDeltas.data()));
|
||||
__m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(src))};
|
||||
__m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(&src[pos]))};
|
||||
|
||||
r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
|
||||
r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
|
||||
const float output{_mm_cvtss_f32(r4)};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "alnumeric.h"
|
||||
#include "alspan.h"
|
||||
#include "core/cubic_defs.h"
|
||||
#include "core/resampler_limits.h"
|
||||
#include "defs.h"
|
||||
#include "opthelpers.h"
|
||||
|
||||
@@ -58,7 +59,7 @@ force_inline __m128 vmadd(const __m128 x, const __m128 y, const __m128 z) noexce
|
||||
} // namespace
|
||||
|
||||
template<>
|
||||
void Resample_<LerpTag,SSE4Tag>(const InterpState*, const float *src, uint frac,
|
||||
void Resample_<LerpTag,SSE4Tag>(const InterpState*, const al::span<const float> src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
@@ -67,20 +68,21 @@ void Resample_<LerpTag,SSE4Tag>(const InterpState*, const float *src, uint frac,
|
||||
const __m128 fracOne4{_mm_set1_ps(1.0f/MixerFracOne)};
|
||||
const __m128i fracMask4{_mm_set1_epi32(MixerFracMask)};
|
||||
|
||||
alignas(16) std::array<uint,4> pos_, frac_;
|
||||
InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_});
|
||||
std::array<uint,4> pos_{}, frac_{};
|
||||
InitPosArrays(MaxResamplerEdge, frac, increment, al::span{frac_}, al::span{pos_});
|
||||
__m128i frac4{_mm_setr_epi32(static_cast<int>(frac_[0]), static_cast<int>(frac_[1]),
|
||||
static_cast<int>(frac_[2]), static_cast<int>(frac_[3]))};
|
||||
__m128i pos4{_mm_setr_epi32(static_cast<int>(pos_[0]), static_cast<int>(pos_[1]),
|
||||
static_cast<int>(pos_[2]), static_cast<int>(pos_[3]))};
|
||||
|
||||
auto vecout = al::span<__m128>{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]() -> __m128
|
||||
auto vecout = al::span{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]
|
||||
{
|
||||
const auto pos0 = static_cast<uint>(_mm_extract_epi32(pos4, 0));
|
||||
const auto pos1 = static_cast<uint>(_mm_extract_epi32(pos4, 1));
|
||||
const auto pos2 = static_cast<uint>(_mm_extract_epi32(pos4, 2));
|
||||
const auto pos3 = static_cast<uint>(_mm_extract_epi32(pos4, 3));
|
||||
ASSUME(pos0 <= pos1); ASSUME(pos1 <= pos2); ASSUME(pos2 <= pos3);
|
||||
const __m128 val1{_mm_setr_ps(src[pos0], src[pos1], src[pos2], src[pos3])};
|
||||
const __m128 val2{_mm_setr_ps(src[pos0+1_uz], src[pos1+1_uz], src[pos2+1_uz], src[pos3+1_uz])};
|
||||
|
||||
@@ -101,24 +103,26 @@ void Resample_<LerpTag,SSE4Tag>(const InterpState*, const float *src, uint frac,
|
||||
* four samples, so the lowest element is the next position to
|
||||
* resample.
|
||||
*/
|
||||
src += static_cast<uint>(_mm_cvtsi128_si32(pos4));
|
||||
auto pos = size_t{static_cast<uint>(_mm_cvtsi128_si32(pos4))};
|
||||
frac = static_cast<uint>(_mm_cvtsi128_si32(frac4));
|
||||
|
||||
std::generate(dst.end()-ptrdiff_t(todo), dst.end(), [&src,&frac,increment]
|
||||
auto out = dst.last(todo);
|
||||
std::generate(out.begin(), out.end(), [&pos,&frac,src,increment]
|
||||
{
|
||||
const float out{lerpf(src[0], src[1], static_cast<float>(frac) * (1.0f/MixerFracOne))};
|
||||
const float smp{lerpf(src[pos+0], src[pos+1],
|
||||
static_cast<float>(frac) * (1.0f/MixerFracOne))};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return out;
|
||||
return smp;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void Resample_<CubicTag,SSE4Tag>(const InterpState *state, const float *src, uint frac,
|
||||
const uint increment, const al::span<float> dst)
|
||||
void Resample_<CubicTag,SSE4Tag>(const InterpState *state, const al::span<const float> src,
|
||||
uint frac, const uint increment, const al::span<float> dst)
|
||||
{
|
||||
ASSUME(frac < MixerFracOne);
|
||||
|
||||
@@ -129,25 +133,25 @@ void Resample_<CubicTag,SSE4Tag>(const InterpState *state, const float *src, uin
|
||||
const __m128 fracDiffOne4{_mm_set1_ps(1.0f/CubicPhaseDiffOne)};
|
||||
const __m128i fracDiffMask4{_mm_set1_epi32(CubicPhaseDiffMask)};
|
||||
|
||||
alignas(16) std::array<uint,4> pos_, frac_;
|
||||
InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_});
|
||||
std::array<uint,4> pos_{}, frac_{};
|
||||
InitPosArrays(MaxResamplerEdge-1, frac, increment, al::span{frac_}, al::span{pos_});
|
||||
__m128i frac4{_mm_setr_epi32(static_cast<int>(frac_[0]), static_cast<int>(frac_[1]),
|
||||
static_cast<int>(frac_[2]), static_cast<int>(frac_[3]))};
|
||||
__m128i pos4{_mm_setr_epi32(static_cast<int>(pos_[0]), static_cast<int>(pos_[1]),
|
||||
static_cast<int>(pos_[2]), static_cast<int>(pos_[3]))};
|
||||
|
||||
src -= 1;
|
||||
auto vecout = al::span<__m128>{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]() -> __m128
|
||||
auto vecout = al::span{reinterpret_cast<__m128*>(dst.data()), dst.size()/4};
|
||||
std::generate(vecout.begin(), vecout.end(), [=,&pos4,&frac4]
|
||||
{
|
||||
const auto pos0 = static_cast<uint>(_mm_extract_epi32(pos4, 0));
|
||||
const auto pos1 = static_cast<uint>(_mm_extract_epi32(pos4, 1));
|
||||
const auto pos2 = static_cast<uint>(_mm_extract_epi32(pos4, 2));
|
||||
const auto pos3 = static_cast<uint>(_mm_extract_epi32(pos4, 3));
|
||||
const __m128 val0{_mm_loadu_ps(src+pos0)};
|
||||
const __m128 val1{_mm_loadu_ps(src+pos1)};
|
||||
const __m128 val2{_mm_loadu_ps(src+pos2)};
|
||||
const __m128 val3{_mm_loadu_ps(src+pos3)};
|
||||
ASSUME(pos0 <= pos1); ASSUME(pos1 <= pos2); ASSUME(pos2 <= pos3);
|
||||
const __m128 val0{_mm_loadu_ps(&src[pos0])};
|
||||
const __m128 val1{_mm_loadu_ps(&src[pos1])};
|
||||
const __m128 val2{_mm_loadu_ps(&src[pos2])};
|
||||
const __m128 val3{_mm_loadu_ps(&src[pos3])};
|
||||
|
||||
const __m128i pi4{_mm_srli_epi32(frac4, CubicPhaseDiffBits)};
|
||||
const auto pi0 = static_cast<uint>(_mm_extract_epi32(pi4, 0));
|
||||
@@ -188,10 +192,11 @@ void Resample_<CubicTag,SSE4Tag>(const InterpState *state, const float *src, uin
|
||||
|
||||
if(const size_t todo{dst.size()&3})
|
||||
{
|
||||
src += static_cast<uint>(_mm_cvtsi128_si32(pos4));
|
||||
auto pos = size_t{static_cast<uint>(_mm_cvtsi128_si32(pos4))};
|
||||
frac = static_cast<uint>(_mm_cvtsi128_si32(frac4));
|
||||
|
||||
std::generate(dst.end()-ptrdiff_t(todo), dst.end(), [&src,&frac,increment,filter]
|
||||
auto out = dst.last(todo);
|
||||
std::generate(out.begin(), out.end(), [&pos,&frac,src,increment,filter]
|
||||
{
|
||||
const uint pi{frac >> CubicPhaseDiffBits}; ASSUME(pi < CubicPhaseCount);
|
||||
const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)};
|
||||
@@ -199,14 +204,14 @@ void Resample_<CubicTag,SSE4Tag>(const InterpState *state, const float *src, uin
|
||||
|
||||
const __m128 f4 = vmadd(_mm_load_ps(filter[pi].mCoeffs.data()), pf4,
|
||||
_mm_load_ps(filter[pi].mDeltas.data()));
|
||||
__m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(src))};
|
||||
__m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(&src[pos]))};
|
||||
|
||||
r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
|
||||
r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
|
||||
const float output{_mm_cvtss_f32(r4)};
|
||||
|
||||
frac += increment;
|
||||
src += frac>>MixerFracBits;
|
||||
pos += frac>>MixerFracBits;
|
||||
frac &= MixerFracMask;
|
||||
return output;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user