Clarify some macro names using ambisonic mixed-mode notation
[openal-soft.git] / Alc / panning.c
blobeaeb264670e9d7540946fb549d17d34167fedc52
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 "alconfig.h"
33 #include "bool.h"
34 #include "ambdec.h"
35 #include "bformatdec.h"
36 #include "uhjfilter.h"
37 #include "bs2b.h"
40 extern inline void CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
41 extern inline void ComputeAmbientGains(const DryMixParams *dry, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
42 extern inline void ComputeDryPanGains(const DryMixParams *dry, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
43 extern inline void ComputeFirstOrderGains(const BFMixParams *foa, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
46 static const ALsizei FuMa2ACN[MAX_AMBI_COEFFS] = {
47 0, /* W */
48 3, /* X */
49 1, /* Y */
50 2, /* Z */
51 6, /* R */
52 7, /* S */
53 5, /* T */
54 8, /* U */
55 4, /* V */
56 12, /* K */
57 13, /* L */
58 11, /* M */
59 14, /* N */
60 10, /* O */
61 15, /* P */
62 9, /* Q */
64 static const ALsizei ACN2ACN[MAX_AMBI_COEFFS] = {
65 0, 1, 2, 3, 4, 5, 6, 7,
66 8, 9, 10, 11, 12, 13, 14, 15
69 /* NOTE: These are scale factors as applied to Ambisonics content. Decoder
70 * coefficients should be divided by these values to get proper N3D scalings.
72 static const ALfloat UnitScale[MAX_AMBI_COEFFS] = {
73 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
74 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
76 static const ALfloat SN3D2N3DScale[MAX_AMBI_COEFFS] = {
77 1.000000000f, /* ACN 0 (W), sqrt(1) */
78 1.732050808f, /* ACN 1 (Y), sqrt(3) */
79 1.732050808f, /* ACN 2 (Z), sqrt(3) */
80 1.732050808f, /* ACN 3 (X), sqrt(3) */
81 2.236067978f, /* ACN 4 (V), sqrt(5) */
82 2.236067978f, /* ACN 5 (T), sqrt(5) */
83 2.236067978f, /* ACN 6 (R), sqrt(5) */
84 2.236067978f, /* ACN 7 (S), sqrt(5) */
85 2.236067978f, /* ACN 8 (U), sqrt(5) */
86 2.645751311f, /* ACN 9 (Q), sqrt(7) */
87 2.645751311f, /* ACN 10 (O), sqrt(7) */
88 2.645751311f, /* ACN 11 (M), sqrt(7) */
89 2.645751311f, /* ACN 12 (K), sqrt(7) */
90 2.645751311f, /* ACN 13 (L), sqrt(7) */
91 2.645751311f, /* ACN 14 (N), sqrt(7) */
92 2.645751311f, /* ACN 15 (P), sqrt(7) */
94 static const ALfloat FuMa2N3DScale[MAX_AMBI_COEFFS] = {
95 1.414213562f, /* ACN 0 (W), sqrt(2) */
96 1.732050808f, /* ACN 1 (Y), sqrt(3) */
97 1.732050808f, /* ACN 2 (Z), sqrt(3) */
98 1.732050808f, /* ACN 3 (X), sqrt(3) */
99 1.936491673f, /* ACN 4 (V), sqrt(15)/2 */
100 1.936491673f, /* ACN 5 (T), sqrt(15)/2 */
101 2.236067978f, /* ACN 6 (R), sqrt(5) */
102 1.936491673f, /* ACN 7 (S), sqrt(15)/2 */
103 1.936491673f, /* ACN 8 (U), sqrt(15)/2 */
104 2.091650066f, /* ACN 9 (Q), sqrt(35/8) */
105 1.972026594f, /* ACN 10 (O), sqrt(35)/3 */
106 2.231093404f, /* ACN 11 (M), sqrt(224/45) */
107 2.645751311f, /* ACN 12 (K), sqrt(7) */
108 2.231093404f, /* ACN 13 (L), sqrt(224/45) */
109 1.972026594f, /* ACN 14 (N), sqrt(35)/3 */
110 2.091650066f, /* ACN 15 (P), sqrt(35/8) */
114 void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
116 /* Convert from OpenAL coords to Ambisonics. */
117 ALfloat x = -dir[2];
118 ALfloat y = -dir[0];
119 ALfloat z = dir[1];
121 /* Zeroth-order */
122 coeffs[0] = 1.0f; /* ACN 0 = 1 */
123 /* First-order */
124 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
125 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
126 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
127 /* Second-order */
128 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
129 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
130 coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
131 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
132 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
133 /* Third-order */
134 coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
135 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
136 coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
137 coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
138 coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
139 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
140 coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
142 if(spread > 0.0f)
144 /* Implement the spread by using a spherical source that subtends the
145 * angle spread. See:
146 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
148 * When adjusted for N3D normalization instead of SN3D, these
149 * calculations are:
151 * ZH0 = -sqrt(pi) * (-1+ca);
152 * ZH1 = 0.5*sqrt(pi) * sa*sa;
153 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
154 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
155 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
156 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
158 * The gain of the source is compensated for size, so that the
159 * loundness doesn't depend on the spread. Thus:
161 * ZH0 = 1.0f;
162 * ZH1 = 0.5f * (ca+1.0f);
163 * ZH2 = 0.5f * (ca+1.0f)*ca;
164 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
165 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
166 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
168 ALfloat ca = cosf(spread * 0.5f);
169 /* Increase the source volume by up to +3dB for a full spread. */
170 ALfloat scale = sqrtf(1.0f + spread/F_TAU);
172 ALfloat ZH0_norm = scale;
173 ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
174 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
175 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
177 /* Zeroth-order */
178 coeffs[0] *= ZH0_norm;
179 /* First-order */
180 coeffs[1] *= ZH1_norm;
181 coeffs[2] *= ZH1_norm;
182 coeffs[3] *= ZH1_norm;
183 /* Second-order */
184 coeffs[4] *= ZH2_norm;
185 coeffs[5] *= ZH2_norm;
186 coeffs[6] *= ZH2_norm;
187 coeffs[7] *= ZH2_norm;
188 coeffs[8] *= ZH2_norm;
189 /* Third-order */
190 coeffs[9] *= ZH3_norm;
191 coeffs[10] *= ZH3_norm;
192 coeffs[11] *= ZH3_norm;
193 coeffs[12] *= ZH3_norm;
194 coeffs[13] *= ZH3_norm;
195 coeffs[14] *= ZH3_norm;
196 coeffs[15] *= ZH3_norm;
200 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
202 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
203 if(!(fabsf(azimuth) > F_PI_2))
204 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
205 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
209 void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
211 ALsizei i;
213 for(i = 0;i < numchans;i++)
214 gains[i] = chancoeffs[i][0] * 1.414213562f * ingain;
215 for(;i < MAX_OUTPUT_CHANNELS;i++)
216 gains[i] = 0.0f;
219 void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
221 ALfloat gain = 0.0f;
222 ALsizei i;
224 for(i = 0;i < numchans;i++)
226 if(chanmap[i].Index == 0)
227 gain += chanmap[i].Scale;
229 gains[0] = gain * 1.414213562f * ingain;
230 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
231 gains[i] = 0.0f;
234 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
236 ALsizei i, j;
238 for(i = 0;i < numchans;i++)
240 float gain = 0.0f;
241 for(j = 0;j < numcoeffs;j++)
242 gain += chancoeffs[i][j]*coeffs[j];
243 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
245 for(;i < MAX_OUTPUT_CHANNELS;i++)
246 gains[i] = 0.0f;
249 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
251 ALsizei i;
253 for(i = 0;i < numchans;i++)
254 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
255 for(;i < MAX_OUTPUT_CHANNELS;i++)
256 gains[i] = 0.0f;
259 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
261 ALsizei i, j;
263 for(i = 0;i < numchans;i++)
265 float gain = 0.0f;
266 for(j = 0;j < 4;j++)
267 gain += chancoeffs[i][j] * mtx[j];
268 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
270 for(;i < MAX_OUTPUT_CHANNELS;i++)
271 gains[i] = 0.0f;
274 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
276 ALsizei i;
278 for(i = 0;i < numchans;i++)
279 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
280 for(;i < MAX_OUTPUT_CHANNELS;i++)
281 gains[i] = 0.0f;
285 static inline const char *GetLabelFromChannel(enum Channel channel)
287 switch(channel)
289 case FrontLeft: return "front-left";
290 case FrontRight: return "front-right";
291 case FrontCenter: return "front-center";
292 case LFE: return "lfe";
293 case BackLeft: return "back-left";
294 case BackRight: return "back-right";
295 case BackCenter: return "back-center";
296 case SideLeft: return "side-left";
297 case SideRight: return "side-right";
299 case UpperFrontLeft: return "upper-front-left";
300 case UpperFrontRight: return "upper-front-right";
301 case UpperBackLeft: return "upper-back-left";
302 case UpperBackRight: return "upper-back-right";
303 case LowerFrontLeft: return "lower-front-left";
304 case LowerFrontRight: return "lower-front-right";
305 case LowerBackLeft: return "lower-back-left";
306 case LowerBackRight: return "lower-back-right";
308 case Aux0: return "aux-0";
309 case Aux1: return "aux-1";
310 case Aux2: return "aux-2";
311 case Aux3: return "aux-3";
312 case Aux4: return "aux-4";
313 case Aux5: return "aux-5";
314 case Aux6: return "aux-6";
315 case Aux7: return "aux-7";
316 case Aux8: return "aux-8";
317 case Aux9: return "aux-9";
318 case Aux10: return "aux-10";
319 case Aux11: return "aux-11";
320 case Aux12: return "aux-12";
321 case Aux13: return "aux-13";
322 case Aux14: return "aux-14";
323 case Aux15: return "aux-15";
325 case InvalidChannel: break;
327 return "(unknown)";
331 typedef struct ChannelMap {
332 enum Channel ChanName;
333 ChannelConfig Config;
334 } ChannelMap;
336 static void SetChannelMap(const enum Channel devchans[MAX_OUTPUT_CHANNELS],
337 ChannelConfig *ambicoeffs, const ChannelMap *chanmap,
338 ALsizei count, ALsizei *outcount)
340 ALsizei maxchans = 0;
341 ALsizei i, j;
343 for(i = 0;i < count;i++)
345 ALint idx = GetChannelIndex(devchans, chanmap[i].ChanName);
346 if(idx < 0)
348 ERR("Failed to find %s channel in device\n",
349 GetLabelFromChannel(chanmap[i].ChanName));
350 continue;
353 maxchans = maxi(maxchans, idx+1);
354 for(j = 0;j < MAX_AMBI_COEFFS;j++)
355 ambicoeffs[idx][j] = chanmap[i].Config[j];
357 *outcount = mini(maxchans, MAX_OUTPUT_CHANNELS);
360 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
362 ALsizei i;
364 for(i = 0;i < conf->NumSpeakers;i++)
366 int c = -1;
368 /* NOTE: AmbDec does not define any standard speaker names, however
369 * for this to work we have to by able to find the output channel
370 * the speaker definition corresponds to. Therefore, OpenAL Soft
371 * requires these channel labels to be recognized:
373 * LF = Front left
374 * RF = Front right
375 * LS = Side left
376 * RS = Side right
377 * LB = Back left
378 * RB = Back right
379 * CE = Front center
380 * CB = Back center
382 * Additionally, surround51 will acknowledge back speakers for side
383 * channels, and surround51rear will acknowledge side speakers for
384 * back channels, to avoid issues with an ambdec expecting 5.1 to
385 * use the side channels when the device is configured for back,
386 * and vice-versa.
388 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
389 c = GetChannelIdxByName(&device->RealOut, FrontLeft);
390 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
391 c = GetChannelIdxByName(&device->RealOut, FrontRight);
392 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
393 c = GetChannelIdxByName(&device->RealOut, FrontCenter);
394 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
396 if(device->FmtChans == DevFmtX51Rear)
397 c = GetChannelIdxByName(&device->RealOut, BackLeft);
398 else
399 c = GetChannelIdxByName(&device->RealOut, SideLeft);
401 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
403 if(device->FmtChans == DevFmtX51Rear)
404 c = GetChannelIdxByName(&device->RealOut, BackRight);
405 else
406 c = GetChannelIdxByName(&device->RealOut, SideRight);
408 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
410 if(device->FmtChans == DevFmtX51)
411 c = GetChannelIdxByName(&device->RealOut, SideLeft);
412 else
413 c = GetChannelIdxByName(&device->RealOut, BackLeft);
415 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
417 if(device->FmtChans == DevFmtX51)
418 c = GetChannelIdxByName(&device->RealOut, SideRight);
419 else
420 c = GetChannelIdxByName(&device->RealOut, BackRight);
422 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
423 c = GetChannelIdxByName(&device->RealOut, BackCenter);
424 else
426 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
427 unsigned int n;
428 char ch;
430 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
431 c = GetChannelIdxByName(&device->RealOut, Aux0+n);
432 else
434 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
435 return false;
438 if(c == -1)
440 ERR("Failed to lookup AmbDec speaker label %s\n",
441 alstr_get_cstr(conf->Speakers[i].Name));
442 return false;
444 speakermap[i] = c;
447 return true;
451 static const ChannelMap MonoCfg[1] = {
452 { FrontCenter, { 1.0f } },
453 }, StereoCfg[2] = {
454 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
455 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
456 }, QuadCfg[4] = {
457 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
458 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
459 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
460 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
461 }, X51SideCfg[4] = {
462 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
463 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
464 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
465 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
466 }, X51RearCfg[4] = {
467 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
468 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
469 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
470 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
471 }, X61Cfg[6] = {
472 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, 0.0f, -4.39996780e-2f, -2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
473 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 0.0f, 1.59658796e-1f, 6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
474 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 0.0f, 1.59658796e-1f, -6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
475 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, 0.0f, -4.39996780e-2f, 2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
476 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
477 }, X71Cfg[6] = {
478 { 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 } },
479 { 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 } },
480 { 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 } },
481 { 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 } },
482 { 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 } },
483 { 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 } },
486 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
488 const char *devname = alstr_get_cstr(device->DeviceName);
489 ALsizei i;
491 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
493 /* NFC is only used when AvgSpeakerDist is greater than 0, and
494 * METERS_PER_UNIT is also greater than 0. In addition, NFC can only be
495 * used when rendering to an ambisonic buffer.
497 device->AvgSpeakerDist = ctrl_dist;
499 device->Dry.NumChannelsPerOrder[0] = 1;
500 if(periphonic)
501 for(i = 1;i < order+1;i++)
502 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
503 else
504 for(i = 1;i < order+1;i++)
505 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
506 for(;i < MAX_AMBI_ORDER+1;i++)
507 device->Dry.NumChannelsPerOrder[i] = 0;
511 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
513 const char *devname = alstr_get_cstr(device->DeviceName);
514 ALfloat maxdist = 0.0f;
515 size_t total = 0;
516 ALsizei i;
518 for(i = 0;i < conf->NumSpeakers;i++)
519 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
521 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
523 ALfloat srate = (ALfloat)device->Frequency;
524 for(i = 0;i < conf->NumSpeakers;i++)
526 ALsizei chan = speakermap[i];
527 ALfloat delay;
529 /* Distance compensation only delays in steps of the sample rate.
530 * This is a bit less accurate since the delay time falls to the
531 * nearest sample time, but it's far simpler as it doesn't have to
532 * deal with phase offsets. This means at 48khz, for instance, the
533 * distance delay will be in steps of about 7 millimeters.
535 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
536 srate + 0.5f);
537 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
538 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
539 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
541 device->ChannelDelay[chan].Length = (ALsizei)clampf(
542 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
544 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
545 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
546 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
547 device->ChannelDelay[chan].Gain
550 /* Round up to the next 4th sample, so each channel buffer starts
551 * 16-byte aligned.
553 total += RoundUp(device->ChannelDelay[chan].Length, 4);
557 if(total > 0)
559 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
560 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
562 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
563 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
568 static void InitPanning(ALCdevice *device)
570 const ChannelMap *chanmap = NULL;
571 ALsizei coeffcount = 0;
572 ALsizei count = 0;
573 ALsizei i, j;
575 switch(device->FmtChans)
577 case DevFmtMono:
578 count = COUNTOF(MonoCfg);
579 chanmap = MonoCfg;
580 coeffcount = 1;
581 break;
583 case DevFmtStereo:
584 count = COUNTOF(StereoCfg);
585 chanmap = StereoCfg;
586 coeffcount = 4;
587 break;
589 case DevFmtQuad:
590 count = COUNTOF(QuadCfg);
591 chanmap = QuadCfg;
592 coeffcount = 4;
593 break;
595 case DevFmtX51:
596 count = COUNTOF(X51SideCfg);
597 chanmap = X51SideCfg;
598 coeffcount = 9;
599 break;
601 case DevFmtX51Rear:
602 count = COUNTOF(X51RearCfg);
603 chanmap = X51RearCfg;
604 coeffcount = 9;
605 break;
607 case DevFmtX61:
608 count = COUNTOF(X61Cfg);
609 chanmap = X61Cfg;
610 coeffcount = 9;
611 break;
613 case DevFmtX71:
614 count = COUNTOF(X71Cfg);
615 chanmap = X71Cfg;
616 coeffcount = 16;
617 break;
619 case DevFmtAmbi3D:
620 break;
623 if(device->FmtChans == DevFmtAmbi3D)
625 const char *devname = alstr_get_cstr(device->DeviceName);
626 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
627 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
628 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
629 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ UnitScale;
630 ALfloat nfc_delay = 0.0f;
632 count = (device->AmbiOrder == 3) ? 16 :
633 (device->AmbiOrder == 2) ? 9 :
634 (device->AmbiOrder == 1) ? 4 : 1;
635 for(i = 0;i < count;i++)
637 ALsizei acn = acnmap[i];
638 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
639 device->Dry.Ambi.Map[i].Index = acn;
641 device->Dry.CoeffCount = 0;
642 device->Dry.NumChannels = count;
644 if(device->AmbiOrder < 2)
646 device->FOAOut.Ambi = device->Dry.Ambi;
647 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
648 device->FOAOut.NumChannels = 0;
650 else
652 /* FOA output is always ACN+N3D for higher-order ambisonic output.
653 * The upsampler expects this and will convert it for output.
655 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
656 for(i = 0;i < 4;i++)
658 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
659 device->FOAOut.Ambi.Map[i].Index = i;
661 device->FOAOut.CoeffCount = 0;
662 device->FOAOut.NumChannels = 4;
664 ambiup_reset(device->AmbiUp, device);
667 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
669 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
670 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
671 device->AmbiOrder, true);
674 else
676 ALfloat w_scale, xyz_scale;
678 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
679 chanmap, count, &device->Dry.NumChannels);
680 device->Dry.CoeffCount = coeffcount;
682 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE_3H0P :
683 (device->Dry.CoeffCount > 4) ? W_SCALE_2H0P : 1.0f;
684 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE_3H0P :
685 (device->Dry.CoeffCount > 4) ? XYZ_SCALE_2H0P : 1.0f;
687 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
688 for(i = 0;i < device->Dry.NumChannels;i++)
690 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
691 for(j = 1;j < 4;j++)
692 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
694 device->FOAOut.CoeffCount = 4;
695 device->FOAOut.NumChannels = 0;
697 device->RealOut.NumChannels = 0;
700 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
702 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
703 const ALfloat *coeff_scale = UnitScale;
704 ALfloat w_scale = 1.0f;
705 ALfloat xyz_scale = 1.0f;
706 ALsizei i, j;
708 if(conf->FreqBands != 1)
709 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
710 conf->XOverFreq);
712 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
714 if(conf->ChanMask > 0x1ff)
716 w_scale = W_SCALE_3H3P;
717 xyz_scale = XYZ_SCALE_3H3P;
719 else if(conf->ChanMask > 0xf)
721 w_scale = W_SCALE_2H2P;
722 xyz_scale = XYZ_SCALE_2H2P;
725 else
727 if(conf->ChanMask > 0x1ff)
729 w_scale = W_SCALE_3H0P;
730 xyz_scale = XYZ_SCALE_3H0P;
732 else if(conf->ChanMask > 0xf)
734 w_scale = W_SCALE_2H0P;
735 xyz_scale = XYZ_SCALE_2H0P;
739 if(conf->CoeffScale == ADS_SN3D)
740 coeff_scale = SN3D2N3DScale;
741 else if(conf->CoeffScale == ADS_FuMa)
742 coeff_scale = FuMa2N3DScale;
744 for(i = 0;i < conf->NumSpeakers;i++)
746 ALsizei chan = speakermap[i];
747 ALfloat gain;
748 ALsizei k = 0;
750 for(j = 0;j < MAX_AMBI_COEFFS;j++)
751 chanmap[i].Config[j] = 0.0f;
753 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
754 for(j = 0;j < MAX_AMBI_COEFFS;j++)
756 if(j == 0) gain = conf->HFOrderGain[0];
757 else if(j == 1) gain = conf->HFOrderGain[1];
758 else if(j == 4) gain = conf->HFOrderGain[2];
759 else if(j == 9) gain = conf->HFOrderGain[3];
760 if((conf->ChanMask&(1<<j)))
761 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
765 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
766 conf->NumSpeakers, &device->Dry.NumChannels);
767 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
768 (conf->ChanMask > 0xf) ? 9 : 4;
770 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
771 for(i = 0;i < device->Dry.NumChannels;i++)
773 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
774 for(j = 1;j < 4;j++)
775 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
777 device->FOAOut.CoeffCount = 4;
778 device->FOAOut.NumChannels = 0;
780 device->RealOut.NumChannels = 0;
782 InitDistanceComp(device, conf, speakermap);
785 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
787 ALfloat avg_dist;
788 ALsizei count;
789 ALsizei i;
791 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
793 count = (conf->ChanMask > 0x1ff) ? 16 :
794 (conf->ChanMask > 0xf) ? 9 : 4;
795 for(i = 0;i < count;i++)
797 device->Dry.Ambi.Map[i].Scale = 1.0f;
798 device->Dry.Ambi.Map[i].Index = i;
801 else
803 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
805 count = (conf->ChanMask > 0x1ff) ? 7 :
806 (conf->ChanMask > 0xf) ? 5 : 3;
807 for(i = 0;i < count;i++)
809 device->Dry.Ambi.Map[i].Scale = 1.0f;
810 device->Dry.Ambi.Map[i].Index = map[i];
813 device->Dry.CoeffCount = 0;
814 device->Dry.NumChannels = count;
816 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
817 (conf->FreqBands == 1) ? "single" : "dual",
818 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
819 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
821 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
823 if(!(conf->ChanMask > 0xf))
825 device->FOAOut.Ambi = device->Dry.Ambi;
826 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
827 device->FOAOut.NumChannels = 0;
829 else
831 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
832 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
834 count = 4;
835 for(i = 0;i < count;i++)
837 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
838 device->FOAOut.Ambi.Map[i].Index = i;
841 else
843 static const int map[3] = { 0, 1, 3 };
844 count = 3;
845 for(i = 0;i < count;i++)
847 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
848 device->FOAOut.Ambi.Map[i].Index = map[i];
851 device->FOAOut.CoeffCount = 0;
852 device->FOAOut.NumChannels = count;
855 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
857 avg_dist = 0.0f;
858 for(i = 0;i < conf->NumSpeakers;i++)
859 avg_dist += conf->Speakers[i].Distance;
860 avg_dist /= (ALfloat)conf->NumSpeakers;
861 InitNearFieldCtrl(device, avg_dist,
862 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
863 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
866 InitDistanceComp(device, conf, speakermap);
869 static void InitHrtfPanning(ALCdevice *device)
871 /* NOTE: azimuth goes clockwise. */
872 static const ALfloat AmbiPoints[][2] = {
873 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
874 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
875 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
876 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
877 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
878 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
879 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
880 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
881 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
882 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
883 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
884 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
885 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
886 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
888 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
889 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
890 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
891 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
892 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
893 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
894 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
895 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
896 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
897 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
898 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
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, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
903 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
904 { { 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 } },
905 { { 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 } },
906 { { 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 } },
907 { { 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 } },
908 { { 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 } },
909 { { 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 } },
910 { { 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 } },
911 { { 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 } },
912 { { 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 } },
913 { { 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 } },
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.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 } },
919 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = device->AmbiUp ? AmbiMatrixHOA :
920 AmbiMatrixFOA;
921 ALsizei count = device->AmbiUp ? 9 : 4;
922 ALsizei i;
924 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
926 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
928 for(i = 0;i < count;i++)
930 device->Dry.Ambi.Map[i].Scale = 1.0f;
931 device->Dry.Ambi.Map[i].Index = i;
933 device->Dry.CoeffCount = 0;
934 device->Dry.NumChannels = count;
936 if(device->AmbiUp)
938 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
939 for(i = 0;i < 4;i++)
941 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
942 device->FOAOut.Ambi.Map[i].Index = i;
944 device->FOAOut.CoeffCount = 0;
945 device->FOAOut.NumChannels = 4;
947 ambiup_reset(device->AmbiUp, device);
949 else
951 device->FOAOut.Ambi = device->Dry.Ambi;
952 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
953 device->FOAOut.NumChannels = 0;
956 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
958 BuildBFormatHrtf(device->HrtfHandle,
959 device->Hrtf, device->Dry.NumChannels, AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
962 InitNearFieldCtrl(device, device->HrtfHandle->distance, device->AmbiUp ? 2 : 1, true);
965 static void InitUhjPanning(ALCdevice *device)
967 ALsizei count = 3;
968 ALsizei i;
970 for(i = 0;i < count;i++)
972 ALsizei acn = FuMa2ACN[i];
973 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
974 device->Dry.Ambi.Map[i].Index = acn;
976 device->Dry.CoeffCount = 0;
977 device->Dry.NumChannels = count;
979 device->FOAOut.Ambi = device->Dry.Ambi;
980 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
981 device->FOAOut.NumChannels = 0;
983 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
986 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
988 /* Hold the HRTF the device last used, in case it's used again. */
989 struct Hrtf *old_hrtf = device->HrtfHandle;
990 const char *mode;
991 bool headphones;
992 int bs2blevel;
993 size_t i;
995 al_free(device->Hrtf);
996 device->Hrtf = NULL;
997 device->HrtfHandle = NULL;
998 alstr_clear(&device->HrtfName);
999 device->Render_Mode = NormalRender;
1001 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
1002 device->Dry.CoeffCount = 0;
1003 device->Dry.NumChannels = 0;
1004 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
1005 device->Dry.NumChannelsPerOrder[i] = 0;
1007 device->AvgSpeakerDist = 0.0f;
1008 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
1009 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1011 device->ChannelDelay[i].Gain = 1.0f;
1012 device->ChannelDelay[i].Length = 0;
1015 al_free(device->Stablizer);
1016 device->Stablizer = NULL;
1018 if(device->FmtChans != DevFmtStereo)
1020 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1021 const char *devname, *layout = NULL;
1022 AmbDecConf conf, *pconf = NULL;
1024 if(old_hrtf)
1025 Hrtf_DecRef(old_hrtf);
1026 old_hrtf = NULL;
1027 if(hrtf_appreq == Hrtf_Enable)
1028 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1030 ambdec_init(&conf);
1032 devname = alstr_get_cstr(device->DeviceName);
1033 switch(device->FmtChans)
1035 case DevFmtQuad: layout = "quad"; break;
1036 case DevFmtX51: /* fall-through */
1037 case DevFmtX51Rear: layout = "surround51"; break;
1038 case DevFmtX61: layout = "surround61"; break;
1039 case DevFmtX71: layout = "surround71"; break;
1040 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1041 case DevFmtMono:
1042 case DevFmtStereo:
1043 case DevFmtAmbi3D:
1044 break;
1046 if(layout)
1048 const char *fname;
1049 if(ConfigValueStr(devname, "decoder", layout, &fname))
1051 if(!ambdec_load(&conf, fname))
1052 ERR("Failed to load layout file %s\n", fname);
1053 else
1055 if(conf.ChanMask > 0xffff)
1056 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1057 else
1059 if(MakeSpeakerMap(device, &conf, speakermap))
1060 pconf = &conf;
1066 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1068 ambiup_free(device->AmbiUp);
1069 device->AmbiUp = NULL;
1070 if(!device->AmbiDecoder)
1071 device->AmbiDecoder = bformatdec_alloc();
1073 else
1075 bformatdec_free(device->AmbiDecoder);
1076 device->AmbiDecoder = NULL;
1077 if(device->FmtChans == DevFmtAmbi3D && device->AmbiOrder > 1)
1079 if(!device->AmbiUp)
1080 device->AmbiUp = ambiup_alloc();
1082 else
1084 ambiup_free(device->AmbiUp);
1085 device->AmbiUp = NULL;
1089 if(!pconf)
1090 InitPanning(device);
1091 else if(device->AmbiDecoder)
1092 InitHQPanning(device, pconf, speakermap);
1093 else
1094 InitCustomPanning(device, pconf, speakermap);
1096 /* Enable the stablizer only for formats that have front-left, front-
1097 * right, and front-center outputs.
1099 switch(device->FmtChans)
1101 case DevFmtX51:
1102 case DevFmtX51Rear:
1103 case DevFmtX61:
1104 case DevFmtX71:
1105 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1107 /* Initialize band-splitting filters for the front-left and
1108 * front-right channels, with a crossover at 5khz (could be
1109 * higher).
1111 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1112 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1114 bandsplit_init(&stablizer->LFilter, scale);
1115 stablizer->RFilter = stablizer->LFilter;
1117 /* Initialize all-pass filters for all other channels. */
1118 splitterap_init(&stablizer->APFilter[0], scale);
1119 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1120 stablizer->APFilter[i] = stablizer->APFilter[0];
1122 device->Stablizer = stablizer;
1124 break;
1125 case DevFmtMono:
1126 case DevFmtStereo:
1127 case DevFmtQuad:
1128 case DevFmtAmbi3D:
1129 break;
1131 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1133 ambdec_deinit(&conf);
1134 return;
1137 bformatdec_free(device->AmbiDecoder);
1138 device->AmbiDecoder = NULL;
1140 headphones = device->IsHeadphones;
1141 if(device->Type != Loopback)
1143 const char *mode;
1144 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1146 if(strcasecmp(mode, "headphones") == 0)
1147 headphones = true;
1148 else if(strcasecmp(mode, "speakers") == 0)
1149 headphones = false;
1150 else if(strcasecmp(mode, "auto") != 0)
1151 ERR("Unexpected stereo-mode: %s\n", mode);
1155 if(hrtf_userreq == Hrtf_Default)
1157 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1158 (hrtf_appreq == Hrtf_Enable);
1159 if(!usehrtf) goto no_hrtf;
1161 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1162 if(headphones && hrtf_appreq != Hrtf_Disable)
1163 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1165 else
1167 if(hrtf_userreq != Hrtf_Enable)
1169 if(hrtf_appreq == Hrtf_Enable)
1170 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1171 goto no_hrtf;
1173 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1176 if(VECTOR_SIZE(device->HrtfList) == 0)
1178 VECTOR_DEINIT(device->HrtfList);
1179 device->HrtfList = EnumerateHrtf(device->DeviceName);
1182 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1184 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1185 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1186 if(hrtf && hrtf->sampleRate == device->Frequency)
1188 device->HrtfHandle = hrtf;
1189 alstr_copy(&device->HrtfName, entry->name);
1191 else if(hrtf)
1192 Hrtf_DecRef(hrtf);
1195 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1197 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1198 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1199 if(hrtf && hrtf->sampleRate == device->Frequency)
1201 device->HrtfHandle = hrtf;
1202 alstr_copy(&device->HrtfName, entry->name);
1204 else if(hrtf)
1205 Hrtf_DecRef(hrtf);
1208 if(device->HrtfHandle)
1210 if(old_hrtf)
1211 Hrtf_DecRef(old_hrtf);
1212 old_hrtf = NULL;
1214 device->Render_Mode = HrtfRender;
1215 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1217 if(strcasecmp(mode, "full") == 0)
1218 device->Render_Mode = HrtfRender;
1219 else if(strcasecmp(mode, "basic") == 0)
1220 device->Render_Mode = NormalRender;
1221 else
1222 ERR("Unexpected hrtf-mode: %s\n", mode);
1225 if(device->Render_Mode == HrtfRender)
1227 /* Don't bother with HOA when using full HRTF rendering. Nothing
1228 * needs it, and it eases the CPU/memory load.
1230 ambiup_free(device->AmbiUp);
1231 device->AmbiUp = NULL;
1233 else
1235 if(!device->AmbiUp)
1236 device->AmbiUp = ambiup_alloc();
1239 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1240 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1241 alstr_get_cstr(device->HrtfName)
1243 InitHrtfPanning(device);
1244 return;
1246 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1248 no_hrtf:
1249 if(old_hrtf)
1250 Hrtf_DecRef(old_hrtf);
1251 old_hrtf = NULL;
1252 TRACE("HRTF disabled\n");
1254 device->Render_Mode = StereoPair;
1256 ambiup_free(device->AmbiUp);
1257 device->AmbiUp = NULL;
1259 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1260 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1261 if(device->Type != Loopback)
1262 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1263 if(bs2blevel > 0 && bs2blevel <= 6)
1265 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1266 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1267 TRACE("BS2B enabled\n");
1268 InitPanning(device);
1269 return;
1272 TRACE("BS2B disabled\n");
1274 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1276 if(strcasecmp(mode, "uhj") == 0)
1277 device->Render_Mode = NormalRender;
1278 else if(strcasecmp(mode, "panpot") != 0)
1279 ERR("Unexpected stereo-encoding: %s\n", mode);
1281 if(device->Render_Mode == NormalRender)
1283 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1284 TRACE("UHJ enabled\n");
1285 InitUhjPanning(device);
1286 return;
1289 TRACE("UHJ disabled\n");
1290 InitPanning(device);
1294 void aluInitEffectPanning(ALeffectslot *slot)
1296 ALsizei i;
1298 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1299 slot->NumChannels = 0;
1301 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1303 slot->ChanMap[i].Scale = 1.0f;
1304 slot->ChanMap[i].Index = i;
1306 slot->NumChannels = i;