2016-05-07 04:28:32 +00:00
|
|
|
#include "AudioSubmix.hpp"
|
|
|
|
#include "AudioVoiceEngine.hpp"
|
|
|
|
#include "AudioVoice.hpp"
|
2017-12-29 07:54:26 +00:00
|
|
|
#include <cstring>
|
2016-05-07 22:11:45 +00:00
|
|
|
#include <algorithm>
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:17:47 +00:00
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
2016-05-07 04:28:32 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2016-07-14 04:59:41 +00:00
|
|
|
AudioSubmix::AudioSubmix(BaseAudioVoiceEngine& root, IAudioSubmixCallback* cb, int busId, bool mainOut)
|
2018-05-25 06:30:42 +00:00
|
|
|
: ListNode<AudioSubmix, BaseAudioVoiceEngine*, IAudioSubmix>(&root), m_busId(busId), m_mainOut(mainOut), m_cb(cb)
|
2016-07-13 03:03:52 +00:00
|
|
|
{
|
|
|
|
if (mainOut)
|
2017-12-03 06:05:16 +00:00
|
|
|
setSendLevel(m_head->m_mainSubmix.get(), 1.f, false);
|
2016-05-07 22:11:45 +00:00
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
|
|
|
AudioSubmix::~AudioSubmix()
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
m_head->m_submixesDirty = true;
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
AudioSubmix*& AudioSubmix::_getHeadPtr(BaseAudioVoiceEngine* head) { return head->m_submixHead; }
|
|
|
|
std::unique_lock<std::recursive_mutex> AudioSubmix::_getHeadLock(BaseAudioVoiceEngine* head)
|
|
|
|
{ return std::unique_lock<std::recursive_mutex>{head->m_dataMutex}; }
|
2017-12-04 02:50:33 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> AudioSubmix::destructorLock()
|
|
|
|
{ return std::unique_lock<std::recursive_mutex>{m_head->m_dataMutex}; }
|
2017-12-03 06:05:16 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
bool AudioSubmix::_isDirectDependencyOf(AudioSubmix* send)
|
2016-05-07 04:28:32 +00:00
|
|
|
{
|
2016-07-13 03:03:52 +00:00
|
|
|
return m_sendGains.find(send) != m_sendGains.cend();
|
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
bool AudioSubmix::_mergeC3(std::list<AudioSubmix*>& output,
|
|
|
|
std::vector<std::list<AudioSubmix*>>& lists)
|
|
|
|
{
|
|
|
|
for (auto outerIt = lists.begin() ; outerIt != lists.cend() ; ++outerIt)
|
|
|
|
{
|
2016-07-14 04:59:41 +00:00
|
|
|
if (outerIt->empty())
|
|
|
|
continue;
|
2016-07-13 03:03:52 +00:00
|
|
|
AudioSubmix* smx = outerIt->front();
|
|
|
|
bool found = false;
|
|
|
|
for (auto innerIt = lists.begin() ; innerIt != lists.cend() ; ++innerIt)
|
|
|
|
{
|
2016-07-14 04:59:41 +00:00
|
|
|
if (innerIt->empty() || outerIt == innerIt)
|
2016-07-13 03:03:52 +00:00
|
|
|
continue;
|
|
|
|
if (smx == innerIt->front())
|
|
|
|
{
|
|
|
|
innerIt->pop_front();
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
outerIt->pop_front();
|
|
|
|
output.push_back(smx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
std::list<AudioSubmix*> AudioSubmix::_linearizeC3()
|
|
|
|
{
|
|
|
|
std::vector<std::list<AudioSubmix*>> lists = {{}};
|
2017-12-03 06:05:16 +00:00
|
|
|
if (m_head->m_submixHead)
|
|
|
|
for (AudioSubmix& smx : *m_head->m_submixHead)
|
|
|
|
{
|
|
|
|
if (&smx == this)
|
|
|
|
continue;
|
|
|
|
if (smx._isDirectDependencyOf(this))
|
|
|
|
lists[0].push_back(&smx);
|
|
|
|
}
|
2016-07-13 03:03:52 +00:00
|
|
|
lists.reserve(lists[0].size() + 1);
|
|
|
|
for (AudioSubmix* smx : lists[0])
|
|
|
|
lists.push_back(smx->_linearizeC3());
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
std::list<AudioSubmix*> ret = {this};
|
|
|
|
while (_mergeC3(ret, lists)) {}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template <typename T>
|
|
|
|
void AudioSubmix::_zeroFill()
|
2016-07-13 03:03:52 +00:00
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
if (_getScratch<T>().size())
|
|
|
|
std::fill(_getScratch<T>().begin(), _getScratch<T>().end(), 0);
|
2016-07-13 03:03:52 +00:00
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template void AudioSubmix::_zeroFill<int16_t>();
|
|
|
|
template void AudioSubmix::_zeroFill<int32_t>();
|
|
|
|
template void AudioSubmix::_zeroFill<float>();
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template <typename T>
|
|
|
|
T* AudioSubmix::_getMergeBuf(size_t frames)
|
2016-07-13 03:03:52 +00:00
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
if (_getRedirect<T>())
|
|
|
|
return _getRedirect<T>();
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
size_t sampleCount = frames * m_head->clientMixInfo().m_channelMap.m_channelCount;
|
|
|
|
if (_getScratch<T>().size() < sampleCount)
|
|
|
|
_getScratch<T>().resize(sampleCount);
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
return _getScratch<T>().data();
|
2016-07-13 03:03:52 +00:00
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template int16_t* AudioSubmix::_getMergeBuf<int16_t>(size_t frames);
|
|
|
|
template int32_t* AudioSubmix::_getMergeBuf<int32_t>(size_t frames);
|
|
|
|
template float* AudioSubmix::_getMergeBuf<float>(size_t frames);
|
2016-07-13 03:03:52 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline T ClampInt(float in)
|
2016-05-07 04:28:32 +00:00
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
if (std::is_floating_point<T>())
|
2016-07-14 04:59:41 +00:00
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
return in; // Allow subsequent mixing stages to work with over-saturated values
|
2016-07-14 04:59:41 +00:00
|
|
|
}
|
2016-07-13 03:03:52 +00:00
|
|
|
else
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
constexpr T MAX = std::numeric_limits<T>::max();
|
|
|
|
constexpr T MIN = std::numeric_limits<T>::min();
|
|
|
|
|
|
|
|
if (in < MIN)
|
|
|
|
return MIN;
|
|
|
|
else if (in > MAX)
|
|
|
|
return MAX;
|
|
|
|
else
|
|
|
|
return in;
|
2016-07-13 03:03:52 +00:00
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template <typename T>
|
|
|
|
size_t AudioSubmix::_pumpAndMix(size_t frames)
|
2016-05-07 04:28:32 +00:00
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
const ChannelMap& chMap = m_head->clientMixInfo().m_channelMap;
|
2016-07-14 04:59:41 +00:00
|
|
|
size_t chanCount = chMap.m_channelCount;
|
2016-07-13 03:03:52 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
if (_getRedirect<T>())
|
2016-07-14 04:59:41 +00:00
|
|
|
{
|
|
|
|
if (m_cb && m_cb->canApplyEffect())
|
2017-12-03 06:05:16 +00:00
|
|
|
m_cb->applyEffect(_getRedirect<T>(), frames, chMap, m_head->mixInfo().m_sampleRate);
|
|
|
|
_getRedirect<T>() += chanCount * frames;
|
2016-07-14 04:59:41 +00:00
|
|
|
}
|
2016-07-13 03:03:52 +00:00
|
|
|
else
|
|
|
|
{
|
2016-07-14 04:59:41 +00:00
|
|
|
size_t sampleCount = frames * chanCount;
|
2017-12-03 06:05:16 +00:00
|
|
|
if (_getScratch<T>().size() < sampleCount)
|
|
|
|
_getScratch<T>().resize(sampleCount);
|
2016-07-14 04:59:41 +00:00
|
|
|
if (m_cb && m_cb->canApplyEffect())
|
2017-12-03 06:05:16 +00:00
|
|
|
m_cb->applyEffect(_getScratch<T>().data(), frames, chMap, m_head->mixInfo().m_sampleRate);
|
2016-07-14 04:59:41 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
size_t curSlewFrame = m_slewFrames;
|
|
|
|
for (auto& smx : m_sendGains)
|
|
|
|
{
|
|
|
|
curSlewFrame = m_curSlewFrame;
|
|
|
|
AudioSubmix& sm = *reinterpret_cast<AudioSubmix*>(smx.first);
|
2017-12-03 06:05:16 +00:00
|
|
|
auto it = _getScratch<T>().begin();
|
|
|
|
T* dataOut = sm._getMergeBuf<T>(frames);
|
2016-07-13 03:03:52 +00:00
|
|
|
|
|
|
|
for (size_t f=0 ; f<frames ; ++f)
|
|
|
|
{
|
|
|
|
if (m_slewFrames && curSlewFrame < m_slewFrames)
|
|
|
|
{
|
|
|
|
double t = curSlewFrame / double(m_slewFrames);
|
|
|
|
double omt = 1.0 - t;
|
|
|
|
|
|
|
|
for (unsigned c=0 ; c<chanCount ; ++c)
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
*dataOut = ClampInt<T>(*dataOut + *it * (smx.second[1] * t + smx.second[0] * omt));
|
2016-07-13 03:03:52 +00:00
|
|
|
++it;
|
|
|
|
++dataOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
++curSlewFrame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (unsigned c=0 ; c<chanCount ; ++c)
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
*dataOut = ClampInt<T>(*dataOut + *it * smx.second[1]);
|
2016-07-13 03:03:52 +00:00
|
|
|
++it;
|
|
|
|
++dataOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_curSlewFrame += curSlewFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frames;
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
template size_t AudioSubmix::_pumpAndMix<int16_t>(size_t frames);
|
|
|
|
template size_t AudioSubmix::_pumpAndMix<int32_t>(size_t frames);
|
|
|
|
template size_t AudioSubmix::_pumpAndMix<float>(size_t frames);
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2016-06-01 04:53:57 +00:00
|
|
|
void AudioSubmix::_resetOutputSampleRate()
|
|
|
|
{
|
|
|
|
if (m_cb)
|
2017-12-03 06:05:16 +00:00
|
|
|
m_cb->resetOutputSampleRate(m_head->mixInfo().m_sampleRate);
|
2016-06-01 04:53:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
void AudioSubmix::resetSendLevels()
|
2016-05-07 04:28:32 +00:00
|
|
|
{
|
2016-07-13 03:03:52 +00:00
|
|
|
if (m_sendGains.empty())
|
|
|
|
return;
|
|
|
|
m_sendGains.clear();
|
2017-12-03 06:05:16 +00:00
|
|
|
m_head->m_submixesDirty = true;
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
void AudioSubmix::setSendLevel(IAudioSubmix* submix, float level, bool slew)
|
2016-05-07 04:28:32 +00:00
|
|
|
{
|
2016-07-13 03:03:52 +00:00
|
|
|
auto search = m_sendGains.find(submix);
|
|
|
|
if (search == m_sendGains.cend())
|
|
|
|
{
|
|
|
|
search = m_sendGains.emplace(submix, std::array<float, 2>{1.f, 1.f}).first;
|
2017-12-03 06:05:16 +00:00
|
|
|
m_head->m_submixesDirty = true;
|
2016-07-13 03:03:52 +00:00
|
|
|
}
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2017-12-03 06:05:16 +00:00
|
|
|
m_slewFrames = slew ? m_head->m_5msFrames : 0;
|
2016-07-13 03:03:52 +00:00
|
|
|
m_curSlewFrame = 0;
|
2016-05-07 04:28:32 +00:00
|
|
|
|
2016-07-13 03:03:52 +00:00
|
|
|
search->second[0] = search->second[1];
|
|
|
|
search->second[1] = level;
|
2016-05-07 22:11:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 04:28:32 +00:00
|
|
|
const AudioVoiceEngineMixInfo& AudioSubmix::mixInfo() const
|
|
|
|
{
|
2017-12-03 06:05:16 +00:00
|
|
|
return m_head->mixInfo();
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 04:47:31 +00:00
|
|
|
double AudioSubmix::getSampleRate() const
|
|
|
|
{
|
|
|
|
return mixInfo().m_sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubmixFormat AudioSubmix::getSampleFormat() const
|
|
|
|
{
|
|
|
|
switch (mixInfo().m_sampleFormat)
|
|
|
|
{
|
|
|
|
case SOXR_INT16_I:
|
|
|
|
default:
|
|
|
|
return SubmixFormat::Int16;
|
|
|
|
case SOXR_INT32_I:
|
|
|
|
return SubmixFormat::Int32;
|
|
|
|
case SOXR_FLOAT32_I:
|
|
|
|
return SubmixFormat::Float;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:28:32 +00:00
|
|
|
}
|