Start a ALC_SOFT_loopback2 extension
[openal-soft.git] / Alc / panning.c
blob77f8e31b3a58dc1469a2cbe644fc930f4c40b7bb
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(al_string_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
390 c = GetChannelIdxByName(device->RealOut, FrontLeft);
391 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
392 c = GetChannelIdxByName(device->RealOut, FrontRight);
393 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
394 c = GetChannelIdxByName(device->RealOut, FrontCenter);
395 else if(al_string_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(al_string_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(al_string_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(al_string_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(al_string_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
424 c = GetChannelIdxByName(device->RealOut, BackCenter);
425 else
427 const char *name = al_string_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 al_string_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 InitPanning(ALCdevice *device)
492 const ChannelMap *chanmap = NULL;
493 ALsizei coeffcount = 0;
494 ALsizei count = 0;
495 ALsizei i, j;
497 switch(device->FmtChans)
499 case DevFmtMono:
500 count = COUNTOF(MonoCfg);
501 chanmap = MonoCfg;
502 coeffcount = 1;
503 break;
505 case DevFmtStereo:
506 count = COUNTOF(StereoCfg);
507 chanmap = StereoCfg;
508 coeffcount = 4;
509 break;
511 case DevFmtQuad:
512 count = COUNTOF(QuadCfg);
513 chanmap = QuadCfg;
514 coeffcount = 4;
515 break;
517 case DevFmtX51:
518 count = COUNTOF(X51SideCfg);
519 chanmap = X51SideCfg;
520 coeffcount = 9;
521 break;
523 case DevFmtX51Rear:
524 count = COUNTOF(X51RearCfg);
525 chanmap = X51RearCfg;
526 coeffcount = 9;
527 break;
529 case DevFmtX61:
530 count = COUNTOF(X61Cfg);
531 chanmap = X61Cfg;
532 coeffcount = 9;
533 break;
535 case DevFmtX71:
536 count = COUNTOF(X71Cfg);
537 chanmap = X71Cfg;
538 coeffcount = 16;
539 break;
541 case DevFmtAmbi1:
542 case DevFmtAmbi2:
543 case DevFmtAmbi3:
544 break;
547 if(device->FmtChans >= DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
549 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
550 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
551 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
552 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ UnitScale;
554 count = (device->FmtChans == DevFmtAmbi3) ? 16 :
555 (device->FmtChans == DevFmtAmbi2) ? 9 :
556 (device->FmtChans == DevFmtAmbi1) ? 4 : 1;
557 for(i = 0;i < count;i++)
559 ALsizei acn = acnmap[i];
560 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
561 device->Dry.Ambi.Map[i].Index = acn;
563 device->Dry.CoeffCount = 0;
564 device->Dry.NumChannels = count;
566 if(device->FmtChans == DevFmtAmbi1)
568 device->FOAOut.Ambi = device->Dry.Ambi;
569 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
570 device->FOAOut.NumChannels = 0;
572 else
574 /* FOA output is always ACN+N3D for higher-order ambisonic output.
575 * The upsampler expects this and will convert it for output.
577 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
578 for(i = 0;i < 4;i++)
580 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
581 device->FOAOut.Ambi.Map[i].Index = i;
583 device->FOAOut.CoeffCount = 0;
584 device->FOAOut.NumChannels = 4;
586 ambiup_reset(device->AmbiUp, device);
589 else
591 ALfloat w_scale, xyz_scale;
593 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
594 chanmap, count, &device->Dry.NumChannels);
595 device->Dry.CoeffCount = coeffcount;
597 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE2D_THIRD :
598 (device->Dry.CoeffCount > 4) ? W_SCALE2D_SECOND : 1.0f;
599 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE2D_THIRD :
600 (device->Dry.CoeffCount > 4) ? XYZ_SCALE2D_SECOND : 1.0f;
602 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
603 for(i = 0;i < device->Dry.NumChannels;i++)
605 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
606 for(j = 1;j < 4;j++)
607 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
609 device->FOAOut.CoeffCount = 4;
610 device->FOAOut.NumChannels = 0;
612 device->RealOut.NumChannels = 0;
615 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
617 const char *devname = al_string_get_cstr(device->DeviceName);
618 ALfloat maxdist = 0.0f;
619 ALsizei i;
621 for(i = 0;i < conf->NumSpeakers;i++)
622 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
624 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
626 ALfloat srate = (ALfloat)device->Frequency;
627 for(i = 0;i < conf->NumSpeakers;i++)
629 ALsizei chan = speakermap[i];
630 ALfloat delay;
632 /* Distance compensation only delays in steps of the sample rate.
633 * This is a bit less accurate since the delay time falls to the
634 * nearest sample time, but it's far simpler as it doesn't have to
635 * deal with phase offsets. This means at 48khz, for instance, the
636 * distance delay will be in steps of about 7 millimeters.
638 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
639 srate + 0.5f);
640 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
641 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
642 al_string_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
644 device->ChannelDelay[chan].Length = (ALsizei)clampf(
645 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
647 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
648 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
649 al_string_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
650 device->ChannelDelay[chan].Gain
656 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
658 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
659 const ALfloat *coeff_scale = UnitScale;
660 ALfloat w_scale = 1.0f;
661 ALfloat xyz_scale = 1.0f;
662 ALsizei i, j;
664 if(conf->FreqBands != 1)
665 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
666 conf->XOverFreq);
668 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
670 if(conf->ChanMask > 0x1ff)
672 w_scale = W_SCALE3D_THIRD;
673 xyz_scale = XYZ_SCALE3D_THIRD;
675 else if(conf->ChanMask > 0xf)
677 w_scale = W_SCALE3D_SECOND;
678 xyz_scale = XYZ_SCALE3D_SECOND;
681 else
683 if(conf->ChanMask > 0x1ff)
685 w_scale = W_SCALE2D_THIRD;
686 xyz_scale = XYZ_SCALE2D_THIRD;
688 else if(conf->ChanMask > 0xf)
690 w_scale = W_SCALE2D_SECOND;
691 xyz_scale = XYZ_SCALE2D_SECOND;
695 if(conf->CoeffScale == ADS_SN3D)
696 coeff_scale = SN3D2N3DScale;
697 else if(conf->CoeffScale == ADS_FuMa)
698 coeff_scale = FuMa2N3DScale;
700 for(i = 0;i < conf->NumSpeakers;i++)
702 ALsizei chan = speakermap[i];
703 ALfloat gain;
704 ALsizei k = 0;
706 for(j = 0;j < MAX_AMBI_COEFFS;j++)
707 chanmap[i].Config[j] = 0.0f;
709 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
710 for(j = 0;j < MAX_AMBI_COEFFS;j++)
712 if(j == 0) gain = conf->HFOrderGain[0];
713 else if(j == 1) gain = conf->HFOrderGain[1];
714 else if(j == 4) gain = conf->HFOrderGain[2];
715 else if(j == 9) gain = conf->HFOrderGain[3];
716 if((conf->ChanMask&(1<<j)))
717 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
721 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
722 conf->NumSpeakers, &device->Dry.NumChannels);
723 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
724 (conf->ChanMask > 0xf) ? 9 : 4;
726 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
727 for(i = 0;i < device->Dry.NumChannels;i++)
729 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
730 for(j = 1;j < 4;j++)
731 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
733 device->FOAOut.CoeffCount = 4;
734 device->FOAOut.NumChannels = 0;
736 device->RealOut.NumChannels = 0;
738 InitDistanceComp(device, conf, speakermap);
741 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
743 size_t count;
744 size_t i;
746 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
748 count = (conf->ChanMask > 0x1ff) ? 16 :
749 (conf->ChanMask > 0xf) ? 9 : 4;
750 for(i = 0;i < count;i++)
752 device->Dry.Ambi.Map[i].Scale = 1.0f;
753 device->Dry.Ambi.Map[i].Index = i;
756 else
758 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
760 count = (conf->ChanMask > 0x1ff) ? 7 :
761 (conf->ChanMask > 0xf) ? 5 : 3;
762 for(i = 0;i < count;i++)
764 device->Dry.Ambi.Map[i].Scale = 1.0f;
765 device->Dry.Ambi.Map[i].Index = map[i];
768 device->Dry.CoeffCount = 0;
769 device->Dry.NumChannels = count;
771 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
772 (conf->FreqBands == 1) ? "single" : "dual",
773 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
774 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
776 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
778 if(bformatdec_getOrder(device->AmbiDecoder) < 2)
780 device->FOAOut.Ambi = device->Dry.Ambi;
781 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
782 device->FOAOut.NumChannels = 0;
784 else
786 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
787 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
789 count = 4;
790 for(i = 0;i < count;i++)
792 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
793 device->FOAOut.Ambi.Map[i].Index = i;
796 else
798 static const int map[3] = { 0, 1, 3 };
799 count = 3;
800 for(i = 0;i < count;i++)
802 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
803 device->FOAOut.Ambi.Map[i].Index = map[i];
806 device->FOAOut.CoeffCount = 0;
807 device->FOAOut.NumChannels = count;
810 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
812 InitDistanceComp(device, conf, speakermap);
815 static void InitHrtfPanning(ALCdevice *device, bool hoa_mode)
817 /* NOTE: azimuth goes clockwise. */
818 static const ALfloat AmbiPoints[][2] = {
819 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
820 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
821 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
822 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
823 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
824 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
825 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
826 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
827 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
828 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
829 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
830 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
831 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
832 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
834 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
835 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
836 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
837 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
838 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
839 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
840 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
841 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
842 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
843 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
844 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
845 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
846 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
847 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
848 { { 1.88982237e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
849 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
850 { { 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 } },
851 { { 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 } },
852 { { 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 } },
853 { { 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 } },
854 { { 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 } },
855 { { 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 } },
856 { { 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 } },
857 { { 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 } },
858 { { 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 } },
859 { { 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 } },
860 { { 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 } },
861 { { 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 } },
862 { { 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 } },
863 { { 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 } },
865 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = hoa_mode ? AmbiMatrixHOA : AmbiMatrixFOA;
866 size_t count = hoa_mode ? 9 : 4;
867 size_t i;
869 static_assert(9 <= COUNTOF(device->Hrtf.Coeffs), "ALCdevice::Hrtf.Values/Coeffs size is too small");
870 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
872 for(i = 0;i < count;i++)
874 device->Dry.Ambi.Map[i].Scale = 1.0f;
875 device->Dry.Ambi.Map[i].Index = i;
877 device->Dry.CoeffCount = 0;
878 device->Dry.NumChannels = count;
880 if(!hoa_mode)
882 device->FOAOut.Ambi = device->Dry.Ambi;
883 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
884 device->FOAOut.NumChannels = 0;
886 else
888 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
889 for(i = 0;i < 4;i++)
891 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
892 device->FOAOut.Ambi.Map[i].Index = i;
894 device->FOAOut.CoeffCount = 0;
895 device->FOAOut.NumChannels = 4;
897 ambiup_reset(device->AmbiUp, device);
900 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
902 memset(device->Hrtf.Coeffs, 0, sizeof(device->Hrtf.Coeffs));
903 device->Hrtf.IrSize = BuildBFormatHrtf(device->Hrtf.Handle,
904 device->Hrtf.Coeffs, device->Dry.NumChannels,
905 AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
908 /* Round up to the nearest multiple of 8 */
909 device->Hrtf.IrSize = (device->Hrtf.IrSize+7)&~7;
912 static void InitUhjPanning(ALCdevice *device)
914 ALsizei count = 3;
915 ALsizei i;
917 for(i = 0;i < count;i++)
919 ALsizei acn = FuMa2ACN[i];
920 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
921 device->Dry.Ambi.Map[i].Index = acn;
923 device->Dry.CoeffCount = 0;
924 device->Dry.NumChannels = count;
926 device->FOAOut.Ambi = device->Dry.Ambi;
927 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
928 device->FOAOut.NumChannels = 0;
930 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
933 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
935 const char *mode;
936 bool headphones;
937 int bs2blevel;
938 size_t i;
940 device->Hrtf.Handle = NULL;
941 al_string_clear(&device->Hrtf.Name);
942 device->Render_Mode = NormalRender;
944 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
945 device->Dry.CoeffCount = 0;
946 device->Dry.NumChannels = 0;
948 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
949 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
951 device->ChannelDelay[i].Gain = 1.0f;
952 device->ChannelDelay[i].Length = 0;
955 if(device->FmtChans != DevFmtStereo)
957 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
958 const char *devname, *layout = NULL;
959 AmbDecConf conf, *pconf = NULL;
961 if(hrtf_appreq == Hrtf_Enable)
962 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
964 ambdec_init(&conf);
966 devname = al_string_get_cstr(device->DeviceName);
967 switch(device->FmtChans)
969 case DevFmtQuad: layout = "quad"; break;
970 case DevFmtX51: /* fall-through */
971 case DevFmtX51Rear: layout = "surround51"; break;
972 case DevFmtX61: layout = "surround61"; break;
973 case DevFmtX71: layout = "surround71"; break;
974 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
975 case DevFmtMono:
976 case DevFmtStereo:
977 case DevFmtAmbi1:
978 case DevFmtAmbi2:
979 case DevFmtAmbi3:
980 break;
982 if(layout)
984 const char *fname;
985 if(ConfigValueStr(devname, "decoder", layout, &fname))
987 if(!ambdec_load(&conf, fname))
988 ERR("Failed to load layout file %s\n", fname);
989 else
991 if(conf.ChanMask > 0xffff)
992 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
993 else
995 if(MakeSpeakerMap(device, &conf, speakermap))
996 pconf = &conf;
1002 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1004 ambiup_free(device->AmbiUp);
1005 device->AmbiUp = NULL;
1006 if(!device->AmbiDecoder)
1007 device->AmbiDecoder = bformatdec_alloc();
1009 else
1011 bformatdec_free(device->AmbiDecoder);
1012 device->AmbiDecoder = NULL;
1013 if(device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
1015 if(!device->AmbiUp)
1016 device->AmbiUp = ambiup_alloc();
1018 else
1020 ambiup_free(device->AmbiUp);
1021 device->AmbiUp = NULL;
1025 if(!pconf)
1026 InitPanning(device);
1027 else if(device->AmbiDecoder)
1028 InitHQPanning(device, pconf, speakermap);
1029 else
1030 InitCustomPanning(device, pconf, speakermap);
1032 ambdec_deinit(&conf);
1033 return;
1036 bformatdec_free(device->AmbiDecoder);
1037 device->AmbiDecoder = NULL;
1039 headphones = device->IsHeadphones;
1040 if(device->Type != Loopback)
1042 const char *mode;
1043 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1045 if(strcasecmp(mode, "headphones") == 0)
1046 headphones = true;
1047 else if(strcasecmp(mode, "speakers") == 0)
1048 headphones = false;
1049 else if(strcasecmp(mode, "auto") != 0)
1050 ERR("Unexpected stereo-mode: %s\n", mode);
1054 if(hrtf_userreq == Hrtf_Default)
1056 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1057 (hrtf_appreq == Hrtf_Enable);
1058 if(!usehrtf) goto no_hrtf;
1060 device->Hrtf.Status = ALC_HRTF_ENABLED_SOFT;
1061 if(headphones && hrtf_appreq != Hrtf_Disable)
1062 device->Hrtf.Status = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1064 else
1066 if(hrtf_userreq != Hrtf_Enable)
1068 if(hrtf_appreq == Hrtf_Enable)
1069 device->Hrtf.Status = ALC_HRTF_DENIED_SOFT;
1070 goto no_hrtf;
1072 device->Hrtf.Status = ALC_HRTF_REQUIRED_SOFT;
1075 if(VECTOR_SIZE(device->Hrtf.List) == 0)
1077 VECTOR_DEINIT(device->Hrtf.List);
1078 device->Hrtf.List = EnumerateHrtf(device->DeviceName);
1081 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->Hrtf.List))
1083 const HrtfEntry *entry = &VECTOR_ELEM(device->Hrtf.List, hrtf_id);
1084 if(entry->hrtf->sampleRate == device->Frequency)
1086 device->Hrtf.Handle = entry->hrtf;
1087 al_string_copy(&device->Hrtf.Name, entry->name);
1091 for(i = 0;!device->Hrtf.Handle && i < VECTOR_SIZE(device->Hrtf.List);i++)
1093 const HrtfEntry *entry = &VECTOR_ELEM(device->Hrtf.List, i);
1094 if(entry->hrtf->sampleRate == device->Frequency)
1096 device->Hrtf.Handle = entry->hrtf;
1097 al_string_copy(&device->Hrtf.Name, entry->name);
1101 if(device->Hrtf.Handle)
1103 bool hoa_mode;
1105 device->Render_Mode = HrtfRender;
1106 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1108 if(strcasecmp(mode, "full") == 0)
1109 device->Render_Mode = HrtfRender;
1110 else if(strcasecmp(mode, "basic") == 0)
1111 device->Render_Mode = NormalRender;
1112 else
1113 ERR("Unexpected hrtf-mode: %s\n", mode);
1116 if(device->Render_Mode == HrtfRender)
1118 /* Don't bother with HOA when using full HRTF rendering. Nothing
1119 * needs it, and it eases the CPU/memory load.
1121 ambiup_free(device->AmbiUp);
1122 device->AmbiUp = NULL;
1123 hoa_mode = false;
1125 else
1127 if(!device->AmbiUp)
1128 device->AmbiUp = ambiup_alloc();
1129 hoa_mode = true;
1132 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1133 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1134 al_string_get_cstr(device->Hrtf.Name)
1136 InitHrtfPanning(device, hoa_mode);
1137 return;
1139 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1141 no_hrtf:
1142 TRACE("HRTF disabled\n");
1144 device->Render_Mode = StereoPair;
1146 ambiup_free(device->AmbiUp);
1147 device->AmbiUp = NULL;
1149 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1150 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1151 if(device->Type != Loopback)
1152 ConfigValueInt(al_string_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1153 if(bs2blevel > 0 && bs2blevel <= 6)
1155 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1156 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1157 TRACE("BS2B enabled\n");
1158 InitPanning(device);
1159 return;
1162 TRACE("BS2B disabled\n");
1164 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1166 if(strcasecmp(mode, "uhj") == 0)
1167 device->Render_Mode = NormalRender;
1168 else if(strcasecmp(mode, "panpot") != 0)
1169 ERR("Unexpected stereo-encoding: %s\n", mode);
1171 if(device->Render_Mode == NormalRender)
1173 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1174 TRACE("UHJ enabled\n");
1175 InitUhjPanning(device);
1176 return;
1179 TRACE("UHJ disabled\n");
1180 InitPanning(device);
1184 void aluInitEffectPanning(ALeffectslot *slot)
1186 ALsizei i;
1188 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1189 slot->NumChannels = 0;
1191 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1193 slot->ChanMap[i].Scale = 1.0f;
1194 slot->ChanMap[i].Index = i;
1196 slot->NumChannels = i;