openal-soft/alc/bformatdec.cpp

178 lines
5.6 KiB
C++
Raw Normal View History

#include "config.h"
#include "bformatdec.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <iterator>
2018-12-08 01:32:43 -08:00
#include <numeric>
2019-08-05 18:36:39 -07:00
#include "AL/al.h"
#include "almalloc.h"
#include "alu.h"
#include "ambdec.h"
#include "filters/splitter.h"
#include "opthelpers.h"
namespace {
2020-01-14 13:10:01 -08:00
constexpr std::array<float,MAX_AMBI_ORDER+1> Ambi3DDecoderHFScale{{
1.00000000e+00f, 1.00000000e+00f
2020-01-14 13:10:01 -08:00
}};
constexpr std::array<float,MAX_AMBI_ORDER+1> Ambi3DDecoderHFScale2O{{
7.45355990e-01f, 1.00000000e+00f, 1.00000000e+00f
}};
constexpr std::array<float,MAX_AMBI_ORDER+1> Ambi3DDecoderHFScale3O{{
5.89792205e-01f, 8.79693856e-01f, 1.00000000e+00f, 1.00000000e+00f
}};
inline auto GetDecoderHFScales(ALuint order) noexcept -> const std::array<float,MAX_AMBI_ORDER+1>&
{
if(order >= 3) return Ambi3DDecoderHFScale3O;
if(order == 2) return Ambi3DDecoderHFScale2O;
return Ambi3DDecoderHFScale;
}
2020-01-14 13:10:01 -08:00
inline auto GetAmbiScales(AmbDecScale scaletype) noexcept
-> const std::array<float,MAX_AMBI_CHANNELS>&
{
2018-12-20 02:46:59 -08:00
if(scaletype == AmbDecScale::FuMa) return AmbiScale::FromFuMa;
if(scaletype == AmbDecScale::SN3D) return AmbiScale::FromSN3D;
return AmbiScale::FromN3D;
}
} // namespace
BFormatDec::BFormatDec(const AmbDecConf *conf, const bool allow_2band, const size_t inchans,
const ALuint srate, const ALuint (&chanmap)[MAX_OUTPUT_CHANNELS]) : mChannelDec{inchans}
{
mDualBand = allow_2band && (conf->FreqBands == 2);
const bool periphonic{(conf->ChanMask&AMBI_PERIPHONIC_MASK) != 0};
const std::array<float,MAX_AMBI_CHANNELS> &coeff_scale = GetAmbiScales(conf->CoeffScale);
2018-12-10 21:30:22 -08:00
2018-12-13 22:48:02 -08:00
if(!mDualBand)
{
for(size_t j{0},k{0};j < mChannelDec.size();++j)
{
const size_t acn{periphonic ? j : AmbiIndex::From2D[j]};
if(!(conf->ChanMask&(1u<<acn))) continue;
const size_t order{AmbiIndex::OrderFromChannel[acn]};
const float gain{conf->HFOrderGain[order] / coeff_scale[acn]};
for(size_t i{0u};i < conf->Speakers.size();++i)
{
const size_t chanidx{chanmap[i]};
mChannelDec[j].mGains.Single[chanidx] = conf->HFMatrix[i][k] * gain;
}
++k;
}
}
else
{
mChannelDec[0].mXOver.init(conf->XOverFreq / static_cast<float>(srate));
for(size_t j{1};j < mChannelDec.size();++j)
mChannelDec[j].mXOver = mChannelDec[0].mXOver;
2018-12-10 21:30:22 -08:00
const float ratio{std::pow(10.0f, conf->XOverRatio / 40.0f)};
for(size_t j{0},k{0};j < mChannelDec.size();++j)
{
const size_t acn{periphonic ? j : AmbiIndex::From2D[j]};
if(!(conf->ChanMask&(1u<<acn))) continue;
const size_t order{AmbiIndex::OrderFromChannel[acn]};
const float hfGain{conf->HFOrderGain[order] * ratio / coeff_scale[acn]};
const float lfGain{conf->LFOrderGain[order] / ratio / coeff_scale[acn]};
for(size_t i{0u};i < conf->Speakers.size();++i)
{
const size_t chanidx{chanmap[i]};
mChannelDec[j].mGains.Dual[sHFBand][chanidx] = conf->HFMatrix[i][k] * hfGain;
mChannelDec[j].mGains.Dual[sLFBand][chanidx] = conf->LFMatrix[i][k] * lfGain;
}
++k;
}
}
}
BFormatDec::BFormatDec(const size_t inchans, const al::span<const ChannelDec> coeffs,
const al::span<const ChannelDec> coeffslf) : mChannelDec{inchans}
{
mDualBand = !coeffslf.empty();
if(!mDualBand)
{
for(size_t j{0};j < mChannelDec.size();++j)
{
float *outcoeffs{mChannelDec[j].mGains.Single};
for(const ChannelDec &incoeffs : coeffs)
*(outcoeffs++) = incoeffs[j];
}
}
else
{
for(size_t j{0};j < mChannelDec.size();++j)
{
float *outcoeffs{mChannelDec[j].mGains.Dual[sHFBand]};
for(const ChannelDec &incoeffs : coeffs)
*(outcoeffs++) = incoeffs[j];
outcoeffs = mChannelDec[j].mGains.Dual[sLFBand];
for(const ChannelDec &incoeffs : coeffslf)
*(outcoeffs++) = incoeffs[j];
}
}
}
2019-07-03 22:59:29 -07:00
void BFormatDec::process(const al::span<FloatBufferLine> OutBuffer,
const FloatBufferLine *InSamples, const size_t SamplesToDo)
{
2019-08-03 13:05:42 -07:00
ASSUME(SamplesToDo > 0);
if(mDualBand)
{
2020-05-05 16:56:25 -07:00
const al::span<float> hfSamples{mSamples[sHFBand].data(), SamplesToDo};
const al::span<float> lfSamples{mSamples[sLFBand].data(), SamplesToDo};
for(auto &chandec : mChannelDec)
{
2020-05-05 16:56:25 -07:00
chandec.mXOver.process({InSamples->data(), SamplesToDo}, hfSamples.data(),
lfSamples.data());
MixSamples(hfSamples, OutBuffer, chandec.mGains.Dual[sHFBand],
chandec.mGains.Dual[sHFBand], 0, 0);
2020-05-05 16:56:25 -07:00
MixSamples(lfSamples, OutBuffer, chandec.mGains.Dual[sLFBand],
chandec.mGains.Dual[sLFBand], 0, 0);
++InSamples;
}
}
else
{
for(auto &chandec : mChannelDec)
{
MixSamples({InSamples->data(), SamplesToDo}, OutBuffer, chandec.mGains.Single,
chandec.mGains.Single, 0, 0);
++InSamples;
}
}
}
2020-01-14 13:10:01 -08:00
auto BFormatDec::GetHFOrderScales(const ALuint in_order, const ALuint out_order) noexcept
-> std::array<float,MAX_AMBI_ORDER+1>
{
2020-01-14 13:10:01 -08:00
std::array<float,MAX_AMBI_ORDER+1> ret{};
assert(out_order >= in_order);
2020-01-14 13:10:01 -08:00
const auto &target = GetDecoderHFScales(out_order);
const auto &input = GetDecoderHFScales(in_order);
2020-01-14 13:10:01 -08:00
for(size_t i{0};i < in_order+1;++i)
ret[i] = input[i] / target[i];
return ret;
}