Alter how panpot/pair-wise panning works
[openal-soft.git] / Alc / panning.c
blob7b551100b98c2eb6e152c368b5a87ef8224483fd
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 CalcXYZCoeffs(ALfloat x, ALfloat y, ALfloat z, 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 CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
197 ALfloat dir[3] = {
198 sinf(azimuth) * cosf(elevation),
199 sinf(elevation),
200 -cosf(azimuth) * cosf(elevation)
202 CalcDirectionCoeffs(dir, spread, coeffs);
205 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
207 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
208 if(!(fabsf(azimuth) > F_PI_2))
209 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
210 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
214 void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
216 ALsizei i;
218 for(i = 0;i < numchans;i++)
219 gains[i] = chancoeffs[i][0] * 1.414213562f * ingain;
220 for(;i < MAX_OUTPUT_CHANNELS;i++)
221 gains[i] = 0.0f;
224 void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
226 ALfloat gain = 0.0f;
227 ALsizei i;
229 for(i = 0;i < numchans;i++)
231 if(chanmap[i].Index == 0)
232 gain += chanmap[i].Scale;
234 gains[0] = gain * 1.414213562f * ingain;
235 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
236 gains[i] = 0.0f;
239 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
241 ALsizei i, j;
243 for(i = 0;i < numchans;i++)
245 float gain = 0.0f;
246 for(j = 0;j < numcoeffs;j++)
247 gain += chancoeffs[i][j]*coeffs[j];
248 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
250 for(;i < MAX_OUTPUT_CHANNELS;i++)
251 gains[i] = 0.0f;
254 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
256 ALsizei i;
258 for(i = 0;i < numchans;i++)
259 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
260 for(;i < MAX_OUTPUT_CHANNELS;i++)
261 gains[i] = 0.0f;
264 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
266 ALsizei i, j;
268 for(i = 0;i < numchans;i++)
270 float gain = 0.0f;
271 for(j = 0;j < 4;j++)
272 gain += chancoeffs[i][j] * mtx[j];
273 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
275 for(;i < MAX_OUTPUT_CHANNELS;i++)
276 gains[i] = 0.0f;
279 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
281 ALsizei i;
283 for(i = 0;i < numchans;i++)
284 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
285 for(;i < MAX_OUTPUT_CHANNELS;i++)
286 gains[i] = 0.0f;
290 static inline const char *GetLabelFromChannel(enum Channel channel)
292 switch(channel)
294 case FrontLeft: return "front-left";
295 case FrontRight: return "front-right";
296 case FrontCenter: return "front-center";
297 case LFE: return "lfe";
298 case BackLeft: return "back-left";
299 case BackRight: return "back-right";
300 case BackCenter: return "back-center";
301 case SideLeft: return "side-left";
302 case SideRight: return "side-right";
304 case UpperFrontLeft: return "upper-front-left";
305 case UpperFrontRight: return "upper-front-right";
306 case UpperBackLeft: return "upper-back-left";
307 case UpperBackRight: return "upper-back-right";
308 case LowerFrontLeft: return "lower-front-left";
309 case LowerFrontRight: return "lower-front-right";
310 case LowerBackLeft: return "lower-back-left";
311 case LowerBackRight: return "lower-back-right";
313 case Aux0: return "aux-0";
314 case Aux1: return "aux-1";
315 case Aux2: return "aux-2";
316 case Aux3: return "aux-3";
317 case Aux4: return "aux-4";
318 case Aux5: return "aux-5";
319 case Aux6: return "aux-6";
320 case Aux7: return "aux-7";
321 case Aux8: return "aux-8";
322 case Aux9: return "aux-9";
323 case Aux10: return "aux-10";
324 case Aux11: return "aux-11";
325 case Aux12: return "aux-12";
326 case Aux13: return "aux-13";
327 case Aux14: return "aux-14";
328 case Aux15: return "aux-15";
330 case InvalidChannel: break;
332 return "(unknown)";
336 typedef struct ChannelMap {
337 enum Channel ChanName;
338 ChannelConfig Config;
339 } ChannelMap;
341 static void SetChannelMap(const enum Channel *devchans, ChannelConfig *ambicoeffs,
342 const ChannelMap *chanmap, size_t count, ALsizei *outcount)
344 size_t j, k;
345 ALsizei i;
347 for(i = 0;i < MAX_OUTPUT_CHANNELS && devchans[i] != InvalidChannel;i++)
349 if(devchans[i] == LFE)
351 for(j = 0;j < MAX_AMBI_COEFFS;j++)
352 ambicoeffs[i][j] = 0.0f;
353 continue;
356 for(j = 0;j < count;j++)
358 if(devchans[i] != chanmap[j].ChanName)
359 continue;
361 for(k = 0;k < MAX_AMBI_COEFFS;++k)
362 ambicoeffs[i][k] = chanmap[j].Config[k];
363 break;
365 if(j == count)
366 ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans[i]), i);
368 *outcount = i;
371 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
373 ALsizei i;
375 for(i = 0;i < conf->NumSpeakers;i++)
377 int c = -1;
379 /* NOTE: AmbDec does not define any standard speaker names, however
380 * for this to work we have to by able to find the output channel
381 * the speaker definition corresponds to. Therefore, OpenAL Soft
382 * requires these channel labels to be recognized:
384 * LF = Front left
385 * RF = Front right
386 * LS = Side left
387 * RS = Side right
388 * LB = Back left
389 * RB = Back right
390 * CE = Front center
391 * CB = Back center
393 * Additionally, surround51 will acknowledge back speakers for side
394 * channels, and surround51rear will acknowledge side speakers for
395 * back channels, to avoid issues with an ambdec expecting 5.1 to
396 * use the side channels when the device is configured for back,
397 * and vice-versa.
399 if(al_string_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
400 c = GetChannelIdxByName(device->RealOut, FrontLeft);
401 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
402 c = GetChannelIdxByName(device->RealOut, FrontRight);
403 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
404 c = GetChannelIdxByName(device->RealOut, FrontCenter);
405 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
407 if(device->FmtChans == DevFmtX51Rear)
408 c = GetChannelIdxByName(device->RealOut, BackLeft);
409 else
410 c = GetChannelIdxByName(device->RealOut, SideLeft);
412 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
414 if(device->FmtChans == DevFmtX51Rear)
415 c = GetChannelIdxByName(device->RealOut, BackRight);
416 else
417 c = GetChannelIdxByName(device->RealOut, SideRight);
419 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
421 if(device->FmtChans == DevFmtX51)
422 c = GetChannelIdxByName(device->RealOut, SideLeft);
423 else
424 c = GetChannelIdxByName(device->RealOut, BackLeft);
426 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
428 if(device->FmtChans == DevFmtX51)
429 c = GetChannelIdxByName(device->RealOut, SideRight);
430 else
431 c = GetChannelIdxByName(device->RealOut, BackRight);
433 else if(al_string_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
434 c = GetChannelIdxByName(device->RealOut, BackCenter);
435 else
437 const char *name = al_string_get_cstr(conf->Speakers[i].Name);
438 unsigned int n;
439 char ch;
441 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
442 c = GetChannelIdxByName(device->RealOut, Aux0+n);
443 else
445 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
446 return false;
449 if(c == -1)
451 ERR("Failed to lookup AmbDec speaker label %s\n",
452 al_string_get_cstr(conf->Speakers[i].Name));
453 return false;
455 speakermap[i] = c;
458 return true;
462 static const ChannelMap MonoCfg[1] = {
463 { FrontCenter, { 1.0f } },
464 }, StereoCfg[2] = {
465 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
466 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
467 }, QuadCfg[4] = {
468 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
469 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
470 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
471 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
472 }, X51SideCfg[5] = {
473 { SideLeft, { 3.33001372e-1f, 1.89085671e-1f, 0.0f, -2.00041334e-1f, -2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
474 { FrontLeft, { 1.47751298e-1f, 1.28994110e-1f, 0.0f, 1.15190495e-1f, 7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
475 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
476 { FrontRight, { 1.47751298e-1f, -1.28994110e-1f, 0.0f, 1.15190495e-1f, -7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
477 { SideRight, { 3.33001372e-1f, -1.89085671e-1f, 0.0f, -2.00041334e-1f, 2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
478 }, X51RearCfg[5] = {
479 { BackLeft, { 3.33001372e-1f, 1.89085671e-1f, 0.0f, -2.00041334e-1f, -2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
480 { FrontLeft, { 1.47751298e-1f, 1.28994110e-1f, 0.0f, 1.15190495e-1f, 7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
481 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
482 { FrontRight, { 1.47751298e-1f, -1.28994110e-1f, 0.0f, 1.15190495e-1f, -7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
483 { BackRight, { 3.33001372e-1f, -1.89085671e-1f, 0.0f, -2.00041334e-1f, 2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
484 }, X61Cfg[6] = {
485 { SideLeft, { 2.04462744e-1f, 2.17178497e-1f, 0.0f, -4.39990188e-2f, -2.60787329e-2f, 0.0f, 0.0f, 0.0f, -6.87238843e-2f } },
486 { FrontLeft, { 1.18130342e-1f, 9.34633906e-2f, 0.0f, 1.08553749e-1f, 6.80658795e-2f, 0.0f, 0.0f, 0.0f, 1.08999485e-2f } },
487 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
488 { FrontRight, { 1.18130342e-1f, -9.34633906e-2f, 0.0f, 1.08553749e-1f, -6.80658795e-2f, 0.0f, 0.0f, 0.0f, 1.08999485e-2f } },
489 { SideRight, { 2.04462744e-1f, -2.17178497e-1f, 0.0f, -4.39990188e-2f, 2.60787329e-2f, 0.0f, 0.0f, 0.0f, -6.87238843e-2f } },
490 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
491 }, X71Cfg[6] = {
492 { 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 } },
493 { 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 } },
494 { 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 } },
495 { 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 } },
496 { 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 } },
497 { 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 } },
500 static void InitPanning(ALCdevice *device)
502 const ChannelMap *chanmap = NULL;
503 ALsizei coeffcount = 0;
504 ALsizei count = 0;
505 ALsizei i, j;
507 switch(device->FmtChans)
509 case DevFmtMono:
510 count = COUNTOF(MonoCfg);
511 chanmap = MonoCfg;
512 coeffcount = 1;
513 break;
515 case DevFmtStereo:
516 count = COUNTOF(StereoCfg);
517 chanmap = StereoCfg;
518 coeffcount = 4;
519 break;
521 case DevFmtQuad:
522 count = COUNTOF(QuadCfg);
523 chanmap = QuadCfg;
524 coeffcount = 4;
525 break;
527 case DevFmtX51:
528 count = COUNTOF(X51SideCfg);
529 chanmap = X51SideCfg;
530 coeffcount = 9;
531 break;
533 case DevFmtX51Rear:
534 count = COUNTOF(X51RearCfg);
535 chanmap = X51RearCfg;
536 coeffcount = 9;
537 break;
539 case DevFmtX61:
540 count = COUNTOF(X61Cfg);
541 chanmap = X61Cfg;
542 coeffcount = 9;
543 break;
545 case DevFmtX71:
546 count = COUNTOF(X71Cfg);
547 chanmap = X71Cfg;
548 coeffcount = 16;
549 break;
551 case DevFmtAmbi1:
552 case DevFmtAmbi2:
553 case DevFmtAmbi3:
554 break;
557 if(device->FmtChans >= DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
559 const ALsizei *acnmap = (device->AmbiFmt == AmbiFormat_FuMa) ? FuMa2ACN : ACN2ACN;
560 const ALfloat *n3dscale = (device->AmbiFmt == AmbiFormat_FuMa) ? FuMa2N3DScale :
561 (device->AmbiFmt == AmbiFormat_ACN_SN3D) ? SN3D2N3DScale :
562 /*(device->AmbiFmt == AmbiFormat_ACN_N3D) ?*/ UnitScale;
564 count = (device->FmtChans == DevFmtAmbi3) ? 16 :
565 (device->FmtChans == DevFmtAmbi2) ? 9 :
566 (device->FmtChans == DevFmtAmbi1) ? 4 : 1;
567 for(i = 0;i < count;i++)
569 ALsizei acn = acnmap[i];
570 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
571 device->Dry.Ambi.Map[i].Index = acn;
573 device->Dry.CoeffCount = 0;
574 device->Dry.NumChannels = count;
576 if(device->FmtChans == DevFmtAmbi1)
578 device->FOAOut.Ambi = device->Dry.Ambi;
579 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
580 device->FOAOut.NumChannels = 0;
582 else
584 /* FOA output is always ACN+N3D for higher-order ambisonic output.
585 * The upsampler expects this and will convert it for output.
587 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
588 for(i = 0;i < 4;i++)
590 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
591 device->FOAOut.Ambi.Map[i].Index = i;
593 device->FOAOut.CoeffCount = 0;
594 device->FOAOut.NumChannels = 4;
596 ambiup_reset(device->AmbiUp, device);
599 else
601 ALfloat w_scale, xyz_scale;
603 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
604 chanmap, count, &device->Dry.NumChannels);
605 device->Dry.CoeffCount = coeffcount;
607 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE2D_THIRD :
608 (device->Dry.CoeffCount > 4) ? W_SCALE2D_SECOND : 1.0f;
609 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE2D_THIRD :
610 (device->Dry.CoeffCount > 4) ? XYZ_SCALE2D_SECOND : 1.0f;
612 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
613 for(i = 0;i < device->Dry.NumChannels;i++)
615 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
616 for(j = 1;j < 4;j++)
617 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
619 device->FOAOut.CoeffCount = 4;
620 device->FOAOut.NumChannels = 0;
622 device->RealOut.NumChannels = 0;
625 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
627 const char *devname = al_string_get_cstr(device->DeviceName);
628 ALfloat maxdist = 0.0f;
629 ALsizei i;
631 for(i = 0;i < conf->NumSpeakers;i++)
632 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
634 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
636 ALfloat srate = (ALfloat)device->Frequency;
637 for(i = 0;i < conf->NumSpeakers;i++)
639 ALsizei chan = speakermap[i];
640 ALfloat delay;
642 /* Distance compensation only delays in steps of the sample rate.
643 * This is a bit less accurate since the delay time falls to the
644 * nearest sample time, but it's far simpler as it doesn't have to
645 * deal with phase offsets. This means at 48khz, for instance, the
646 * distance delay will be in steps of about 7 millimeters.
648 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
649 srate + 0.5f);
650 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
651 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
652 al_string_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
654 device->ChannelDelay[chan].Length = (ALsizei)clampf(
655 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
657 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
658 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
659 al_string_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
660 device->ChannelDelay[chan].Gain
666 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
668 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
669 const ALfloat *coeff_scale = UnitScale;
670 ALfloat w_scale = 1.0f;
671 ALfloat xyz_scale = 1.0f;
672 ALsizei i, j;
674 if(conf->FreqBands != 1)
675 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
676 conf->XOverFreq);
678 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
680 if(conf->ChanMask > 0x1ff)
682 w_scale = W_SCALE3D_THIRD;
683 xyz_scale = XYZ_SCALE3D_THIRD;
685 else if(conf->ChanMask > 0xf)
687 w_scale = W_SCALE3D_SECOND;
688 xyz_scale = XYZ_SCALE3D_SECOND;
691 else
693 if(conf->ChanMask > 0x1ff)
695 w_scale = W_SCALE2D_THIRD;
696 xyz_scale = XYZ_SCALE2D_THIRD;
698 else if(conf->ChanMask > 0xf)
700 w_scale = W_SCALE2D_SECOND;
701 xyz_scale = XYZ_SCALE2D_SECOND;
705 if(conf->CoeffScale == ADS_SN3D)
706 coeff_scale = SN3D2N3DScale;
707 else if(conf->CoeffScale == ADS_FuMa)
708 coeff_scale = FuMa2N3DScale;
710 for(i = 0;i < conf->NumSpeakers;i++)
712 ALsizei chan = speakermap[i];
713 ALfloat gain;
714 ALsizei k = 0;
716 for(j = 0;j < MAX_AMBI_COEFFS;j++)
717 chanmap[i].Config[j] = 0.0f;
719 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
720 for(j = 0;j < MAX_AMBI_COEFFS;j++)
722 if(j == 0) gain = conf->HFOrderGain[0];
723 else if(j == 1) gain = conf->HFOrderGain[1];
724 else if(j == 4) gain = conf->HFOrderGain[2];
725 else if(j == 9) gain = conf->HFOrderGain[3];
726 if((conf->ChanMask&(1<<j)))
727 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
731 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
732 conf->NumSpeakers, &device->Dry.NumChannels);
733 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
734 (conf->ChanMask > 0xf) ? 9 : 4;
736 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
737 for(i = 0;i < device->Dry.NumChannels;i++)
739 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
740 for(j = 1;j < 4;j++)
741 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
743 device->FOAOut.CoeffCount = 4;
744 device->FOAOut.NumChannels = 0;
746 device->RealOut.NumChannels = 0;
748 InitDistanceComp(device, conf, speakermap);
751 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
753 size_t count;
754 size_t i;
756 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
758 count = (conf->ChanMask > 0x1ff) ? 16 :
759 (conf->ChanMask > 0xf) ? 9 : 4;
760 for(i = 0;i < count;i++)
762 device->Dry.Ambi.Map[i].Scale = 1.0f;
763 device->Dry.Ambi.Map[i].Index = i;
766 else
768 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
770 count = (conf->ChanMask > 0x1ff) ? 7 :
771 (conf->ChanMask > 0xf) ? 5 : 3;
772 for(i = 0;i < count;i++)
774 device->Dry.Ambi.Map[i].Scale = 1.0f;
775 device->Dry.Ambi.Map[i].Index = map[i];
778 device->Dry.CoeffCount = 0;
779 device->Dry.NumChannels = count;
781 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
782 (conf->FreqBands == 1) ? "single" : "dual",
783 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
784 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
786 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
788 if(bformatdec_getOrder(device->AmbiDecoder) < 2)
790 device->FOAOut.Ambi = device->Dry.Ambi;
791 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
792 device->FOAOut.NumChannels = 0;
794 else
796 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
797 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
799 count = 4;
800 for(i = 0;i < count;i++)
802 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
803 device->FOAOut.Ambi.Map[i].Index = i;
806 else
808 static const int map[3] = { 0, 1, 3 };
809 count = 3;
810 for(i = 0;i < count;i++)
812 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
813 device->FOAOut.Ambi.Map[i].Index = map[i];
816 device->FOAOut.CoeffCount = 0;
817 device->FOAOut.NumChannels = count;
820 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
822 InitDistanceComp(device, conf, speakermap);
825 static void InitHrtfPanning(ALCdevice *device, bool hoa_mode)
827 /* NOTE: azimuth goes clockwise. */
828 static const ALfloat AmbiPoints[][2] = {
829 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
830 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
831 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
832 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
833 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
834 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
835 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
836 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
837 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
838 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
839 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
840 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
841 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
842 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
844 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
845 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
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, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
849 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
850 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
851 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
852 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
853 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
854 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
855 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
856 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
857 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
858 { { 1.88982237e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
859 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
860 { { 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 } },
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.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 } },
864 { { 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 } },
865 { { 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 } },
866 { { 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 } },
867 { { 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 } },
868 { { 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 } },
869 { { 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 } },
870 { { 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 } },
871 { { 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 } },
872 { { 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 } },
873 { { 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 } },
875 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = hoa_mode ? AmbiMatrixHOA : AmbiMatrixFOA;
876 size_t count = hoa_mode ? 9 : 4;
877 size_t i;
879 static_assert(9 <= COUNTOF(device->Hrtf.Coeffs), "ALCdevice::Hrtf.Values/Coeffs size is too small");
880 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
882 for(i = 0;i < count;i++)
884 device->Dry.Ambi.Map[i].Scale = 1.0f;
885 device->Dry.Ambi.Map[i].Index = i;
887 device->Dry.CoeffCount = 0;
888 device->Dry.NumChannels = count;
890 if(!hoa_mode)
892 device->FOAOut.Ambi = device->Dry.Ambi;
893 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
894 device->FOAOut.NumChannels = 0;
896 else
898 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
899 for(i = 0;i < 4;i++)
901 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
902 device->FOAOut.Ambi.Map[i].Index = i;
904 device->FOAOut.CoeffCount = 0;
905 device->FOAOut.NumChannels = 4;
907 ambiup_reset(device->AmbiUp, device);
910 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
912 memset(device->Hrtf.Coeffs, 0, sizeof(device->Hrtf.Coeffs));
913 device->Hrtf.IrSize = BuildBFormatHrtf(device->Hrtf.Handle,
914 device->Hrtf.Coeffs, device->Dry.NumChannels,
915 AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
918 /* Round up to the nearest multiple of 8 */
919 device->Hrtf.IrSize = (device->Hrtf.IrSize+7)&~7;
922 static void InitUhjPanning(ALCdevice *device)
924 ALsizei count = 3;
925 ALsizei i;
927 for(i = 0;i < count;i++)
929 ALsizei acn = FuMa2ACN[i];
930 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
931 device->Dry.Ambi.Map[i].Index = acn;
933 device->Dry.CoeffCount = 0;
934 device->Dry.NumChannels = count;
936 device->FOAOut.Ambi = device->Dry.Ambi;
937 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
938 device->FOAOut.NumChannels = 0;
940 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
943 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
945 const char *mode;
946 bool headphones;
947 int bs2blevel;
948 size_t i;
950 device->Hrtf.Handle = NULL;
951 al_string_clear(&device->Hrtf.Name);
952 device->Render_Mode = NormalRender;
954 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
955 device->Dry.CoeffCount = 0;
956 device->Dry.NumChannels = 0;
958 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
959 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
961 device->ChannelDelay[i].Gain = 1.0f;
962 device->ChannelDelay[i].Length = 0;
965 if(device->FmtChans != DevFmtStereo)
967 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
968 const char *devname, *layout = NULL;
969 AmbDecConf conf, *pconf = NULL;
971 if(hrtf_appreq == Hrtf_Enable)
972 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
974 ambdec_init(&conf);
976 devname = al_string_get_cstr(device->DeviceName);
977 switch(device->FmtChans)
979 case DevFmtQuad: layout = "quad"; break;
980 case DevFmtX51: /* fall-through */
981 case DevFmtX51Rear: layout = "surround51"; break;
982 case DevFmtX61: layout = "surround61"; break;
983 case DevFmtX71: layout = "surround71"; break;
984 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
985 case DevFmtMono:
986 case DevFmtStereo:
987 case DevFmtAmbi1:
988 case DevFmtAmbi2:
989 case DevFmtAmbi3:
990 break;
992 if(layout)
994 const char *fname;
995 if(ConfigValueStr(devname, "decoder", layout, &fname))
997 if(!ambdec_load(&conf, fname))
998 ERR("Failed to load layout file %s\n", fname);
999 else
1001 if(conf.ChanMask > 0xffff)
1002 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1003 else
1005 if(MakeSpeakerMap(device, &conf, speakermap))
1006 pconf = &conf;
1012 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1014 ambiup_free(device->AmbiUp);
1015 device->AmbiUp = NULL;
1016 if(!device->AmbiDecoder)
1017 device->AmbiDecoder = bformatdec_alloc();
1019 else
1021 bformatdec_free(device->AmbiDecoder);
1022 device->AmbiDecoder = NULL;
1023 if(device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
1025 if(!device->AmbiUp)
1026 device->AmbiUp = ambiup_alloc();
1028 else
1030 ambiup_free(device->AmbiUp);
1031 device->AmbiUp = NULL;
1035 if(!pconf)
1036 InitPanning(device);
1037 else if(device->AmbiDecoder)
1038 InitHQPanning(device, pconf, speakermap);
1039 else
1040 InitCustomPanning(device, pconf, speakermap);
1042 ambdec_deinit(&conf);
1043 return;
1046 bformatdec_free(device->AmbiDecoder);
1047 device->AmbiDecoder = NULL;
1049 headphones = device->IsHeadphones;
1050 if(device->Type != Loopback)
1052 const char *mode;
1053 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1055 if(strcasecmp(mode, "headphones") == 0)
1056 headphones = true;
1057 else if(strcasecmp(mode, "speakers") == 0)
1058 headphones = false;
1059 else if(strcasecmp(mode, "auto") != 0)
1060 ERR("Unexpected stereo-mode: %s\n", mode);
1064 if(hrtf_userreq == Hrtf_Default)
1066 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1067 (hrtf_appreq == Hrtf_Enable);
1068 if(!usehrtf) goto no_hrtf;
1070 device->Hrtf.Status = ALC_HRTF_ENABLED_SOFT;
1071 if(headphones && hrtf_appreq != Hrtf_Disable)
1072 device->Hrtf.Status = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1074 else
1076 if(hrtf_userreq != Hrtf_Enable)
1078 if(hrtf_appreq == Hrtf_Enable)
1079 device->Hrtf.Status = ALC_HRTF_DENIED_SOFT;
1080 goto no_hrtf;
1082 device->Hrtf.Status = ALC_HRTF_REQUIRED_SOFT;
1085 if(VECTOR_SIZE(device->Hrtf.List) == 0)
1087 VECTOR_DEINIT(device->Hrtf.List);
1088 device->Hrtf.List = EnumerateHrtf(device->DeviceName);
1091 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->Hrtf.List))
1093 const HrtfEntry *entry = &VECTOR_ELEM(device->Hrtf.List, hrtf_id);
1094 if(entry->hrtf->sampleRate == device->Frequency)
1096 device->Hrtf.Handle = entry->hrtf;
1097 al_string_copy(&device->Hrtf.Name, entry->name);
1101 for(i = 0;!device->Hrtf.Handle && i < VECTOR_SIZE(device->Hrtf.List);i++)
1103 const HrtfEntry *entry = &VECTOR_ELEM(device->Hrtf.List, i);
1104 if(entry->hrtf->sampleRate == device->Frequency)
1106 device->Hrtf.Handle = entry->hrtf;
1107 al_string_copy(&device->Hrtf.Name, entry->name);
1111 if(device->Hrtf.Handle)
1113 bool hoa_mode;
1115 device->Render_Mode = HrtfRender;
1116 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1118 if(strcasecmp(mode, "full") == 0)
1119 device->Render_Mode = HrtfRender;
1120 else if(strcasecmp(mode, "basic") == 0)
1121 device->Render_Mode = NormalRender;
1122 else
1123 ERR("Unexpected hrtf-mode: %s\n", mode);
1126 if(device->Render_Mode == HrtfRender)
1128 /* Don't bother with HOA when using full HRTF rendering. Nothing
1129 * needs it, and it eases the CPU/memory load.
1131 ambiup_free(device->AmbiUp);
1132 device->AmbiUp = NULL;
1133 hoa_mode = false;
1135 else
1137 if(!device->AmbiUp)
1138 device->AmbiUp = ambiup_alloc();
1139 hoa_mode = true;
1142 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1143 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1144 al_string_get_cstr(device->Hrtf.Name)
1146 InitHrtfPanning(device, hoa_mode);
1147 return;
1149 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1151 no_hrtf:
1152 TRACE("HRTF disabled\n");
1154 device->Render_Mode = StereoPair;
1156 ambiup_free(device->AmbiUp);
1157 device->AmbiUp = NULL;
1159 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1160 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1161 if(device->Type != Loopback)
1162 ConfigValueInt(al_string_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1163 if(bs2blevel > 0 && bs2blevel <= 6)
1165 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1166 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1167 TRACE("BS2B enabled\n");
1168 InitPanning(device);
1169 return;
1172 TRACE("BS2B disabled\n");
1174 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1176 if(strcasecmp(mode, "uhj") == 0)
1177 device->Render_Mode = NormalRender;
1178 else if(strcasecmp(mode, "panpot") != 0)
1179 ERR("Unexpected stereo-encoding: %s\n", mode);
1181 if(device->Render_Mode == NormalRender)
1183 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1184 TRACE("UHJ enabled\n");
1185 InitUhjPanning(device);
1186 return;
1189 TRACE("UHJ disabled\n");
1190 InitPanning(device);
1194 void aluInitEffectPanning(ALeffectslot *slot)
1196 ALsizei i;
1198 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1199 slot->NumChannels = 0;
1201 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1203 slot->ChanMap[i].Scale = 1.0f;
1204 slot->ChanMap[i].Index = i;
1206 slot->NumChannels = i;