2016-03-24 00:01:57 +00:00
|
|
|
#include "AudioVoiceEngine.hpp"
|
2017-12-03 06:05:16 +00:00
|
|
|
#include <cassert>
|
2016-03-24 00:01:57 +00:00
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2016-05-16 22:14:07 +00:00
|
|
|
BaseAudioVoiceEngine::~BaseAudioVoiceEngine()
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
m_mainSubmix.reset();
|
|
|
|
assert(m_voiceHead == nullptr && "Dangling voices detected");
|
|
|
|
assert(m_submixHead == nullptr && "Dangling submixes detected");
|
2016-05-16 22:14:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template <typename T>
|
|
|
|
void BaseAudioVoiceEngine::_pumpAndMixVoices(size_t frames, T* dataOut)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
2018-05-27 04:20:01 +00:00
|
|
|
if (dataOut)
|
|
|
|
memset(dataOut, 0, sizeof(T) * frames * m_mixInfo.m_channelMap.m_channelCount);
|
|
|
|
|
2017-09-28 03:11:40 +00:00
|
|
|
if (m_ltRtProcessing)
|
|
|
|
{
|
|
|
|
size_t sampleCount = m_5msFrames * 5;
|
2017-12-03 06:05:16 +00:00
|
|
|
if (_getLtRtIn<T>().size() < sampleCount)
|
|
|
|
_getLtRtIn<T>().resize(sampleCount);
|
|
|
|
m_mainSubmix->_getRedirect<T>() = _getLtRtIn<T>().data();
|
2017-09-28 03:11:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
m_mainSubmix->_getRedirect<T>() = dataOut;
|
2017-09-28 03:11:40 +00:00
|
|
|
}
|
2016-07-13 03:03:52 +00:00
|
|
|
|
|
|
|
if (m_submixesDirty)
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
m_linearizedSubmixes = m_mainSubmix->_linearizeC3();
|
2016-07-13 03:03:52 +00:00
|
|
|
m_submixesDirty = false;
|
|
|
|
}
|
2016-05-19 10:14:21 +00:00
|
|
|
|
|
|
|
size_t remFrames = frames;
|
|
|
|
while (remFrames)
|
|
|
|
{
|
|
|
|
size_t thisFrames;
|
|
|
|
if (remFrames < m_5msFrames)
|
|
|
|
{
|
|
|
|
thisFrames = remFrames;
|
2017-02-15 06:00:10 +00:00
|
|
|
if (m_engineCallback)
|
|
|
|
m_engineCallback->on5MsInterval(*this, thisFrames / double(m_5msFrames) * 5.0 / 1000.0);
|
2016-05-19 10:14:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thisFrames = m_5msFrames;
|
2017-02-15 06:00:10 +00:00
|
|
|
if (m_engineCallback)
|
|
|
|
m_engineCallback->on5MsInterval(*this, 5.0 / 1000.0);
|
2016-05-19 10:14:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 00:59:18 +00:00
|
|
|
if (m_ltRtProcessing)
|
2017-12-03 06:05:16 +00:00
|
|
|
std::fill(_getLtRtIn<T>().begin(), _getLtRtIn<T>().end(), 0.f);
|
2017-10-02 00:59:18 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
for (auto it = m_linearizedSubmixes.rbegin() ; it != m_linearizedSubmixes.rend() ; ++it)
|
2017-12-03 06:05:16 +00:00
|
|
|
(*it)->_zeroFill<T>();
|
2016-07-13 03:03:52 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
if (m_voiceHead)
|
|
|
|
for (AudioVoice& vox : *m_voiceHead)
|
|
|
|
if (vox.m_running)
|
|
|
|
vox.pumpAndMix<T>(thisFrames);
|
2016-07-13 03:03:52 +00:00
|
|
|
|
|
|
|
for (auto it = m_linearizedSubmixes.rbegin() ; it != m_linearizedSubmixes.rend() ; ++it)
|
2017-12-03 06:05:16 +00:00
|
|
|
(*it)->_pumpAndMix<T>(thisFrames);
|
2016-07-13 03:03:52 +00:00
|
|
|
|
2018-05-27 04:20:01 +00:00
|
|
|
remFrames -= thisFrames;
|
|
|
|
if (!dataOut)
|
|
|
|
continue;
|
|
|
|
|
2017-09-28 03:11:40 +00:00
|
|
|
if (m_ltRtProcessing)
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
m_ltRtProcessing->Process(_getLtRtIn<T>().data(), dataOut, int(thisFrames));
|
|
|
|
m_mainSubmix->_getRedirect<T>() = _getLtRtIn<T>().data();
|
2017-09-28 03:11:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:16:40 +00:00
|
|
|
size_t sampleCount = thisFrames * m_mixInfo.m_channelMap.m_channelCount;
|
|
|
|
for (size_t i=0 ; i<sampleCount ; ++i)
|
|
|
|
dataOut[i] *= m_totalVol;
|
|
|
|
|
|
|
|
dataOut += sampleCount;
|
2016-05-19 10:14:21 +00:00
|
|
|
}
|
2017-02-15 06:00:10 +00:00
|
|
|
|
|
|
|
if (m_engineCallback)
|
|
|
|
m_engineCallback->onPumpCycleComplete(*this);
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template void BaseAudioVoiceEngine::_pumpAndMixVoices<int16_t>(size_t frames, int16_t* dataOut);
|
|
|
|
template void BaseAudioVoiceEngine::_pumpAndMixVoices<int32_t>(size_t frames, int32_t* dataOut);
|
|
|
|
template void BaseAudioVoiceEngine::_pumpAndMixVoices<float>(size_t frames, float* dataOut);
|
2016-03-24 00:01:57 +00:00
|
|
|
|
2018-08-18 22:08:58 +00:00
|
|
|
void BaseAudioVoiceEngine::_resetSampleRate()
|
|
|
|
{
|
|
|
|
if (m_voiceHead)
|
|
|
|
for (boo::AudioVoice& vox : *m_voiceHead)
|
|
|
|
vox._resetSampleRate(vox.m_sampleRateIn);
|
|
|
|
if (m_submixHead)
|
|
|
|
for (boo::AudioSubmix& smx : *m_submixHead)
|
|
|
|
smx._resetOutputSampleRate();
|
|
|
|
}
|
|
|
|
|
2017-12-04 02:50:33 +00:00
|
|
|
ObjToken<IAudioVoice>
|
2016-03-24 00:01:57 +00:00
|
|
|
BaseAudioVoiceEngine::allocateNewMonoVoice(double sampleRate,
|
2016-05-07 04:28:32 +00:00
|
|
|
IAudioVoiceCallback* cb,
|
|
|
|
bool dynamicPitch)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
2017-12-04 02:50:33 +00:00
|
|
|
return {new AudioVoiceMono(*this, cb, sampleRate, dynamicPitch)};
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 02:50:33 +00:00
|
|
|
ObjToken<IAudioVoice>
|
2016-03-24 00:01:57 +00:00
|
|
|
BaseAudioVoiceEngine::allocateNewStereoVoice(double sampleRate,
|
2016-05-07 04:28:32 +00:00
|
|
|
IAudioVoiceCallback* cb,
|
|
|
|
bool dynamicPitch)
|
2016-03-24 00:01:57 +00:00
|
|
|
{
|
2017-12-04 02:50:33 +00:00
|
|
|
return {new AudioVoiceStereo(*this, cb, sampleRate, dynamicPitch)};
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 02:50:33 +00:00
|
|
|
ObjToken<IAudioSubmix>
|
2016-07-14 04:59:41 +00:00
|
|
|
BaseAudioVoiceEngine::allocateNewSubmix(bool mainOut, IAudioSubmixCallback* cb, int busId)
|
2016-05-07 04:28:32 +00:00
|
|
|
{
|
2017-12-04 02:50:33 +00:00
|
|
|
return {new AudioSubmix(*this, cb, busId, mainOut)};
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 06:00:10 +00:00
|
|
|
void BaseAudioVoiceEngine::setCallbackInterface(IAudioVoiceEngineCallback* cb)
|
2017-01-23 07:20:40 +00:00
|
|
|
{
|
2017-02-15 06:00:10 +00:00
|
|
|
m_engineCallback = cb;
|
2017-01-23 07:20:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:16:40 +00:00
|
|
|
void BaseAudioVoiceEngine::setVolume(float vol)
|
|
|
|
{
|
|
|
|
m_totalVol = vol;
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:11:40 +00:00
|
|
|
bool BaseAudioVoiceEngine::enableLtRt(bool enable)
|
|
|
|
{
|
|
|
|
if (enable && m_mixInfo.m_channelMap.m_channelCount == 2 &&
|
|
|
|
m_mixInfo.m_channels == AudioChannelSet::Stereo)
|
|
|
|
m_ltRtProcessing = std::make_unique<LtRtProcessing>(m_5msFrames, m_mixInfo);
|
|
|
|
else
|
|
|
|
m_ltRtProcessing.reset();
|
|
|
|
return m_ltRtProcessing.operator bool();
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:28:32 +00:00
|
|
|
const AudioVoiceEngineMixInfo& BaseAudioVoiceEngine::mixInfo() const
|
|
|
|
{
|
|
|
|
return m_mixInfo;
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:11:40 +00:00
|
|
|
const AudioVoiceEngineMixInfo& BaseAudioVoiceEngine::clientMixInfo() const
|
|
|
|
{
|
|
|
|
return m_ltRtProcessing ? m_ltRtProcessing->inMixInfo() : m_mixInfo;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|