2016-03-24 00:01:57 +00:00
|
|
|
#include "AudioMatrix.hpp"
|
|
|
|
#include "AudioVoiceEngine.hpp"
|
2016-01-28 23:53:51 +00:00
|
|
|
#include <string.h>
|
2016-03-24 00:01:57 +00:00
|
|
|
#include <limits.h>
|
2016-01-28 23:53:51 +00:00
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
static inline int16_t Clamp16(float in)
|
|
|
|
{
|
|
|
|
if (in < SHRT_MIN)
|
|
|
|
return SHRT_MIN;
|
|
|
|
else if (in > SHRT_MAX)
|
|
|
|
return SHRT_MAX;
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t Clamp32(float in)
|
|
|
|
{
|
|
|
|
if (in < INT_MIN)
|
|
|
|
return INT_MIN;
|
|
|
|
else if (in > INT_MAX)
|
|
|
|
return INT_MAX;
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float ClampFlt(float in)
|
|
|
|
{
|
|
|
|
if (in < -1.f)
|
|
|
|
return -1.f;
|
|
|
|
else if (in > 1.f)
|
|
|
|
return 1.f;
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMatrixMono::setDefaultMatrixCoefficients(AudioChannelSet acSet)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
|
|
|
memset(m_coefs, 0, sizeof(m_coefs));
|
2016-03-24 00:01:57 +00:00
|
|
|
switch (acSet)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
|
|
|
case AudioChannelSet::Stereo:
|
|
|
|
case AudioChannelSet::Quad:
|
2016-03-08 07:09:58 +00:00
|
|
|
m_coefs[int(AudioChannel::FrontLeft)] = 1.0;
|
|
|
|
m_coefs[int(AudioChannel::FrontRight)] = 1.0;
|
2016-01-28 23:53:51 +00:00
|
|
|
break;
|
|
|
|
case AudioChannelSet::Surround51:
|
|
|
|
case AudioChannelSet::Surround71:
|
2016-03-08 07:09:58 +00:00
|
|
|
m_coefs[int(AudioChannel::FrontCenter)] = 1.0;
|
2016-01-28 23:53:51 +00:00
|
|
|
break;
|
2016-03-08 07:09:58 +00:00
|
|
|
default: break;
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
void AudioMatrixMono::mixMonoSampleData(const AudioVoiceEngineMixInfo& info,
|
|
|
|
const int16_t* dataIn, int16_t* dataOut, size_t samples) const
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
const ChannelMap& chmap = info.m_channelMap;
|
|
|
|
for (size_t s=0 ; s<samples ; ++s, ++dataIn)
|
2016-03-08 20:28:44 +00:00
|
|
|
for (unsigned c=0 ; c<chmap.m_channelCount ; ++c)
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
|
|
|
AudioChannel ch = chmap.m_channels[c];
|
2016-03-24 00:01:57 +00:00
|
|
|
if (ch != AudioChannel::Unknown)
|
|
|
|
{
|
|
|
|
*dataOut = Clamp16(*dataOut + *dataIn * m_coefs[int(ch)]);
|
|
|
|
++dataOut;
|
|
|
|
}
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
void AudioMatrixMono::mixMonoSampleData(const AudioVoiceEngineMixInfo& info,
|
|
|
|
const int32_t* dataIn, int32_t* dataOut, size_t samples) const
|
|
|
|
{
|
|
|
|
const ChannelMap& chmap = info.m_channelMap;
|
|
|
|
for (size_t s=0 ; s<samples ; ++s, ++dataIn)
|
|
|
|
for (unsigned c=0 ; c<chmap.m_channelCount ; ++c)
|
|
|
|
{
|
|
|
|
AudioChannel ch = chmap.m_channels[c];
|
|
|
|
if (ch != AudioChannel::Unknown)
|
|
|
|
{
|
|
|
|
*dataOut = Clamp32(*dataOut + *dataIn * m_coefs[int(ch)]);
|
|
|
|
++dataOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMatrixMono::mixMonoSampleData(const AudioVoiceEngineMixInfo& info,
|
|
|
|
const float* dataIn, float* dataOut, size_t samples) const
|
|
|
|
{
|
|
|
|
const ChannelMap& chmap = info.m_channelMap;
|
|
|
|
for (size_t s=0 ; s<samples ; ++s, ++dataIn)
|
|
|
|
for (unsigned c=0 ; c<chmap.m_channelCount ; ++c)
|
|
|
|
{
|
|
|
|
AudioChannel ch = chmap.m_channels[c];
|
|
|
|
if (ch != AudioChannel::Unknown)
|
|
|
|
{
|
|
|
|
*dataOut = ClampFlt(*dataOut + *dataIn * m_coefs[int(ch)]);
|
|
|
|
++dataOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMatrixStereo::setDefaultMatrixCoefficients(AudioChannelSet acSet)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
|
|
|
memset(m_coefs, 0, sizeof(m_coefs));
|
2016-03-24 00:01:57 +00:00
|
|
|
switch (acSet)
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
|
|
|
case AudioChannelSet::Stereo:
|
|
|
|
case AudioChannelSet::Quad:
|
2016-03-08 07:09:58 +00:00
|
|
|
m_coefs[int(AudioChannel::FrontLeft)][0] = 1.0;
|
|
|
|
m_coefs[int(AudioChannel::FrontRight)][1] = 1.0;
|
2016-01-28 23:53:51 +00:00
|
|
|
break;
|
|
|
|
case AudioChannelSet::Surround51:
|
|
|
|
case AudioChannelSet::Surround71:
|
2016-03-08 07:09:58 +00:00
|
|
|
m_coefs[int(AudioChannel::FrontLeft)][0] = 1.0;
|
|
|
|
m_coefs[int(AudioChannel::FrontRight)][1] = 1.0;
|
2016-01-28 23:53:51 +00:00
|
|
|
break;
|
2016-03-08 07:09:58 +00:00
|
|
|
default: break;
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
void AudioMatrixStereo::mixStereoSampleData(const AudioVoiceEngineMixInfo& info,
|
|
|
|
const int16_t* dataIn, int16_t* dataOut, size_t frames) const
|
|
|
|
{
|
|
|
|
const ChannelMap& chmap = info.m_channelMap;
|
|
|
|
for (size_t f=0 ; f<frames ; ++f, dataIn += 2)
|
|
|
|
for (unsigned c=0 ; c<chmap.m_channelCount ; ++c)
|
|
|
|
{
|
|
|
|
AudioChannel ch = chmap.m_channels[c];
|
|
|
|
if (ch != AudioChannel::Unknown)
|
|
|
|
{
|
|
|
|
*dataOut = Clamp16(*dataOut +
|
|
|
|
dataIn[0] * m_coefs[int(ch)][0] +
|
|
|
|
dataIn[1] * m_coefs[int(ch)][1]);
|
|
|
|
++dataOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMatrixStereo::mixStereoSampleData(const AudioVoiceEngineMixInfo& info,
|
|
|
|
const int32_t* dataIn, int32_t* dataOut, size_t frames) const
|
|
|
|
{
|
|
|
|
const ChannelMap& chmap = info.m_channelMap;
|
|
|
|
for (size_t f=0 ; f<frames ; ++f, dataIn += 2)
|
|
|
|
for (unsigned c=0 ; c<chmap.m_channelCount ; ++c)
|
|
|
|
{
|
|
|
|
AudioChannel ch = chmap.m_channels[c];
|
|
|
|
if (ch != AudioChannel::Unknown)
|
|
|
|
{
|
|
|
|
*dataOut = Clamp32(*dataOut +
|
|
|
|
dataIn[0] * m_coefs[int(ch)][0] +
|
|
|
|
dataIn[1] * m_coefs[int(ch)][1]);
|
|
|
|
++dataOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMatrixStereo::mixStereoSampleData(const AudioVoiceEngineMixInfo& info,
|
|
|
|
const float* dataIn, float* dataOut, size_t frames) const
|
2016-01-28 23:53:51 +00:00
|
|
|
{
|
2016-03-24 00:01:57 +00:00
|
|
|
const ChannelMap& chmap = info.m_channelMap;
|
|
|
|
for (size_t f=0 ; f<frames ; ++f, dataIn += 2)
|
2016-03-08 20:28:44 +00:00
|
|
|
for (unsigned c=0 ; c<chmap.m_channelCount ; ++c)
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
|
|
|
AudioChannel ch = chmap.m_channels[c];
|
2016-03-24 00:01:57 +00:00
|
|
|
if (ch != AudioChannel::Unknown)
|
|
|
|
{
|
|
|
|
*dataOut = ClampFlt(*dataOut +
|
|
|
|
dataIn[0] * m_coefs[int(ch)][0] +
|
|
|
|
dataIn[1] * m_coefs[int(ch)][1]);
|
|
|
|
++dataOut;
|
|
|
|
}
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
2016-01-28 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|