Enable NFC filters for HRTF
[openal-soft.git] / Alc / panning.c
blobaaae6bbde3a5c737f032b143cb6bfad4ea8ad3e1
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. Thus:
157 * ZH0 = 1.0f;
158 * ZH1 = 0.5f * (ca+1.0f);
159 * ZH2 = 0.5f * (ca+1.0f)*ca;
160 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
161 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
162 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
164 ALfloat ca = cosf(spread * 0.5f);
165 /* Increase the source volume by up to +3dB for a full spread. */
166 ALfloat scale = sqrtf(1.0f + spread/F_TAU);
168 ALfloat ZH0_norm = scale;
169 ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
170 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
171 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
173 /* Zeroth-order */
174 coeffs[0] *= ZH0_norm;
175 /* First-order */
176 coeffs[1] *= ZH1_norm;
177 coeffs[2] *= ZH1_norm;
178 coeffs[3] *= ZH1_norm;
179 /* Second-order */
180 coeffs[4] *= ZH2_norm;
181 coeffs[5] *= ZH2_norm;
182 coeffs[6] *= ZH2_norm;
183 coeffs[7] *= ZH2_norm;
184 coeffs[8] *= ZH2_norm;
185 /* Third-order */
186 coeffs[9] *= ZH3_norm;
187 coeffs[10] *= ZH3_norm;
188 coeffs[11] *= ZH3_norm;
189 coeffs[12] *= ZH3_norm;
190 coeffs[13] *= ZH3_norm;
191 coeffs[14] *= ZH3_norm;
192 coeffs[15] *= ZH3_norm;
196 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
198 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
199 if(!(fabsf(azimuth) > F_PI_2))
200 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
201 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
205 void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
207 ALsizei i;
209 for(i = 0;i < numchans;i++)
210 gains[i] = chancoeffs[i][0] * 1.414213562f * ingain;
211 for(;i < MAX_OUTPUT_CHANNELS;i++)
212 gains[i] = 0.0f;
215 void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
217 ALfloat gain = 0.0f;
218 ALsizei i;
220 for(i = 0;i < numchans;i++)
222 if(chanmap[i].Index == 0)
223 gain += chanmap[i].Scale;
225 gains[0] = gain * 1.414213562f * ingain;
226 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
227 gains[i] = 0.0f;
230 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
232 ALsizei i, j;
234 for(i = 0;i < numchans;i++)
236 float gain = 0.0f;
237 for(j = 0;j < numcoeffs;j++)
238 gain += chancoeffs[i][j]*coeffs[j];
239 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
241 for(;i < MAX_OUTPUT_CHANNELS;i++)
242 gains[i] = 0.0f;
245 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
247 ALsizei i;
249 for(i = 0;i < numchans;i++)
250 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
251 for(;i < MAX_OUTPUT_CHANNELS;i++)
252 gains[i] = 0.0f;
255 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
257 ALsizei i, j;
259 for(i = 0;i < numchans;i++)
261 float gain = 0.0f;
262 for(j = 0;j < 4;j++)
263 gain += chancoeffs[i][j] * mtx[j];
264 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
266 for(;i < MAX_OUTPUT_CHANNELS;i++)
267 gains[i] = 0.0f;
270 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
272 ALsizei i;
274 for(i = 0;i < numchans;i++)
275 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
276 for(;i < MAX_OUTPUT_CHANNELS;i++)
277 gains[i] = 0.0f;
281 static inline const char *GetLabelFromChannel(enum Channel channel)
283 switch(channel)
285 case FrontLeft: return "front-left";
286 case FrontRight: return "front-right";
287 case FrontCenter: return "front-center";
288 case LFE: return "lfe";
289 case BackLeft: return "back-left";
290 case BackRight: return "back-right";
291 case BackCenter: return "back-center";
292 case SideLeft: return "side-left";
293 case SideRight: return "side-right";
295 case UpperFrontLeft: return "upper-front-left";
296 case UpperFrontRight: return "upper-front-right";
297 case UpperBackLeft: return "upper-back-left";
298 case UpperBackRight: return "upper-back-right";
299 case LowerFrontLeft: return "lower-front-left";
300 case LowerFrontRight: return "lower-front-right";
301 case LowerBackLeft: return "lower-back-left";
302 case LowerBackRight: return "lower-back-right";
304 case Aux0: return "aux-0";
305 case Aux1: return "aux-1";
306 case Aux2: return "aux-2";
307 case Aux3: return "aux-3";
308 case Aux4: return "aux-4";
309 case Aux5: return "aux-5";
310 case Aux6: return "aux-6";
311 case Aux7: return "aux-7";
312 case Aux8: return "aux-8";
313 case Aux9: return "aux-9";
314 case Aux10: return "aux-10";
315 case Aux11: return "aux-11";
316 case Aux12: return "aux-12";
317 case Aux13: return "aux-13";
318 case Aux14: return "aux-14";
319 case Aux15: return "aux-15";
321 case InvalidChannel: break;
323 return "(unknown)";
327 typedef struct ChannelMap {
328 enum Channel ChanName;
329 ChannelConfig Config;
330 } ChannelMap;
332 static void SetChannelMap(const enum Channel devchans[MAX_OUTPUT_CHANNELS],
333 ChannelConfig *ambicoeffs, const ChannelMap *chanmap,
334 ALsizei count, ALsizei *outcount)
336 ALsizei maxchans = 0;
337 ALsizei i, j;
339 for(i = 0;i < count;i++)
341 ALint idx = GetChannelIndex(devchans, chanmap[i].ChanName);
342 if(idx < 0)
344 ERR("Failed to find %s channel in device\n",
345 GetLabelFromChannel(chanmap[i].ChanName));
346 continue;
349 maxchans = maxi(maxchans, idx+1);
350 for(j = 0;j < MAX_AMBI_COEFFS;j++)
351 ambicoeffs[idx][j] = chanmap[i].Config[j];
353 *outcount = mini(maxchans, MAX_OUTPUT_CHANNELS);
356 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
358 ALsizei i;
360 for(i = 0;i < conf->NumSpeakers;i++)
362 int c = -1;
364 /* NOTE: AmbDec does not define any standard speaker names, however
365 * for this to work we have to by able to find the output channel
366 * the speaker definition corresponds to. Therefore, OpenAL Soft
367 * requires these channel labels to be recognized:
369 * LF = Front left
370 * RF = Front right
371 * LS = Side left
372 * RS = Side right
373 * LB = Back left
374 * RB = Back right
375 * CE = Front center
376 * CB = Back center
378 * Additionally, surround51 will acknowledge back speakers for side
379 * channels, and surround51rear will acknowledge side speakers for
380 * back channels, to avoid issues with an ambdec expecting 5.1 to
381 * use the side channels when the device is configured for back,
382 * and vice-versa.
384 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
385 c = GetChannelIdxByName(device->RealOut, FrontLeft);
386 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
387 c = GetChannelIdxByName(device->RealOut, FrontRight);
388 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
389 c = GetChannelIdxByName(device->RealOut, FrontCenter);
390 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
392 if(device->FmtChans == DevFmtX51Rear)
393 c = GetChannelIdxByName(device->RealOut, BackLeft);
394 else
395 c = GetChannelIdxByName(device->RealOut, SideLeft);
397 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
399 if(device->FmtChans == DevFmtX51Rear)
400 c = GetChannelIdxByName(device->RealOut, BackRight);
401 else
402 c = GetChannelIdxByName(device->RealOut, SideRight);
404 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
406 if(device->FmtChans == DevFmtX51)
407 c = GetChannelIdxByName(device->RealOut, SideLeft);
408 else
409 c = GetChannelIdxByName(device->RealOut, BackLeft);
411 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
413 if(device->FmtChans == DevFmtX51)
414 c = GetChannelIdxByName(device->RealOut, SideRight);
415 else
416 c = GetChannelIdxByName(device->RealOut, BackRight);
418 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
419 c = GetChannelIdxByName(device->RealOut, BackCenter);
420 else
422 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
423 unsigned int n;
424 char ch;
426 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
427 c = GetChannelIdxByName(device->RealOut, Aux0+n);
428 else
430 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
431 return false;
434 if(c == -1)
436 ERR("Failed to lookup AmbDec speaker label %s\n",
437 alstr_get_cstr(conf->Speakers[i].Name));
438 return false;
440 speakermap[i] = c;
443 return true;
447 static const ChannelMap MonoCfg[1] = {
448 { FrontCenter, { 1.0f } },
449 }, StereoCfg[2] = {
450 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
451 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
452 }, QuadCfg[4] = {
453 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
454 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
455 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
456 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
457 }, X51SideCfg[4] = {
458 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
459 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
460 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
461 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
462 }, X51RearCfg[4] = {
463 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
464 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
465 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
466 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
467 }, X61Cfg[6] = {
468 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, 0.0f, -4.39996780e-2f, -2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
469 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 0.0f, 1.59658796e-1f, 6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
470 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 0.0f, 1.59658796e-1f, -6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
471 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, 0.0f, -4.39996780e-2f, 2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
472 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
473 }, X71Cfg[6] = {
474 { 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 } },
475 { 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 } },
476 { 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 } },
477 { 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 } },
478 { 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 } },
479 { 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 } },
482 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
484 const char *devname = alstr_get_cstr(device->DeviceName);
485 ALsizei i;
487 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
489 /* NFC is only used when AvgSpeakerDist is greater than 0, and
490 * METERS_PER_UNIT is also greater than 0. In addition, NFC can only be
491 * used when rendering to an ambisonic buffer.
493 device->AvgSpeakerDist = ctrl_dist;
495 device->Dry.NumChannelsPerOrder[0] = 1;
496 if(periphonic)
497 for(i = 1;i < order+1;i++)
498 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
499 else
500 for(i = 1;i < order+1;i++)
501 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
502 for(;i < MAX_AMBI_ORDER+1;i++)
503 device->Dry.NumChannelsPerOrder[i] = 0;
507 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
509 const char *devname = alstr_get_cstr(device->DeviceName);
510 ALfloat maxdist = 0.0f;
511 ALsizei total = 0;
512 ALsizei i;
514 for(i = 0;i < conf->NumSpeakers;i++)
515 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
517 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
519 ALfloat srate = (ALfloat)device->Frequency;
520 for(i = 0;i < conf->NumSpeakers;i++)
522 ALsizei chan = speakermap[i];
523 ALfloat delay;
525 /* Distance compensation only delays in steps of the sample rate.
526 * This is a bit less accurate since the delay time falls to the
527 * nearest sample time, but it's far simpler as it doesn't have to
528 * deal with phase offsets. This means at 48khz, for instance, the
529 * distance delay will be in steps of about 7 millimeters.
531 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
532 srate + 0.5f);
533 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
534 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
535 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
537 device->ChannelDelay[chan].Length = (ALsizei)clampf(
538 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
540 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
541 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
542 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
543 device->ChannelDelay[chan].Gain
546 /* Round up to the next 4th sample, so each channel buffer starts
547 * 16-byte aligned.
549 total += RoundUp(device->ChannelDelay[chan].Length, 4);
553 if(total > 0)
555 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
556 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
558 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
559 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
564 static void InitPanning(ALCdevice *device)
566 const ChannelMap *chanmap = NULL;
567 ALsizei coeffcount = 0;
568 ALsizei count = 0;
569 ALsizei i, j;
571 switch(device->FmtChans)
573 case DevFmtMono:
574 count = COUNTOF(MonoCfg);
575 chanmap = MonoCfg;
576 coeffcount = 1;
577 break;
579 case DevFmtStereo:
580 count = COUNTOF(StereoCfg);
581 chanmap = StereoCfg;
582 coeffcount = 4;
583 break;
585 case DevFmtQuad:
586 count = COUNTOF(QuadCfg);
587 chanmap = QuadCfg;
588 coeffcount = 4;
589 break;
591 case DevFmtX51:
592 count = COUNTOF(X51SideCfg);
593 chanmap = X51SideCfg;
594 coeffcount = 9;
595 break;
597 case DevFmtX51Rear:
598 count = COUNTOF(X51RearCfg);
599 chanmap = X51RearCfg;
600 coeffcount = 9;
601 break;
603 case DevFmtX61:
604 count = COUNTOF(X61Cfg);
605 chanmap = X61Cfg;
606 coeffcount = 9;
607 break;
609 case DevFmtX71:
610 count = COUNTOF(X71Cfg);
611 chanmap = X71Cfg;
612 coeffcount = 16;
613 break;
615 case DevFmtAmbi3D:
616 break;
619 if(device->FmtChans == DevFmtAmbi3D)
621 const char *devname = alstr_get_cstr(device->DeviceName);
622 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
623 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
624 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
625 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ UnitScale;
626 ALfloat nfc_delay = 0.0f;
628 count = (device->AmbiOrder == 3) ? 16 :
629 (device->AmbiOrder == 2) ? 9 :
630 (device->AmbiOrder == 1) ? 4 : 1;
631 for(i = 0;i < count;i++)
633 ALsizei acn = acnmap[i];
634 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
635 device->Dry.Ambi.Map[i].Index = acn;
637 device->Dry.CoeffCount = 0;
638 device->Dry.NumChannels = count;
640 if(device->AmbiOrder < 2)
642 device->FOAOut.Ambi = device->Dry.Ambi;
643 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
644 device->FOAOut.NumChannels = 0;
646 else
648 /* FOA output is always ACN+N3D for higher-order ambisonic output.
649 * The upsampler expects this and will convert it for output.
651 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
652 for(i = 0;i < 4;i++)
654 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
655 device->FOAOut.Ambi.Map[i].Index = i;
657 device->FOAOut.CoeffCount = 0;
658 device->FOAOut.NumChannels = 4;
660 ambiup_reset(device->AmbiUp, device);
663 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
665 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
666 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
667 device->AmbiOrder, true);
670 else
672 ALfloat w_scale, xyz_scale;
674 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
675 chanmap, count, &device->Dry.NumChannels);
676 device->Dry.CoeffCount = coeffcount;
678 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE2D_THIRD :
679 (device->Dry.CoeffCount > 4) ? W_SCALE2D_SECOND : 1.0f;
680 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE2D_THIRD :
681 (device->Dry.CoeffCount > 4) ? XYZ_SCALE2D_SECOND : 1.0f;
683 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
684 for(i = 0;i < device->Dry.NumChannels;i++)
686 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
687 for(j = 1;j < 4;j++)
688 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
690 device->FOAOut.CoeffCount = 4;
691 device->FOAOut.NumChannels = 0;
693 device->RealOut.NumChannels = 0;
696 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
698 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
699 const ALfloat *coeff_scale = UnitScale;
700 ALfloat w_scale = 1.0f;
701 ALfloat xyz_scale = 1.0f;
702 ALsizei i, j;
704 if(conf->FreqBands != 1)
705 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
706 conf->XOverFreq);
708 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
710 if(conf->ChanMask > 0x1ff)
712 w_scale = W_SCALE3D_THIRD;
713 xyz_scale = XYZ_SCALE3D_THIRD;
715 else if(conf->ChanMask > 0xf)
717 w_scale = W_SCALE3D_SECOND;
718 xyz_scale = XYZ_SCALE3D_SECOND;
721 else
723 if(conf->ChanMask > 0x1ff)
725 w_scale = W_SCALE2D_THIRD;
726 xyz_scale = XYZ_SCALE2D_THIRD;
728 else if(conf->ChanMask > 0xf)
730 w_scale = W_SCALE2D_SECOND;
731 xyz_scale = XYZ_SCALE2D_SECOND;
735 if(conf->CoeffScale == ADS_SN3D)
736 coeff_scale = SN3D2N3DScale;
737 else if(conf->CoeffScale == ADS_FuMa)
738 coeff_scale = FuMa2N3DScale;
740 for(i = 0;i < conf->NumSpeakers;i++)
742 ALsizei chan = speakermap[i];
743 ALfloat gain;
744 ALsizei k = 0;
746 for(j = 0;j < MAX_AMBI_COEFFS;j++)
747 chanmap[i].Config[j] = 0.0f;
749 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
750 for(j = 0;j < MAX_AMBI_COEFFS;j++)
752 if(j == 0) gain = conf->HFOrderGain[0];
753 else if(j == 1) gain = conf->HFOrderGain[1];
754 else if(j == 4) gain = conf->HFOrderGain[2];
755 else if(j == 9) gain = conf->HFOrderGain[3];
756 if((conf->ChanMask&(1<<j)))
757 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
761 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
762 conf->NumSpeakers, &device->Dry.NumChannels);
763 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
764 (conf->ChanMask > 0xf) ? 9 : 4;
766 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
767 for(i = 0;i < device->Dry.NumChannels;i++)
769 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
770 for(j = 1;j < 4;j++)
771 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
773 device->FOAOut.CoeffCount = 4;
774 device->FOAOut.NumChannels = 0;
776 device->RealOut.NumChannels = 0;
778 InitDistanceComp(device, conf, speakermap);
781 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
783 ALfloat avg_dist;
784 ALsizei count;
785 ALsizei i;
787 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
789 count = (conf->ChanMask > 0x1ff) ? 16 :
790 (conf->ChanMask > 0xf) ? 9 : 4;
791 for(i = 0;i < count;i++)
793 device->Dry.Ambi.Map[i].Scale = 1.0f;
794 device->Dry.Ambi.Map[i].Index = i;
797 else
799 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
801 count = (conf->ChanMask > 0x1ff) ? 7 :
802 (conf->ChanMask > 0xf) ? 5 : 3;
803 for(i = 0;i < count;i++)
805 device->Dry.Ambi.Map[i].Scale = 1.0f;
806 device->Dry.Ambi.Map[i].Index = map[i];
809 device->Dry.CoeffCount = 0;
810 device->Dry.NumChannels = count;
812 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
813 (conf->FreqBands == 1) ? "single" : "dual",
814 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
815 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
817 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
819 if(!(conf->ChanMask > 0xf))
821 device->FOAOut.Ambi = device->Dry.Ambi;
822 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
823 device->FOAOut.NumChannels = 0;
825 else
827 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
828 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
830 count = 4;
831 for(i = 0;i < count;i++)
833 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
834 device->FOAOut.Ambi.Map[i].Index = i;
837 else
839 static const int map[3] = { 0, 1, 3 };
840 count = 3;
841 for(i = 0;i < count;i++)
843 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
844 device->FOAOut.Ambi.Map[i].Index = map[i];
847 device->FOAOut.CoeffCount = 0;
848 device->FOAOut.NumChannels = count;
851 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
853 avg_dist = 0.0f;
854 for(i = 0;i < conf->NumSpeakers;i++)
855 avg_dist += conf->Speakers[i].Distance;
856 avg_dist /= (ALfloat)conf->NumSpeakers;
857 InitNearFieldCtrl(device, avg_dist,
858 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
859 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
862 InitDistanceComp(device, conf, speakermap);
865 static void InitHrtfPanning(ALCdevice *device)
867 /* NOTE: azimuth goes clockwise. */
868 static const ALfloat AmbiPoints[][2] = {
869 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
870 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
871 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
872 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
873 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
874 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
875 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
876 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
877 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
878 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
879 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
880 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
881 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
882 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
884 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
885 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
886 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
887 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, 7.13950780e-002f } },
888 { { 1.88982237e-001f, -1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
889 { { 1.88982237e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
890 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
891 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
892 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
893 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
894 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
895 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
896 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
897 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
898 { { 1.88982237e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
899 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
900 { { 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 } },
901 { { 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 } },
902 { { 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 } },
903 { { 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 } },
904 { { 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 } },
905 { { 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 } },
906 { { 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 } },
907 { { 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 } },
908 { { 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 } },
909 { { 1.40852210e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f, 7.58818830e-002f, -7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f, 9.61978444e-002f, -9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
910 { { 1.40852210e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f, -7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f, -9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
911 { { 1.40852210e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f, 7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, 7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f, 9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, 9.71456952e-002f, 0.00000000e+000f } },
912 { { 1.40852210e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f, -7.58818830e-002f, -7.66295578e-002f, -3.28314629e-004f, 7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f, -9.61978444e-002f, -9.71456952e-002f, -4.16214759e-004f, 9.71456952e-002f, 0.00000000e+000f } },
913 { { 1.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 } },
915 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = device->AmbiUp ? AmbiMatrixHOA :
916 AmbiMatrixFOA;
917 ALsizei count = device->AmbiUp ? 9 : 4;
918 ALsizei i;
920 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
922 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
924 for(i = 0;i < count;i++)
926 device->Dry.Ambi.Map[i].Scale = 1.0f;
927 device->Dry.Ambi.Map[i].Index = i;
929 device->Dry.CoeffCount = 0;
930 device->Dry.NumChannels = count;
932 if(device->AmbiUp)
934 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
935 for(i = 0;i < 4;i++)
937 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
938 device->FOAOut.Ambi.Map[i].Index = i;
940 device->FOAOut.CoeffCount = 0;
941 device->FOAOut.NumChannels = 4;
943 ambiup_reset(device->AmbiUp, device);
945 else
947 device->FOAOut.Ambi = device->Dry.Ambi;
948 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
949 device->FOAOut.NumChannels = 0;
952 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
954 BuildBFormatHrtf(device->HrtfHandle,
955 device->Hrtf, device->Dry.NumChannels, AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
958 InitNearFieldCtrl(device, device->HrtfHandle->distance, device->AmbiUp ? 2 : 1, true);
961 static void InitUhjPanning(ALCdevice *device)
963 ALsizei count = 3;
964 ALsizei i;
966 for(i = 0;i < count;i++)
968 ALsizei acn = FuMa2ACN[i];
969 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
970 device->Dry.Ambi.Map[i].Index = acn;
972 device->Dry.CoeffCount = 0;
973 device->Dry.NumChannels = count;
975 device->FOAOut.Ambi = device->Dry.Ambi;
976 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
977 device->FOAOut.NumChannels = 0;
979 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
982 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
984 /* Hold the HRTF the device last used, in case it's used again. */
985 struct Hrtf *old_hrtf = device->HrtfHandle;
986 const char *mode;
987 bool headphones;
988 int bs2blevel;
989 size_t i;
991 al_free(device->Hrtf);
992 device->Hrtf = NULL;
993 device->HrtfHandle = NULL;
994 alstr_clear(&device->HrtfName);
995 device->Render_Mode = NormalRender;
997 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
998 device->Dry.CoeffCount = 0;
999 device->Dry.NumChannels = 0;
1000 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
1001 device->Dry.NumChannelsPerOrder[i] = 0;
1003 device->AvgSpeakerDist = 0.0f;
1004 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
1005 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1007 device->ChannelDelay[i].Gain = 1.0f;
1008 device->ChannelDelay[i].Length = 0;
1011 al_free(device->Stablizer);
1012 device->Stablizer = NULL;
1014 if(device->FmtChans != DevFmtStereo)
1016 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1017 const char *devname, *layout = NULL;
1018 AmbDecConf conf, *pconf = NULL;
1020 if(old_hrtf)
1021 Hrtf_DecRef(old_hrtf);
1022 old_hrtf = NULL;
1023 if(hrtf_appreq == Hrtf_Enable)
1024 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1026 ambdec_init(&conf);
1028 devname = alstr_get_cstr(device->DeviceName);
1029 switch(device->FmtChans)
1031 case DevFmtQuad: layout = "quad"; break;
1032 case DevFmtX51: /* fall-through */
1033 case DevFmtX51Rear: layout = "surround51"; break;
1034 case DevFmtX61: layout = "surround61"; break;
1035 case DevFmtX71: layout = "surround71"; break;
1036 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1037 case DevFmtMono:
1038 case DevFmtStereo:
1039 case DevFmtAmbi3D:
1040 break;
1042 if(layout)
1044 const char *fname;
1045 if(ConfigValueStr(devname, "decoder", layout, &fname))
1047 if(!ambdec_load(&conf, fname))
1048 ERR("Failed to load layout file %s\n", fname);
1049 else
1051 if(conf.ChanMask > 0xffff)
1052 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1053 else
1055 if(MakeSpeakerMap(device, &conf, speakermap))
1056 pconf = &conf;
1062 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1064 ambiup_free(device->AmbiUp);
1065 device->AmbiUp = NULL;
1066 if(!device->AmbiDecoder)
1067 device->AmbiDecoder = bformatdec_alloc();
1069 else
1071 bformatdec_free(device->AmbiDecoder);
1072 device->AmbiDecoder = NULL;
1073 if(device->FmtChans == DevFmtAmbi3D && device->AmbiOrder > 1)
1075 if(!device->AmbiUp)
1076 device->AmbiUp = ambiup_alloc();
1078 else
1080 ambiup_free(device->AmbiUp);
1081 device->AmbiUp = NULL;
1085 if(!pconf)
1086 InitPanning(device);
1087 else if(device->AmbiDecoder)
1088 InitHQPanning(device, pconf, speakermap);
1089 else
1090 InitCustomPanning(device, pconf, speakermap);
1092 /* Enable the stablizer only for formats that have front-left, front-
1093 * right, and front-center outputs.
1095 switch(device->FmtChans)
1097 case DevFmtX51:
1098 case DevFmtX51Rear:
1099 case DevFmtX61:
1100 case DevFmtX71:
1101 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1103 /* Initialize band-splitting filters for the front-left and
1104 * front-right channels, with a crossover at 5khz (could be
1105 * higher).
1107 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1108 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1110 bandsplit_init(&stablizer->LFilter, scale);
1111 stablizer->RFilter = stablizer->LFilter;
1113 /* Initialize all-pass filters for all other channels. */
1114 splitterap_init(&stablizer->APFilter[0], scale);
1115 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1116 stablizer->APFilter[i] = stablizer->APFilter[0];
1118 device->Stablizer = stablizer;
1120 break;
1121 case DevFmtMono:
1122 case DevFmtStereo:
1123 case DevFmtQuad:
1124 case DevFmtAmbi3D:
1125 break;
1127 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1129 ambdec_deinit(&conf);
1130 return;
1133 bformatdec_free(device->AmbiDecoder);
1134 device->AmbiDecoder = NULL;
1136 headphones = device->IsHeadphones;
1137 if(device->Type != Loopback)
1139 const char *mode;
1140 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1142 if(strcasecmp(mode, "headphones") == 0)
1143 headphones = true;
1144 else if(strcasecmp(mode, "speakers") == 0)
1145 headphones = false;
1146 else if(strcasecmp(mode, "auto") != 0)
1147 ERR("Unexpected stereo-mode: %s\n", mode);
1151 if(hrtf_userreq == Hrtf_Default)
1153 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1154 (hrtf_appreq == Hrtf_Enable);
1155 if(!usehrtf) goto no_hrtf;
1157 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1158 if(headphones && hrtf_appreq != Hrtf_Disable)
1159 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1161 else
1163 if(hrtf_userreq != Hrtf_Enable)
1165 if(hrtf_appreq == Hrtf_Enable)
1166 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1167 goto no_hrtf;
1169 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1172 if(VECTOR_SIZE(device->HrtfList) == 0)
1174 VECTOR_DEINIT(device->HrtfList);
1175 device->HrtfList = EnumerateHrtf(device->DeviceName);
1178 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1180 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1181 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1182 if(hrtf && hrtf->sampleRate == device->Frequency)
1184 device->HrtfHandle = hrtf;
1185 alstr_copy(&device->HrtfName, entry->name);
1187 else if(hrtf)
1188 Hrtf_DecRef(hrtf);
1191 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1193 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1194 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1195 if(hrtf && hrtf->sampleRate == device->Frequency)
1197 device->HrtfHandle = hrtf;
1198 alstr_copy(&device->HrtfName, entry->name);
1200 else if(hrtf)
1201 Hrtf_DecRef(hrtf);
1204 if(device->HrtfHandle)
1206 if(old_hrtf)
1207 Hrtf_DecRef(old_hrtf);
1208 old_hrtf = NULL;
1210 device->Render_Mode = HrtfRender;
1211 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1213 if(strcasecmp(mode, "full") == 0)
1214 device->Render_Mode = HrtfRender;
1215 else if(strcasecmp(mode, "basic") == 0)
1216 device->Render_Mode = NormalRender;
1217 else
1218 ERR("Unexpected hrtf-mode: %s\n", mode);
1221 if(device->Render_Mode == HrtfRender)
1223 /* Don't bother with HOA when using full HRTF rendering. Nothing
1224 * needs it, and it eases the CPU/memory load.
1226 ambiup_free(device->AmbiUp);
1227 device->AmbiUp = NULL;
1229 else
1231 if(!device->AmbiUp)
1232 device->AmbiUp = ambiup_alloc();
1235 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1236 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1237 alstr_get_cstr(device->HrtfName)
1239 InitHrtfPanning(device);
1240 return;
1242 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1244 no_hrtf:
1245 if(old_hrtf)
1246 Hrtf_DecRef(old_hrtf);
1247 old_hrtf = NULL;
1248 TRACE("HRTF disabled\n");
1250 device->Render_Mode = StereoPair;
1252 ambiup_free(device->AmbiUp);
1253 device->AmbiUp = NULL;
1255 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1256 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1257 if(device->Type != Loopback)
1258 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1259 if(bs2blevel > 0 && bs2blevel <= 6)
1261 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1262 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1263 TRACE("BS2B enabled\n");
1264 InitPanning(device);
1265 return;
1268 TRACE("BS2B disabled\n");
1270 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1272 if(strcasecmp(mode, "uhj") == 0)
1273 device->Render_Mode = NormalRender;
1274 else if(strcasecmp(mode, "panpot") != 0)
1275 ERR("Unexpected stereo-encoding: %s\n", mode);
1277 if(device->Render_Mode == NormalRender)
1279 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1280 TRACE("UHJ enabled\n");
1281 InitUhjPanning(device);
1282 return;
1285 TRACE("UHJ disabled\n");
1286 InitPanning(device);
1290 void aluInitEffectPanning(ALeffectslot *slot)
1292 ALsizei i;
1294 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1295 slot->NumChannels = 0;
1297 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1299 slot->ChanMap[i].Scale = 1.0f;
1300 slot->ChanMap[i].Index = i;
1302 slot->NumChannels = i;