2010-08-03 23:19:36 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 1999-2007 by authors.
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2010-08-03 23:19:36 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-01-09 19:42:40 +01:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cassert>
|
2010-08-03 23:19:36 -07:00
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
#include <numeric>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2010-08-03 23:19:36 -07:00
|
|
|
#include "AL/al.h"
|
|
|
|
#include "AL/alc.h"
|
2018-11-17 23:02:27 -08:00
|
|
|
|
|
|
|
#include "alMain.h"
|
|
|
|
#include "alcontext.h"
|
2010-08-03 23:19:36 -07:00
|
|
|
#include "alSource.h"
|
|
|
|
#include "alBuffer.h"
|
|
|
|
#include "alListener.h"
|
|
|
|
#include "alAuxEffectSlot.h"
|
2018-01-17 08:49:49 -08:00
|
|
|
#include "sample_cvt.h"
|
2010-08-03 23:19:36 -07:00
|
|
|
#include "alu.h"
|
2018-01-11 07:56:54 -08:00
|
|
|
#include "alconfig.h"
|
2018-02-01 01:36:03 -08:00
|
|
|
#include "ringbuffer.h"
|
2014-06-13 16:07:25 -07:00
|
|
|
|
2018-01-11 07:19:19 -08:00
|
|
|
#include "cpu_caps.h"
|
2018-03-22 05:06:15 -07:00
|
|
|
#include "mixer/defs.h"
|
2010-08-03 23:19:36 -07:00
|
|
|
|
2011-05-06 00:20:40 -07:00
|
|
|
|
2015-09-26 11:11:04 -07:00
|
|
|
static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
|
|
|
|
"MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
|
|
|
|
|
2017-12-29 16:28:49 -08:00
|
|
|
/* BSinc24 requires up to 23 extra samples before the current position, and 24 after. */
|
2018-01-09 23:55:59 -08:00
|
|
|
static_assert(MAX_RESAMPLE_PADDING >= 24, "MAX_RESAMPLE_PADDING must be at least 24!");
|
2015-10-01 00:34:13 -07:00
|
|
|
|
|
|
|
|
2018-12-24 19:29:01 -08:00
|
|
|
Resampler ResamplerDefault = LinearResampler;
|
2017-04-20 23:21:46 -07:00
|
|
|
|
2017-12-17 21:48:07 -08:00
|
|
|
MixerFunc MixSamples = Mix_C;
|
2018-01-16 12:18:59 -08:00
|
|
|
RowMixerFunc MixRowSamples = MixRow_C;
|
2016-09-06 13:21:11 -07:00
|
|
|
static HrtfMixerFunc MixHrtfSamples = MixHrtf_C;
|
2018-01-10 19:13:41 -08:00
|
|
|
static HrtfMixerBlendFunc MixHrtfBlendSamples = MixHrtfBlend_C;
|
2014-06-06 01:52:53 -07:00
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
static MixerFunc SelectMixer()
|
2014-11-23 10:49:54 -08:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
2016-09-06 13:21:11 -07:00
|
|
|
return Mix_Neon;
|
2014-11-23 10:49:54 -08:00
|
|
|
#endif
|
2017-04-20 20:58:32 -07:00
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
return Mix_SSE;
|
|
|
|
#endif
|
2016-09-06 13:21:11 -07:00
|
|
|
return Mix_C;
|
2014-11-23 10:49:54 -08:00
|
|
|
}
|
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
static RowMixerFunc SelectRowMixer()
|
2016-10-04 16:25:43 -07:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
|
|
|
return MixRow_Neon;
|
2017-04-20 20:58:32 -07:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
return MixRow_SSE;
|
2016-10-04 16:25:43 -07:00
|
|
|
#endif
|
|
|
|
return MixRow_C;
|
|
|
|
}
|
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
static inline HrtfMixerFunc SelectHrtfMixer()
|
2014-06-13 16:07:25 -07:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
2016-09-06 13:21:11 -07:00
|
|
|
return MixHrtf_Neon;
|
2014-06-13 16:07:25 -07:00
|
|
|
#endif
|
2017-03-14 19:16:59 -07:00
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
return MixHrtf_SSE;
|
|
|
|
#endif
|
2016-09-06 13:21:11 -07:00
|
|
|
return MixHrtf_C;
|
2014-06-13 16:07:25 -07:00
|
|
|
}
|
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
static inline HrtfMixerBlendFunc SelectHrtfBlendMixer()
|
2017-05-03 03:29:21 -07:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
|
|
|
return MixHrtfBlend_Neon;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
return MixHrtfBlend_SSE;
|
|
|
|
#endif
|
|
|
|
return MixHrtfBlend_C;
|
|
|
|
}
|
|
|
|
|
2018-12-24 19:29:01 -08:00
|
|
|
ResamplerFunc SelectResampler(Resampler resampler)
|
2014-06-13 16:07:25 -07:00
|
|
|
{
|
2014-11-02 00:27:26 -07:00
|
|
|
switch(resampler)
|
2014-06-13 16:07:25 -07:00
|
|
|
{
|
|
|
|
case PointResampler:
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_point_C;
|
2014-06-13 16:07:25 -07:00
|
|
|
case LinearResampler:
|
2017-02-12 21:03:30 -08:00
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_lerp_Neon;
|
2017-02-12 21:03:30 -08:00
|
|
|
#endif
|
2014-06-13 16:07:25 -07:00
|
|
|
#ifdef HAVE_SSE4_1
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE4_1))
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_lerp_SSE41;
|
2014-06-13 16:07:25 -07:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SSE2
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE2))
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_lerp_SSE2;
|
2014-06-13 16:07:25 -07:00
|
|
|
#endif
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_lerp_C;
|
2015-09-27 23:52:16 -07:00
|
|
|
case FIR4Resampler:
|
2018-01-07 05:32:07 -08:00
|
|
|
return Resample_cubic_C;
|
2017-08-25 05:52:19 -07:00
|
|
|
case BSinc12Resampler:
|
2017-08-27 10:16:36 -07:00
|
|
|
case BSinc24Resampler:
|
2017-02-12 21:03:30 -08:00
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_bsinc_Neon;
|
2017-02-12 21:03:30 -08:00
|
|
|
#endif
|
2015-11-05 09:42:08 -08:00
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_bsinc_SSE;
|
2015-11-05 09:42:08 -08:00
|
|
|
#endif
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_bsinc_C;
|
2014-06-13 16:07:25 -07:00
|
|
|
}
|
|
|
|
|
2017-08-16 18:09:53 -07:00
|
|
|
return Resample_point_C;
|
2014-06-13 16:07:25 -07:00
|
|
|
}
|
|
|
|
|
2015-11-04 06:40:54 -08:00
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
void aluInitMixer()
|
2015-09-27 20:55:39 -07:00
|
|
|
{
|
2015-10-01 00:34:13 -07:00
|
|
|
const char *str;
|
|
|
|
|
2019-01-07 12:37:13 +01:00
|
|
|
if(ConfigValueStr(nullptr, nullptr, "resampler", &str))
|
2015-10-01 00:34:13 -07:00
|
|
|
{
|
|
|
|
if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
|
2017-04-20 23:21:46 -07:00
|
|
|
ResamplerDefault = PointResampler;
|
2015-10-01 00:34:13 -07:00
|
|
|
else if(strcasecmp(str, "linear") == 0)
|
2017-04-20 23:21:46 -07:00
|
|
|
ResamplerDefault = LinearResampler;
|
2018-01-07 05:32:07 -08:00
|
|
|
else if(strcasecmp(str, "cubic") == 0)
|
2017-04-20 23:21:46 -07:00
|
|
|
ResamplerDefault = FIR4Resampler;
|
2017-08-27 10:16:36 -07:00
|
|
|
else if(strcasecmp(str, "bsinc12") == 0)
|
|
|
|
ResamplerDefault = BSinc12Resampler;
|
|
|
|
else if(strcasecmp(str, "bsinc24") == 0)
|
|
|
|
ResamplerDefault = BSinc24Resampler;
|
2015-11-05 09:42:08 -08:00
|
|
|
else if(strcasecmp(str, "bsinc") == 0)
|
2017-08-27 10:16:36 -07:00
|
|
|
{
|
|
|
|
WARN("Resampler option \"%s\" is deprecated, using bsinc12\n", str);
|
2017-08-25 05:52:19 -07:00
|
|
|
ResamplerDefault = BSinc12Resampler;
|
2017-08-27 10:16:36 -07:00
|
|
|
}
|
2018-01-07 05:32:07 -08:00
|
|
|
else if(strcasecmp(str, "sinc4") == 0 || strcasecmp(str, "sinc8") == 0)
|
2015-10-01 00:34:13 -07:00
|
|
|
{
|
2018-01-07 05:32:07 -08:00
|
|
|
WARN("Resampler option \"%s\" is deprecated, using cubic\n", str);
|
2017-04-20 23:21:46 -07:00
|
|
|
ResamplerDefault = FIR4Resampler;
|
2015-10-01 00:34:13 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
long n = strtol(str, &end, 0);
|
|
|
|
if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == FIR4Resampler))
|
2018-12-24 19:29:01 -08:00
|
|
|
ResamplerDefault = static_cast<Resampler>(n);
|
2015-10-01 00:34:13 -07:00
|
|
|
else
|
|
|
|
WARN("Invalid resampler: %s\n", str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 03:29:21 -07:00
|
|
|
MixHrtfBlendSamples = SelectHrtfBlendMixer();
|
2015-09-27 20:55:39 -07:00
|
|
|
MixHrtfSamples = SelectHrtfMixer();
|
|
|
|
MixSamples = SelectMixer();
|
2018-01-16 12:18:59 -08:00
|
|
|
MixRowSamples = SelectRowMixer();
|
2015-09-27 20:55:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Base template left undefined. Should be marked =delete, but Clang 3.8.1
|
|
|
|
* chokes on that given the inline specializations.
|
|
|
|
*/
|
|
|
|
template<FmtType T>
|
|
|
|
inline ALfloat LoadSample(typename FmtTypeTraits<T>::Type val);
|
2011-10-04 09:47:08 -07:00
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
template<> inline ALfloat LoadSample<FmtUByte>(FmtTypeTraits<FmtUByte>::Type val)
|
|
|
|
{ return (val-128) * (1.0f/128.0f); }
|
|
|
|
template<> inline ALfloat LoadSample<FmtShort>(FmtTypeTraits<FmtShort>::Type val)
|
2017-03-31 06:54:46 -07:00
|
|
|
{ return val * (1.0f/32768.0f); }
|
2018-11-20 02:59:02 -08:00
|
|
|
template<> inline ALfloat LoadSample<FmtFloat>(FmtTypeTraits<FmtFloat>::Type val)
|
2011-10-04 09:47:08 -07:00
|
|
|
{ return val; }
|
2018-11-20 02:59:02 -08:00
|
|
|
template<> inline ALfloat LoadSample<FmtDouble>(FmtTypeTraits<FmtDouble>::Type val)
|
2019-01-08 19:42:44 +01:00
|
|
|
{ return static_cast<ALfloat>(val); }
|
2018-11-20 02:59:02 -08:00
|
|
|
template<> inline ALfloat LoadSample<FmtMulaw>(FmtTypeTraits<FmtMulaw>::Type val)
|
2018-01-17 08:49:49 -08:00
|
|
|
{ return muLawDecompressionTable[val] * (1.0f/32768.0f); }
|
2018-11-20 02:59:02 -08:00
|
|
|
template<> inline ALfloat LoadSample<FmtAlaw>(FmtTypeTraits<FmtAlaw>::Type val)
|
2018-01-17 08:49:49 -08:00
|
|
|
{ return aLawDecompressionTable[val] * (1.0f/32768.0f); }
|
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
template<FmtType T>
|
|
|
|
inline void LoadSampleArray(ALfloat *RESTRICT dst, const void *src, ALint srcstep, ALsizei samples)
|
|
|
|
{
|
|
|
|
using SampleType = typename FmtTypeTraits<T>::Type;
|
2011-10-04 09:47:08 -07:00
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
const SampleType *ssrc = static_cast<const SampleType*>(src);
|
|
|
|
for(ALsizei i{0};i < samples;i++)
|
|
|
|
dst[i] += LoadSample<T>(ssrc[i*srcstep]);
|
|
|
|
}
|
2011-10-04 09:47:08 -07:00
|
|
|
|
2018-12-04 16:33:26 -08:00
|
|
|
void LoadSamples(ALfloat *RESTRICT dst, const ALvoid *RESTRICT src, ALint srcstep, FmtType srctype,
|
|
|
|
ALsizei samples)
|
2011-10-04 09:47:08 -07:00
|
|
|
{
|
2018-11-20 02:59:02 -08:00
|
|
|
#define HANDLE_FMT(T) \
|
|
|
|
case T: LoadSampleArray<T>(dst, src, srcstep, samples); break
|
2011-10-04 09:47:08 -07:00
|
|
|
switch(srctype)
|
|
|
|
{
|
2018-11-20 02:59:02 -08:00
|
|
|
HANDLE_FMT(FmtUByte);
|
|
|
|
HANDLE_FMT(FmtShort);
|
|
|
|
HANDLE_FMT(FmtFloat);
|
|
|
|
HANDLE_FMT(FmtDouble);
|
|
|
|
HANDLE_FMT(FmtMulaw);
|
|
|
|
HANDLE_FMT(FmtAlaw);
|
2011-10-04 09:47:08 -07:00
|
|
|
}
|
2018-01-17 08:49:49 -08:00
|
|
|
#undef HANDLE_FMT
|
2011-10-04 09:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
const ALfloat *DoFilters(BiquadFilter *lpfilter, BiquadFilter *hpfilter,
|
|
|
|
ALfloat *RESTRICT dst, const ALfloat *RESTRICT src,
|
|
|
|
ALsizei numsamples, int type)
|
2012-09-11 05:24:19 -07:00
|
|
|
{
|
2017-01-18 07:13:23 -08:00
|
|
|
ALsizei i;
|
2014-05-17 07:17:48 -07:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case AF_None:
|
2018-12-04 22:31:08 -08:00
|
|
|
lpfilter->passthru(numsamples);
|
|
|
|
hpfilter->passthru(numsamples);
|
2014-05-17 07:17:48 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_LowPass:
|
2018-12-04 22:31:08 -08:00
|
|
|
lpfilter->process(dst, src, numsamples);
|
|
|
|
hpfilter->passthru(numsamples);
|
2014-05-19 01:54:52 -07:00
|
|
|
return dst;
|
2014-05-17 07:54:25 -07:00
|
|
|
case AF_HighPass:
|
2018-12-04 22:31:08 -08:00
|
|
|
lpfilter->passthru(numsamples);
|
|
|
|
hpfilter->process(dst, src, numsamples);
|
2014-05-19 01:54:52 -07:00
|
|
|
return dst;
|
2014-05-17 07:54:25 -07:00
|
|
|
|
|
|
|
case AF_BandPass:
|
2014-05-18 05:02:34 -07:00
|
|
|
for(i = 0;i < numsamples;)
|
|
|
|
{
|
2014-12-18 09:23:55 -08:00
|
|
|
ALfloat temp[256];
|
2017-01-18 07:13:23 -08:00
|
|
|
ALsizei todo = mini(256, numsamples-i);
|
2014-05-18 05:02:34 -07:00
|
|
|
|
2018-12-04 22:31:08 -08:00
|
|
|
lpfilter->process(temp, src+i, todo);
|
|
|
|
hpfilter->process(dst+i, temp, todo);
|
2014-05-18 05:02:34 -07:00
|
|
|
i += todo;
|
|
|
|
}
|
2014-05-19 01:54:52 -07:00
|
|
|
return dst;
|
2014-05-17 07:17:48 -07:00
|
|
|
}
|
2014-05-19 01:54:52 -07:00
|
|
|
return src;
|
2012-09-11 05:24:19 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
} // namespace
|
2012-09-11 05:24:19 -07:00
|
|
|
|
2018-01-09 22:01:46 -08:00
|
|
|
/* This function uses these device temp buffers. */
|
|
|
|
#define SOURCE_DATA_BUF 0
|
|
|
|
#define RESAMPLED_BUF 1
|
|
|
|
#define FILTERED_BUF 2
|
|
|
|
#define NFC_DATA_BUF 3
|
2018-12-25 19:54:14 -08:00
|
|
|
ALboolean MixSource(ALvoice *voice, const ALuint SourceID, ALCcontext *Context, const ALsizei SamplesToDo)
|
2010-09-22 09:38:24 -07:00
|
|
|
{
|
2018-11-25 13:19:01 -08:00
|
|
|
ASSUME(SamplesToDo > 0);
|
2010-09-22 09:38:24 -07:00
|
|
|
|
|
|
|
/* Get source info */
|
2018-11-25 13:19:01 -08:00
|
|
|
bool isplaying{true}; /* Will only be called while playing. */
|
|
|
|
bool isstatic{(voice->Flags&VOICE_IS_STATIC) != 0};
|
2019-01-08 19:42:44 +01:00
|
|
|
ALsizei DataPosInt{static_cast<ALsizei>(voice->position.load(std::memory_order_acquire))};
|
2018-11-25 13:19:01 -08:00
|
|
|
ALsizei DataPosFrac{voice->position_fraction.load(std::memory_order_relaxed)};
|
|
|
|
ALbufferlistitem *BufferListItem{voice->current_buffer.load(std::memory_order_relaxed)};
|
|
|
|
ALbufferlistitem *BufferLoopItem{voice->loop_buffer.load(std::memory_order_relaxed)};
|
|
|
|
ALsizei NumChannels{voice->NumChannels};
|
|
|
|
ALsizei SampleSize{voice->SampleSize};
|
|
|
|
ALint increment{voice->Step};
|
|
|
|
|
|
|
|
ASSUME(DataPosInt >= 0);
|
|
|
|
ASSUME(DataPosFrac >= 0);
|
|
|
|
ASSUME(NumChannels > 0);
|
|
|
|
ASSUME(SampleSize > 0);
|
|
|
|
ASSUME(increment > 0);
|
|
|
|
|
|
|
|
ALCdevice *Device{Context->Device};
|
2018-12-25 19:54:14 -08:00
|
|
|
const ALsizei IrSize{Device->mHrtf ? Device->mHrtf->irSize : 0};
|
|
|
|
const ALsizei NumAuxSends{Device->NumAuxSends};
|
|
|
|
const int OutLIdx{GetChannelIdxByName(Device->RealOut, FrontLeft)};
|
|
|
|
const int OutRIdx{GetChannelIdxByName(Device->RealOut, FrontRight)};
|
|
|
|
|
|
|
|
ASSUME(IrSize >= 0);
|
|
|
|
ASSUME(NumAuxSends >= 0);
|
2018-11-25 13:19:01 -08:00
|
|
|
|
|
|
|
ResamplerFunc Resample{(increment == FRACTIONONE && DataPosFrac == 0) ?
|
|
|
|
Resample_copy_C : voice->Resampler};
|
|
|
|
|
|
|
|
ALsizei Counter{(voice->Flags&VOICE_IS_FADING) ? SamplesToDo : 0};
|
2018-12-25 19:54:14 -08:00
|
|
|
if(!Counter)
|
|
|
|
{
|
|
|
|
/* No fading, just overwrite the old/current params. */
|
|
|
|
for(ALsizei chan{0};chan < NumChannels;chan++)
|
|
|
|
{
|
|
|
|
DirectParams &parms = voice->Direct.Params[chan];
|
|
|
|
if(!(voice->Flags&VOICE_HAS_HRTF))
|
|
|
|
std::copy(std::begin(parms.Gains.Target), std::end(parms.Gains.Target),
|
|
|
|
std::begin(parms.Gains.Current));
|
|
|
|
else
|
|
|
|
parms.Hrtf.Old = parms.Hrtf.Target;
|
|
|
|
auto set_current = [chan](ALvoice::SendData &send) -> void
|
|
|
|
{
|
|
|
|
if(!send.Buffer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SendParams &parms = send.Params[chan];
|
|
|
|
std::copy(std::begin(parms.Gains.Target), std::end(parms.Gains.Target),
|
|
|
|
std::begin(parms.Gains.Current));
|
|
|
|
};
|
|
|
|
std::for_each(voice->Send, voice->Send+NumAuxSends, set_current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((voice->Flags&VOICE_HAS_HRTF))
|
|
|
|
{
|
|
|
|
for(ALsizei chan{0};chan < NumChannels;chan++)
|
|
|
|
{
|
|
|
|
DirectParams &parms = voice->Direct.Params[chan];
|
|
|
|
if(!(parms.Hrtf.Old.Gain > GAIN_SILENCE_THRESHOLD))
|
|
|
|
{
|
|
|
|
/* The old HRTF params are silent, so overwrite the old
|
|
|
|
* coefficients with the new, and reset the old gain to 0. The
|
|
|
|
* future mix will then fade from silence.
|
|
|
|
*/
|
|
|
|
parms.Hrtf.Old = parms.Hrtf.Target;
|
|
|
|
parms.Hrtf.Old.Gain = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
ALsizei buffers_done{0};
|
|
|
|
ALsizei OutPos{0};
|
2010-09-22 09:38:24 -07:00
|
|
|
do {
|
2013-10-26 08:49:37 -07:00
|
|
|
/* Figure out how many buffer samples will be needed */
|
2018-12-01 13:47:56 -08:00
|
|
|
ALsizei DstBufferSize{SamplesToDo - OutPos};
|
|
|
|
|
|
|
|
/* Calculate the last written dst sample pos. */
|
|
|
|
ALint64 DataSize64{DstBufferSize - 1};
|
|
|
|
/* Calculate the last read src sample pos. */
|
|
|
|
DataSize64 = (DataSize64*increment + DataPosFrac) >> FRACTIONBITS;
|
|
|
|
/* +1 to get the src sample count, include padding. */
|
|
|
|
DataSize64 += 1 + MAX_RESAMPLE_PADDING*2;
|
|
|
|
|
|
|
|
auto SrcBufferSize = static_cast<ALsizei>(mini64(DataSize64, BUFFERSIZE+1));
|
|
|
|
if(SrcBufferSize > BUFFERSIZE)
|
|
|
|
{
|
|
|
|
SrcBufferSize = BUFFERSIZE;
|
|
|
|
/* If the source buffer got saturated, we can't fill the desired
|
|
|
|
* dst size. Figure out how many samples we can actually mix from
|
|
|
|
* this.
|
|
|
|
*/
|
|
|
|
DataSize64 = SrcBufferSize - MAX_RESAMPLE_PADDING*2;
|
|
|
|
DataSize64 = ((DataSize64<<FRACTIONBITS) - DataPosFrac + increment-1) / increment;
|
|
|
|
DstBufferSize = static_cast<ALsizei>(mini64(DataSize64, DstBufferSize));
|
|
|
|
|
|
|
|
/* Some mixers like having a multiple of 4, so try to give that
|
|
|
|
* unless this is the last update.
|
|
|
|
*/
|
|
|
|
if(DstBufferSize < SamplesToDo-OutPos)
|
|
|
|
DstBufferSize &= ~3;
|
|
|
|
}
|
2012-09-26 17:16:25 -07:00
|
|
|
|
2017-12-16 11:34:52 -08:00
|
|
|
/* It's impossible to have a buffer list item with no entries. */
|
|
|
|
assert(BufferListItem->num_buffers > 0);
|
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
for(ALsizei chan{0};chan < NumChannels;chan++)
|
2010-09-22 09:38:24 -07:00
|
|
|
{
|
2018-11-30 21:23:43 -08:00
|
|
|
ALfloat (&SrcData)[BUFFERSIZE] = Device->TempBuffer[SOURCE_DATA_BUF];
|
2015-10-15 07:29:25 -07:00
|
|
|
|
2017-12-16 09:41:27 -08:00
|
|
|
/* Load the previous samples into the source data first, and clear the rest. */
|
2018-11-30 21:23:43 -08:00
|
|
|
auto srciter = std::copy(std::begin(voice->PrevSamples[chan]),
|
|
|
|
std::end(voice->PrevSamples[chan]), std::begin(SrcData));
|
|
|
|
std::fill(srciter, std::end(SrcData), 0.0f);
|
2010-11-25 11:16:19 -08:00
|
|
|
|
2018-11-30 21:23:43 -08:00
|
|
|
auto FilledAmt = static_cast<ALsizei>(voice->PrevSamples[chan].size());
|
2017-08-14 22:58:24 -07:00
|
|
|
if(isstatic)
|
2010-09-22 09:38:24 -07:00
|
|
|
{
|
2017-12-16 11:34:52 -08:00
|
|
|
/* TODO: For static sources, loop points are taken from the
|
|
|
|
* first buffer (should be adjusted by any buffer offset, to
|
|
|
|
* possibly be added later).
|
|
|
|
*/
|
2018-11-25 13:19:01 -08:00
|
|
|
const ALbuffer *Buffer0{BufferListItem->buffers[0]};
|
|
|
|
const ALsizei LoopStart{Buffer0->LoopStart};
|
|
|
|
const ALsizei LoopEnd{Buffer0->LoopEnd};
|
|
|
|
ASSUME(LoopStart >= 0);
|
|
|
|
ASSUME(LoopEnd > LoopStart);
|
2014-12-18 08:42:03 -08:00
|
|
|
|
2012-09-26 17:16:25 -07:00
|
|
|
/* If current pos is beyond the loop range, do not loop */
|
2017-12-16 11:34:52 -08:00
|
|
|
if(!BufferLoopItem || DataPosInt >= LoopEnd)
|
2010-11-25 21:42:15 -08:00
|
|
|
{
|
2018-12-20 07:10:09 -08:00
|
|
|
const ALsizei SizeToDo{SrcBufferSize - FilledAmt};
|
2017-12-16 11:34:52 -08:00
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
BufferLoopItem = nullptr;
|
2010-11-25 21:42:15 -08:00
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
auto load_buffer = [DataPosInt,&SrcData,NumChannels,SampleSize,chan,FilledAmt,SizeToDo](ALsizei CompLen, const ALbuffer *buffer) -> ALsizei
|
2017-12-16 11:34:52 -08:00
|
|
|
{
|
|
|
|
if(DataPosInt >= buffer->SampleLen)
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2017-12-16 11:34:52 -08:00
|
|
|
|
|
|
|
/* Load what's left to play from the buffer */
|
2018-12-20 07:10:09 -08:00
|
|
|
const ALsizei DataSize{mini(SizeToDo, buffer->SampleLen - DataPosInt)};
|
2017-12-16 11:34:52 -08:00
|
|
|
CompLen = maxi(CompLen, DataSize);
|
2010-11-25 11:16:19 -08:00
|
|
|
|
2018-12-02 13:35:07 -08:00
|
|
|
const ALbyte *Data{buffer->mData.data()};
|
2017-12-16 11:34:52 -08:00
|
|
|
LoadSamples(&SrcData[FilledAmt],
|
|
|
|
&Data[(DataPosInt*NumChannels + chan)*SampleSize],
|
2018-12-24 19:29:01 -08:00
|
|
|
NumChannels, buffer->mFmtType, DataSize
|
2017-12-16 11:34:52 -08:00
|
|
|
);
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2018-12-02 13:35:07 -08:00
|
|
|
};
|
|
|
|
auto buffers_end = BufferListItem->buffers + BufferListItem->num_buffers;
|
2018-12-20 07:10:09 -08:00
|
|
|
FilledAmt += std::accumulate(BufferListItem->buffers, buffers_end, ALsizei{0},
|
|
|
|
load_buffer);
|
2010-11-25 21:42:15 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-02 13:35:07 -08:00
|
|
|
const ALsizei SizeToDo{mini(SrcBufferSize - FilledAmt, LoopEnd - DataPosInt)};
|
2010-11-25 21:42:15 -08:00
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
auto load_buffer = [DataPosInt,&SrcData,NumChannels,SampleSize,chan,FilledAmt,SizeToDo](ALsizei CompLen, const ALbuffer *buffer) -> ALsizei
|
2017-12-16 11:34:52 -08:00
|
|
|
{
|
|
|
|
if(DataPosInt >= buffer->SampleLen)
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2010-11-25 11:16:19 -08:00
|
|
|
|
2017-12-16 11:34:52 -08:00
|
|
|
/* Load what's left of this loop iteration */
|
2018-12-20 07:10:09 -08:00
|
|
|
const ALsizei DataSize{mini(SizeToDo, buffer->SampleLen - DataPosInt)};
|
2017-12-16 11:34:52 -08:00
|
|
|
CompLen = maxi(CompLen, DataSize);
|
|
|
|
|
2018-12-02 13:35:07 -08:00
|
|
|
const ALbyte *Data{buffer->mData.data()};
|
2017-12-16 11:34:52 -08:00
|
|
|
LoadSamples(&SrcData[FilledAmt],
|
|
|
|
&Data[(DataPosInt*NumChannels + chan)*SampleSize],
|
2018-12-24 19:29:01 -08:00
|
|
|
NumChannels, buffer->mFmtType, DataSize
|
2017-12-16 11:34:52 -08:00
|
|
|
);
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2018-12-02 13:35:07 -08:00
|
|
|
};
|
|
|
|
auto buffers_end = BufferListItem->buffers + BufferListItem->num_buffers;
|
2018-12-20 07:10:09 -08:00
|
|
|
FilledAmt = std::accumulate(BufferListItem->buffers, buffers_end, ALsizei{0}, load_buffer);
|
2012-09-26 17:16:25 -07:00
|
|
|
|
2018-12-02 13:35:07 -08:00
|
|
|
const ALsizei LoopSize{LoopEnd - LoopStart};
|
2017-12-16 08:58:46 -08:00
|
|
|
while(SrcBufferSize > FilledAmt)
|
2012-09-26 17:16:25 -07:00
|
|
|
{
|
2018-12-02 13:35:07 -08:00
|
|
|
const ALsizei SizeToDo{mini(SrcBufferSize - FilledAmt, LoopSize)};
|
2017-12-16 11:34:52 -08:00
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
auto load_buffer_loop = [LoopStart,&SrcData,NumChannels,SampleSize,chan,FilledAmt,SizeToDo](ALsizei CompLen, const ALbuffer *buffer) -> ALsizei
|
2017-12-16 11:34:52 -08:00
|
|
|
{
|
2018-01-04 21:21:48 -08:00
|
|
|
if(LoopStart >= buffer->SampleLen)
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2012-09-26 17:16:25 -07:00
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
const ALsizei DataSize{mini(SizeToDo, buffer->SampleLen - LoopStart)};
|
2017-12-16 11:34:52 -08:00
|
|
|
CompLen = maxi(CompLen, DataSize);
|
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
const ALbyte *Data{buffer->mData.data()};
|
2017-12-16 11:34:52 -08:00
|
|
|
LoadSamples(&SrcData[FilledAmt],
|
2018-01-04 20:21:46 -08:00
|
|
|
&Data[(LoopStart*NumChannels + chan)*SampleSize],
|
2018-12-24 19:29:01 -08:00
|
|
|
NumChannels, buffer->mFmtType, DataSize
|
2017-12-16 11:34:52 -08:00
|
|
|
);
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2018-12-02 13:35:07 -08:00
|
|
|
};
|
2018-12-20 07:10:09 -08:00
|
|
|
FilledAmt += std::accumulate(BufferListItem->buffers, buffers_end,
|
|
|
|
ALsizei{0}, load_buffer_loop);
|
2012-09-26 17:16:25 -07:00
|
|
|
}
|
2010-11-25 11:16:19 -08:00
|
|
|
}
|
2010-09-22 09:38:24 -07:00
|
|
|
}
|
2010-11-25 21:42:15 -08:00
|
|
|
else
|
|
|
|
{
|
2012-09-26 17:16:25 -07:00
|
|
|
/* Crawl the buffer queue to fill in the temp buffer */
|
2018-11-25 13:19:01 -08:00
|
|
|
ALbufferlistitem *tmpiter{BufferListItem};
|
|
|
|
ALsizei pos{DataPosInt};
|
2010-11-25 11:16:19 -08:00
|
|
|
|
2017-12-16 08:58:46 -08:00
|
|
|
while(tmpiter && SrcBufferSize > FilledAmt)
|
2010-11-25 11:16:19 -08:00
|
|
|
{
|
2018-11-25 13:19:01 -08:00
|
|
|
if(pos >= tmpiter->max_samples)
|
|
|
|
{
|
|
|
|
pos -= tmpiter->max_samples;
|
|
|
|
tmpiter = tmpiter->next.load(std::memory_order_acquire);
|
|
|
|
if(!tmpiter) tmpiter = BufferLoopItem;
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-16 11:34:52 -08:00
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
const ALsizei SizeToDo{SrcBufferSize - FilledAmt};
|
2018-12-20 07:10:09 -08:00
|
|
|
auto load_buffer = [pos,&SrcData,NumChannels,SampleSize,chan,FilledAmt,SizeToDo](ALsizei CompLen, const ALbuffer *buffer) -> ALsizei
|
2010-11-25 11:16:19 -08:00
|
|
|
{
|
2018-12-20 07:10:09 -08:00
|
|
|
if(!buffer) return CompLen;
|
2018-12-02 15:29:26 -08:00
|
|
|
ALsizei DataSize{buffer->SampleLen};
|
2018-12-20 07:10:09 -08:00
|
|
|
if(pos >= DataSize) return CompLen;
|
2018-09-16 17:38:55 -07:00
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
DataSize = mini(SizeToDo, DataSize - pos);
|
|
|
|
CompLen = maxi(CompLen, DataSize);
|
|
|
|
|
2018-12-02 13:35:07 -08:00
|
|
|
const ALbyte *Data{buffer->mData.data()};
|
|
|
|
Data += (pos*NumChannels + chan)*SampleSize;
|
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
LoadSamples(&SrcData[FilledAmt], Data, NumChannels,
|
2018-12-24 19:29:01 -08:00
|
|
|
buffer->mFmtType, DataSize);
|
2018-12-20 07:10:09 -08:00
|
|
|
return CompLen;
|
2018-12-02 13:35:07 -08:00
|
|
|
};
|
|
|
|
auto buffers_end = tmpiter->buffers + tmpiter->num_buffers;
|
2018-12-20 07:10:09 -08:00
|
|
|
FilledAmt += std::accumulate(tmpiter->buffers, buffers_end, ALsizei{0},
|
|
|
|
load_buffer);
|
2018-12-02 13:35:07 -08:00
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
if(SrcBufferSize <= FilledAmt)
|
|
|
|
break;
|
|
|
|
pos = 0;
|
|
|
|
tmpiter = tmpiter->next.load(std::memory_order_acquire);
|
2018-09-16 17:38:55 -07:00
|
|
|
if(!tmpiter) tmpiter = BufferLoopItem;
|
2010-11-25 11:16:19 -08:00
|
|
|
}
|
2010-09-22 09:38:24 -07:00
|
|
|
}
|
|
|
|
|
2015-10-15 07:29:25 -07:00
|
|
|
/* Store the last source samples used for next time. */
|
2018-11-24 19:16:21 -08:00
|
|
|
std::copy_n(&SrcData[(increment*DstBufferSize + DataPosFrac)>>FRACTIONBITS],
|
2018-11-30 21:23:43 -08:00
|
|
|
voice->PrevSamples[chan].size(), std::begin(voice->PrevSamples[chan]));
|
2015-10-15 07:29:25 -07:00
|
|
|
|
2012-09-26 17:16:25 -07:00
|
|
|
/* Now resample, then filter and mix to the appropriate outputs. */
|
2018-11-24 19:16:21 -08:00
|
|
|
const ALfloat *ResampledData{Resample(&voice->ResampleState,
|
2018-01-09 23:55:59 -08:00
|
|
|
&SrcData[MAX_RESAMPLE_PADDING], DataPosFrac, increment,
|
2018-01-09 22:01:46 -08:00
|
|
|
Device->TempBuffer[RESAMPLED_BUF], DstBufferSize
|
2018-11-24 19:16:21 -08:00
|
|
|
)};
|
2012-09-26 17:16:25 -07:00
|
|
|
{
|
2018-12-25 19:54:14 -08:00
|
|
|
DirectParams &parms = voice->Direct.Params[chan];
|
|
|
|
const ALfloat *samples{DoFilters(&parms.LowPass, &parms.HighPass,
|
2018-11-25 13:19:01 -08:00
|
|
|
Device->TempBuffer[FILTERED_BUF], ResampledData, DstBufferSize,
|
|
|
|
voice->Direct.FilterType
|
|
|
|
)};
|
2012-09-14 04:13:18 -07:00
|
|
|
|
2017-05-02 04:25:08 -07:00
|
|
|
if(!(voice->Flags&VOICE_HAS_HRTF))
|
2016-02-14 01:22:01 -08:00
|
|
|
{
|
2017-03-10 04:35:32 -08:00
|
|
|
if(!(voice->Flags&VOICE_HAS_NFC))
|
|
|
|
MixSamples(samples, voice->Direct.Channels, voice->Direct.Buffer,
|
2018-12-25 19:54:14 -08:00
|
|
|
parms.Gains.Current, parms.Gains.Target, Counter, OutPos,
|
|
|
|
DstBufferSize);
|
2017-03-10 04:35:32 -08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
MixSamples(samples,
|
|
|
|
voice->Direct.ChannelsPerOrder[0], voice->Direct.Buffer,
|
2018-12-25 19:54:14 -08:00
|
|
|
parms.Gains.Current, parms.Gains.Target, Counter, OutPos,
|
|
|
|
DstBufferSize);
|
2018-12-01 23:25:34 -08:00
|
|
|
|
|
|
|
ALfloat *nfcsamples{Device->TempBuffer[NFC_DATA_BUF]};
|
|
|
|
ALsizei chanoffset{voice->Direct.ChannelsPerOrder[0]};
|
2018-12-05 15:20:52 -08:00
|
|
|
using FilterProc = void (NfcFilter::*)(float*,const float*,int);
|
2018-12-25 19:54:14 -08:00
|
|
|
auto apply_nfc = [voice,&parms,samples,DstBufferSize,Counter,OutPos,&chanoffset,nfcsamples](FilterProc process, ALsizei order) -> void
|
2018-12-01 23:25:34 -08:00
|
|
|
{
|
|
|
|
if(voice->Direct.ChannelsPerOrder[order] < 1)
|
|
|
|
return;
|
2018-12-25 19:54:14 -08:00
|
|
|
(parms.NFCtrlFilter.*process)(nfcsamples, samples, DstBufferSize);
|
2018-12-01 23:25:34 -08:00
|
|
|
MixSamples(nfcsamples, voice->Direct.ChannelsPerOrder[order],
|
2018-12-25 19:54:14 -08:00
|
|
|
voice->Direct.Buffer+chanoffset, parms.Gains.Current+chanoffset,
|
|
|
|
parms.Gains.Target+chanoffset, Counter, OutPos, DstBufferSize);
|
2018-12-01 23:25:34 -08:00
|
|
|
chanoffset += voice->Direct.ChannelsPerOrder[order];
|
|
|
|
};
|
2018-12-05 15:20:52 -08:00
|
|
|
apply_nfc(&NfcFilter::process1, 1);
|
|
|
|
apply_nfc(&NfcFilter::process2, 2);
|
|
|
|
apply_nfc(&NfcFilter::process3, 3);
|
2017-03-10 04:35:32 -08:00
|
|
|
}
|
2016-02-14 01:22:01 -08:00
|
|
|
}
|
2014-11-23 10:49:54 -08:00
|
|
|
else
|
2016-02-14 03:23:06 -08:00
|
|
|
{
|
2018-12-20 07:10:09 -08:00
|
|
|
ALsizei fademix{0};
|
2018-12-25 19:54:14 -08:00
|
|
|
/* If fading, the old gain is not silence, and this is the
|
|
|
|
* first mixing pass, fade between the IRs.
|
|
|
|
*/
|
|
|
|
if(Counter && (parms.Hrtf.Old.Gain > GAIN_SILENCE_THRESHOLD) && OutPos == 0)
|
2016-02-14 03:23:06 -08:00
|
|
|
{
|
2017-05-05 03:10:58 -07:00
|
|
|
fademix = mini(DstBufferSize, 128);
|
2016-03-11 20:59:12 -08:00
|
|
|
|
2017-03-11 18:04:06 -08:00
|
|
|
/* The new coefficients need to fade in completely
|
|
|
|
* since they're replacing the old ones. To keep the
|
2017-04-28 10:05:57 -07:00
|
|
|
* gain fading consistent, interpolate between the old
|
|
|
|
* and new target gains given how much of the fade time
|
|
|
|
* this mix handles.
|
2017-03-11 18:04:06 -08:00
|
|
|
*/
|
2018-12-25 19:54:14 -08:00
|
|
|
ALfloat gain{lerp(parms.Hrtf.Old.Gain, parms.Hrtf.Target.Gain,
|
2019-01-08 19:42:44 +01:00
|
|
|
minf(1.0f, static_cast<ALfloat>(fademix))/Counter)};
|
2018-12-20 07:10:09 -08:00
|
|
|
MixHrtfParams hrtfparams;
|
2018-12-26 15:35:05 -08:00
|
|
|
hrtfparams.Coeffs = &parms.Hrtf.Target.Coeffs;
|
2018-12-25 19:54:14 -08:00
|
|
|
hrtfparams.Delay[0] = parms.Hrtf.Target.Delay[0];
|
|
|
|
hrtfparams.Delay[1] = parms.Hrtf.Target.Delay[1];
|
2017-03-11 18:04:06 -08:00
|
|
|
hrtfparams.Gain = 0.0f;
|
2019-01-08 19:42:44 +01:00
|
|
|
hrtfparams.GainStep = gain / static_cast<ALfloat>(fademix);
|
2017-05-03 03:29:21 -07:00
|
|
|
|
|
|
|
MixHrtfBlendSamples(
|
2018-12-25 19:54:14 -08:00
|
|
|
voice->Direct.Buffer[OutLIdx], voice->Direct.Buffer[OutRIdx],
|
|
|
|
samples, voice->Offset, OutPos, IrSize, &parms.Hrtf.Old,
|
|
|
|
&hrtfparams, &parms.Hrtf.State, fademix);
|
2017-03-11 18:04:06 -08:00
|
|
|
/* Update the old parameters with the result. */
|
2018-12-25 19:54:14 -08:00
|
|
|
parms.Hrtf.Old = parms.Hrtf.Target;
|
2017-04-28 10:05:57 -07:00
|
|
|
if(fademix < Counter)
|
2018-12-25 19:54:14 -08:00
|
|
|
parms.Hrtf.Old.Gain = hrtfparams.Gain;
|
2017-03-11 18:04:06 -08:00
|
|
|
}
|
2017-04-28 10:05:57 -07:00
|
|
|
|
|
|
|
if(fademix < DstBufferSize)
|
|
|
|
{
|
2018-12-20 07:10:09 -08:00
|
|
|
const ALsizei todo{DstBufferSize - fademix};
|
2018-12-25 19:54:14 -08:00
|
|
|
ALfloat gain{parms.Hrtf.Target.Gain};
|
2017-04-28 10:05:57 -07:00
|
|
|
|
|
|
|
/* Interpolate the target gain if the gain fading lasts
|
|
|
|
* longer than this mix.
|
|
|
|
*/
|
|
|
|
if(Counter > DstBufferSize)
|
2018-12-25 19:54:14 -08:00
|
|
|
gain = lerp(parms.Hrtf.Old.Gain, gain,
|
2019-01-08 19:42:44 +01:00
|
|
|
static_cast<ALfloat>(todo)/(Counter-fademix));
|
2017-04-28 10:05:57 -07:00
|
|
|
|
2018-12-20 07:10:09 -08:00
|
|
|
MixHrtfParams hrtfparams;
|
2018-12-26 15:35:05 -08:00
|
|
|
hrtfparams.Coeffs = &parms.Hrtf.Target.Coeffs;
|
2018-12-25 19:54:14 -08:00
|
|
|
hrtfparams.Delay[0] = parms.Hrtf.Target.Delay[0];
|
|
|
|
hrtfparams.Delay[1] = parms.Hrtf.Target.Delay[1];
|
|
|
|
hrtfparams.Gain = parms.Hrtf.Old.Gain;
|
2019-01-08 19:42:44 +01:00
|
|
|
hrtfparams.GainStep = (gain - parms.Hrtf.Old.Gain) / static_cast<ALfloat>(todo);
|
2017-04-28 10:05:57 -07:00
|
|
|
MixHrtfSamples(
|
2018-12-25 19:54:14 -08:00
|
|
|
voice->Direct.Buffer[OutLIdx], voice->Direct.Buffer[OutRIdx],
|
2017-04-28 10:05:57 -07:00
|
|
|
samples+fademix, voice->Offset+fademix, OutPos+fademix, IrSize,
|
2018-12-25 19:54:14 -08:00
|
|
|
&hrtfparams, &parms.Hrtf.State, todo);
|
2017-04-28 10:05:57 -07:00
|
|
|
/* Store the interpolated gain or the final target gain
|
|
|
|
* depending if the fade is done.
|
|
|
|
*/
|
|
|
|
if(DstBufferSize < Counter)
|
2018-12-25 19:54:14 -08:00
|
|
|
parms.Hrtf.Old.Gain = gain;
|
2017-04-28 10:05:57 -07:00
|
|
|
else
|
2018-12-25 19:54:14 -08:00
|
|
|
parms.Hrtf.Old.Gain = parms.Hrtf.Target.Gain;
|
2017-04-28 10:05:57 -07:00
|
|
|
}
|
2016-02-14 03:23:06 -08:00
|
|
|
}
|
2012-09-26 17:16:25 -07:00
|
|
|
}
|
2012-09-11 05:24:19 -07:00
|
|
|
|
2018-12-02 13:35:07 -08:00
|
|
|
ALfloat (&FilterBuf)[BUFFERSIZE] = Device->TempBuffer[FILTERED_BUF];
|
|
|
|
auto mix_send = [Counter,OutPos,DstBufferSize,chan,ResampledData,&FilterBuf](ALvoice::SendData &send) -> void
|
2012-09-08 21:34:36 -07:00
|
|
|
{
|
2018-12-02 13:35:07 -08:00
|
|
|
if(!send.Buffer)
|
|
|
|
return;
|
2012-09-11 05:24:19 -07:00
|
|
|
|
2018-12-25 19:54:14 -08:00
|
|
|
SendParams &parms = send.Params[chan];
|
|
|
|
const ALfloat *samples{DoFilters(&parms.LowPass, &parms.HighPass,
|
|
|
|
FilterBuf, ResampledData, DstBufferSize, send.FilterType)};
|
2016-02-14 01:22:01 -08:00
|
|
|
|
2018-12-25 19:54:14 -08:00
|
|
|
MixSamples(samples, send.Channels, send.Buffer, parms.Gains.Current,
|
|
|
|
parms.Gains.Target, Counter, OutPos, DstBufferSize);
|
2018-12-02 13:35:07 -08:00
|
|
|
};
|
2018-12-25 19:54:14 -08:00
|
|
|
std::for_each(voice->Send, voice->Send+NumAuxSends, mix_send);
|
2012-04-27 23:46:51 -07:00
|
|
|
}
|
2012-09-26 17:16:25 -07:00
|
|
|
/* Update positions */
|
2014-06-02 19:19:22 -07:00
|
|
|
DataPosFrac += increment*DstBufferSize;
|
|
|
|
DataPosInt += DataPosFrac>>FRACTIONBITS;
|
|
|
|
DataPosFrac &= FRACTIONMASK;
|
|
|
|
|
2012-09-26 17:16:25 -07:00
|
|
|
OutPos += DstBufferSize;
|
2014-08-21 03:24:48 -07:00
|
|
|
voice->Offset += DstBufferSize;
|
2017-01-18 07:13:23 -08:00
|
|
|
Counter = maxi(DstBufferSize, Counter) - DstBufferSize;
|
2010-08-03 23:19:36 -07:00
|
|
|
|
2018-02-01 21:07:55 -08:00
|
|
|
if(isstatic)
|
2010-08-03 23:19:36 -07:00
|
|
|
{
|
2018-02-01 21:07:55 -08:00
|
|
|
if(BufferLoopItem)
|
2010-11-25 11:16:19 -08:00
|
|
|
{
|
2018-02-01 21:07:55 -08:00
|
|
|
/* Handle looping static source */
|
2018-11-25 13:19:01 -08:00
|
|
|
const ALbuffer *Buffer{BufferListItem->buffers[0]};
|
|
|
|
ALsizei LoopStart{Buffer->LoopStart};
|
|
|
|
ALsizei LoopEnd{Buffer->LoopEnd};
|
2018-02-01 21:07:55 -08:00
|
|
|
if(DataPosInt >= LoopEnd)
|
|
|
|
{
|
|
|
|
assert(LoopEnd > LoopStart);
|
|
|
|
DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Handle non-looping static source */
|
2018-03-27 08:27:16 -07:00
|
|
|
if(DataPosInt >= BufferListItem->max_samples)
|
2018-02-01 21:07:55 -08:00
|
|
|
{
|
|
|
|
isplaying = false;
|
2019-01-07 12:37:13 +01:00
|
|
|
BufferListItem = nullptr;
|
2018-02-01 21:07:55 -08:00
|
|
|
DataPosInt = 0;
|
|
|
|
DataPosFrac = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-06 01:07:37 -08:00
|
|
|
}
|
2017-12-16 11:34:52 -08:00
|
|
|
}
|
|
|
|
else while(1)
|
|
|
|
{
|
2018-02-01 21:07:55 -08:00
|
|
|
/* Handle streaming source */
|
2018-03-27 08:27:16 -07:00
|
|
|
if(BufferListItem->max_samples > DataPosInt)
|
2011-02-06 01:07:37 -08:00
|
|
|
break;
|
|
|
|
|
2018-09-16 17:38:55 -07:00
|
|
|
DataPosInt -= BufferListItem->max_samples;
|
|
|
|
|
2018-02-25 09:51:07 -08:00
|
|
|
buffers_done += BufferListItem->num_buffers;
|
2018-11-26 18:19:58 -08:00
|
|
|
BufferListItem = BufferListItem->next.load(std::memory_order_relaxed);
|
2018-02-01 01:36:03 -08:00
|
|
|
if(!BufferListItem && !(BufferListItem=BufferLoopItem))
|
2010-08-03 23:19:36 -07:00
|
|
|
{
|
2018-02-01 01:36:03 -08:00
|
|
|
isplaying = false;
|
|
|
|
DataPosInt = 0;
|
|
|
|
DataPosFrac = 0;
|
|
|
|
break;
|
2010-08-03 23:19:36 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-20 16:53:41 -07:00
|
|
|
} while(isplaying && OutPos < SamplesToDo);
|
2010-08-03 23:19:36 -07:00
|
|
|
|
2017-05-02 04:09:01 -07:00
|
|
|
voice->Flags |= VOICE_IS_FADING;
|
2016-02-14 01:22:01 -08:00
|
|
|
|
2010-08-03 23:19:36 -07:00
|
|
|
/* Update source info */
|
2018-11-25 13:19:01 -08:00
|
|
|
voice->position.store(DataPosInt, std::memory_order_relaxed);
|
|
|
|
voice->position_fraction.store(DataPosFrac, std::memory_order_relaxed);
|
|
|
|
voice->current_buffer.store(BufferListItem, std::memory_order_release);
|
2018-02-01 01:36:03 -08:00
|
|
|
|
2018-02-01 21:07:55 -08:00
|
|
|
/* Send any events now, after the position/buffer info was updated. */
|
2018-11-25 13:19:01 -08:00
|
|
|
ALbitfieldSOFT enabledevt{Context->EnabledEvts.load(std::memory_order_acquire)};
|
2018-02-01 01:36:03 -08:00
|
|
|
if(buffers_done > 0 && (enabledevt&EventType_BufferCompleted))
|
2018-12-04 16:33:26 -08:00
|
|
|
{
|
2018-12-27 10:44:02 -08:00
|
|
|
RingBuffer *ring{Context->AsyncEvents.get()};
|
|
|
|
auto evt_vec = ring->getWriteVector();
|
|
|
|
if(evt_vec.first.len > 0)
|
2018-12-25 09:32:38 -08:00
|
|
|
{
|
2018-12-27 10:44:02 -08:00
|
|
|
AsyncEvent *evt{new (evt_vec.first.buf) AsyncEvent{EventType_BufferCompleted}};
|
2018-12-25 09:32:38 -08:00
|
|
|
evt->u.bufcomp.id = SourceID;
|
|
|
|
evt->u.bufcomp.count = buffers_done;
|
2018-12-26 21:22:17 -08:00
|
|
|
ring->writeAdvance(1);
|
2018-12-04 16:33:26 -08:00
|
|
|
Context->EventSem.post();
|
2018-12-25 09:32:38 -08:00
|
|
|
}
|
2018-12-04 16:33:26 -08:00
|
|
|
}
|
2018-02-01 01:36:03 -08:00
|
|
|
|
2017-03-20 16:53:41 -07:00
|
|
|
return isplaying;
|
2010-08-03 23:19:36 -07:00
|
|
|
}
|