58 { QAudioFormat::ChannelConfigSurround5Dot0,
59 {{ decoderMatrix_5dot0_1_lf, decoderMatrix_5dot0_2_lf, decoderMatrix_5dot0_3_lf }},
60 {{ decoderMatrix_5dot0_1_hf, decoderMatrix_5dot0_2_hf, decoderMatrix_5dot0_3_hf }},
62 { QAudioFormat::ChannelConfigSurround5Dot1,
63 {{ decoderMatrix_5dot1_1_lf, decoderMatrix_5dot1_2_lf, decoderMatrix_5dot1_3_lf }},
64 {{ decoderMatrix_5dot1_1_hf, decoderMatrix_5dot1_2_hf, decoderMatrix_5dot1_3_hf }},
66 { QAudioFormat::ChannelConfigSurround7Dot0,
67 {{ decoderMatrix_7dot0_1_lf, decoderMatrix_7dot0_2_lf, decoderMatrix_7dot0_3_lf }},
68 {{ decoderMatrix_7dot0_1_hf, decoderMatrix_7dot0_2_hf, decoderMatrix_7dot0_3_hf }},
70 { QAudioFormat::ChannelConfigSurround7Dot1,
71 {{ decoderMatrix_7dot1_1_lf, decoderMatrix_7dot1_2_lf, decoderMatrix_7dot1_3_lf }},
72 {{ decoderMatrix_7dot1_1_hf, decoderMatrix_7dot1_2_hf, decoderMatrix_7dot1_3_hf }},
144 : order(ambisonicOrder)
146 Q_ASSERT(order > 0 && order <= 3);
147 inputChannels = (order + 1) * (order + 1);
148 outputChannels = format.channelCount();
150 channelConfig = format.channelConfig();
151 if (channelConfig == QAudioFormat::ChannelConfigUnknown)
152 channelConfig = QAudioFormat::defaultChannelConfigForChannelCount(format.channelCount());
154 if (channelConfig == QAudioFormat::ChannelConfigMono ||
155 channelConfig == QAudioFormat::ChannelConfigStereo ||
156 channelConfig == QAudioFormat::ChannelConfig2Dot1 ||
157 channelConfig == QAudioFormat::ChannelConfig3Dot0 ||
158 channelConfig == QAudioFormat::ChannelConfig3Dot1) {
168 simpleDecoderFactors = std::make_unique<
float[]>(4 * outputChannels);
169 m_reverbFactorsOwned = std::make_unique<
float[]>(2 * outputChannels);
170 reverbFactors = m_reverbFactorsOwned.get();
171 float *f = simpleDecoderFactors.get();
172 float *r = m_reverbFactorsOwned.get();
173 if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::FrontLeft)) {
174 f[0] = 0.5f; f[1] = 0.5f; f[2] = 0.; f[3] = 0.f;
176 r[0] = 1.; r[1] = 0.;
179 if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::FrontRight)) {
180 f[0] = 0.5f; f[1] = -0.5f; f[2] = 0.; f[3] = 0.f;
182 r[0] = 0.; r[1] = 1.;
185 if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::FrontCenter)) {
186 f[0] = 0.5f; f[1] = -0.f; f[2] = 0.; f[3] = 0.5f;
188 r[0] = .5; r[1] = .5;
191 if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::LFE)) {
192 f[0] = 0.5f; f[1] = -0.f; f[2] = 0.; f[3] = 0.0f;
194 r[0] = 0.; r[1] = 0.;
199 Q_ASSERT((f - simpleDecoderFactors.get()) == 4 * outputChannels);
200 Q_ASSERT((r - reverbFactors) == 2*outputChannels);
205 for (
const auto &d : decoderMap) {
206 if (d.config == channelConfig) {
208 reverbFactors = decoderData->reverb;
218 filters = std::make_unique<QAmbisonicDecoderFilter[]>(inputChannels);
219 for (
int i = 0; i < inputChannels; ++i)
220 filters[i].configure(format.sampleRate());
227 const int nSamples =
int(output.size()) / outputChannels;
228 std::fill(output.begin(), output.end(), 0.f);
229 float *o = output.data();
231 if (simpleDecoderFactors) {
232 for (
int i = 0; i < nSamples; ++i) {
233 for (
int j = 0; j < 4; ++j) {
234 for (
int k = 0; k < outputChannels; ++k)
235 o[k] += simpleDecoderFactors[k*4 + j]*input[j][i];
242 const float *matrix_hi = decoderData->hf[order - 1];
243 const float *matrix_lo = decoderData->lf[order - 1];
244 for (
int i = 0; i < nSamples; ++i) {
246 for (
int j = 0; j < inputChannels; ++j)
247 buf[j] = filters[j].next(input[j][i]);
248 for (
int j = 0; j < inputChannels; ++j) {
249 for (
int k = 0; k < outputChannels; ++k)
250 o[k] += matrix_lo[k*inputChannels + j]*buf[j].lf + matrix_hi[k*inputChannels + j]*buf[j].hf;
263 QSpan<
const float *, 2> reverb, QSpan<
short> output)
265 Q_ASSERT(outputChannels > 0);
266 Q_ASSERT(
int(output.size()) % outputChannels == 0);
267 const int nSamples =
int(output.size()) / outputChannels;
269 using QtMultimediaPrivate::drop;
271 if (simpleDecoderFactors) {
273 for (
int i = 0; i < nSamples; ++i) {
274 std::array<
float, 4> o = {};
275 for (
int k = 0; k < outputChannels; ++k) {
276 for (
int j = 0; j < 4; ++j)
277 o[k] += simpleDecoderFactors[k*4 + j]*input[j][i];
280 for (
int k = 0; k < outputChannels; ++k) {
281 o[k] += reverb[0][i]*reverbFactors[2*k] + reverb[1][i]*reverbFactors[2*k+1];
285 for (
int k = 0; k < outputChannels; ++k)
286 out[k] =
static_cast<
short>(o[k] * 32768.);
287 out = drop(out, outputChannels);
295 const float *matrix_hi = decoderData->hf[order - 1];
296 const float *matrix_lo = decoderData->lf[order - 1];
297 for (
int i = 0; i < nSamples; ++i) {
300 for (
int j = 0; j < inputChannels; ++j)
301 buf[j] = filters[j].next(input[j][i]);
302 std::array<
float, 32> o = {};
303 for (
int j = 0; j < inputChannels; ++j) {
304 for (
int k = 0; k < outputChannels; ++k)
305 o[k] += matrix_lo[k*inputChannels + j]*buf[j].lf + matrix_hi[k*inputChannels + j]*buf[j].hf;
308 for (
int k = 0; k < outputChannels; ++k) {
309 o[k] += reverb[0][i]*reverbFactors[2*k] + reverb[1][i]*reverbFactors[2*k+1];
312 for (
int k = 0; k < outputChannels; ++k)
313 out[k] =
static_cast<
short>(o[k] * 32768.);
314 out = drop(out, outputChannels);