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
30 #include "alAuxEffectSlot.h"
34 #include "bformatdec.h"
35 #include "uhjfilter.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
] = {
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. */
118 coeffs
[0] = 1.0f
; /* ACN 0 = 1 */
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 */
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) */
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) */
140 /* Implement the spread by using a spherical source that subtends the
142 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
144 * When adjusted for N3D normalization instead of SN3D, these
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:
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
;
174 coeffs
[0] *= ZH0_norm
;
176 coeffs
[1] *= ZH1_norm
;
177 coeffs
[2] *= ZH1_norm
;
178 coeffs
[3] *= ZH1_norm
;
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
;
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
])
209 for(i
= 0;i
< numchans
;i
++)
210 gains
[i
] = chancoeffs
[i
][0] * 1.414213562f
* ingain
;
211 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
215 void ComputeAmbientGainsBF(const BFChannelConfig
*chanmap
, ALsizei numchans
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
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
++)
230 void ComputePanningGainsMC(const ChannelConfig
*chancoeffs
, ALsizei numchans
, ALsizei numcoeffs
, const ALfloat coeffs
[MAX_AMBI_COEFFS
], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
234 for(i
= 0;i
< numchans
;i
++)
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
++)
245 void ComputePanningGainsBF(const BFChannelConfig
*chanmap
, ALsizei numchans
, const ALfloat coeffs
[MAX_AMBI_COEFFS
], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
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
++)
255 void ComputeFirstOrderGainsMC(const ChannelConfig
*chancoeffs
, ALsizei numchans
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
259 for(i
= 0;i
< numchans
;i
++)
263 gain
+= chancoeffs
[i
][j
] * mtx
[j
];
264 gains
[i
] = clampf(gain
, 0.0f
, 1.0f
) * ingain
;
266 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
270 void ComputeFirstOrderGainsBF(const BFChannelConfig
*chanmap
, ALsizei numchans
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
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
++)
281 static inline const char *GetLabelFromChannel(enum Channel 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;
327 typedef struct ChannelMap
{
328 enum Channel ChanName
;
329 ChannelConfig Config
;
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;
339 for(i
= 0;i
< count
;i
++)
341 ALint idx
= GetChannelIndex(devchans
, chanmap
[i
].ChanName
);
344 ERR("Failed to find %s channel in device\n",
345 GetLabelFromChannel(chanmap
[i
].ChanName
));
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
])
360 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
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:
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,
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
);
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
);
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
);
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
);
416 c
= GetChannelIdxByName(device
->RealOut
, BackRight
);
418 else if(alstr_cmp_cstr(conf
->Speakers
[i
].Name
, "CB") == 0)
419 c
= GetChannelIdxByName(device
->RealOut
, BackCenter
);
422 const char *name
= alstr_get_cstr(conf
->Speakers
[i
].Name
);
426 if(sscanf(name
, "AUX%u%c", &n
, &ch
) == 1 && n
< 16)
427 c
= GetChannelIdxByName(device
->RealOut
, Aux0
+n
);
430 ERR("AmbDec speaker label \"%s\" not recognized\n", name
);
436 ERR("Failed to lookup AmbDec speaker label %s\n",
437 alstr_get_cstr(conf
->Speakers
[i
].Name
));
447 static const ChannelMap MonoCfg
[1] = {
448 { FrontCenter
, { 1.0f
} },
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
} },
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
} },
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
} },
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
} },
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
} },
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
);
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;
497 for(i
= 1;i
< order
+1;i
++)
498 device
->Dry
.NumChannelsPerOrder
[i
] = (i
+1)*(i
+1) - i
*i
;
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
;
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
];
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
*
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
549 total
+= RoundUp(device
->ChannelDelay
[chan
].Length
, 4);
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;
571 switch(device
->FmtChans
)
574 count
= COUNTOF(MonoCfg
);
580 count
= COUNTOF(StereoCfg
);
586 count
= COUNTOF(QuadCfg
);
592 count
= COUNTOF(X51SideCfg
);
593 chanmap
= X51SideCfg
;
598 count
= COUNTOF(X51RearCfg
);
599 chanmap
= X51RearCfg
;
604 count
= COUNTOF(X61Cfg
);
610 count
= COUNTOF(X71Cfg
);
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;
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
));
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);
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
;
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
;
704 if(conf
->FreqBands
!= 1)
705 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
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
;
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
];
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
;
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
])
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
;
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;
827 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
828 if((conf
->ChanMask
&AMBI_PERIPHONIC_MASK
))
831 for(i
= 0;i
< count
;i
++)
833 device
->FOAOut
.Ambi
.Map
[i
].Scale
= 1.0f
;
834 device
->FOAOut
.Ambi
.Map
[i
].Index
= i
;
839 static const int map
[3] = { 0, 1, 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
);
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
:
917 ALsizei count
= device
->AmbiUp
? 9 : 4;
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
;
934 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
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
);
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
)
959 static void InitUhjPanning(ALCdevice
*device
)
964 for(i
= 0;i
< count
;i
++)
966 ALsizei acn
= FuMa2ACN
[i
];
967 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
/FuMa2N3DScale
[acn
];
968 device
->Dry
.Ambi
.Map
[i
].Index
= acn
;
970 device
->Dry
.CoeffCount
= 0;
971 device
->Dry
.NumChannels
= count
;
973 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
974 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
975 device
->FOAOut
.NumChannels
= 0;
977 device
->RealOut
.NumChannels
= ChannelsFromDevFmt(device
->FmtChans
, device
->AmbiOrder
);
980 void aluInitRenderer(ALCdevice
*device
, ALint hrtf_id
, enum HrtfRequestMode hrtf_appreq
, enum HrtfRequestMode hrtf_userreq
)
982 /* Hold the HRTF the device last used, in case it's used again. */
983 struct Hrtf
*old_hrtf
= device
->HrtfHandle
;
989 al_free(device
->Hrtf
);
991 device
->HrtfHandle
= NULL
;
992 alstr_clear(&device
->HrtfName
);
993 device
->Render_Mode
= NormalRender
;
995 memset(&device
->Dry
.Ambi
, 0, sizeof(device
->Dry
.Ambi
));
996 device
->Dry
.CoeffCount
= 0;
997 device
->Dry
.NumChannels
= 0;
998 for(i
= 0;i
< MAX_AMBI_ORDER
+1;i
++)
999 device
->Dry
.NumChannelsPerOrder
[i
] = 0;
1001 device
->AvgSpeakerDist
= 0.0f
;
1002 memset(device
->ChannelDelay
, 0, sizeof(device
->ChannelDelay
));
1003 for(i
= 0;i
< MAX_OUTPUT_CHANNELS
;i
++)
1005 device
->ChannelDelay
[i
].Gain
= 1.0f
;
1006 device
->ChannelDelay
[i
].Length
= 0;
1009 al_free(device
->Stablizer
);
1010 device
->Stablizer
= NULL
;
1012 if(device
->FmtChans
!= DevFmtStereo
)
1014 ALsizei speakermap
[MAX_OUTPUT_CHANNELS
];
1015 const char *devname
, *layout
= NULL
;
1016 AmbDecConf conf
, *pconf
= NULL
;
1019 Hrtf_DecRef(old_hrtf
);
1021 if(hrtf_appreq
== Hrtf_Enable
)
1022 device
->HrtfStatus
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
1026 devname
= alstr_get_cstr(device
->DeviceName
);
1027 switch(device
->FmtChans
)
1029 case DevFmtQuad
: layout
= "quad"; break;
1030 case DevFmtX51
: /* fall-through */
1031 case DevFmtX51Rear
: layout
= "surround51"; break;
1032 case DevFmtX61
: layout
= "surround61"; break;
1033 case DevFmtX71
: layout
= "surround71"; break;
1034 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1043 if(ConfigValueStr(devname
, "decoder", layout
, &fname
))
1045 if(!ambdec_load(&conf
, fname
))
1046 ERR("Failed to load layout file %s\n", fname
);
1049 if(conf
.ChanMask
> 0xffff)
1050 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf
.ChanMask
);
1053 if(MakeSpeakerMap(device
, &conf
, speakermap
))
1060 if(pconf
&& GetConfigValueBool(devname
, "decoder", "hq-mode", 0))
1062 ambiup_free(device
->AmbiUp
);
1063 device
->AmbiUp
= NULL
;
1064 if(!device
->AmbiDecoder
)
1065 device
->AmbiDecoder
= bformatdec_alloc();
1069 bformatdec_free(device
->AmbiDecoder
);
1070 device
->AmbiDecoder
= NULL
;
1071 if(device
->FmtChans
== DevFmtAmbi3D
&& device
->AmbiOrder
> 1)
1074 device
->AmbiUp
= ambiup_alloc();
1078 ambiup_free(device
->AmbiUp
);
1079 device
->AmbiUp
= NULL
;
1084 InitPanning(device
);
1085 else if(device
->AmbiDecoder
)
1086 InitHQPanning(device
, pconf
, speakermap
);
1088 InitCustomPanning(device
, pconf
, speakermap
);
1090 /* Enable the stablizer only for formats that have front-left, front-
1091 * right, and front-center outputs.
1093 switch(device
->FmtChans
)
1099 if(GetConfigValueBool(devname
, NULL
, "front-stablizer", 0))
1101 /* Initialize band-splitting filters for the front-left and
1102 * front-right channels, with a crossover at 5khz (could be
1105 ALfloat scale
= (ALfloat
)(5000.0 / device
->Frequency
);
1106 FrontStablizer
*stablizer
= al_calloc(16, sizeof(*stablizer
));
1108 bandsplit_init(&stablizer
->LFilter
, scale
);
1109 stablizer
->RFilter
= stablizer
->LFilter
;
1111 /* Initialize all-pass filters for all other channels. */
1112 splitterap_init(&stablizer
->APFilter
[0], scale
);
1113 for(i
= 1;i
< (size_t)device
->RealOut
.NumChannels
;i
++)
1114 stablizer
->APFilter
[i
] = stablizer
->APFilter
[0];
1116 device
->Stablizer
= stablizer
;
1125 TRACE("Front stablizer %s\n", device
->Stablizer
? "enabled" : "disabled");
1127 ambdec_deinit(&conf
);
1131 bformatdec_free(device
->AmbiDecoder
);
1132 device
->AmbiDecoder
= NULL
;
1134 headphones
= device
->IsHeadphones
;
1135 if(device
->Type
!= Loopback
)
1138 if(ConfigValueStr(alstr_get_cstr(device
->DeviceName
), NULL
, "stereo-mode", &mode
))
1140 if(strcasecmp(mode
, "headphones") == 0)
1142 else if(strcasecmp(mode
, "speakers") == 0)
1144 else if(strcasecmp(mode
, "auto") != 0)
1145 ERR("Unexpected stereo-mode: %s\n", mode
);
1149 if(hrtf_userreq
== Hrtf_Default
)
1151 bool usehrtf
= (headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
1152 (hrtf_appreq
== Hrtf_Enable
);
1153 if(!usehrtf
) goto no_hrtf
;
1155 device
->HrtfStatus
= ALC_HRTF_ENABLED_SOFT
;
1156 if(headphones
&& hrtf_appreq
!= Hrtf_Disable
)
1157 device
->HrtfStatus
= ALC_HRTF_HEADPHONES_DETECTED_SOFT
;
1161 if(hrtf_userreq
!= Hrtf_Enable
)
1163 if(hrtf_appreq
== Hrtf_Enable
)
1164 device
->HrtfStatus
= ALC_HRTF_DENIED_SOFT
;
1167 device
->HrtfStatus
= ALC_HRTF_REQUIRED_SOFT
;
1170 if(VECTOR_SIZE(device
->HrtfList
) == 0)
1172 VECTOR_DEINIT(device
->HrtfList
);
1173 device
->HrtfList
= EnumerateHrtf(device
->DeviceName
);
1176 if(hrtf_id
>= 0 && (size_t)hrtf_id
< VECTOR_SIZE(device
->HrtfList
))
1178 const EnumeratedHrtf
*entry
= &VECTOR_ELEM(device
->HrtfList
, hrtf_id
);
1179 struct Hrtf
*hrtf
= GetLoadedHrtf(entry
->hrtf
);
1180 if(hrtf
&& hrtf
->sampleRate
== device
->Frequency
)
1182 device
->HrtfHandle
= hrtf
;
1183 alstr_copy(&device
->HrtfName
, entry
->name
);
1189 for(i
= 0;!device
->HrtfHandle
&& i
< VECTOR_SIZE(device
->HrtfList
);i
++)
1191 const EnumeratedHrtf
*entry
= &VECTOR_ELEM(device
->HrtfList
, i
);
1192 struct Hrtf
*hrtf
= GetLoadedHrtf(entry
->hrtf
);
1193 if(hrtf
&& hrtf
->sampleRate
== device
->Frequency
)
1195 device
->HrtfHandle
= hrtf
;
1196 alstr_copy(&device
->HrtfName
, entry
->name
);
1202 if(device
->HrtfHandle
)
1205 Hrtf_DecRef(old_hrtf
);
1208 device
->Render_Mode
= HrtfRender
;
1209 if(ConfigValueStr(alstr_get_cstr(device
->DeviceName
), NULL
, "hrtf-mode", &mode
))
1211 if(strcasecmp(mode
, "full") == 0)
1212 device
->Render_Mode
= HrtfRender
;
1213 else if(strcasecmp(mode
, "basic") == 0)
1214 device
->Render_Mode
= NormalRender
;
1216 ERR("Unexpected hrtf-mode: %s\n", mode
);
1219 if(device
->Render_Mode
== HrtfRender
)
1221 /* Don't bother with HOA when using full HRTF rendering. Nothing
1222 * needs it, and it eases the CPU/memory load.
1224 ambiup_free(device
->AmbiUp
);
1225 device
->AmbiUp
= NULL
;
1230 device
->AmbiUp
= ambiup_alloc();
1233 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1234 ((device
->Render_Mode
== HrtfRender
) ? "Full" : "Basic"),
1235 alstr_get_cstr(device
->HrtfName
)
1237 InitHrtfPanning(device
);
1240 device
->HrtfStatus
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
1244 Hrtf_DecRef(old_hrtf
);
1246 TRACE("HRTF disabled\n");
1248 device
->Render_Mode
= StereoPair
;
1250 ambiup_free(device
->AmbiUp
);
1251 device
->AmbiUp
= NULL
;
1253 bs2blevel
= ((headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
1254 (hrtf_appreq
== Hrtf_Enable
)) ? 5 : 0;
1255 if(device
->Type
!= Loopback
)
1256 ConfigValueInt(alstr_get_cstr(device
->DeviceName
), NULL
, "cf_level", &bs2blevel
);
1257 if(bs2blevel
> 0 && bs2blevel
<= 6)
1259 device
->Bs2b
= al_calloc(16, sizeof(*device
->Bs2b
));
1260 bs2b_set_params(device
->Bs2b
, bs2blevel
, device
->Frequency
);
1261 TRACE("BS2B enabled\n");
1262 InitPanning(device
);
1266 TRACE("BS2B disabled\n");
1268 if(ConfigValueStr(alstr_get_cstr(device
->DeviceName
), NULL
, "stereo-encoding", &mode
))
1270 if(strcasecmp(mode
, "uhj") == 0)
1271 device
->Render_Mode
= NormalRender
;
1272 else if(strcasecmp(mode
, "panpot") != 0)
1273 ERR("Unexpected stereo-encoding: %s\n", mode
);
1275 if(device
->Render_Mode
== NormalRender
)
1277 device
->Uhj_Encoder
= al_calloc(16, sizeof(Uhj2Encoder
));
1278 TRACE("UHJ enabled\n");
1279 InitUhjPanning(device
);
1283 TRACE("UHJ disabled\n");
1284 InitPanning(device
);
1288 void aluInitEffectPanning(ALeffectslot
*slot
)
1292 memset(slot
->ChanMap
, 0, sizeof(slot
->ChanMap
));
1293 slot
->NumChannels
= 0;
1295 for(i
= 0;i
< MAX_EFFECT_CHANNELS
;i
++)
1297 slot
->ChanMap
[i
].Scale
= 1.0f
;
1298 slot
->ChanMap
[i
].Index
= i
;
1300 slot
->NumChannels
= i
;