2019-08-19 23:08:54 +00:00
|
|
|
#include "lib/audiodev/AudioVoiceEngine.hpp"
|
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
#include <cassert>
|
2019-08-19 23:08:54 +00:00
|
|
|
#include <cstring>
|
2016-03-24 00:01:57 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
namespace boo {
|
|
|
|
|
|
|
|
BaseAudioVoiceEngine::~BaseAudioVoiceEngine() {
|
|
|
|
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>
|
2018-12-08 05:17:51 +00:00
|
|
|
void BaseAudioVoiceEngine::_pumpAndMixVoices(size_t frames, T* dataOut) {
|
|
|
|
if (dataOut)
|
|
|
|
memset(dataOut, 0, sizeof(T) * frames * m_mixInfo.m_channelMap.m_channelCount);
|
|
|
|
|
|
|
|
if (m_ltRtProcessing) {
|
|
|
|
size_t sampleCount = m_5msFrames * 5;
|
|
|
|
if (_getLtRtIn<T>().size() < sampleCount)
|
|
|
|
_getLtRtIn<T>().resize(sampleCount);
|
|
|
|
m_mainSubmix->_getRedirect<T>() = _getLtRtIn<T>().data();
|
|
|
|
} else {
|
|
|
|
m_mainSubmix->_getRedirect<T>() = dataOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_submixesDirty) {
|
|
|
|
m_linearizedSubmixes = m_mainSubmix->_linearizeC3();
|
|
|
|
m_submixesDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t remFrames = frames;
|
|
|
|
while (remFrames) {
|
|
|
|
size_t thisFrames;
|
|
|
|
if (remFrames < m_5msFrames) {
|
|
|
|
thisFrames = remFrames;
|
|
|
|
if (m_engineCallback)
|
|
|
|
m_engineCallback->on5MsInterval(*this, thisFrames / double(m_5msFrames) * 5.0 / 1000.0);
|
|
|
|
} else {
|
|
|
|
thisFrames = m_5msFrames;
|
|
|
|
if (m_engineCallback)
|
|
|
|
m_engineCallback->on5MsInterval(*this, 5.0 / 1000.0);
|
|
|
|
}
|
2018-05-27 04:20:01 +00:00
|
|
|
|
2017-09-28 03:11:40 +00:00
|
|
|
if (m_ltRtProcessing)
|
2018-12-08 05:17:51 +00:00
|
|
|
std::fill(_getLtRtIn<T>().begin(), _getLtRtIn<T>().end(), 0.f);
|
2016-07-13 03:03:52 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
for (auto it = m_linearizedSubmixes.rbegin(); it != m_linearizedSubmixes.rend(); ++it)
|
|
|
|
(*it)->_zeroFill<T>();
|
|
|
|
|
|
|
|
if (m_voiceHead)
|
|
|
|
for (AudioVoice& vox : *m_voiceHead)
|
|
|
|
if (vox.m_running)
|
|
|
|
vox.pumpAndMix<T>(thisFrames);
|
|
|
|
|
|
|
|
for (auto it = m_linearizedSubmixes.rbegin(); it != m_linearizedSubmixes.rend(); ++it)
|
|
|
|
(*it)->_pumpAndMix<T>(thisFrames);
|
2016-05-19 10:14:21 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
remFrames -= thisFrames;
|
|
|
|
if (!dataOut)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (m_ltRtProcessing) {
|
|
|
|
m_ltRtProcessing->Process(_getLtRtIn<T>().data(), dataOut, int(thisFrames));
|
|
|
|
m_mainSubmix->_getRedirect<T>() = _getLtRtIn<T>().data();
|
2016-05-19 10:14:21 +00:00
|
|
|
}
|
2017-02-15 06:00:10 +00:00
|
|
|
|
2018-12-08 05:17:51 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-08 05:17:51 +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();
|
2018-08-18 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
ObjToken<IAudioVoice> BaseAudioVoiceEngine::allocateNewMonoVoice(double sampleRate, IAudioVoiceCallback* cb,
|
|
|
|
bool dynamicPitch) {
|
|
|
|
return {new AudioVoiceMono(*this, cb, sampleRate, dynamicPitch)};
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
ObjToken<IAudioVoice> BaseAudioVoiceEngine::allocateNewStereoVoice(double sampleRate, IAudioVoiceCallback* cb,
|
|
|
|
bool dynamicPitch) {
|
|
|
|
return {new AudioVoiceStereo(*this, cb, sampleRate, dynamicPitch)};
|
2016-03-24 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
ObjToken<IAudioSubmix> BaseAudioVoiceEngine::allocateNewSubmix(bool mainOut, IAudioSubmixCallback* cb, int busId) {
|
|
|
|
return {new AudioSubmix(*this, cb, busId, mainOut)};
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void BaseAudioVoiceEngine::setCallbackInterface(IAudioVoiceEngineCallback* cb) { m_engineCallback = cb; }
|
2017-01-23 07:20:40 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void BaseAudioVoiceEngine::setVolume(float vol) { m_totalVol = vol; }
|
2016-07-14 06:16:40 +00:00
|
|
|
|
2018-12-08 05:17:51 +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();
|
2017-09-28 03:11:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
const AudioVoiceEngineMixInfo& BaseAudioVoiceEngine::mixInfo() const { return m_mixInfo; }
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
const AudioVoiceEngineMixInfo& BaseAudioVoiceEngine::clientMixInfo() const {
|
|
|
|
return m_ltRtProcessing ? m_ltRtProcessing->inMixInfo() : m_mixInfo;
|
2017-09-28 03:11:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
} // namespace boo
|