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-10-02 16:53:23 -07:00
|
|
|
#include "voice.h"
|
|
|
|
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
|
|
|
#include <cassert>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <new>
|
|
|
|
#include <utility>
|
2018-12-20 07:10:09 -08:00
|
|
|
|
2019-07-29 17:54:07 -07:00
|
|
|
#include "al/buffer.h"
|
|
|
|
#include "al/event.h"
|
|
|
|
#include "al/source.h"
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "albyte.h"
|
2018-01-11 07:56:54 -08:00
|
|
|
#include "alconfig.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "alcontext.h"
|
|
|
|
#include "alnumeric.h"
|
|
|
|
#include "aloptional.h"
|
|
|
|
#include "alspan.h"
|
2019-09-16 15:10:36 -07:00
|
|
|
#include "alstring.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "alu.h"
|
2020-11-27 19:18:17 -08:00
|
|
|
#include "core/devformat.h"
|
2018-01-11 07:19:19 -08:00
|
|
|
#include "cpu_caps.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "filters/biquad.h"
|
|
|
|
#include "filters/nfc.h"
|
|
|
|
#include "filters/splitter.h"
|
2020-08-26 21:29:16 -07:00
|
|
|
#include "fmt_traits.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "hrtf.h"
|
|
|
|
#include "inprogext.h"
|
|
|
|
#include "logging.h"
|
2018-03-22 05:06:15 -07:00
|
|
|
#include "mixer/defs.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "opthelpers.h"
|
|
|
|
#include "ringbuffer.h"
|
|
|
|
#include "threads.h"
|
|
|
|
#include "vector.h"
|
2019-05-26 19:41:11 -07:00
|
|
|
|
2020-04-03 03:52:23 -07:00
|
|
|
struct CTag;
|
|
|
|
#ifdef HAVE_SSE
|
|
|
|
struct SSETag;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
struct NEONTag;
|
|
|
|
#endif
|
|
|
|
struct CopyTag;
|
|
|
|
|
2011-05-06 00:20:40 -07:00
|
|
|
|
2019-09-22 21:19:19 -07:00
|
|
|
Resampler ResamplerDefault{Resampler::Linear};
|
2017-04-20 23:21:46 -07:00
|
|
|
|
2020-03-25 22:24:09 -07:00
|
|
|
MixerFunc MixSamples{Mix_<CTag>};
|
|
|
|
|
2019-09-19 21:01:10 -07:00
|
|
|
namespace {
|
|
|
|
|
2020-11-20 03:59:14 -08:00
|
|
|
using HrtfMixerFunc = void(*)(const float *InSamples, float2 *AccumSamples, const uint IrSize,
|
2020-01-05 17:12:48 -08:00
|
|
|
const MixHrtfFilter *hrtfparams, const size_t BufferSize);
|
2020-04-08 06:17:04 -07:00
|
|
|
using HrtfMixerBlendFunc = void(*)(const float *InSamples, float2 *AccumSamples,
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint IrSize, const HrtfFilter *oldparams, const MixHrtfFilter *newparams,
|
2019-11-03 00:30:33 -07:00
|
|
|
const size_t BufferSize);
|
2019-10-02 15:37:33 -07:00
|
|
|
|
2020-03-25 22:24:09 -07:00
|
|
|
HrtfMixerFunc MixHrtfSamples{MixHrtf_<CTag>};
|
|
|
|
HrtfMixerBlendFunc MixHrtfBlendSamples{MixHrtfBlend_<CTag>};
|
|
|
|
|
|
|
|
inline MixerFunc SelectMixer()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
|
|
|
return Mix_<NEONTag>;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
return Mix_<SSETag>;
|
|
|
|
#endif
|
|
|
|
return Mix_<CTag>;
|
|
|
|
}
|
|
|
|
|
2019-09-19 21:01:10 -07:00
|
|
|
inline HrtfMixerFunc SelectHrtfMixer()
|
2014-06-13 16:07:25 -07:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
2019-01-23 12:23:05 -08:00
|
|
|
return MixHrtf_<NEONTag>;
|
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))
|
2019-01-23 12:23:05 -08:00
|
|
|
return MixHrtf_<SSETag>;
|
2017-03-14 19:16:59 -07:00
|
|
|
#endif
|
2019-01-23 12:23:05 -08:00
|
|
|
return MixHrtf_<CTag>;
|
2014-06-13 16:07:25 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 21:01:10 -07:00
|
|
|
inline HrtfMixerBlendFunc SelectHrtfBlendMixer()
|
2017-05-03 03:29:21 -07:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NEON
|
|
|
|
if((CPUCapFlags&CPU_CAP_NEON))
|
2019-01-23 12:23:05 -08:00
|
|
|
return MixHrtfBlend_<NEONTag>;
|
2017-05-03 03:29:21 -07:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
2019-01-23 12:23:05 -08:00
|
|
|
return MixHrtfBlend_<SSETag>;
|
2017-05-03 03:29:21 -07:00
|
|
|
#endif
|
2019-01-23 12:23:05 -08:00
|
|
|
return MixHrtfBlend_<CTag>;
|
2017-05-03 03:29:21 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 21:01:10 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
void aluInitMixer()
|
2015-09-27 20:55:39 -07:00
|
|
|
{
|
2019-06-30 16:15:15 -07:00
|
|
|
if(auto resopt = ConfigValueStr(nullptr, nullptr, "resampler"))
|
2015-10-01 00:34:13 -07:00
|
|
|
{
|
2019-09-21 12:08:45 -07:00
|
|
|
struct ResamplerEntry {
|
2019-09-28 01:58:29 -07:00
|
|
|
const char name[16];
|
2019-09-21 12:08:45 -07:00
|
|
|
const Resampler resampler;
|
|
|
|
};
|
|
|
|
constexpr ResamplerEntry ResamplerList[]{
|
2019-09-22 21:19:19 -07:00
|
|
|
{ "none", Resampler::Point },
|
|
|
|
{ "point", Resampler::Point },
|
2020-09-24 14:21:57 -07:00
|
|
|
{ "linear", Resampler::Linear },
|
2019-09-22 21:19:19 -07:00
|
|
|
{ "cubic", Resampler::Cubic },
|
|
|
|
{ "bsinc12", Resampler::BSinc12 },
|
2019-09-28 01:58:29 -07:00
|
|
|
{ "fast_bsinc12", Resampler::FastBSinc12 },
|
2019-09-22 21:19:19 -07:00
|
|
|
{ "bsinc24", Resampler::BSinc24 },
|
2019-09-28 01:58:29 -07:00
|
|
|
{ "fast_bsinc24", Resampler::FastBSinc24 },
|
2019-09-21 12:08:45 -07:00
|
|
|
};
|
|
|
|
|
2019-06-30 16:15:15 -07:00
|
|
|
const char *str{resopt->c_str()};
|
2019-09-21 12:08:45 -07:00
|
|
|
if(al::strcasecmp(str, "bsinc") == 0)
|
2017-08-27 10:16:36 -07:00
|
|
|
{
|
|
|
|
WARN("Resampler option \"%s\" is deprecated, using bsinc12\n", str);
|
2019-09-21 12:08:45 -07:00
|
|
|
str = "bsinc12";
|
2017-08-27 10:16:36 -07:00
|
|
|
}
|
2019-09-16 15:10:36 -07:00
|
|
|
else if(al::strcasecmp(str, "sinc4") == 0 || al::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);
|
2019-09-21 12:08:45 -07:00
|
|
|
str = "cubic";
|
2015-10-01 00:34:13 -07:00
|
|
|
}
|
2019-09-21 12:08:45 -07:00
|
|
|
|
|
|
|
auto iter = std::find_if(std::begin(ResamplerList), std::end(ResamplerList),
|
|
|
|
[str](const ResamplerEntry &entry) -> bool
|
|
|
|
{ return al::strcasecmp(str, entry.name) == 0; });
|
|
|
|
if(iter == std::end(ResamplerList))
|
2019-08-04 17:29:55 -07:00
|
|
|
ERR("Invalid resampler: %s\n", str);
|
2019-09-21 12:08:45 -07:00
|
|
|
else
|
|
|
|
ResamplerDefault = iter->resampler;
|
2015-10-01 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
2020-03-25 22:24:09 -07:00
|
|
|
MixSamples = SelectMixer();
|
2017-05-03 03:29:21 -07:00
|
|
|
MixHrtfBlendSamples = SelectHrtfBlendMixer();
|
2015-09-27 20:55:39 -07:00
|
|
|
MixHrtfSamples = SelectHrtfMixer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
namespace {
|
|
|
|
|
2020-11-20 01:37:19 -08:00
|
|
|
void SendSourceStoppedEvent(ALCcontext *context, uint id)
|
2019-03-09 16:48:07 -08:00
|
|
|
{
|
2019-07-30 09:05:54 -07:00
|
|
|
RingBuffer *ring{context->mAsyncEvents.get()};
|
2019-03-09 16:48:07 -08:00
|
|
|
auto evt_vec = ring->getWriteVector();
|
|
|
|
if(evt_vec.first.len < 1) return;
|
|
|
|
|
2020-04-24 07:04:32 -07:00
|
|
|
AsyncEvent *evt{::new(evt_vec.first.buf) AsyncEvent{EventType_SourceStateChange}};
|
2019-03-09 16:48:07 -08:00
|
|
|
evt->u.srcstate.id = id;
|
|
|
|
evt->u.srcstate.state = AL_STOPPED;
|
|
|
|
|
|
|
|
ring->writeAdvance(1);
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:19:24 -07:00
|
|
|
|
2020-04-23 22:13:52 -07:00
|
|
|
const float *DoFilters(BiquadFilter &lpfilter, BiquadFilter &hpfilter, float *dst,
|
2019-12-25 18:39:22 -08:00
|
|
|
const al::span<const float> src, int type)
|
2019-04-12 19:19:24 -07:00
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
2019-12-25 18:39:22 -08:00
|
|
|
case AF_None:
|
2020-04-23 22:13:52 -07:00
|
|
|
lpfilter.clear();
|
|
|
|
hpfilter.clear();
|
2019-12-25 18:39:22 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_LowPass:
|
2020-04-23 22:13:52 -07:00
|
|
|
lpfilter.process(src, dst);
|
|
|
|
hpfilter.clear();
|
2019-12-25 18:39:22 -08:00
|
|
|
return dst;
|
|
|
|
case AF_HighPass:
|
2020-04-23 22:13:52 -07:00
|
|
|
lpfilter.clear();
|
|
|
|
hpfilter.process(src, dst);
|
2019-12-25 18:39:22 -08:00
|
|
|
return dst;
|
|
|
|
|
|
|
|
case AF_BandPass:
|
2020-04-23 22:13:52 -07:00
|
|
|
DualBiquad{lpfilter, hpfilter}.process(src, dst);
|
2019-12-25 18:39:22 -08:00
|
|
|
return dst;
|
2019-04-12 19:19:24 -07:00
|
|
|
}
|
2019-12-25 18:39:22 -08:00
|
|
|
return src.data();
|
2019-04-12 19:19:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 06:17:04 -07:00
|
|
|
void LoadSamples(float *RESTRICT dst, const al::byte *src, const size_t srcstep, FmtType srctype,
|
2019-09-14 16:55:28 -07:00
|
|
|
const size_t samples) noexcept
|
2011-10-04 09:47:08 -07:00
|
|
|
{
|
2020-08-26 21:29:16 -07:00
|
|
|
#define HANDLE_FMT(T) case T: al::LoadSampleArray<T>(dst, src, srcstep, samples); break
|
2011-10-04 09:47:08 -07:00
|
|
|
switch(srctype)
|
|
|
|
{
|
2020-08-26 21:29:16 -07: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
|
|
|
}
|
|
|
|
|
2020-11-19 04:18:25 -08:00
|
|
|
float *LoadBufferStatic(BufferlistItem *BufferListItem, BufferlistItem *&BufferLoopItem,
|
2019-09-11 14:33:26 -07:00
|
|
|
const size_t NumChannels, const size_t SampleSize, const size_t chan, size_t DataPosInt,
|
2020-04-08 06:17:04 -07:00
|
|
|
al::span<float> SrcBuffer)
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
2020-11-19 04:18:25 -08:00
|
|
|
const BufferStorage &Buffer = *BufferListItem->mBuffer;
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint LoopStart{Buffer.mLoopStart};
|
|
|
|
const uint LoopEnd{Buffer.mLoopEnd};
|
2019-03-09 21:32:14 -08:00
|
|
|
ASSUME(LoopEnd > LoopStart);
|
|
|
|
|
|
|
|
/* If current pos is beyond the loop range, do not loop */
|
|
|
|
if(!BufferLoopItem || DataPosInt >= LoopEnd)
|
|
|
|
{
|
|
|
|
BufferLoopItem = nullptr;
|
|
|
|
|
2019-08-11 03:34:35 -07:00
|
|
|
/* Load what's left to play from the buffer */
|
2020-11-19 03:23:46 -08:00
|
|
|
const size_t DataRem{minz(SrcBuffer.size(), Buffer.mSampleLen-DataPosInt)};
|
2019-08-11 03:34:35 -07:00
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
const al::byte *Data{Buffer.mData.data()};
|
2019-08-11 03:34:35 -07:00
|
|
|
Data += (DataPosInt*NumChannels + chan)*SampleSize;
|
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
LoadSamples(SrcBuffer.data(), Data, NumChannels, Buffer.mType, DataRem);
|
2019-09-14 18:27:57 -07:00
|
|
|
SrcBuffer = SrcBuffer.subspan(DataRem);
|
2019-03-09 21:32:14 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-11 03:34:35 -07:00
|
|
|
/* Load what's left of this loop iteration */
|
2019-09-14 18:27:57 -07:00
|
|
|
const size_t DataRem{minz(SrcBuffer.size(), LoopEnd-DataPosInt)};
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
const al::byte *Data{Buffer.mData.data()};
|
2019-08-11 03:34:35 -07:00
|
|
|
Data += (DataPosInt*NumChannels + chan)*SampleSize;
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
LoadSamples(SrcBuffer.data(), Data, NumChannels, Buffer.mType, DataRem);
|
2019-09-14 18:27:57 -07:00
|
|
|
SrcBuffer = SrcBuffer.subspan(DataRem);
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2019-08-11 03:34:35 -07:00
|
|
|
/* Load any repeats of the loop we can to fill the buffer. */
|
2019-06-25 17:10:46 -07:00
|
|
|
const auto LoopSize = static_cast<size_t>(LoopEnd - LoopStart);
|
|
|
|
while(!SrcBuffer.empty())
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
2019-08-11 03:34:35 -07:00
|
|
|
const size_t DataSize{minz(SrcBuffer.size(), LoopSize)};
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
Data = Buffer.mData.data() + (LoopStart*NumChannels + chan)*SampleSize;
|
2019-04-12 16:24:11 -07:00
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
LoadSamples(SrcBuffer.data(), Data, NumChannels, Buffer.mType, DataSize);
|
2019-08-11 03:34:35 -07:00
|
|
|
SrcBuffer = SrcBuffer.subspan(DataSize);
|
2019-03-09 21:32:14 -08:00
|
|
|
}
|
|
|
|
}
|
2019-06-25 17:10:46 -07:00
|
|
|
return SrcBuffer.begin();
|
2019-03-09 21:32:14 -08:00
|
|
|
}
|
|
|
|
|
2020-11-19 04:18:25 -08:00
|
|
|
float *LoadBufferCallback(BufferlistItem *BufferListItem, const size_t NumChannels,
|
2020-02-17 17:13:00 -08:00
|
|
|
const size_t SampleSize, const size_t chan, size_t NumCallbackSamples,
|
2020-04-08 06:17:04 -07:00
|
|
|
al::span<float> SrcBuffer)
|
2020-02-17 17:13:00 -08:00
|
|
|
{
|
2020-11-19 04:18:25 -08:00
|
|
|
const BufferStorage &Buffer = *BufferListItem->mBuffer;
|
2020-02-17 17:13:00 -08:00
|
|
|
|
|
|
|
/* Load what's left to play from the buffer */
|
|
|
|
const size_t DataRem{minz(SrcBuffer.size(), NumCallbackSamples)};
|
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
const al::byte *Data{Buffer.mData.data() + chan*SampleSize};
|
2020-02-17 17:13:00 -08:00
|
|
|
|
2020-11-19 03:23:46 -08:00
|
|
|
LoadSamples(SrcBuffer.data(), Data, NumChannels, Buffer.mType, DataRem);
|
2020-02-17 17:13:00 -08:00
|
|
|
SrcBuffer = SrcBuffer.subspan(DataRem);
|
|
|
|
|
|
|
|
return SrcBuffer.begin();
|
|
|
|
}
|
|
|
|
|
2020-11-19 04:18:25 -08:00
|
|
|
float *LoadBufferQueue(BufferlistItem *BufferListItem, BufferlistItem *BufferLoopItem,
|
2019-09-11 14:33:26 -07:00
|
|
|
const size_t NumChannels, const size_t SampleSize, const size_t chan, size_t DataPosInt,
|
2020-04-08 06:17:04 -07:00
|
|
|
al::span<float> SrcBuffer)
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
|
|
|
/* Crawl the buffer queue to fill in the temp buffer */
|
2019-06-25 17:10:46 -07:00
|
|
|
while(BufferListItem && !SrcBuffer.empty())
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
2020-11-19 04:18:25 -08:00
|
|
|
BufferStorage *Buffer{BufferListItem->mBuffer};
|
|
|
|
if(!(Buffer && DataPosInt < Buffer->mSampleLen))
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
2020-11-19 04:18:25 -08:00
|
|
|
if(Buffer) DataPosInt -= Buffer->mSampleLen;
|
2019-07-31 09:20:53 -07:00
|
|
|
BufferListItem = BufferListItem->mNext.load(std::memory_order_acquire);
|
2019-03-11 22:00:28 -07:00
|
|
|
if(!BufferListItem) BufferListItem = BufferLoopItem;
|
2019-03-09 21:32:14 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-19 04:18:25 -08:00
|
|
|
const size_t DataSize{minz(SrcBuffer.size(), Buffer->mSampleLen-DataPosInt)};
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2020-11-19 04:18:25 -08:00
|
|
|
const al::byte *Data{Buffer->mData.data()};
|
2019-08-11 03:34:35 -07:00
|
|
|
Data += (DataPosInt*NumChannels + chan)*SampleSize;
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2020-11-19 04:18:25 -08:00
|
|
|
LoadSamples(SrcBuffer.data(), Data, NumChannels, Buffer->mType, DataSize);
|
2019-08-11 03:34:35 -07:00
|
|
|
SrcBuffer = SrcBuffer.subspan(DataSize);
|
|
|
|
if(SrcBuffer.empty()) break;
|
2019-03-09 21:32:14 -08:00
|
|
|
|
2019-03-11 22:00:28 -07:00
|
|
|
DataPosInt = 0;
|
2019-07-31 09:20:53 -07:00
|
|
|
BufferListItem = BufferListItem->mNext.load(std::memory_order_acquire);
|
2019-03-11 22:00:28 -07:00
|
|
|
if(!BufferListItem) BufferListItem = BufferLoopItem;
|
2019-03-09 21:32:14 -08:00
|
|
|
}
|
|
|
|
|
2019-06-25 17:10:46 -07:00
|
|
|
return SrcBuffer.begin();
|
2019-03-09 21:32:14 -08:00
|
|
|
}
|
|
|
|
|
2019-09-21 21:14:11 -07:00
|
|
|
|
2020-11-20 03:59:14 -08:00
|
|
|
void DoHrtfMix(const float *samples, const uint DstBufferSize, DirectParams &parms,
|
|
|
|
const float TargetGain, const uint Counter, uint OutPos, const uint IrSize,
|
2019-11-03 00:30:33 -07:00
|
|
|
ALCdevice *Device)
|
2019-09-21 21:14:11 -07:00
|
|
|
{
|
|
|
|
auto &HrtfSamples = Device->HrtfSourceData;
|
2020-05-20 11:13:06 -07:00
|
|
|
/* Source HRTF mixing needs to include the direct delay so it remains
|
|
|
|
* aligned with the direct mix's HRTF filtering.
|
|
|
|
*/
|
|
|
|
float2 *AccumSamples{Device->HrtfAccumData + HRTF_DIRECT_DELAY};
|
2019-09-21 21:14:11 -07:00
|
|
|
|
|
|
|
/* Copy the HRTF history and new input samples into a temp buffer. */
|
2020-01-13 19:45:13 -08:00
|
|
|
auto src_iter = std::copy(parms.Hrtf.History.begin(), parms.Hrtf.History.end(),
|
2019-09-21 21:14:11 -07:00
|
|
|
std::begin(HrtfSamples));
|
|
|
|
std::copy_n(samples, DstBufferSize, src_iter);
|
|
|
|
/* Copy the last used samples back into the history buffer for later. */
|
2020-01-13 19:45:13 -08:00
|
|
|
std::copy_n(std::begin(HrtfSamples) + DstBufferSize, parms.Hrtf.History.size(),
|
|
|
|
parms.Hrtf.History.begin());
|
2019-09-21 21:14:11 -07:00
|
|
|
|
2020-05-25 16:57:38 -07:00
|
|
|
/* If fading and this is the first mixing pass, fade between the IRs. */
|
2020-11-20 03:59:14 -08:00
|
|
|
uint fademix{0u};
|
2020-05-25 16:57:38 -07:00
|
|
|
if(Counter && OutPos == 0)
|
2019-09-21 21:14:11 -07:00
|
|
|
{
|
2020-05-25 16:57:38 -07:00
|
|
|
fademix = minu(DstBufferSize, Counter);
|
2019-09-21 21:14:11 -07:00
|
|
|
|
|
|
|
float gain{TargetGain};
|
|
|
|
|
|
|
|
/* The new coefficients need to fade in completely since they're
|
|
|
|
* replacing the old ones. To keep the gain fading consistent,
|
|
|
|
* interpolate between the old and new target gains given how much of
|
|
|
|
* the fade time this mix handles.
|
|
|
|
*/
|
2020-05-25 16:57:38 -07:00
|
|
|
if(Counter > fademix)
|
2019-09-21 21:14:11 -07:00
|
|
|
{
|
2020-04-08 06:17:04 -07:00
|
|
|
const float a{static_cast<float>(fademix) / static_cast<float>(Counter)};
|
2019-09-21 21:14:11 -07:00
|
|
|
gain = lerp(parms.Hrtf.Old.Gain, TargetGain, a);
|
|
|
|
}
|
|
|
|
MixHrtfFilter hrtfparams;
|
|
|
|
hrtfparams.Coeffs = &parms.Hrtf.Target.Coeffs;
|
2020-02-02 16:11:43 -08:00
|
|
|
hrtfparams.Delay = parms.Hrtf.Target.Delay;
|
2019-09-21 21:14:11 -07:00
|
|
|
hrtfparams.Gain = 0.0f;
|
|
|
|
hrtfparams.GainStep = gain / static_cast<float>(fademix);
|
|
|
|
|
2019-11-03 00:30:33 -07:00
|
|
|
MixHrtfBlendSamples(HrtfSamples, AccumSamples+OutPos, IrSize, &parms.Hrtf.Old, &hrtfparams,
|
|
|
|
fademix);
|
2019-09-21 21:14:11 -07:00
|
|
|
/* Update the old parameters with the result. */
|
|
|
|
parms.Hrtf.Old = parms.Hrtf.Target;
|
2020-01-05 17:12:48 -08:00
|
|
|
parms.Hrtf.Old.Gain = gain;
|
2019-11-03 00:30:33 -07:00
|
|
|
OutPos += fademix;
|
2019-09-21 21:14:11 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 16:57:38 -07:00
|
|
|
if(fademix < DstBufferSize)
|
2019-09-21 21:14:11 -07:00
|
|
|
{
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint todo{DstBufferSize - fademix};
|
2019-09-21 21:14:11 -07:00
|
|
|
float gain{TargetGain};
|
|
|
|
|
|
|
|
/* Interpolate the target gain if the gain fading lasts longer than
|
|
|
|
* this mix.
|
|
|
|
*/
|
|
|
|
if(Counter > DstBufferSize)
|
|
|
|
{
|
|
|
|
const float a{static_cast<float>(todo) / static_cast<float>(Counter-fademix)};
|
|
|
|
gain = lerp(parms.Hrtf.Old.Gain, TargetGain, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
MixHrtfFilter hrtfparams;
|
|
|
|
hrtfparams.Coeffs = &parms.Hrtf.Target.Coeffs;
|
2020-02-02 16:11:43 -08:00
|
|
|
hrtfparams.Delay = parms.Hrtf.Target.Delay;
|
2019-09-21 21:14:11 -07:00
|
|
|
hrtfparams.Gain = parms.Hrtf.Old.Gain;
|
|
|
|
hrtfparams.GainStep = (gain - parms.Hrtf.Old.Gain) / static_cast<float>(todo);
|
2019-11-03 00:30:33 -07:00
|
|
|
MixHrtfSamples(HrtfSamples+fademix, AccumSamples+OutPos, IrSize, &hrtfparams, todo);
|
2020-01-05 17:12:48 -08:00
|
|
|
/* Store the now-current gain for next time. */
|
|
|
|
parms.Hrtf.Old.Gain = gain;
|
2019-09-21 21:14:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 18:25:50 -08:00
|
|
|
void DoNfcMix(const al::span<const float> samples, FloatBufferLine *OutBuffer, DirectParams &parms,
|
2020-11-20 03:59:14 -08:00
|
|
|
const float *TargetGains, const uint Counter, const uint OutPos, ALCdevice *Device)
|
2019-09-21 21:14:11 -07:00
|
|
|
{
|
2020-01-05 18:25:50 -08:00
|
|
|
using FilterProc = void (NfcFilter::*)(const al::span<const float>, float*);
|
|
|
|
static constexpr FilterProc NfcProcess[MAX_AMBI_ORDER+1]{
|
|
|
|
nullptr, &NfcFilter::process1, &NfcFilter::process2, &NfcFilter::process3};
|
|
|
|
|
|
|
|
float *CurrentGains{parms.Gains.Current.data()};
|
|
|
|
MixSamples(samples, {OutBuffer, 1u}, CurrentGains, TargetGains, Counter, OutPos);
|
|
|
|
++OutBuffer;
|
|
|
|
++CurrentGains;
|
|
|
|
++TargetGains;
|
2019-09-21 21:14:11 -07:00
|
|
|
|
2019-12-24 23:36:08 -08:00
|
|
|
const al::span<float> nfcsamples{Device->NfcSampleData, samples.size()};
|
2020-01-05 18:25:50 -08:00
|
|
|
size_t order{1};
|
|
|
|
while(const size_t chancount{Device->NumChannelsPerOrder[order]})
|
2019-09-21 21:14:11 -07:00
|
|
|
{
|
2020-01-05 18:25:50 -08:00
|
|
|
(parms.NFCtrlFilter.*NfcProcess[order])(samples, nfcsamples.data());
|
|
|
|
MixSamples(nfcsamples, {OutBuffer, chancount}, CurrentGains, TargetGains, Counter, OutPos);
|
|
|
|
OutBuffer += chancount;
|
|
|
|
CurrentGains += chancount;
|
|
|
|
TargetGains += chancount;
|
|
|
|
if(++order == MAX_AMBI_ORDER+1)
|
|
|
|
break;
|
|
|
|
}
|
2019-09-21 21:14:11 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:59:02 -08:00
|
|
|
} // namespace
|
2012-09-11 05:24:19 -07:00
|
|
|
|
2020-11-20 01:37:19 -08:00
|
|
|
void Voice::mix(const State vstate, ALCcontext *Context, const uint SamplesToDo)
|
2010-09-22 09:38:24 -07:00
|
|
|
{
|
2019-10-05 21:23:31 -07:00
|
|
|
static constexpr std::array<float,MAX_OUTPUT_CHANNELS> SilentTarget{};
|
2019-03-09 18:34:00 -08:00
|
|
|
|
2018-11-25 13:19:01 -08:00
|
|
|
ASSUME(SamplesToDo > 0);
|
2010-09-22 09:38:24 -07:00
|
|
|
|
2019-03-11 20:00:14 -07:00
|
|
|
/* Get voice info */
|
2020-11-20 03:59:14 -08:00
|
|
|
uint DataPosInt{mPosition.load(std::memory_order_relaxed)};
|
|
|
|
uint DataPosFrac{mPositionFrac.load(std::memory_order_relaxed)};
|
2020-11-19 04:18:25 -08:00
|
|
|
BufferlistItem *BufferListItem{mCurrentBuffer.load(std::memory_order_relaxed)};
|
|
|
|
BufferlistItem *BufferLoopItem{mLoopBuffer.load(std::memory_order_relaxed)};
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint SampleSize{mSampleSize};
|
|
|
|
const uint increment{mStep};
|
2020-01-08 06:40:58 -08:00
|
|
|
if UNLIKELY(increment < 1)
|
|
|
|
{
|
|
|
|
/* If the voice is supposed to be stopping but can't be mixed, just
|
|
|
|
* stop it before bailing.
|
|
|
|
*/
|
2020-03-28 17:20:38 -07:00
|
|
|
if(vstate == Stopping)
|
|
|
|
mPlayState.store(Stopped, std::memory_order_release);
|
2020-01-08 06:40:58 -08:00
|
|
|
return;
|
|
|
|
}
|
2018-11-25 13:19:01 -08:00
|
|
|
|
|
|
|
ASSUME(SampleSize > 0);
|
2020-03-25 21:06:24 -07:00
|
|
|
|
|
|
|
const size_t FrameSize{mChans.size() * SampleSize};
|
|
|
|
ASSUME(FrameSize > 0);
|
2018-11-25 13:19:01 -08:00
|
|
|
|
2019-08-01 19:44:09 -07:00
|
|
|
ALCdevice *Device{Context->mDevice.get()};
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint NumSends{Device->NumAuxSends};
|
|
|
|
const uint IrSize{Device->mHrtf ? Device->mHrtf->irSize : 0};
|
2018-11-25 13:19:01 -08:00
|
|
|
|
2020-10-21 17:16:27 -07:00
|
|
|
ResamplerFunc Resample{(increment == MixerFracOne && DataPosFrac == 0) ?
|
2019-08-31 14:53:28 -07:00
|
|
|
Resample_<CopyTag,CTag> : mResampler};
|
2018-11-25 13:19:01 -08:00
|
|
|
|
2020-11-20 03:59:14 -08:00
|
|
|
uint Counter{(mFlags&VoiceIsFading) ? SamplesToDo : 0};
|
2018-12-25 19:54:14 -08:00
|
|
|
if(!Counter)
|
|
|
|
{
|
|
|
|
/* No fading, just overwrite the old/current params. */
|
2020-03-25 21:06:24 -07:00
|
|
|
for(auto &chandata : mChans)
|
2018-12-25 19:54:14 -08:00
|
|
|
{
|
2019-09-14 18:27:57 -07:00
|
|
|
{
|
|
|
|
DirectParams &parms = chandata.mDryParams;
|
2020-10-21 10:39:00 -07:00
|
|
|
if(!(mFlags&VoiceHasHrtf))
|
2019-10-05 21:23:31 -07:00
|
|
|
parms.Gains.Current = parms.Gains.Target;
|
2019-09-14 18:27:57 -07:00
|
|
|
else
|
|
|
|
parms.Hrtf.Old = parms.Hrtf.Target;
|
|
|
|
}
|
2020-11-20 03:59:14 -08:00
|
|
|
for(uint send{0};send < NumSends;++send)
|
2018-12-25 19:54:14 -08:00
|
|
|
{
|
2019-08-31 14:53:28 -07:00
|
|
|
if(mSend[send].Buffer.empty())
|
2019-06-03 22:24:26 -07:00
|
|
|
continue;
|
2018-12-25 19:54:14 -08:00
|
|
|
|
2019-06-24 13:21:06 -07:00
|
|
|
SendParams &parms = chandata.mWetParams[send];
|
2019-10-05 21:23:31 -07:00
|
|
|
parms.Gains.Current = parms.Gains.Target;
|
2019-06-03 22:24:26 -07:00
|
|
|
}
|
2018-12-25 19:54:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 03:59:14 -08:00
|
|
|
uint buffers_done{0u};
|
|
|
|
uint OutPos{0u};
|
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 */
|
2020-11-20 03:59:14 -08:00
|
|
|
uint DstBufferSize{SamplesToDo - OutPos};
|
|
|
|
uint SrcBufferSize;
|
2018-12-01 13:47:56 -08:00
|
|
|
|
2020-10-21 17:16:27 -07:00
|
|
|
if(increment <= MixerFracOne)
|
2018-12-01 13:47:56 -08:00
|
|
|
{
|
2020-06-29 12:16:30 -07:00
|
|
|
/* Calculate the last written dst sample pos. */
|
|
|
|
uint64_t DataSize64{DstBufferSize - 1};
|
|
|
|
/* Calculate the last read src sample pos. */
|
2020-10-21 17:16:27 -07:00
|
|
|
DataSize64 = (DataSize64*increment + DataPosFrac) >> MixerFracBits;
|
2020-06-29 12:16:30 -07:00
|
|
|
/* +1 to get the src sample count, include padding. */
|
2020-11-28 03:38:20 -08:00
|
|
|
DataSize64 += 1 + MaxResamplerPadding;
|
2020-06-29 12:16:30 -07:00
|
|
|
|
2020-11-28 03:38:20 -08:00
|
|
|
/* Result is guaranteed to be <= BufferLineSize+MaxResamplerPadding
|
2020-06-29 12:16:30 -07:00
|
|
|
* since we won't use more src samples than dst samples+padding.
|
2018-12-01 13:47:56 -08:00
|
|
|
*/
|
2020-11-20 03:59:14 -08:00
|
|
|
SrcBufferSize = static_cast<uint>(DataSize64);
|
2020-06-29 12:16:30 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint64_t DataSize64{DstBufferSize};
|
|
|
|
/* Calculate the end src sample pos, include padding. */
|
2020-10-21 17:16:27 -07:00
|
|
|
DataSize64 = (DataSize64*increment + DataPosFrac) >> MixerFracBits;
|
2020-11-28 03:38:20 -08:00
|
|
|
DataSize64 += MaxResamplerPadding;
|
2020-06-29 12:16:30 -07:00
|
|
|
|
2020-11-28 03:38:20 -08:00
|
|
|
if(DataSize64 <= BufferLineSize + MaxResamplerPadding)
|
2020-11-20 03:59:14 -08:00
|
|
|
SrcBufferSize = static_cast<uint>(DataSize64);
|
2020-07-12 18:51:10 -07:00
|
|
|
else
|
2020-03-23 11:35:08 -07:00
|
|
|
{
|
2020-06-29 12:16:30 -07:00
|
|
|
/* If the source size got saturated, we can't fill the desired
|
2020-07-12 18:51:10 -07:00
|
|
|
* dst size. Figure out how many samples we can actually mix.
|
2020-03-23 11:35:08 -07:00
|
|
|
*/
|
2020-11-28 03:38:20 -08:00
|
|
|
SrcBufferSize = BufferLineSize + MaxResamplerPadding;
|
2020-07-12 18:51:10 -07:00
|
|
|
|
2020-11-28 03:38:20 -08:00
|
|
|
DataSize64 = SrcBufferSize - MaxResamplerPadding;
|
2020-10-21 17:16:27 -07:00
|
|
|
DataSize64 = ((DataSize64<<MixerFracBits) - DataPosFrac) / increment;
|
2020-06-29 12:16:30 -07:00
|
|
|
if(DataSize64 < DstBufferSize)
|
|
|
|
{
|
|
|
|
/* Some mixers require being 16-byte aligned, so also limit
|
|
|
|
* to a multiple of 4 samples to maintain alignment.
|
|
|
|
*/
|
2020-11-20 03:59:14 -08:00
|
|
|
DstBufferSize = static_cast<uint>(DataSize64) & ~3u;
|
2020-06-29 12:16:30 -07:00
|
|
|
}
|
2020-03-23 11:35:08 -07:00
|
|
|
}
|
2018-12-01 13:47:56 -08:00
|
|
|
}
|
2012-09-26 17:16:25 -07:00
|
|
|
|
2020-10-21 10:39:00 -07:00
|
|
|
if((mFlags&(VoiceIsCallback|VoiceCallbackStopped)) == VoiceIsCallback && BufferListItem)
|
2020-02-17 17:13:00 -08:00
|
|
|
{
|
2020-11-19 04:18:25 -08:00
|
|
|
BufferStorage *buffer{BufferListItem->mBuffer};
|
2020-02-17 17:13:00 -08:00
|
|
|
|
|
|
|
/* Exclude resampler pre-padding from the needed size. */
|
2020-11-28 03:38:20 -08:00
|
|
|
const uint toLoad{SrcBufferSize - (MaxResamplerPadding>>1)};
|
2020-02-17 17:13:00 -08:00
|
|
|
if(toLoad > mNumCallbackSamples)
|
|
|
|
{
|
|
|
|
const size_t byteOffset{mNumCallbackSamples*FrameSize};
|
|
|
|
const size_t needBytes{toLoad*FrameSize - byteOffset};
|
|
|
|
|
2020-11-20 03:59:14 -08:00
|
|
|
const int gotBytes{buffer->mCallback(buffer->mUserData,
|
|
|
|
&buffer->mData[byteOffset], static_cast<int>(needBytes))};
|
2020-02-17 17:13:00 -08:00
|
|
|
if(gotBytes < 1)
|
2020-10-21 10:39:00 -07:00
|
|
|
mFlags |= VoiceCallbackStopped;
|
2020-11-20 03:59:14 -08:00
|
|
|
else if(static_cast<uint>(gotBytes) < needBytes)
|
2020-02-17 17:13:00 -08:00
|
|
|
{
|
2020-10-21 10:39:00 -07:00
|
|
|
mFlags |= VoiceCallbackStopped;
|
2020-11-20 03:59:14 -08:00
|
|
|
mNumCallbackSamples += static_cast<uint>(static_cast<uint>(gotBytes) /
|
2020-03-22 10:41:23 -07:00
|
|
|
FrameSize);
|
2020-02-17 17:13:00 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mNumCallbackSamples = toLoad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 13:05:42 -07:00
|
|
|
ASSUME(DstBufferSize > 0);
|
2020-03-25 21:06:24 -07:00
|
|
|
for(auto &chandata : mChans)
|
2010-09-22 09:38:24 -07:00
|
|
|
{
|
2020-03-25 21:06:24 -07:00
|
|
|
const size_t num_chans{mChans.size()};
|
|
|
|
const auto chan = static_cast<size_t>(std::distance(mChans.data(),
|
|
|
|
std::addressof(chandata)));
|
|
|
|
const al::span<float> SrcData{Device->SourceData, SrcBufferSize};
|
2015-10-15 07:29:25 -07:00
|
|
|
|
2019-08-11 03:34:35 -07:00
|
|
|
/* Load the previous samples into the source data first, then load
|
|
|
|
* what we can from the buffer queue.
|
|
|
|
*/
|
2020-11-28 03:38:20 -08:00
|
|
|
auto srciter = std::copy_n(chandata.mPrevSamples.begin(), MaxResamplerPadding>>1,
|
2019-06-24 13:33:09 -07:00
|
|
|
SrcData.begin());
|
2010-11-25 11:16:19 -08:00
|
|
|
|
2019-08-04 11:59:14 -07:00
|
|
|
if UNLIKELY(!BufferListItem)
|
2020-11-28 03:38:20 -08:00
|
|
|
srciter = std::copy(chandata.mPrevSamples.begin()+(MaxResamplerPadding>>1),
|
2019-06-24 13:33:09 -07:00
|
|
|
chandata.mPrevSamples.end(), srciter);
|
2020-10-21 10:39:00 -07:00
|
|
|
else if((mFlags&VoiceIsStatic))
|
2020-03-25 21:06:24 -07:00
|
|
|
srciter = LoadBufferStatic(BufferListItem, BufferLoopItem, num_chans,
|
2019-06-25 17:10:46 -07:00
|
|
|
SampleSize, chan, DataPosInt, {srciter, SrcData.end()});
|
2020-10-21 10:39:00 -07:00
|
|
|
else if((mFlags&VoiceIsCallback))
|
2020-03-25 21:06:24 -07:00
|
|
|
srciter = LoadBufferCallback(BufferListItem, num_chans, SampleSize, chan,
|
2020-02-17 17:13:00 -08:00
|
|
|
mNumCallbackSamples, {srciter, SrcData.end()});
|
2010-11-25 21:42:15 -08:00
|
|
|
else
|
2020-03-25 21:06:24 -07:00
|
|
|
srciter = LoadBufferQueue(BufferListItem, BufferLoopItem, num_chans,
|
2019-06-25 17:10:46 -07:00
|
|
|
SampleSize, chan, DataPosInt, {srciter, SrcData.end()});
|
2010-11-25 11:16:19 -08:00
|
|
|
|
2019-08-04 11:59:14 -07:00
|
|
|
if UNLIKELY(srciter != SrcData.end())
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
2019-05-19 00:53:39 -07:00
|
|
|
/* If the source buffer wasn't filled, copy the last sample for
|
|
|
|
* the remaining buffer. Ideally it should have ended with
|
|
|
|
* silence, but if not the gain fading should help avoid clicks
|
|
|
|
* from sudden amplitude changes.
|
2019-03-09 21:32:14 -08:00
|
|
|
*/
|
2020-04-08 06:17:04 -07:00
|
|
|
const float sample{*(srciter-1)};
|
2019-05-26 19:41:11 -07:00
|
|
|
std::fill(srciter, SrcData.end(), sample);
|
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. */
|
2020-10-21 17:16:27 -07:00
|
|
|
std::copy_n(&SrcData[(increment*DstBufferSize + DataPosFrac)>>MixerFracBits],
|
2019-06-24 13:33:09 -07:00
|
|
|
chandata.mPrevSamples.size(), chandata.mPrevSamples.begin());
|
2015-10-15 07:29:25 -07:00
|
|
|
|
2019-02-21 17:48:08 -08:00
|
|
|
/* Resample, then apply ambisonic upsampling as needed. */
|
2020-04-08 06:17:04 -07:00
|
|
|
const float *ResampledData{Resample(&mResampleState,
|
2020-11-28 03:38:20 -08:00
|
|
|
&SrcData[MaxResamplerPadding>>1], DataPosFrac, increment,
|
2019-09-28 14:35:42 -07:00
|
|
|
{Device->ResampledData, DstBufferSize})};
|
2020-10-21 10:39:00 -07:00
|
|
|
if((mFlags&VoiceIsAmbisonic))
|
2019-02-21 17:48:08 -08:00
|
|
|
{
|
2019-12-25 21:48:58 -08:00
|
|
|
const float hfscale{chandata.mAmbiScale};
|
2019-03-10 10:30:33 -07:00
|
|
|
/* Beware the evil const_cast. It's safe since it's pointing to
|
2019-05-26 19:41:11 -07:00
|
|
|
* either SourceData or ResampledData (both non-const), but the
|
|
|
|
* resample method takes the source as const float* and may
|
|
|
|
* return it without copying to output, making it currently
|
2019-03-10 10:30:33 -07:00
|
|
|
* unavoidable.
|
|
|
|
*/
|
2019-12-25 21:48:58 -08:00
|
|
|
const al::span<float> samples{const_cast<float*>(ResampledData), DstBufferSize};
|
2020-05-19 13:58:55 -07:00
|
|
|
chandata.mAmbiSplitter.processHfScale(samples, hfscale);
|
2019-02-21 17:48:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now filter and mix to the appropriate outputs. */
|
2020-11-28 03:38:20 -08:00
|
|
|
float (&FilterBuf)[BufferLineSize] = Device->FilteredData;
|
2012-09-26 17:16:25 -07:00
|
|
|
{
|
2019-06-24 13:33:09 -07:00
|
|
|
DirectParams &parms = chandata.mDryParams;
|
2020-04-23 22:13:52 -07:00
|
|
|
const float *samples{DoFilters(parms.LowPass, parms.HighPass, FilterBuf,
|
2019-12-25 18:39:22 -08:00
|
|
|
{ResampledData, DstBufferSize}, mDirect.FilterType)};
|
2012-09-14 04:13:18 -07:00
|
|
|
|
2020-10-21 10:39:00 -07:00
|
|
|
if((mFlags&VoiceHasHrtf))
|
2016-02-14 01:22:01 -08:00
|
|
|
{
|
2020-04-08 06:17:04 -07:00
|
|
|
const float TargetGain{UNLIKELY(vstate == Stopping) ? 0.0f :
|
2019-03-09 18:34:00 -08:00
|
|
|
parms.Hrtf.Target.Gain};
|
2019-12-24 23:36:08 -08:00
|
|
|
DoHrtfMix(samples, DstBufferSize, parms, TargetGain, Counter, OutPos, IrSize,
|
2019-11-03 00:30:33 -07:00
|
|
|
Device);
|
2016-02-14 03:23:06 -08:00
|
|
|
}
|
2020-10-21 10:39:00 -07:00
|
|
|
else if((mFlags&VoiceHasNfc))
|
2019-03-10 12:50:38 -07:00
|
|
|
{
|
2020-03-28 17:20:38 -07:00
|
|
|
const float *TargetGains{UNLIKELY(vstate == Stopping) ? SilentTarget.data()
|
|
|
|
: parms.Gains.Target.data()};
|
2020-01-05 18:25:50 -08:00
|
|
|
DoNfcMix({samples, DstBufferSize}, mDirect.Buffer.data(), parms, TargetGains,
|
|
|
|
Counter, OutPos, Device);
|
2019-03-10 12:50:38 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-28 17:20:38 -07:00
|
|
|
const float *TargetGains{UNLIKELY(vstate == Stopping) ? SilentTarget.data()
|
|
|
|
: parms.Gains.Target.data()};
|
2019-10-05 21:23:31 -07:00
|
|
|
MixSamples({samples, DstBufferSize}, mDirect.Buffer,
|
|
|
|
parms.Gains.Current.data(), TargetGains, Counter, OutPos);
|
2019-03-10 12:50:38 -07:00
|
|
|
}
|
2012-09-26 17:16:25 -07:00
|
|
|
}
|
2012-09-11 05:24:19 -07:00
|
|
|
|
2020-11-20 03:59:14 -08:00
|
|
|
for(uint send{0};send < NumSends;++send)
|
2012-09-08 21:34:36 -07:00
|
|
|
{
|
2019-08-31 14:53:28 -07:00
|
|
|
if(mSend[send].Buffer.empty())
|
2019-06-03 22:24:26 -07:00
|
|
|
continue;
|
2012-09-11 05:24:19 -07:00
|
|
|
|
2019-06-24 13:33:09 -07:00
|
|
|
SendParams &parms = chandata.mWetParams[send];
|
2020-04-23 22:13:52 -07:00
|
|
|
const float *samples{DoFilters(parms.LowPass, parms.HighPass, FilterBuf,
|
2019-12-25 18:39:22 -08:00
|
|
|
{ResampledData, DstBufferSize}, mSend[send].FilterType)};
|
2016-02-14 01:22:01 -08:00
|
|
|
|
2020-03-28 17:20:38 -07:00
|
|
|
const float *TargetGains{UNLIKELY(vstate == Stopping) ? SilentTarget.data()
|
|
|
|
: parms.Gains.Target.data()};
|
2019-10-05 21:23:31 -07:00
|
|
|
MixSamples({samples, DstBufferSize}, mSend[send].Buffer,
|
|
|
|
parms.Gains.Current.data(), TargetGains, Counter, OutPos);
|
2019-09-03 21:23:54 -07:00
|
|
|
}
|
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;
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint SrcSamplesDone{DataPosFrac>>MixerFracBits};
|
2020-02-17 17:13:00 -08:00
|
|
|
DataPosInt += SrcSamplesDone;
|
2020-10-21 17:16:27 -07:00
|
|
|
DataPosFrac &= MixerFracMask;
|
2014-06-02 19:19:22 -07:00
|
|
|
|
2012-09-26 17:16:25 -07:00
|
|
|
OutPos += DstBufferSize;
|
2019-08-20 06:03:31 -07:00
|
|
|
Counter = maxu(DstBufferSize, Counter) - DstBufferSize;
|
2010-08-03 23:19:36 -07:00
|
|
|
|
2019-08-04 11:59:14 -07:00
|
|
|
if UNLIKELY(!BufferListItem)
|
2019-03-09 18:34:00 -08:00
|
|
|
{
|
2019-05-05 01:51:46 -07:00
|
|
|
/* Do nothing extra when there's no buffers. */
|
2019-03-09 18:34:00 -08:00
|
|
|
}
|
2020-10-21 10:39:00 -07:00
|
|
|
else if((mFlags&VoiceIsStatic))
|
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 */
|
2020-11-19 04:18:25 -08:00
|
|
|
const BufferStorage &Buffer = *BufferListItem->mBuffer;
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint LoopStart{Buffer.mLoopStart};
|
|
|
|
const uint LoopEnd{Buffer.mLoopEnd};
|
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 */
|
2019-08-11 03:34:35 -07:00
|
|
|
if(DataPosInt >= BufferListItem->mSampleLen)
|
2018-02-01 21:07:55 -08:00
|
|
|
{
|
2019-01-07 12:37:13 +01:00
|
|
|
BufferListItem = nullptr;
|
2018-02-01 21:07:55 -08:00
|
|
|
break;
|
|
|
|
}
|
2011-02-06 01:07:37 -08:00
|
|
|
}
|
2017-12-16 11:34:52 -08:00
|
|
|
}
|
2020-10-21 10:39:00 -07:00
|
|
|
else if((mFlags&VoiceIsCallback))
|
2020-02-17 04:50:00 -08:00
|
|
|
{
|
2020-11-19 04:18:25 -08:00
|
|
|
BufferStorage &buffer = *BufferListItem->mBuffer;
|
2020-02-17 17:13:00 -08:00
|
|
|
if(SrcSamplesDone < mNumCallbackSamples)
|
|
|
|
{
|
|
|
|
const size_t byteOffset{SrcSamplesDone*FrameSize};
|
|
|
|
const size_t byteEnd{mNumCallbackSamples*FrameSize};
|
2020-11-19 03:23:46 -08:00
|
|
|
al::byte *data{buffer.mData.data()};
|
2020-08-28 00:09:46 -07:00
|
|
|
std::copy(data+byteOffset, data+byteEnd, data);
|
2020-02-17 17:13:00 -08:00
|
|
|
mNumCallbackSamples -= SrcSamplesDone;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BufferListItem = nullptr;
|
|
|
|
mNumCallbackSamples = 0;
|
|
|
|
}
|
2020-02-17 04:50:00 -08:00
|
|
|
}
|
2019-10-14 20:45:23 -07:00
|
|
|
else
|
2017-12-16 11:34:52 -08:00
|
|
|
{
|
2018-02-01 21:07:55 -08:00
|
|
|
/* Handle streaming source */
|
2019-10-14 20:45:23 -07:00
|
|
|
do {
|
|
|
|
if(BufferListItem->mSampleLen > DataPosInt)
|
|
|
|
break;
|
2011-02-06 01:07:37 -08:00
|
|
|
|
2019-10-14 20:45:23 -07:00
|
|
|
DataPosInt -= BufferListItem->mSampleLen;
|
2018-09-16 17:38:55 -07:00
|
|
|
|
2019-10-14 20:45:23 -07:00
|
|
|
++buffers_done;
|
|
|
|
BufferListItem = BufferListItem->mNext.load(std::memory_order_relaxed);
|
|
|
|
if(!BufferListItem) BufferListItem = BufferLoopItem;
|
|
|
|
} while(BufferListItem);
|
2010-08-03 23:19:36 -07:00
|
|
|
}
|
2019-03-09 21:32:14 -08:00
|
|
|
} while(OutPos < SamplesToDo);
|
2010-08-03 23:19:36 -07:00
|
|
|
|
2020-10-21 10:39:00 -07:00
|
|
|
mFlags |= VoiceIsFading;
|
2016-02-14 01:22:01 -08:00
|
|
|
|
2019-03-09 18:34:00 -08:00
|
|
|
/* Don't update positions and buffers if we were stopping. */
|
2020-03-28 17:20:38 -07:00
|
|
|
if UNLIKELY(vstate == Stopping)
|
2019-03-09 18:34:00 -08:00
|
|
|
{
|
2020-03-28 17:20:38 -07:00
|
|
|
mPlayState.store(Stopped, std::memory_order_release);
|
2019-03-09 18:34:00 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-31 14:53:28 -07:00
|
|
|
/* Capture the source ID in case it's reset for stopping. */
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint SourceID{mSourceID.load(std::memory_order_relaxed)};
|
2019-08-31 14:53:28 -07:00
|
|
|
|
2019-03-11 20:00:14 -07:00
|
|
|
/* Update voice info */
|
2019-08-31 14:53:28 -07:00
|
|
|
mPosition.store(DataPosInt, std::memory_order_relaxed);
|
|
|
|
mPositionFrac.store(DataPosFrac, std::memory_order_relaxed);
|
|
|
|
mCurrentBuffer.store(BufferListItem, std::memory_order_relaxed);
|
2019-10-14 20:45:23 -07:00
|
|
|
if(!BufferListItem)
|
2019-03-09 21:32:14 -08:00
|
|
|
{
|
2019-08-31 14:53:28 -07:00
|
|
|
mLoopBuffer.store(nullptr, std::memory_order_relaxed);
|
|
|
|
mSourceID.store(0u, std::memory_order_relaxed);
|
2019-03-09 21:32:14 -08:00
|
|
|
}
|
|
|
|
std::atomic_thread_fence(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. */
|
2020-11-20 03:59:14 -08:00
|
|
|
const uint enabledevt{Context->mEnabledEvts.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
|
|
|
{
|
2019-07-30 09:05:54 -07:00
|
|
|
RingBuffer *ring{Context->mAsyncEvents.get()};
|
2018-12-27 10:44:02 -08:00
|
|
|
auto evt_vec = ring->getWriteVector();
|
|
|
|
if(evt_vec.first.len > 0)
|
2018-12-25 09:32:38 -08:00
|
|
|
{
|
2020-04-24 07:04:32 -07: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-25 09:32:38 -08:00
|
|
|
}
|
2018-12-04 16:33:26 -08:00
|
|
|
}
|
2018-02-01 01:36:03 -08:00
|
|
|
|
2019-10-14 20:45:23 -07:00
|
|
|
if(!BufferListItem)
|
2019-03-09 16:48:07 -08:00
|
|
|
{
|
2019-03-09 21:32:14 -08:00
|
|
|
/* If the voice just ended, set it to Stopping so the next render
|
|
|
|
* ensures any residual noise fades to 0 amplitude.
|
|
|
|
*/
|
2020-03-28 17:20:38 -07:00
|
|
|
mPlayState.store(Stopping, std::memory_order_release);
|
2019-08-31 14:53:28 -07:00
|
|
|
if((enabledevt&EventType_SourceStateChange))
|
|
|
|
SendSourceStoppedEvent(Context, SourceID);
|
2019-03-09 16:48:07 -08:00
|
|
|
}
|
2010-08-03 23:19:36 -07:00
|
|
|
}
|