2012-08-15 01:01:55 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-01-09 19:42:40 +01:00
|
|
|
#include <cassert>
|
2012-10-05 06:03:19 -07:00
|
|
|
|
2018-12-22 16:01:14 -08:00
|
|
|
#include <limits>
|
|
|
|
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2012-08-15 01:01:55 -07:00
|
|
|
#include "alu.h"
|
2019-07-29 17:54:07 -07:00
|
|
|
|
2018-03-22 05:06:15 -07:00
|
|
|
#include "defs.h"
|
2019-01-23 15:09:11 -08:00
|
|
|
#include "hrtfbase.h"
|
2012-08-15 01:01:55 -07:00
|
|
|
|
|
|
|
|
2019-05-25 08:17:37 -07:00
|
|
|
namespace {
|
|
|
|
|
2019-07-01 22:42:21 -07:00
|
|
|
inline ALfloat do_point(const InterpState&, const ALfloat *RESTRICT vals, const ALsizei)
|
2013-10-07 07:44:09 -07:00
|
|
|
{ return vals[0]; }
|
2019-07-01 22:42:21 -07:00
|
|
|
inline ALfloat do_lerp(const InterpState&, const ALfloat *RESTRICT vals, const ALsizei frac)
|
2012-09-27 02:46:15 -07:00
|
|
|
{ return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
|
2019-07-01 22:42:21 -07:00
|
|
|
inline ALfloat do_cubic(const InterpState&, const ALfloat *RESTRICT vals, const ALsizei frac)
|
2018-01-07 17:24:29 -08:00
|
|
|
{ return cubic(vals[0], vals[1], vals[2], vals[3], frac * (1.0f/FRACTIONONE)); }
|
2019-07-01 22:42:21 -07:00
|
|
|
inline ALfloat do_bsinc(const InterpState &istate, const ALfloat *RESTRICT vals, const ALsizei frac)
|
2018-09-17 01:43:02 -07:00
|
|
|
{
|
2019-01-06 21:09:21 -08:00
|
|
|
ASSUME(istate.bsinc.m > 0);
|
2018-09-17 01:43:02 -07:00
|
|
|
|
|
|
|
// Calculate the phase index and factor.
|
|
|
|
#define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
|
2019-01-06 21:09:21 -08:00
|
|
|
const ALsizei pi{frac >> FRAC_PHASE_BITDIFF};
|
|
|
|
const ALfloat pf{(frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF))};
|
2018-09-17 01:43:02 -07:00
|
|
|
#undef FRAC_PHASE_BITDIFF
|
|
|
|
|
2019-01-06 21:09:21 -08:00
|
|
|
const ALfloat *fil{istate.bsinc.filter + istate.bsinc.m*pi*4};
|
|
|
|
const ALfloat *scd{fil + istate.bsinc.m};
|
|
|
|
const ALfloat *phd{scd + istate.bsinc.m};
|
|
|
|
const ALfloat *spd{phd + istate.bsinc.m};
|
2018-09-17 01:43:02 -07:00
|
|
|
|
|
|
|
// Apply the scale and phase interpolated filter.
|
2019-01-06 21:09:21 -08:00
|
|
|
ALfloat r{0.0f};
|
|
|
|
for(ALsizei j_f{0};j_f < istate.bsinc.m;j_f++)
|
|
|
|
r += (fil[j_f] + istate.bsinc.sf*scd[j_f] + pf*(phd[j_f] + istate.bsinc.sf*spd[j_f])) * vals[j_f];
|
2018-09-17 01:43:02 -07:00
|
|
|
return r;
|
|
|
|
}
|
2015-11-05 09:42:08 -08:00
|
|
|
|
2019-03-28 10:00:35 -07:00
|
|
|
using SamplerT = ALfloat(const InterpState&, const ALfloat*RESTRICT, const ALsizei);
|
|
|
|
template<SamplerT &Sampler>
|
2019-05-25 08:17:37 -07:00
|
|
|
const ALfloat *DoResample(const InterpState *state, const ALfloat *RESTRICT src,
|
2019-08-20 08:46:12 -07:00
|
|
|
ALsizei frac, ALint increment, const al::span<float> dst)
|
2019-03-28 10:00:35 -07:00
|
|
|
{
|
|
|
|
ASSUME(increment > 0);
|
|
|
|
ASSUME(frac >= 0);
|
|
|
|
|
|
|
|
const InterpState istate{*state};
|
|
|
|
auto proc_sample = [&src,&frac,istate,increment]() -> ALfloat
|
|
|
|
{
|
|
|
|
const ALfloat ret{Sampler(istate, src, frac)};
|
|
|
|
|
|
|
|
frac += increment;
|
|
|
|
src += frac>>FRACTIONBITS;
|
|
|
|
frac &= FRACTIONMASK;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
2019-08-20 08:46:12 -07:00
|
|
|
std::generate(dst.begin(), dst.end(), proc_sample);
|
2019-03-28 10:00:35 -07:00
|
|
|
|
2019-08-20 08:46:12 -07:00
|
|
|
return dst.begin();
|
2019-03-28 10:00:35 -07:00
|
|
|
}
|
|
|
|
|
2019-05-25 08:17:37 -07:00
|
|
|
} // namespace
|
2019-03-28 10:00:35 -07:00
|
|
|
|
2019-01-23 11:11:41 -08:00
|
|
|
template<>
|
2019-07-28 17:15:34 -07:00
|
|
|
const ALfloat *Resample_<CopyTag,CTag>(const InterpState*, const ALfloat *RESTRICT src, ALsizei,
|
2019-08-20 08:46:12 -07:00
|
|
|
ALint, const al::span<float> dst)
|
2012-10-05 06:03:19 -07:00
|
|
|
{
|
2014-05-21 15:24:40 -07:00
|
|
|
#if defined(HAVE_SSE) || defined(HAVE_NEON)
|
|
|
|
/* Avoid copying the source data if it's aligned like the destination. */
|
2019-08-20 08:46:12 -07:00
|
|
|
if((reinterpret_cast<intptr_t>(src)&15) == (reinterpret_cast<intptr_t>(dst.data())&15))
|
2014-05-21 15:24:40 -07:00
|
|
|
return src;
|
|
|
|
#endif
|
2019-08-20 08:46:12 -07:00
|
|
|
std::copy_n(src, dst.size(), dst.begin());
|
|
|
|
return dst.begin();
|
2012-10-05 06:03:19 -07:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:11:41 -08:00
|
|
|
template<>
|
|
|
|
const ALfloat *Resample_<PointTag,CTag>(const InterpState *state, const ALfloat *RESTRICT src,
|
2019-08-20 08:46:12 -07:00
|
|
|
ALsizei frac, ALint increment, const al::span<float> dst)
|
|
|
|
{ return DoResample<do_point>(state, src, frac, increment, dst); }
|
2019-01-23 11:11:41 -08:00
|
|
|
|
|
|
|
template<>
|
|
|
|
const ALfloat *Resample_<LerpTag,CTag>(const InterpState *state, const ALfloat *RESTRICT src,
|
2019-08-20 08:46:12 -07:00
|
|
|
ALsizei frac, ALint increment, const al::span<float> dst)
|
|
|
|
{ return DoResample<do_lerp>(state, src, frac, increment, dst); }
|
2019-01-23 11:11:41 -08:00
|
|
|
|
|
|
|
template<>
|
|
|
|
const ALfloat *Resample_<CubicTag,CTag>(const InterpState *state, const ALfloat *RESTRICT src,
|
2019-08-20 08:46:12 -07:00
|
|
|
ALsizei frac, ALint increment, const al::span<float> dst)
|
|
|
|
{ return DoResample<do_cubic>(state, src-1, frac, increment, dst); }
|
2019-01-23 11:11:41 -08:00
|
|
|
|
|
|
|
template<>
|
|
|
|
const ALfloat *Resample_<BSincTag,CTag>(const InterpState *state, const ALfloat *RESTRICT src,
|
2019-08-20 08:46:12 -07:00
|
|
|
ALsizei frac, ALint increment, const al::span<float> dst)
|
|
|
|
{ return DoResample<do_bsinc>(state, src-state->bsinc.l, frac, increment, dst); }
|
2015-11-05 21:57:12 -08:00
|
|
|
|
2012-09-14 04:13:18 -07:00
|
|
|
|
2019-03-29 11:28:38 -07:00
|
|
|
static inline void ApplyCoeffs(ALsizei /*Offset*/, float2 *RESTRICT Values, const ALsizei IrSize,
|
2019-07-31 10:46:33 -07:00
|
|
|
const HrirArray &Coeffs, const ALfloat left, const ALfloat right)
|
2012-08-15 01:01:55 -07:00
|
|
|
{
|
2018-12-26 15:35:05 -08:00
|
|
|
ASSUME(IrSize >= 2);
|
2019-03-29 11:28:38 -07:00
|
|
|
for(ALsizei c{0};c < IrSize;++c)
|
2012-08-15 01:01:55 -07:00
|
|
|
{
|
2019-03-29 11:28:38 -07:00
|
|
|
Values[c][0] += Coeffs[c][0] * left;
|
|
|
|
Values[c][1] += Coeffs[c][1] * right;
|
2012-08-15 01:01:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 12:23:05 -08:00
|
|
|
template<>
|
2019-05-28 22:44:50 -07:00
|
|
|
void MixHrtf_<CTag>(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
|
|
|
|
const ALfloat *InSamples, float2 *AccumSamples, const ALsizei OutPos, const ALsizei IrSize,
|
2019-06-18 06:20:35 -07:00
|
|
|
MixHrtfFilter *hrtfparams, const ALsizei BufferSize)
|
2019-01-23 12:23:05 -08:00
|
|
|
{
|
2019-05-28 22:44:50 -07:00
|
|
|
MixHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, OutPos, IrSize,
|
|
|
|
hrtfparams, BufferSize);
|
2019-01-23 12:23:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-05-28 22:44:50 -07:00
|
|
|
void MixHrtfBlend_<CTag>(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
|
|
|
|
const ALfloat *InSamples, float2 *AccumSamples, const ALsizei OutPos, const ALsizei IrSize,
|
2019-06-18 06:20:35 -07:00
|
|
|
const HrtfFilter *oldparams, MixHrtfFilter *newparams, const ALsizei BufferSize)
|
2019-01-23 12:23:05 -08:00
|
|
|
{
|
2019-05-28 22:44:50 -07:00
|
|
|
MixHrtfBlendBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, OutPos, IrSize,
|
|
|
|
oldparams, newparams, BufferSize);
|
2019-01-23 12:23:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-05-28 22:44:50 -07:00
|
|
|
void MixDirectHrtf_<CTag>(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
|
2019-05-29 17:32:16 -07:00
|
|
|
const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples, DirectHrtfState *State,
|
|
|
|
const ALsizei BufferSize)
|
2019-03-29 11:28:38 -07:00
|
|
|
{
|
2019-05-29 17:32:16 -07:00
|
|
|
MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, State, BufferSize);
|
2019-03-29 11:28:38 -07:00
|
|
|
}
|
2019-01-23 12:23:05 -08:00
|
|
|
|
2012-09-16 08:14:26 -07:00
|
|
|
|
2019-01-23 11:21:03 -08:00
|
|
|
template<>
|
2019-08-20 04:16:44 -07:00
|
|
|
void Mix_<CTag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
|
|
|
|
float *CurrentGains, const float *TargetGains, const ALsizei Counter, const ALsizei OutPos)
|
2012-09-16 08:14:26 -07:00
|
|
|
{
|
2019-01-08 19:42:44 +01:00
|
|
|
const ALfloat delta{(Counter > 0) ? 1.0f / static_cast<ALfloat>(Counter) : 0.0f};
|
2019-08-20 04:16:44 -07:00
|
|
|
const bool reached_target{InSamples.size() >= static_cast<size_t>(Counter)};
|
|
|
|
const auto min_end = reached_target ? InSamples.begin() + Counter : InSamples.end();
|
2019-05-29 21:58:37 -07:00
|
|
|
for(FloatBufferLine &output : OutBuffer)
|
2012-09-16 08:14:26 -07:00
|
|
|
{
|
2019-08-20 04:16:44 -07:00
|
|
|
ALfloat *RESTRICT dst{al::assume_aligned<16>(output.data()+OutPos)};
|
2019-05-29 21:58:37 -07:00
|
|
|
ALfloat gain{*CurrentGains};
|
|
|
|
const ALfloat diff{*TargetGains - gain};
|
2018-09-22 08:26:52 -07:00
|
|
|
|
2019-08-20 04:16:44 -07:00
|
|
|
auto in_iter = InSamples.begin();
|
2019-01-06 21:09:21 -08:00
|
|
|
if(std::fabs(diff) > std::numeric_limits<float>::epsilon())
|
2014-03-23 06:57:00 -07:00
|
|
|
{
|
2019-01-06 21:09:21 -08:00
|
|
|
const ALfloat step{diff * delta};
|
|
|
|
ALfloat step_count{0.0f};
|
2019-08-20 04:16:44 -07:00
|
|
|
while(in_iter != min_end)
|
2014-03-23 06:57:00 -07:00
|
|
|
{
|
2019-08-20 04:16:44 -07:00
|
|
|
*(dst++) += *(in_iter++) * (gain + step*step_count);
|
2018-05-14 23:41:29 -07:00
|
|
|
step_count += 1.0f;
|
2014-03-23 06:57:00 -07:00
|
|
|
}
|
2019-08-20 04:16:44 -07:00
|
|
|
if(reached_target)
|
2019-05-29 21:58:37 -07:00
|
|
|
gain = *TargetGains;
|
2018-05-14 23:41:29 -07:00
|
|
|
else
|
|
|
|
gain += step*step_count;
|
2019-05-29 21:58:37 -07:00
|
|
|
*CurrentGains = gain;
|
2014-03-23 06:57:00 -07:00
|
|
|
}
|
2019-05-29 21:58:37 -07:00
|
|
|
++CurrentGains;
|
|
|
|
++TargetGains;
|
2014-03-23 06:57:00 -07:00
|
|
|
|
2019-01-06 21:09:21 -08:00
|
|
|
if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
|
2012-09-24 15:33:44 -07:00
|
|
|
continue;
|
2019-08-20 04:16:44 -07:00
|
|
|
while(in_iter != InSamples.end())
|
|
|
|
*(dst++) += *(in_iter++) * gain;
|
2014-03-23 16:11:21 -07:00
|
|
|
}
|
2012-09-16 08:14:26 -07:00
|
|
|
}
|
2016-05-31 10:18:34 -07:00
|
|
|
|
|
|
|
/* Basically the inverse of the above. Rather than one input going to multiple
|
|
|
|
* outputs (each with its own gain), it's multiple inputs (each with its own
|
|
|
|
* gain) going to one output. This applies one row (vs one column) of a matrix
|
|
|
|
* transform. And as the matrices are more or less static once set up, no
|
|
|
|
* stepping is necessary.
|
|
|
|
*/
|
2019-01-23 11:21:03 -08:00
|
|
|
template<>
|
2019-08-20 00:27:28 -07:00
|
|
|
void MixRow_<CTag>(const al::span<float> OutBuffer, const al::span<const float> Gains,
|
|
|
|
const float *InSamples, const size_t InStride)
|
2016-05-31 10:18:34 -07:00
|
|
|
{
|
2019-08-20 00:27:28 -07:00
|
|
|
for(const float gain : Gains)
|
2016-05-31 10:18:34 -07:00
|
|
|
{
|
2019-08-20 00:27:28 -07:00
|
|
|
const float *RESTRICT src{InSamples};
|
2019-08-19 08:02:08 -07:00
|
|
|
InSamples += InStride;
|
|
|
|
|
2019-01-06 21:09:21 -08:00
|
|
|
if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
|
2016-05-31 10:18:34 -07:00
|
|
|
continue;
|
|
|
|
|
2019-08-20 00:27:28 -07:00
|
|
|
std::transform(OutBuffer.begin(), OutBuffer.end(), src, OutBuffer.begin(),
|
|
|
|
[gain](const ALfloat cur, const ALfloat src) -> ALfloat { return cur + src*gain; });
|
2016-05-31 10:18:34 -07:00
|
|
|
}
|
|
|
|
}
|