Clean up a comment
[openal-soft.git] / Alc / panning.c
blob63ff618e4ec12ee2d97843cc6eb2c89c2731baf2
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 DevFmtAmbi1:
624 case DevFmtAmbi2:
625 case DevFmtAmbi3:
626 break;
629 if(device->FmtChans >= DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
631 const char *devname = alstr_get_cstr(device->DeviceName);
632 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
633 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
634 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
635 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ UnitScale;
636 ALfloat nfc_delay = 0.0f;
638 count = (device->FmtChans == DevFmtAmbi3) ? 16 :
639 (device->FmtChans == DevFmtAmbi2) ? 9 :
640 (device->FmtChans == DevFmtAmbi1) ? 4 : 1;
641 for(i = 0;i < count;i++)
643 ALsizei acn = acnmap[i];
644 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
645 device->Dry.Ambi.Map[i].Index = acn;
647 device->Dry.CoeffCount = 0;
648 device->Dry.NumChannels = count;
650 if(device->FmtChans == DevFmtAmbi1)
652 device->FOAOut.Ambi = device->Dry.Ambi;
653 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
654 device->FOAOut.NumChannels = 0;
656 else
658 /* FOA output is always ACN+N3D for higher-order ambisonic output.
659 * The upsampler expects this and will convert it for output.
661 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
662 for(i = 0;i < 4;i++)
664 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
665 device->FOAOut.Ambi.Map[i].Index = i;
667 device->FOAOut.CoeffCount = 0;
668 device->FOAOut.NumChannels = 4;
670 ambiup_reset(device->AmbiUp, device);
673 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay))
675 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
676 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
677 (device->FmtChans == DevFmtAmbi3) ? 3 :
678 (device->FmtChans == DevFmtAmbi2) ? 2 : 1,
679 true
683 else
685 ALfloat w_scale, xyz_scale;
687 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
688 chanmap, count, &device->Dry.NumChannels);
689 device->Dry.CoeffCount = coeffcount;
691 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE2D_THIRD :
692 (device->Dry.CoeffCount > 4) ? W_SCALE2D_SECOND : 1.0f;
693 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE2D_THIRD :
694 (device->Dry.CoeffCount > 4) ? XYZ_SCALE2D_SECOND : 1.0f;
696 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
697 for(i = 0;i < device->Dry.NumChannels;i++)
699 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
700 for(j = 1;j < 4;j++)
701 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
703 device->FOAOut.CoeffCount = 4;
704 device->FOAOut.NumChannels = 0;
706 device->RealOut.NumChannels = 0;
709 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
711 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
712 const ALfloat *coeff_scale = UnitScale;
713 ALfloat w_scale = 1.0f;
714 ALfloat xyz_scale = 1.0f;
715 ALsizei i, j;
717 if(conf->FreqBands != 1)
718 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
719 conf->XOverFreq);
721 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
723 if(conf->ChanMask > 0x1ff)
725 w_scale = W_SCALE3D_THIRD;
726 xyz_scale = XYZ_SCALE3D_THIRD;
728 else if(conf->ChanMask > 0xf)
730 w_scale = W_SCALE3D_SECOND;
731 xyz_scale = XYZ_SCALE3D_SECOND;
734 else
736 if(conf->ChanMask > 0x1ff)
738 w_scale = W_SCALE2D_THIRD;
739 xyz_scale = XYZ_SCALE2D_THIRD;
741 else if(conf->ChanMask > 0xf)
743 w_scale = W_SCALE2D_SECOND;
744 xyz_scale = XYZ_SCALE2D_SECOND;
748 if(conf->CoeffScale == ADS_SN3D)
749 coeff_scale = SN3D2N3DScale;
750 else if(conf->CoeffScale == ADS_FuMa)
751 coeff_scale = FuMa2N3DScale;
753 for(i = 0;i < conf->NumSpeakers;i++)
755 ALsizei chan = speakermap[i];
756 ALfloat gain;
757 ALsizei k = 0;
759 for(j = 0;j < MAX_AMBI_COEFFS;j++)
760 chanmap[i].Config[j] = 0.0f;
762 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
763 for(j = 0;j < MAX_AMBI_COEFFS;j++)
765 if(j == 0) gain = conf->HFOrderGain[0];
766 else if(j == 1) gain = conf->HFOrderGain[1];
767 else if(j == 4) gain = conf->HFOrderGain[2];
768 else if(j == 9) gain = conf->HFOrderGain[3];
769 if((conf->ChanMask&(1<<j)))
770 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
774 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
775 conf->NumSpeakers, &device->Dry.NumChannels);
776 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
777 (conf->ChanMask > 0xf) ? 9 : 4;
779 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
780 for(i = 0;i < device->Dry.NumChannels;i++)
782 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
783 for(j = 1;j < 4;j++)
784 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
786 device->FOAOut.CoeffCount = 4;
787 device->FOAOut.NumChannels = 0;
789 device->RealOut.NumChannels = 0;
791 InitDistanceComp(device, conf, speakermap);
794 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
796 ALfloat avg_dist;
797 ALsizei count;
798 ALsizei i;
800 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
802 count = (conf->ChanMask > 0x1ff) ? 16 :
803 (conf->ChanMask > 0xf) ? 9 : 4;
804 for(i = 0;i < count;i++)
806 device->Dry.Ambi.Map[i].Scale = 1.0f;
807 device->Dry.Ambi.Map[i].Index = i;
810 else
812 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
814 count = (conf->ChanMask > 0x1ff) ? 7 :
815 (conf->ChanMask > 0xf) ? 5 : 3;
816 for(i = 0;i < count;i++)
818 device->Dry.Ambi.Map[i].Scale = 1.0f;
819 device->Dry.Ambi.Map[i].Index = map[i];
822 device->Dry.CoeffCount = 0;
823 device->Dry.NumChannels = count;
825 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
826 (conf->FreqBands == 1) ? "single" : "dual",
827 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
828 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
830 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
832 if(!(conf->ChanMask > 0xf))
834 device->FOAOut.Ambi = device->Dry.Ambi;
835 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
836 device->FOAOut.NumChannels = 0;
838 else
840 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
841 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
843 count = 4;
844 for(i = 0;i < count;i++)
846 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
847 device->FOAOut.Ambi.Map[i].Index = i;
850 else
852 static const int map[3] = { 0, 1, 3 };
853 count = 3;
854 for(i = 0;i < count;i++)
856 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
857 device->FOAOut.Ambi.Map[i].Index = map[i];
860 device->FOAOut.CoeffCount = 0;
861 device->FOAOut.NumChannels = count;
864 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
866 avg_dist = 0.0f;
867 for(i = 0;i < conf->NumSpeakers;i++)
868 avg_dist += conf->Speakers[i].Distance;
869 avg_dist /= (ALfloat)conf->NumSpeakers;
870 InitNearFieldCtrl(device, avg_dist,
871 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
872 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
875 InitDistanceComp(device, conf, speakermap);
878 static void InitHrtfPanning(ALCdevice *device, bool hoa_mode)
880 /* NOTE: azimuth goes clockwise. */
881 static const ALfloat AmbiPoints[][2] = {
882 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
883 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
884 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
885 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
886 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
887 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
888 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
889 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
890 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
891 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
892 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
893 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
894 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
895 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
897 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
898 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
899 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
900 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
901 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
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, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
904 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
905 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
906 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
907 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
908 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
909 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
910 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
911 { { 1.88982237e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
912 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
913 { { 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 } },
914 { { 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 } },
915 { { 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 } },
916 { { 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 } },
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.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 } },
919 { { 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 } },
920 { { 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 } },
921 { { 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 } },
922 { { 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 } },
923 { { 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 } },
924 { { 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 } },
925 { { 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 } },
926 { { 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 } },
928 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = hoa_mode ? AmbiMatrixHOA : AmbiMatrixFOA;
929 ALsizei count = hoa_mode ? 9 : 4;
930 size_t sizeof_hrtfstate;
931 ALsizei i;
933 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
935 sizeof_hrtfstate = offsetof(DirectHrtfState, Chan[count]);
936 device->Hrtf = al_calloc(16, sizeof_hrtfstate);
938 for(i = 0;i < count;i++)
940 device->Dry.Ambi.Map[i].Scale = 1.0f;
941 device->Dry.Ambi.Map[i].Index = i;
943 device->Dry.CoeffCount = 0;
944 device->Dry.NumChannels = count;
946 if(!hoa_mode)
948 device->FOAOut.Ambi = device->Dry.Ambi;
949 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
950 device->FOAOut.NumChannels = 0;
952 else
954 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
955 for(i = 0;i < 4;i++)
957 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
958 device->FOAOut.Ambi.Map[i].Index = i;
960 device->FOAOut.CoeffCount = 0;
961 device->FOAOut.NumChannels = 4;
963 ambiup_reset(device->AmbiUp, device);
966 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
968 device->Hrtf->IrSize = BuildBFormatHrtf(device->HrtfHandle,
969 device->Hrtf, device->Dry.NumChannels,
970 AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
973 /* Round up to the nearest multiple of 8 */
974 device->Hrtf->IrSize = (device->Hrtf->IrSize+7)&~7;
977 static void InitUhjPanning(ALCdevice *device)
979 ALsizei count = 3;
980 ALsizei i;
982 for(i = 0;i < count;i++)
984 ALsizei acn = FuMa2ACN[i];
985 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
986 device->Dry.Ambi.Map[i].Index = acn;
988 device->Dry.CoeffCount = 0;
989 device->Dry.NumChannels = count;
991 device->FOAOut.Ambi = device->Dry.Ambi;
992 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
993 device->FOAOut.NumChannels = 0;
995 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
998 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
1000 /* Hold the HRTF the device last used, in case it's used again. */
1001 struct Hrtf *old_hrtf = device->HrtfHandle;
1002 const char *mode;
1003 bool headphones;
1004 int bs2blevel;
1005 size_t i;
1007 al_free(device->Hrtf);
1008 device->Hrtf = NULL;
1009 device->HrtfHandle = NULL;
1010 alstr_clear(&device->HrtfName);
1011 device->Render_Mode = NormalRender;
1013 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
1014 device->Dry.CoeffCount = 0;
1015 device->Dry.NumChannels = 0;
1016 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
1017 device->Dry.NumChannelsPerOrder[i] = 0;
1019 device->AvgSpeakerDist = 0.0f;
1020 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
1021 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1023 device->ChannelDelay[i].Gain = 1.0f;
1024 device->ChannelDelay[i].Length = 0;
1027 if(device->FmtChans != DevFmtStereo)
1029 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1030 const char *devname, *layout = NULL;
1031 AmbDecConf conf, *pconf = NULL;
1033 if(old_hrtf)
1034 Hrtf_DecRef(old_hrtf);
1035 old_hrtf = NULL;
1036 if(hrtf_appreq == Hrtf_Enable)
1037 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1039 ambdec_init(&conf);
1041 devname = alstr_get_cstr(device->DeviceName);
1042 switch(device->FmtChans)
1044 case DevFmtQuad: layout = "quad"; break;
1045 case DevFmtX51: /* fall-through */
1046 case DevFmtX51Rear: layout = "surround51"; break;
1047 case DevFmtX61: layout = "surround61"; break;
1048 case DevFmtX71: layout = "surround71"; break;
1049 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1050 case DevFmtMono:
1051 case DevFmtStereo:
1052 case DevFmtAmbi1:
1053 case DevFmtAmbi2:
1054 case DevFmtAmbi3:
1055 break;
1057 if(layout)
1059 const char *fname;
1060 if(ConfigValueStr(devname, "decoder", layout, &fname))
1062 if(!ambdec_load(&conf, fname))
1063 ERR("Failed to load layout file %s\n", fname);
1064 else
1066 if(conf.ChanMask > 0xffff)
1067 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1068 else
1070 if(MakeSpeakerMap(device, &conf, speakermap))
1071 pconf = &conf;
1077 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1079 ambiup_free(device->AmbiUp);
1080 device->AmbiUp = NULL;
1081 if(!device->AmbiDecoder)
1082 device->AmbiDecoder = bformatdec_alloc();
1084 else
1086 bformatdec_free(device->AmbiDecoder);
1087 device->AmbiDecoder = NULL;
1088 if(device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
1090 if(!device->AmbiUp)
1091 device->AmbiUp = ambiup_alloc();
1093 else
1095 ambiup_free(device->AmbiUp);
1096 device->AmbiUp = NULL;
1100 if(!pconf)
1101 InitPanning(device);
1102 else if(device->AmbiDecoder)
1103 InitHQPanning(device, pconf, speakermap);
1104 else
1105 InitCustomPanning(device, pconf, speakermap);
1107 ambdec_deinit(&conf);
1108 return;
1111 bformatdec_free(device->AmbiDecoder);
1112 device->AmbiDecoder = NULL;
1114 headphones = device->IsHeadphones;
1115 if(device->Type != Loopback)
1117 const char *mode;
1118 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1120 if(strcasecmp(mode, "headphones") == 0)
1121 headphones = true;
1122 else if(strcasecmp(mode, "speakers") == 0)
1123 headphones = false;
1124 else if(strcasecmp(mode, "auto") != 0)
1125 ERR("Unexpected stereo-mode: %s\n", mode);
1129 if(hrtf_userreq == Hrtf_Default)
1131 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1132 (hrtf_appreq == Hrtf_Enable);
1133 if(!usehrtf) goto no_hrtf;
1135 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1136 if(headphones && hrtf_appreq != Hrtf_Disable)
1137 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1139 else
1141 if(hrtf_userreq != Hrtf_Enable)
1143 if(hrtf_appreq == Hrtf_Enable)
1144 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1145 goto no_hrtf;
1147 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1150 if(VECTOR_SIZE(device->HrtfList) == 0)
1152 VECTOR_DEINIT(device->HrtfList);
1153 device->HrtfList = EnumerateHrtf(device->DeviceName);
1156 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1158 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1159 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1160 if(hrtf && hrtf->sampleRate == device->Frequency)
1162 device->HrtfHandle = hrtf;
1163 alstr_copy(&device->HrtfName, entry->name);
1165 else if(hrtf)
1166 Hrtf_DecRef(hrtf);
1169 /* Reuse the old HRTF if it's compatible and any desired HRTF wasn't
1170 * compatible.
1172 if(!device->HrtfHandle && old_hrtf)
1174 if(old_hrtf->sampleRate == device->Frequency)
1176 device->HrtfHandle = old_hrtf;
1177 old_hrtf = NULL;
1181 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1183 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1184 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1185 if(hrtf && hrtf->sampleRate == device->Frequency)
1187 device->HrtfHandle = hrtf;
1188 alstr_copy(&device->HrtfName, entry->name);
1190 else if(hrtf)
1191 Hrtf_DecRef(hrtf);
1194 if(device->HrtfHandle)
1196 bool hoa_mode;
1198 if(old_hrtf)
1199 Hrtf_DecRef(old_hrtf);
1200 old_hrtf = NULL;
1202 device->Render_Mode = HrtfRender;
1203 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1205 if(strcasecmp(mode, "full") == 0)
1206 device->Render_Mode = HrtfRender;
1207 else if(strcasecmp(mode, "basic") == 0)
1208 device->Render_Mode = NormalRender;
1209 else
1210 ERR("Unexpected hrtf-mode: %s\n", mode);
1213 if(device->Render_Mode == HrtfRender)
1215 /* Don't bother with HOA when using full HRTF rendering. Nothing
1216 * needs it, and it eases the CPU/memory load.
1218 ambiup_free(device->AmbiUp);
1219 device->AmbiUp = NULL;
1220 hoa_mode = false;
1222 else
1224 if(!device->AmbiUp)
1225 device->AmbiUp = ambiup_alloc();
1226 hoa_mode = true;
1229 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1230 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1231 alstr_get_cstr(device->HrtfName)
1233 InitHrtfPanning(device, hoa_mode);
1234 return;
1236 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1238 no_hrtf:
1239 if(old_hrtf)
1240 Hrtf_DecRef(old_hrtf);
1241 old_hrtf = NULL;
1242 TRACE("HRTF disabled\n");
1244 device->Render_Mode = StereoPair;
1246 ambiup_free(device->AmbiUp);
1247 device->AmbiUp = NULL;
1249 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1250 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1251 if(device->Type != Loopback)
1252 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1253 if(bs2blevel > 0 && bs2blevel <= 6)
1255 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1256 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1257 TRACE("BS2B enabled\n");
1258 InitPanning(device);
1259 return;
1262 TRACE("BS2B disabled\n");
1264 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1266 if(strcasecmp(mode, "uhj") == 0)
1267 device->Render_Mode = NormalRender;
1268 else if(strcasecmp(mode, "panpot") != 0)
1269 ERR("Unexpected stereo-encoding: %s\n", mode);
1271 if(device->Render_Mode == NormalRender)
1273 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1274 TRACE("UHJ enabled\n");
1275 InitUhjPanning(device);
1276 return;
1279 TRACE("UHJ disabled\n");
1280 InitPanning(device);
1284 void aluInitEffectPanning(ALeffectslot *slot)
1286 ALsizei i;
1288 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1289 slot->NumChannels = 0;
1291 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1293 slot->ChanMap[i].Scale = 1.0f;
1294 slot->ChanMap[i].Index = i;
1296 slot->NumChannels = i;