Round the B-Format HRTF response where the multiple is defined
[openal-soft.git] / Alc / panning.c
bloba5cddcb26ed01f21762b9ab8b6e6125076d7385a
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2010 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <assert.h>
29 #include "alMain.h"
30 #include "alAuxEffectSlot.h"
31 #include "alu.h"
32 #include "bool.h"
33 #include "ambdec.h"
34 #include "bformatdec.h"
35 #include "uhjfilter.h"
36 #include "bs2b.h"
39 extern inline void CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
42 static const ALsizei FuMa2ACN[MAX_AMBI_COEFFS] = {
43 0, /* W */
44 3, /* X */
45 1, /* Y */
46 2, /* Z */
47 6, /* R */
48 7, /* S */
49 5, /* T */
50 8, /* U */
51 4, /* V */
52 12, /* K */
53 13, /* L */
54 11, /* M */
55 14, /* N */
56 10, /* O */
57 15, /* P */
58 9, /* Q */
60 static const ALsizei ACN2ACN[MAX_AMBI_COEFFS] = {
61 0, 1, 2, 3, 4, 5, 6, 7,
62 8, 9, 10, 11, 12, 13, 14, 15
65 /* NOTE: These are scale factors as applied to Ambisonics content. Decoder
66 * coefficients should be divided by these values to get proper N3D scalings.
68 static const ALfloat UnitScale[MAX_AMBI_COEFFS] = {
69 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
70 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
72 static const ALfloat SN3D2N3DScale[MAX_AMBI_COEFFS] = {
73 1.000000000f, /* ACN 0 (W), sqrt(1) */
74 1.732050808f, /* ACN 1 (Y), sqrt(3) */
75 1.732050808f, /* ACN 2 (Z), sqrt(3) */
76 1.732050808f, /* ACN 3 (X), sqrt(3) */
77 2.236067978f, /* ACN 4 (V), sqrt(5) */
78 2.236067978f, /* ACN 5 (T), sqrt(5) */
79 2.236067978f, /* ACN 6 (R), sqrt(5) */
80 2.236067978f, /* ACN 7 (S), sqrt(5) */
81 2.236067978f, /* ACN 8 (U), sqrt(5) */
82 2.645751311f, /* ACN 9 (Q), sqrt(7) */
83 2.645751311f, /* ACN 10 (O), sqrt(7) */
84 2.645751311f, /* ACN 11 (M), sqrt(7) */
85 2.645751311f, /* ACN 12 (K), sqrt(7) */
86 2.645751311f, /* ACN 13 (L), sqrt(7) */
87 2.645751311f, /* ACN 14 (N), sqrt(7) */
88 2.645751311f, /* ACN 15 (P), sqrt(7) */
90 static const ALfloat FuMa2N3DScale[MAX_AMBI_COEFFS] = {
91 1.414213562f, /* ACN 0 (W), sqrt(2) */
92 1.732050808f, /* ACN 1 (Y), sqrt(3) */
93 1.732050808f, /* ACN 2 (Z), sqrt(3) */
94 1.732050808f, /* ACN 3 (X), sqrt(3) */
95 1.936491673f, /* ACN 4 (V), sqrt(15)/2 */
96 1.936491673f, /* ACN 5 (T), sqrt(15)/2 */
97 2.236067978f, /* ACN 6 (R), sqrt(5) */
98 1.936491673f, /* ACN 7 (S), sqrt(15)/2 */
99 1.936491673f, /* ACN 8 (U), sqrt(15)/2 */
100 2.091650066f, /* ACN 9 (Q), sqrt(35/8) */
101 1.972026594f, /* ACN 10 (O), sqrt(35)/3 */
102 2.231093404f, /* ACN 11 (M), sqrt(224/45) */
103 2.645751311f, /* ACN 12 (K), sqrt(7) */
104 2.231093404f, /* ACN 13 (L), sqrt(224/45) */
105 1.972026594f, /* ACN 14 (N), sqrt(35)/3 */
106 2.091650066f, /* ACN 15 (P), sqrt(35/8) */
110 void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
112 /* Convert from OpenAL coords to Ambisonics. */
113 ALfloat x = -dir[2];
114 ALfloat y = -dir[0];
115 ALfloat z = dir[1];
117 /* Zeroth-order */
118 coeffs[0] = 1.0f; /* ACN 0 = 1 */
119 /* First-order */
120 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
121 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
122 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
123 /* Second-order */
124 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
125 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
126 coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
127 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
128 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
129 /* Third-order */
130 coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
131 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
132 coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
133 coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
134 coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
135 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
136 coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
138 if(spread > 0.0f)
140 /* Implement the spread by using a spherical source that subtends the
141 * angle spread. See:
142 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
144 * When adjusted for N3D normalization instead of SN3D, these
145 * calculations are:
147 * ZH0 = -sqrt(pi) * (-1+ca);
148 * ZH1 = 0.5*sqrt(pi) * sa*sa;
149 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
150 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
151 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
152 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
154 * The gain of the source is compensated for size, so that the
155 * loundness doesn't depend on the spread. That is, the factors are
156 * scaled so that ZH0 remains 1 regardless of the spread. Thus:
158 * ZH0 = 1.0f;
159 * ZH1 = 0.5f * (ca+1.0f);
160 * ZH2 = 0.5f * (ca+1.0f)*ca;
161 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
162 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
163 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
165 ALfloat ca = cosf(spread * 0.5f);
167 ALfloat ZH0_norm = 1.0f;
168 ALfloat ZH1_norm = 0.5f * (ca+1.f);
169 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca;
170 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f);
172 /* Zeroth-order */
173 coeffs[0] *= ZH0_norm;
174 /* First-order */
175 coeffs[1] *= ZH1_norm;
176 coeffs[2] *= ZH1_norm;
177 coeffs[3] *= ZH1_norm;
178 /* Second-order */
179 coeffs[4] *= ZH2_norm;
180 coeffs[5] *= ZH2_norm;
181 coeffs[6] *= ZH2_norm;
182 coeffs[7] *= ZH2_norm;
183 coeffs[8] *= ZH2_norm;
184 /* Third-order */
185 coeffs[9] *= ZH3_norm;
186 coeffs[10] *= ZH3_norm;
187 coeffs[11] *= ZH3_norm;
188 coeffs[12] *= ZH3_norm;
189 coeffs[13] *= ZH3_norm;
190 coeffs[14] *= ZH3_norm;
191 coeffs[15] *= ZH3_norm;
195 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
197 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
198 if(!(fabsf(azimuth) > F_PI_2))
199 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
200 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
204 void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
206 ALsizei i;
208 for(i = 0;i < numchans;i++)
209 gains[i] = chancoeffs[i][0] * 1.414213562f * ingain;
210 for(;i < MAX_OUTPUT_CHANNELS;i++)
211 gains[i] = 0.0f;
214 void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
216 ALfloat gain = 0.0f;
217 ALsizei i;
219 for(i = 0;i < numchans;i++)
221 if(chanmap[i].Index == 0)
222 gain += chanmap[i].Scale;
224 gains[0] = gain * 1.414213562f * ingain;
225 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
226 gains[i] = 0.0f;
229 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
231 ALsizei i, j;
233 for(i = 0;i < numchans;i++)
235 float gain = 0.0f;
236 for(j = 0;j < numcoeffs;j++)
237 gain += chancoeffs[i][j]*coeffs[j];
238 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
240 for(;i < MAX_OUTPUT_CHANNELS;i++)
241 gains[i] = 0.0f;
244 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
246 ALsizei i;
248 for(i = 0;i < numchans;i++)
249 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
250 for(;i < MAX_OUTPUT_CHANNELS;i++)
251 gains[i] = 0.0f;
254 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
256 ALsizei i, j;
258 for(i = 0;i < numchans;i++)
260 float gain = 0.0f;
261 for(j = 0;j < 4;j++)
262 gain += chancoeffs[i][j] * mtx[j];
263 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
265 for(;i < MAX_OUTPUT_CHANNELS;i++)
266 gains[i] = 0.0f;
269 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
271 ALsizei i;
273 for(i = 0;i < numchans;i++)
274 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
275 for(;i < MAX_OUTPUT_CHANNELS;i++)
276 gains[i] = 0.0f;
280 static inline const char *GetLabelFromChannel(enum Channel channel)
282 switch(channel)
284 case FrontLeft: return "front-left";
285 case FrontRight: return "front-right";
286 case FrontCenter: return "front-center";
287 case LFE: return "lfe";
288 case BackLeft: return "back-left";
289 case BackRight: return "back-right";
290 case BackCenter: return "back-center";
291 case SideLeft: return "side-left";
292 case SideRight: return "side-right";
294 case UpperFrontLeft: return "upper-front-left";
295 case UpperFrontRight: return "upper-front-right";
296 case UpperBackLeft: return "upper-back-left";
297 case UpperBackRight: return "upper-back-right";
298 case LowerFrontLeft: return "lower-front-left";
299 case LowerFrontRight: return "lower-front-right";
300 case LowerBackLeft: return "lower-back-left";
301 case LowerBackRight: return "lower-back-right";
303 case Aux0: return "aux-0";
304 case Aux1: return "aux-1";
305 case Aux2: return "aux-2";
306 case Aux3: return "aux-3";
307 case Aux4: return "aux-4";
308 case Aux5: return "aux-5";
309 case Aux6: return "aux-6";
310 case Aux7: return "aux-7";
311 case Aux8: return "aux-8";
312 case Aux9: return "aux-9";
313 case Aux10: return "aux-10";
314 case Aux11: return "aux-11";
315 case Aux12: return "aux-12";
316 case Aux13: return "aux-13";
317 case Aux14: return "aux-14";
318 case Aux15: return "aux-15";
320 case InvalidChannel: break;
322 return "(unknown)";
326 typedef struct ChannelMap {
327 enum Channel ChanName;
328 ChannelConfig Config;
329 } ChannelMap;
331 static void SetChannelMap(const enum Channel *devchans, ChannelConfig *ambicoeffs,
332 const ChannelMap *chanmap, size_t count, ALsizei *outcount)
334 size_t j, k;
335 ALsizei i;
337 for(i = 0;i < MAX_OUTPUT_CHANNELS && devchans[i] != InvalidChannel;i++)
339 if(devchans[i] == LFE)
341 for(j = 0;j < MAX_AMBI_COEFFS;j++)
342 ambicoeffs[i][j] = 0.0f;
343 continue;
346 for(j = 0;j < count;j++)
348 if(devchans[i] != chanmap[j].ChanName)
349 continue;
351 for(k = 0;k < MAX_AMBI_COEFFS;++k)
352 ambicoeffs[i][k] = chanmap[j].Config[k];
353 break;
355 if(j == count)
356 ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans[i]), i);
358 *outcount = i;
361 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
363 ALsizei i;
365 for(i = 0;i < conf->NumSpeakers;i++)
367 int c = -1;
369 /* NOTE: AmbDec does not define any standard speaker names, however
370 * for this to work we have to by able to find the output channel
371 * the speaker definition corresponds to. Therefore, OpenAL Soft
372 * requires these channel labels to be recognized:
374 * LF = Front left
375 * RF = Front right
376 * LS = Side left
377 * RS = Side right
378 * LB = Back left
379 * RB = Back right
380 * CE = Front center
381 * CB = Back center
383 * Additionally, surround51 will acknowledge back speakers for side
384 * channels, and surround51rear will acknowledge side speakers for
385 * back channels, to avoid issues with an ambdec expecting 5.1 to
386 * use the side channels when the device is configured for back,
387 * and vice-versa.
389 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
390 c = GetChannelIdxByName(device->RealOut, FrontLeft);
391 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
392 c = GetChannelIdxByName(device->RealOut, FrontRight);
393 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
394 c = GetChannelIdxByName(device->RealOut, FrontCenter);
395 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
397 if(device->FmtChans == DevFmtX51Rear)
398 c = GetChannelIdxByName(device->RealOut, BackLeft);
399 else
400 c = GetChannelIdxByName(device->RealOut, SideLeft);
402 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
404 if(device->FmtChans == DevFmtX51Rear)
405 c = GetChannelIdxByName(device->RealOut, BackRight);
406 else
407 c = GetChannelIdxByName(device->RealOut, SideRight);
409 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
411 if(device->FmtChans == DevFmtX51)
412 c = GetChannelIdxByName(device->RealOut, SideLeft);
413 else
414 c = GetChannelIdxByName(device->RealOut, BackLeft);
416 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
418 if(device->FmtChans == DevFmtX51)
419 c = GetChannelIdxByName(device->RealOut, SideRight);
420 else
421 c = GetChannelIdxByName(device->RealOut, BackRight);
423 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
424 c = GetChannelIdxByName(device->RealOut, BackCenter);
425 else
427 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
428 unsigned int n;
429 char ch;
431 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
432 c = GetChannelIdxByName(device->RealOut, Aux0+n);
433 else
435 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
436 return false;
439 if(c == -1)
441 ERR("Failed to lookup AmbDec speaker label %s\n",
442 alstr_get_cstr(conf->Speakers[i].Name));
443 return false;
445 speakermap[i] = c;
448 return true;
452 static const ChannelMap MonoCfg[1] = {
453 { FrontCenter, { 1.0f } },
454 }, StereoCfg[2] = {
455 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
456 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
457 }, QuadCfg[4] = {
458 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
459 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
460 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
461 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
462 }, X51SideCfg[5] = {
463 { SideLeft, { 3.33001372e-1f, 1.89085671e-1f, 0.0f, -2.00041334e-1f, -2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
464 { FrontLeft, { 1.47751298e-1f, 1.28994110e-1f, 0.0f, 1.15190495e-1f, 7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
465 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
466 { FrontRight, { 1.47751298e-1f, -1.28994110e-1f, 0.0f, 1.15190495e-1f, -7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
467 { SideRight, { 3.33001372e-1f, -1.89085671e-1f, 0.0f, -2.00041334e-1f, 2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
468 }, X51RearCfg[5] = {
469 { BackLeft, { 3.33001372e-1f, 1.89085671e-1f, 0.0f, -2.00041334e-1f, -2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
470 { FrontLeft, { 1.47751298e-1f, 1.28994110e-1f, 0.0f, 1.15190495e-1f, 7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
471 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
472 { FrontRight, { 1.47751298e-1f, -1.28994110e-1f, 0.0f, 1.15190495e-1f, -7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
473 { BackRight, { 3.33001372e-1f, -1.89085671e-1f, 0.0f, -2.00041334e-1f, 2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
474 }, X61Cfg[6] = {
475 { SideLeft, { 2.04462744e-1f, 2.17178497e-1f, 0.0f, -4.39990188e-2f, -2.60787329e-2f, 0.0f, 0.0f, 0.0f, -6.87238843e-2f } },
476 { FrontLeft, { 1.18130342e-1f, 9.34633906e-2f, 0.0f, 1.08553749e-1f, 6.80658795e-2f, 0.0f, 0.0f, 0.0f, 1.08999485e-2f } },
477 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
478 { FrontRight, { 1.18130342e-1f, -9.34633906e-2f, 0.0f, 1.08553749e-1f, -6.80658795e-2f, 0.0f, 0.0f, 0.0f, 1.08999485e-2f } },
479 { SideRight, { 2.04462744e-1f, -2.17178497e-1f, 0.0f, -4.39990188e-2f, 2.60787329e-2f, 0.0f, 0.0f, 0.0f, -6.87238843e-2f } },
480 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
481 }, X71Cfg[6] = {
482 { BackLeft, { 2.04124145e-1f, 1.08880247e-1f, 0.0f, -1.88586120e-1f, -1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, 3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
483 { SideLeft, { 2.04124145e-1f, 2.17760495e-1f, 0.0f, 0.00000000e+0f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, -1.49071198e-1f, -3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
484 { FrontLeft, { 2.04124145e-1f, 1.08880247e-1f, 0.0f, 1.88586120e-1f, 1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, 3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
485 { FrontRight, { 2.04124145e-1f, -1.08880247e-1f, 0.0f, 1.88586120e-1f, -1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, -3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
486 { SideRight, { 2.04124145e-1f, -2.17760495e-1f, 0.0f, 0.00000000e+0f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, -1.49071198e-1f, 3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
487 { BackRight, { 2.04124145e-1f, -1.08880247e-1f, 0.0f, -1.88586120e-1f, 1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, -3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
490 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
492 const char *devname = alstr_get_cstr(device->DeviceName);
493 ALsizei i;
495 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
497 /* NFC is only used when AvgSpeakerDist is greater than 0, and
498 * METERS_PER_UNIT is also greater than 0. In addition, NFC can only be
499 * used when rendering to an ambisonic buffer.
501 device->AvgSpeakerDist = ctrl_dist;
503 device->Dry.NumChannelsPerOrder[0] = 1;
504 if(periphonic)
505 for(i = 1;i < order+1;i++)
506 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
507 else
508 for(i = 1;i < order+1;i++)
509 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
510 for(;i < MAX_AMBI_ORDER+1;i++)
511 device->Dry.NumChannelsPerOrder[i] = 0;
515 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
517 const char *devname = alstr_get_cstr(device->DeviceName);
518 ALfloat maxdist = 0.0f;
519 ALsizei total = 0;
520 ALsizei i;
522 for(i = 0;i < conf->NumSpeakers;i++)
523 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
525 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
527 ALfloat srate = (ALfloat)device->Frequency;
528 for(i = 0;i < conf->NumSpeakers;i++)
530 ALsizei chan = speakermap[i];
531 ALfloat delay;
533 /* Distance compensation only delays in steps of the sample rate.
534 * This is a bit less accurate since the delay time falls to the
535 * nearest sample time, but it's far simpler as it doesn't have to
536 * deal with phase offsets. This means at 48khz, for instance, the
537 * distance delay will be in steps of about 7 millimeters.
539 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
540 srate + 0.5f);
541 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
542 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
543 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
545 device->ChannelDelay[chan].Length = (ALsizei)clampf(
546 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
548 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
549 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
550 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
551 device->ChannelDelay[chan].Gain
554 /* Round up to the next 4th sample, so each channel buffer starts
555 * 16-byte aligned.
557 total += RoundUp(device->ChannelDelay[chan].Length, 4);
561 if(total > 0)
563 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
564 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
566 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
567 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
572 static void InitPanning(ALCdevice *device)
574 const ChannelMap *chanmap = NULL;
575 ALsizei coeffcount = 0;
576 ALsizei count = 0;
577 ALsizei i, j;
579 switch(device->FmtChans)
581 case DevFmtMono:
582 count = COUNTOF(MonoCfg);
583 chanmap = MonoCfg;
584 coeffcount = 1;
585 break;
587 case DevFmtStereo:
588 count = COUNTOF(StereoCfg);
589 chanmap = StereoCfg;
590 coeffcount = 4;
591 break;
593 case DevFmtQuad:
594 count = COUNTOF(QuadCfg);
595 chanmap = QuadCfg;
596 coeffcount = 4;
597 break;
599 case DevFmtX51:
600 count = COUNTOF(X51SideCfg);
601 chanmap = X51SideCfg;
602 coeffcount = 9;
603 break;
605 case DevFmtX51Rear:
606 count = COUNTOF(X51RearCfg);
607 chanmap = X51RearCfg;
608 coeffcount = 9;
609 break;
611 case DevFmtX61:
612 count = COUNTOF(X61Cfg);
613 chanmap = X61Cfg;
614 coeffcount = 9;
615 break;
617 case DevFmtX71:
618 count = COUNTOF(X71Cfg);
619 chanmap = X71Cfg;
620 coeffcount = 16;
621 break;
623 case DevFmtAmbi3D:
624 break;
627 if(device->FmtChans == DevFmtAmbi3D)
629 const char *devname = alstr_get_cstr(device->DeviceName);
630 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
631 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
632 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
633 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ UnitScale;
634 ALfloat nfc_delay = 0.0f;
636 count = (device->AmbiOrder == 3) ? 16 :
637 (device->AmbiOrder == 2) ? 9 :
638 (device->AmbiOrder == 1) ? 4 : 1;
639 for(i = 0;i < count;i++)
641 ALsizei acn = acnmap[i];
642 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
643 device->Dry.Ambi.Map[i].Index = acn;
645 device->Dry.CoeffCount = 0;
646 device->Dry.NumChannels = count;
648 if(device->AmbiOrder < 2)
650 device->FOAOut.Ambi = device->Dry.Ambi;
651 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
652 device->FOAOut.NumChannels = 0;
654 else
656 /* FOA output is always ACN+N3D for higher-order ambisonic output.
657 * The upsampler expects this and will convert it for output.
659 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
660 for(i = 0;i < 4;i++)
662 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
663 device->FOAOut.Ambi.Map[i].Index = i;
665 device->FOAOut.CoeffCount = 0;
666 device->FOAOut.NumChannels = 4;
668 ambiup_reset(device->AmbiUp, device);
671 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
673 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
674 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
675 device->AmbiOrder, true);
678 else
680 ALfloat w_scale, xyz_scale;
682 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
683 chanmap, count, &device->Dry.NumChannels);
684 device->Dry.CoeffCount = coeffcount;
686 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE2D_THIRD :
687 (device->Dry.CoeffCount > 4) ? W_SCALE2D_SECOND : 1.0f;
688 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE2D_THIRD :
689 (device->Dry.CoeffCount > 4) ? XYZ_SCALE2D_SECOND : 1.0f;
691 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
692 for(i = 0;i < device->Dry.NumChannels;i++)
694 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
695 for(j = 1;j < 4;j++)
696 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
698 device->FOAOut.CoeffCount = 4;
699 device->FOAOut.NumChannels = 0;
701 device->RealOut.NumChannels = 0;
704 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
706 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
707 const ALfloat *coeff_scale = UnitScale;
708 ALfloat w_scale = 1.0f;
709 ALfloat xyz_scale = 1.0f;
710 ALsizei i, j;
712 if(conf->FreqBands != 1)
713 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
714 conf->XOverFreq);
716 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
718 if(conf->ChanMask > 0x1ff)
720 w_scale = W_SCALE3D_THIRD;
721 xyz_scale = XYZ_SCALE3D_THIRD;
723 else if(conf->ChanMask > 0xf)
725 w_scale = W_SCALE3D_SECOND;
726 xyz_scale = XYZ_SCALE3D_SECOND;
729 else
731 if(conf->ChanMask > 0x1ff)
733 w_scale = W_SCALE2D_THIRD;
734 xyz_scale = XYZ_SCALE2D_THIRD;
736 else if(conf->ChanMask > 0xf)
738 w_scale = W_SCALE2D_SECOND;
739 xyz_scale = XYZ_SCALE2D_SECOND;
743 if(conf->CoeffScale == ADS_SN3D)
744 coeff_scale = SN3D2N3DScale;
745 else if(conf->CoeffScale == ADS_FuMa)
746 coeff_scale = FuMa2N3DScale;
748 for(i = 0;i < conf->NumSpeakers;i++)
750 ALsizei chan = speakermap[i];
751 ALfloat gain;
752 ALsizei k = 0;
754 for(j = 0;j < MAX_AMBI_COEFFS;j++)
755 chanmap[i].Config[j] = 0.0f;
757 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
758 for(j = 0;j < MAX_AMBI_COEFFS;j++)
760 if(j == 0) gain = conf->HFOrderGain[0];
761 else if(j == 1) gain = conf->HFOrderGain[1];
762 else if(j == 4) gain = conf->HFOrderGain[2];
763 else if(j == 9) gain = conf->HFOrderGain[3];
764 if((conf->ChanMask&(1<<j)))
765 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
769 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
770 conf->NumSpeakers, &device->Dry.NumChannels);
771 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
772 (conf->ChanMask > 0xf) ? 9 : 4;
774 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
775 for(i = 0;i < device->Dry.NumChannels;i++)
777 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
778 for(j = 1;j < 4;j++)
779 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
781 device->FOAOut.CoeffCount = 4;
782 device->FOAOut.NumChannels = 0;
784 device->RealOut.NumChannels = 0;
786 InitDistanceComp(device, conf, speakermap);
789 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
791 ALfloat avg_dist;
792 ALsizei count;
793 ALsizei i;
795 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
797 count = (conf->ChanMask > 0x1ff) ? 16 :
798 (conf->ChanMask > 0xf) ? 9 : 4;
799 for(i = 0;i < count;i++)
801 device->Dry.Ambi.Map[i].Scale = 1.0f;
802 device->Dry.Ambi.Map[i].Index = i;
805 else
807 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
809 count = (conf->ChanMask > 0x1ff) ? 7 :
810 (conf->ChanMask > 0xf) ? 5 : 3;
811 for(i = 0;i < count;i++)
813 device->Dry.Ambi.Map[i].Scale = 1.0f;
814 device->Dry.Ambi.Map[i].Index = map[i];
817 device->Dry.CoeffCount = 0;
818 device->Dry.NumChannels = count;
820 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
821 (conf->FreqBands == 1) ? "single" : "dual",
822 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
823 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
825 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
827 if(!(conf->ChanMask > 0xf))
829 device->FOAOut.Ambi = device->Dry.Ambi;
830 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
831 device->FOAOut.NumChannels = 0;
833 else
835 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
836 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
838 count = 4;
839 for(i = 0;i < count;i++)
841 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
842 device->FOAOut.Ambi.Map[i].Index = i;
845 else
847 static const int map[3] = { 0, 1, 3 };
848 count = 3;
849 for(i = 0;i < count;i++)
851 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
852 device->FOAOut.Ambi.Map[i].Index = map[i];
855 device->FOAOut.CoeffCount = 0;
856 device->FOAOut.NumChannels = count;
859 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
861 avg_dist = 0.0f;
862 for(i = 0;i < conf->NumSpeakers;i++)
863 avg_dist += conf->Speakers[i].Distance;
864 avg_dist /= (ALfloat)conf->NumSpeakers;
865 InitNearFieldCtrl(device, avg_dist,
866 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
867 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
870 InitDistanceComp(device, conf, speakermap);
873 static void InitHrtfPanning(ALCdevice *device)
875 /* NOTE: azimuth goes clockwise. */
876 static const ALfloat AmbiPoints[][2] = {
877 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
878 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
879 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
880 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
881 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
882 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
883 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
884 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
885 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
886 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
887 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
888 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
889 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
890 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
892 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
893 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
894 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
895 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
896 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
897 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
898 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
899 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
900 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
901 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
902 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
903 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
904 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
905 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
906 { { 1.88982237e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
907 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
908 { { 1.43315266e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 1.18020996e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.26741039e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 1.49618920e-001f, 0.00000000e+000f, 0.00000000e+000f } },
909 { { 1.40852210e-001f, 1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f, 7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, 7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f, 9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, 9.71456952e-002f, 0.00000000e+000f } },
910 { { 1.40852210e-001f, -1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f, -7.58818830e-002f, -7.66295578e-002f, -3.28314629e-004f, 7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, -7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f, -9.61978444e-002f, -9.71456952e-002f, -4.16214759e-004f, 9.71456952e-002f, 0.00000000e+000f } },
911 { { 1.40852210e-001f, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f, 7.58818830e-002f, -7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f, 9.61978444e-002f, -9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
912 { { 1.40852210e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f, -7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f, -9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
913 { { 1.39644596e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f, -5.83538687e-002f, 0.00000000e+000f, 1.01835015e-001f }, { 7.08127349e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f, -7.39770307e-002f, 0.00000000e+000f, 1.29099445e-001f } },
914 { { 1.39644596e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, -5.83538687e-002f, 0.00000000e+000f, -1.01835015e-001f }, { 7.08127349e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, -7.39770307e-002f, 0.00000000e+000f, -1.29099445e-001f } },
915 { { 1.39644596e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f, -5.83538687e-002f, 0.00000000e+000f, 1.01835015e-001f }, { 7.08127349e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f, -7.39770307e-002f, 0.00000000e+000f, 1.29099445e-001f } },
916 { { 1.39644596e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, -5.83538687e-002f, 0.00000000e+000f, -1.01835015e-001f }, { 7.08127349e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, -7.39770307e-002f, 0.00000000e+000f, -1.29099445e-001f } },
917 { { 1.40852210e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f, 7.58818830e-002f, -7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f, 9.61978444e-002f, -9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
918 { { 1.40852210e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f, -7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f, -9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
919 { { 1.40852210e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f, 7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, 7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f, 9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, 9.71456952e-002f, 0.00000000e+000f } },
920 { { 1.40852210e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f, -7.58818830e-002f, -7.66295578e-002f, -3.28314629e-004f, 7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f, -9.61978444e-002f, -9.71456952e-002f, -4.16214759e-004f, 9.71456952e-002f, 0.00000000e+000f } },
921 { { 1.43315266e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 1.18020996e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.26741039e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f, 0.00000000e+000f, 0.00000000e+000f, 1.49618920e-001f, 0.00000000e+000f, 0.00000000e+000f } },
923 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = device->AmbiUp ? AmbiMatrixHOA :
924 AmbiMatrixFOA;
925 ALsizei count = device->AmbiUp ? 9 : 4;
926 ALsizei i;
928 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
930 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
932 for(i = 0;i < count;i++)
934 device->Dry.Ambi.Map[i].Scale = 1.0f;
935 device->Dry.Ambi.Map[i].Index = i;
937 device->Dry.CoeffCount = 0;
938 device->Dry.NumChannels = count;
940 if(device->AmbiUp)
942 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
943 for(i = 0;i < 4;i++)
945 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
946 device->FOAOut.Ambi.Map[i].Index = i;
948 device->FOAOut.CoeffCount = 0;
949 device->FOAOut.NumChannels = 4;
951 ambiup_reset(device->AmbiUp, device);
953 else
955 device->FOAOut.Ambi = device->Dry.Ambi;
956 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
957 device->FOAOut.NumChannels = 0;
960 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
962 device->Hrtf->IrSize = BuildBFormatHrtf(device->HrtfHandle,
963 device->Hrtf, device->Dry.NumChannels,
964 AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
968 static void InitUhjPanning(ALCdevice *device)
970 ALsizei count = 3;
971 ALsizei i;
973 for(i = 0;i < count;i++)
975 ALsizei acn = FuMa2ACN[i];
976 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
977 device->Dry.Ambi.Map[i].Index = acn;
979 device->Dry.CoeffCount = 0;
980 device->Dry.NumChannels = count;
982 device->FOAOut.Ambi = device->Dry.Ambi;
983 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
984 device->FOAOut.NumChannels = 0;
986 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
989 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
991 /* Hold the HRTF the device last used, in case it's used again. */
992 struct Hrtf *old_hrtf = device->HrtfHandle;
993 const char *mode;
994 bool headphones;
995 int bs2blevel;
996 size_t i;
998 al_free(device->Hrtf);
999 device->Hrtf = NULL;
1000 device->HrtfHandle = NULL;
1001 alstr_clear(&device->HrtfName);
1002 device->Render_Mode = NormalRender;
1004 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
1005 device->Dry.CoeffCount = 0;
1006 device->Dry.NumChannels = 0;
1007 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
1008 device->Dry.NumChannelsPerOrder[i] = 0;
1010 device->AvgSpeakerDist = 0.0f;
1011 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
1012 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1014 device->ChannelDelay[i].Gain = 1.0f;
1015 device->ChannelDelay[i].Length = 0;
1018 if(device->FmtChans != DevFmtStereo)
1020 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1021 const char *devname, *layout = NULL;
1022 AmbDecConf conf, *pconf = NULL;
1024 if(old_hrtf)
1025 Hrtf_DecRef(old_hrtf);
1026 old_hrtf = NULL;
1027 if(hrtf_appreq == Hrtf_Enable)
1028 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1030 ambdec_init(&conf);
1032 devname = alstr_get_cstr(device->DeviceName);
1033 switch(device->FmtChans)
1035 case DevFmtQuad: layout = "quad"; break;
1036 case DevFmtX51: /* fall-through */
1037 case DevFmtX51Rear: layout = "surround51"; break;
1038 case DevFmtX61: layout = "surround61"; break;
1039 case DevFmtX71: layout = "surround71"; break;
1040 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1041 case DevFmtMono:
1042 case DevFmtStereo:
1043 case DevFmtAmbi3D:
1044 break;
1046 if(layout)
1048 const char *fname;
1049 if(ConfigValueStr(devname, "decoder", layout, &fname))
1051 if(!ambdec_load(&conf, fname))
1052 ERR("Failed to load layout file %s\n", fname);
1053 else
1055 if(conf.ChanMask > 0xffff)
1056 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1057 else
1059 if(MakeSpeakerMap(device, &conf, speakermap))
1060 pconf = &conf;
1066 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1068 ambiup_free(device->AmbiUp);
1069 device->AmbiUp = NULL;
1070 if(!device->AmbiDecoder)
1071 device->AmbiDecoder = bformatdec_alloc();
1073 else
1075 bformatdec_free(device->AmbiDecoder);
1076 device->AmbiDecoder = NULL;
1077 if(device->FmtChans == DevFmtAmbi3D && device->AmbiOrder > 1)
1079 if(!device->AmbiUp)
1080 device->AmbiUp = ambiup_alloc();
1082 else
1084 ambiup_free(device->AmbiUp);
1085 device->AmbiUp = NULL;
1089 if(!pconf)
1090 InitPanning(device);
1091 else if(device->AmbiDecoder)
1092 InitHQPanning(device, pconf, speakermap);
1093 else
1094 InitCustomPanning(device, pconf, speakermap);
1096 ambdec_deinit(&conf);
1097 return;
1100 bformatdec_free(device->AmbiDecoder);
1101 device->AmbiDecoder = NULL;
1103 headphones = device->IsHeadphones;
1104 if(device->Type != Loopback)
1106 const char *mode;
1107 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1109 if(strcasecmp(mode, "headphones") == 0)
1110 headphones = true;
1111 else if(strcasecmp(mode, "speakers") == 0)
1112 headphones = false;
1113 else if(strcasecmp(mode, "auto") != 0)
1114 ERR("Unexpected stereo-mode: %s\n", mode);
1118 if(hrtf_userreq == Hrtf_Default)
1120 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1121 (hrtf_appreq == Hrtf_Enable);
1122 if(!usehrtf) goto no_hrtf;
1124 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1125 if(headphones && hrtf_appreq != Hrtf_Disable)
1126 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1128 else
1130 if(hrtf_userreq != Hrtf_Enable)
1132 if(hrtf_appreq == Hrtf_Enable)
1133 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1134 goto no_hrtf;
1136 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1139 if(VECTOR_SIZE(device->HrtfList) == 0)
1141 VECTOR_DEINIT(device->HrtfList);
1142 device->HrtfList = EnumerateHrtf(device->DeviceName);
1145 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1147 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1148 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1149 if(hrtf && hrtf->sampleRate == device->Frequency)
1151 device->HrtfHandle = hrtf;
1152 alstr_copy(&device->HrtfName, entry->name);
1154 else if(hrtf)
1155 Hrtf_DecRef(hrtf);
1158 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1160 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1161 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1162 if(hrtf && hrtf->sampleRate == device->Frequency)
1164 device->HrtfHandle = hrtf;
1165 alstr_copy(&device->HrtfName, entry->name);
1167 else if(hrtf)
1168 Hrtf_DecRef(hrtf);
1171 if(device->HrtfHandle)
1173 if(old_hrtf)
1174 Hrtf_DecRef(old_hrtf);
1175 old_hrtf = NULL;
1177 device->Render_Mode = HrtfRender;
1178 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1180 if(strcasecmp(mode, "full") == 0)
1181 device->Render_Mode = HrtfRender;
1182 else if(strcasecmp(mode, "basic") == 0)
1183 device->Render_Mode = NormalRender;
1184 else
1185 ERR("Unexpected hrtf-mode: %s\n", mode);
1188 if(device->Render_Mode == HrtfRender)
1190 /* Don't bother with HOA when using full HRTF rendering. Nothing
1191 * needs it, and it eases the CPU/memory load.
1193 ambiup_free(device->AmbiUp);
1194 device->AmbiUp = NULL;
1196 else
1198 if(!device->AmbiUp)
1199 device->AmbiUp = ambiup_alloc();
1202 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1203 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1204 alstr_get_cstr(device->HrtfName)
1206 InitHrtfPanning(device);
1207 return;
1209 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1211 no_hrtf:
1212 if(old_hrtf)
1213 Hrtf_DecRef(old_hrtf);
1214 old_hrtf = NULL;
1215 TRACE("HRTF disabled\n");
1217 device->Render_Mode = StereoPair;
1219 ambiup_free(device->AmbiUp);
1220 device->AmbiUp = NULL;
1222 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1223 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1224 if(device->Type != Loopback)
1225 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1226 if(bs2blevel > 0 && bs2blevel <= 6)
1228 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1229 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1230 TRACE("BS2B enabled\n");
1231 InitPanning(device);
1232 return;
1235 TRACE("BS2B disabled\n");
1237 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1239 if(strcasecmp(mode, "uhj") == 0)
1240 device->Render_Mode = NormalRender;
1241 else if(strcasecmp(mode, "panpot") != 0)
1242 ERR("Unexpected stereo-encoding: %s\n", mode);
1244 if(device->Render_Mode == NormalRender)
1246 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1247 TRACE("UHJ enabled\n");
1248 InitUhjPanning(device);
1249 return;
1252 TRACE("UHJ disabled\n");
1253 InitPanning(device);
1257 void aluInitEffectPanning(ALeffectslot *slot)
1259 ALsizei i;
1261 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1262 slot->NumChannels = 0;
1264 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1266 slot->ChanMap[i].Scale = 1.0f;
1267 slot->ChanMap[i].Index = i;
1269 slot->NumChannels = i;