Scale the source volume by +3dB for a full spread
[openal-soft.git] / Alc / panning.c
blob0abf18db5014242264f266291199170a47f4f497
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, ChannelConfig *ambicoeffs,
333 const ChannelMap *chanmap, size_t count, ALsizei *outcount)
335 size_t j, k;
336 ALsizei i;
338 for(i = 0;i < MAX_OUTPUT_CHANNELS && devchans[i] != InvalidChannel;i++)
340 if(devchans[i] == LFE)
342 for(j = 0;j < MAX_AMBI_COEFFS;j++)
343 ambicoeffs[i][j] = 0.0f;
344 continue;
347 for(j = 0;j < count;j++)
349 if(devchans[i] != chanmap[j].ChanName)
350 continue;
352 for(k = 0;k < MAX_AMBI_COEFFS;++k)
353 ambicoeffs[i][k] = chanmap[j].Config[k];
354 break;
356 if(j == count)
357 ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans[i]), i);
359 *outcount = i;
362 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
364 ALsizei i;
366 for(i = 0;i < conf->NumSpeakers;i++)
368 int c = -1;
370 /* NOTE: AmbDec does not define any standard speaker names, however
371 * for this to work we have to by able to find the output channel
372 * the speaker definition corresponds to. Therefore, OpenAL Soft
373 * requires these channel labels to be recognized:
375 * LF = Front left
376 * RF = Front right
377 * LS = Side left
378 * RS = Side right
379 * LB = Back left
380 * RB = Back right
381 * CE = Front center
382 * CB = Back center
384 * Additionally, surround51 will acknowledge back speakers for side
385 * channels, and surround51rear will acknowledge side speakers for
386 * back channels, to avoid issues with an ambdec expecting 5.1 to
387 * use the side channels when the device is configured for back,
388 * and vice-versa.
390 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
391 c = GetChannelIdxByName(device->RealOut, FrontLeft);
392 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
393 c = GetChannelIdxByName(device->RealOut, FrontRight);
394 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
395 c = GetChannelIdxByName(device->RealOut, FrontCenter);
396 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
398 if(device->FmtChans == DevFmtX51Rear)
399 c = GetChannelIdxByName(device->RealOut, BackLeft);
400 else
401 c = GetChannelIdxByName(device->RealOut, SideLeft);
403 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
405 if(device->FmtChans == DevFmtX51Rear)
406 c = GetChannelIdxByName(device->RealOut, BackRight);
407 else
408 c = GetChannelIdxByName(device->RealOut, SideRight);
410 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
412 if(device->FmtChans == DevFmtX51)
413 c = GetChannelIdxByName(device->RealOut, SideLeft);
414 else
415 c = GetChannelIdxByName(device->RealOut, BackLeft);
417 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
419 if(device->FmtChans == DevFmtX51)
420 c = GetChannelIdxByName(device->RealOut, SideRight);
421 else
422 c = GetChannelIdxByName(device->RealOut, BackRight);
424 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
425 c = GetChannelIdxByName(device->RealOut, BackCenter);
426 else
428 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
429 unsigned int n;
430 char ch;
432 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
433 c = GetChannelIdxByName(device->RealOut, Aux0+n);
434 else
436 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
437 return false;
440 if(c == -1)
442 ERR("Failed to lookup AmbDec speaker label %s\n",
443 alstr_get_cstr(conf->Speakers[i].Name));
444 return false;
446 speakermap[i] = c;
449 return true;
453 static const ChannelMap MonoCfg[1] = {
454 { FrontCenter, { 1.0f } },
455 }, StereoCfg[2] = {
456 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
457 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
458 }, QuadCfg[4] = {
459 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
460 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
461 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
462 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
463 }, X51SideCfg[5] = {
464 { SideLeft, { 3.33001372e-1f, 1.89085671e-1f, 0.0f, -2.00041334e-1f, -2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
465 { FrontLeft, { 1.47751298e-1f, 1.28994110e-1f, 0.0f, 1.15190495e-1f, 7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
466 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
467 { FrontRight, { 1.47751298e-1f, -1.28994110e-1f, 0.0f, 1.15190495e-1f, -7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
468 { SideRight, { 3.33001372e-1f, -1.89085671e-1f, 0.0f, -2.00041334e-1f, 2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
469 }, X51RearCfg[5] = {
470 { BackLeft, { 3.33001372e-1f, 1.89085671e-1f, 0.0f, -2.00041334e-1f, -2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
471 { FrontLeft, { 1.47751298e-1f, 1.28994110e-1f, 0.0f, 1.15190495e-1f, 7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
472 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
473 { FrontRight, { 1.47751298e-1f, -1.28994110e-1f, 0.0f, 1.15190495e-1f, -7.44949143e-2f, 0.0f, 0.0f, 0.0f, -6.47739980e-3f } },
474 { BackRight, { 3.33001372e-1f, -1.89085671e-1f, 0.0f, -2.00041334e-1f, 2.12309737e-2f, 0.0f, 0.0f, 0.0f, -1.14573483e-2f } },
475 }, X61Cfg[6] = {
476 { SideLeft, { 2.04462744e-1f, 2.17178497e-1f, 0.0f, -4.39990188e-2f, -2.60787329e-2f, 0.0f, 0.0f, 0.0f, -6.87238843e-2f } },
477 { FrontLeft, { 1.18130342e-1f, 9.34633906e-2f, 0.0f, 1.08553749e-1f, 6.80658795e-2f, 0.0f, 0.0f, 0.0f, 1.08999485e-2f } },
478 { FrontCenter, { 7.73595729e-2f, 0.00000000e+0f, 0.0f, 9.71390298e-2f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 5.18625335e-2f } },
479 { FrontRight, { 1.18130342e-1f, -9.34633906e-2f, 0.0f, 1.08553749e-1f, -6.80658795e-2f, 0.0f, 0.0f, 0.0f, 1.08999485e-2f } },
480 { SideRight, { 2.04462744e-1f, -2.17178497e-1f, 0.0f, -4.39990188e-2f, 2.60787329e-2f, 0.0f, 0.0f, 0.0f, -6.87238843e-2f } },
481 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
482 }, X71Cfg[6] = {
483 { 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 } },
484 { 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 } },
485 { 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 } },
486 { 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 } },
487 { 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 } },
488 { 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 } },
491 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
493 const char *devname = alstr_get_cstr(device->DeviceName);
494 ALsizei i;
496 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
498 /* NFC is only used when AvgSpeakerDist is greater than 0, and
499 * METERS_PER_UNIT is also greater than 0. In addition, NFC can only be
500 * used when rendering to an ambisonic buffer.
502 device->AvgSpeakerDist = ctrl_dist;
504 device->Dry.NumChannelsPerOrder[0] = 1;
505 if(periphonic)
506 for(i = 1;i < order+1;i++)
507 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
508 else
509 for(i = 1;i < order+1;i++)
510 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
511 for(;i < MAX_AMBI_ORDER+1;i++)
512 device->Dry.NumChannelsPerOrder[i] = 0;
516 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
518 const char *devname = alstr_get_cstr(device->DeviceName);
519 ALfloat maxdist = 0.0f;
520 ALsizei total = 0;
521 ALsizei i;
523 for(i = 0;i < conf->NumSpeakers;i++)
524 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
526 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
528 ALfloat srate = (ALfloat)device->Frequency;
529 for(i = 0;i < conf->NumSpeakers;i++)
531 ALsizei chan = speakermap[i];
532 ALfloat delay;
534 /* Distance compensation only delays in steps of the sample rate.
535 * This is a bit less accurate since the delay time falls to the
536 * nearest sample time, but it's far simpler as it doesn't have to
537 * deal with phase offsets. This means at 48khz, for instance, the
538 * distance delay will be in steps of about 7 millimeters.
540 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
541 srate + 0.5f);
542 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
543 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
544 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
546 device->ChannelDelay[chan].Length = (ALsizei)clampf(
547 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
549 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
550 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
551 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
552 device->ChannelDelay[chan].Gain
555 /* Round up to the next 4th sample, so each channel buffer starts
556 * 16-byte aligned.
558 total += RoundUp(device->ChannelDelay[chan].Length, 4);
562 if(total > 0)
564 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
565 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
567 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
568 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
573 static void InitPanning(ALCdevice *device)
575 const ChannelMap *chanmap = NULL;
576 ALsizei coeffcount = 0;
577 ALsizei count = 0;
578 ALsizei i, j;
580 switch(device->FmtChans)
582 case DevFmtMono:
583 count = COUNTOF(MonoCfg);
584 chanmap = MonoCfg;
585 coeffcount = 1;
586 break;
588 case DevFmtStereo:
589 count = COUNTOF(StereoCfg);
590 chanmap = StereoCfg;
591 coeffcount = 4;
592 break;
594 case DevFmtQuad:
595 count = COUNTOF(QuadCfg);
596 chanmap = QuadCfg;
597 coeffcount = 4;
598 break;
600 case DevFmtX51:
601 count = COUNTOF(X51SideCfg);
602 chanmap = X51SideCfg;
603 coeffcount = 9;
604 break;
606 case DevFmtX51Rear:
607 count = COUNTOF(X51RearCfg);
608 chanmap = X51RearCfg;
609 coeffcount = 9;
610 break;
612 case DevFmtX61:
613 count = COUNTOF(X61Cfg);
614 chanmap = X61Cfg;
615 coeffcount = 9;
616 break;
618 case DevFmtX71:
619 count = COUNTOF(X71Cfg);
620 chanmap = X71Cfg;
621 coeffcount = 16;
622 break;
624 case DevFmtAmbi3D:
625 break;
628 if(device->FmtChans == DevFmtAmbi3D)
630 const char *devname = alstr_get_cstr(device->DeviceName);
631 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
632 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
633 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
634 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ UnitScale;
635 ALfloat nfc_delay = 0.0f;
637 count = (device->AmbiOrder == 3) ? 16 :
638 (device->AmbiOrder == 2) ? 9 :
639 (device->AmbiOrder == 1) ? 4 : 1;
640 for(i = 0;i < count;i++)
642 ALsizei acn = acnmap[i];
643 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
644 device->Dry.Ambi.Map[i].Index = acn;
646 device->Dry.CoeffCount = 0;
647 device->Dry.NumChannels = count;
649 if(device->AmbiOrder < 2)
651 device->FOAOut.Ambi = device->Dry.Ambi;
652 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
653 device->FOAOut.NumChannels = 0;
655 else
657 /* FOA output is always ACN+N3D for higher-order ambisonic output.
658 * The upsampler expects this and will convert it for output.
660 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
661 for(i = 0;i < 4;i++)
663 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
664 device->FOAOut.Ambi.Map[i].Index = i;
666 device->FOAOut.CoeffCount = 0;
667 device->FOAOut.NumChannels = 4;
669 ambiup_reset(device->AmbiUp, device);
672 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
674 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
675 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
676 device->AmbiOrder, true);
679 else
681 ALfloat w_scale, xyz_scale;
683 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
684 chanmap, count, &device->Dry.NumChannels);
685 device->Dry.CoeffCount = coeffcount;
687 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE2D_THIRD :
688 (device->Dry.CoeffCount > 4) ? W_SCALE2D_SECOND : 1.0f;
689 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE2D_THIRD :
690 (device->Dry.CoeffCount > 4) ? XYZ_SCALE2D_SECOND : 1.0f;
692 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
693 for(i = 0;i < device->Dry.NumChannels;i++)
695 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
696 for(j = 1;j < 4;j++)
697 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
699 device->FOAOut.CoeffCount = 4;
700 device->FOAOut.NumChannels = 0;
702 device->RealOut.NumChannels = 0;
705 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
707 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
708 const ALfloat *coeff_scale = UnitScale;
709 ALfloat w_scale = 1.0f;
710 ALfloat xyz_scale = 1.0f;
711 ALsizei i, j;
713 if(conf->FreqBands != 1)
714 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
715 conf->XOverFreq);
717 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
719 if(conf->ChanMask > 0x1ff)
721 w_scale = W_SCALE3D_THIRD;
722 xyz_scale = XYZ_SCALE3D_THIRD;
724 else if(conf->ChanMask > 0xf)
726 w_scale = W_SCALE3D_SECOND;
727 xyz_scale = XYZ_SCALE3D_SECOND;
730 else
732 if(conf->ChanMask > 0x1ff)
734 w_scale = W_SCALE2D_THIRD;
735 xyz_scale = XYZ_SCALE2D_THIRD;
737 else if(conf->ChanMask > 0xf)
739 w_scale = W_SCALE2D_SECOND;
740 xyz_scale = XYZ_SCALE2D_SECOND;
744 if(conf->CoeffScale == ADS_SN3D)
745 coeff_scale = SN3D2N3DScale;
746 else if(conf->CoeffScale == ADS_FuMa)
747 coeff_scale = FuMa2N3DScale;
749 for(i = 0;i < conf->NumSpeakers;i++)
751 ALsizei chan = speakermap[i];
752 ALfloat gain;
753 ALsizei k = 0;
755 for(j = 0;j < MAX_AMBI_COEFFS;j++)
756 chanmap[i].Config[j] = 0.0f;
758 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
759 for(j = 0;j < MAX_AMBI_COEFFS;j++)
761 if(j == 0) gain = conf->HFOrderGain[0];
762 else if(j == 1) gain = conf->HFOrderGain[1];
763 else if(j == 4) gain = conf->HFOrderGain[2];
764 else if(j == 9) gain = conf->HFOrderGain[3];
765 if((conf->ChanMask&(1<<j)))
766 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
770 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
771 conf->NumSpeakers, &device->Dry.NumChannels);
772 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
773 (conf->ChanMask > 0xf) ? 9 : 4;
775 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
776 for(i = 0;i < device->Dry.NumChannels;i++)
778 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
779 for(j = 1;j < 4;j++)
780 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
782 device->FOAOut.CoeffCount = 4;
783 device->FOAOut.NumChannels = 0;
785 device->RealOut.NumChannels = 0;
787 InitDistanceComp(device, conf, speakermap);
790 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
792 ALfloat avg_dist;
793 ALsizei count;
794 ALsizei i;
796 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
798 count = (conf->ChanMask > 0x1ff) ? 16 :
799 (conf->ChanMask > 0xf) ? 9 : 4;
800 for(i = 0;i < count;i++)
802 device->Dry.Ambi.Map[i].Scale = 1.0f;
803 device->Dry.Ambi.Map[i].Index = i;
806 else
808 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
810 count = (conf->ChanMask > 0x1ff) ? 7 :
811 (conf->ChanMask > 0xf) ? 5 : 3;
812 for(i = 0;i < count;i++)
814 device->Dry.Ambi.Map[i].Scale = 1.0f;
815 device->Dry.Ambi.Map[i].Index = map[i];
818 device->Dry.CoeffCount = 0;
819 device->Dry.NumChannels = count;
821 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
822 (conf->FreqBands == 1) ? "single" : "dual",
823 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
824 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
826 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
828 if(!(conf->ChanMask > 0xf))
830 device->FOAOut.Ambi = device->Dry.Ambi;
831 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
832 device->FOAOut.NumChannels = 0;
834 else
836 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
837 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
839 count = 4;
840 for(i = 0;i < count;i++)
842 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
843 device->FOAOut.Ambi.Map[i].Index = i;
846 else
848 static const int map[3] = { 0, 1, 3 };
849 count = 3;
850 for(i = 0;i < count;i++)
852 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
853 device->FOAOut.Ambi.Map[i].Index = map[i];
856 device->FOAOut.CoeffCount = 0;
857 device->FOAOut.NumChannels = count;
860 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
862 avg_dist = 0.0f;
863 for(i = 0;i < conf->NumSpeakers;i++)
864 avg_dist += conf->Speakers[i].Distance;
865 avg_dist /= (ALfloat)conf->NumSpeakers;
866 InitNearFieldCtrl(device, avg_dist,
867 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
868 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
871 InitDistanceComp(device, conf, speakermap);
874 static void InitHrtfPanning(ALCdevice *device)
876 /* NOTE: azimuth goes clockwise. */
877 static const ALfloat AmbiPoints[][2] = {
878 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
879 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
880 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
881 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
882 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
883 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
884 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
885 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
886 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
887 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
888 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
889 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
890 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
891 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
893 static const ALfloat AmbiMatrixFOA[][2][MAX_AMBI_COEFFS] = {
894 { { 1.88982237e-001f, 0.00000000e+000f, 1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, 1.24646009e-001f, 0.00000000e+000f } },
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, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f } },
899 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, 1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, 1.23259031e-001f } },
900 { { 1.88982237e-001f, -1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, -1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
901 { { 1.88982237e-001f, 0.00000000e+000f, 0.00000000e+000f, -1.88281281e-001f }, { 7.14285714e-002f, 0.00000000e+000f, 0.00000000e+000f, -1.23259031e-001f } },
902 { { 1.88982237e-001f, 1.88281281e-001f, 0.00000000e+000f, 0.00000000e+000f }, { 7.14285714e-002f, 1.23259031e-001f, 0.00000000e+000f, 0.00000000e+000f } },
903 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
904 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, 1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, 7.13950780e-002f } },
905 { { 1.88982237e-001f, -1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, -7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
906 { { 1.88982237e-001f, 1.09057783e-001f, -1.09208910e-001f, -1.09057783e-001f }, { 7.14285714e-002f, 7.13950780e-002f, -7.14940135e-002f, -7.13950780e-002f } },
907 { { 1.88982237e-001f, 0.00000000e+000f, -1.90399923e-001f, 0.00000000e+000f }, { 7.14285714e-002f, 0.00000000e+000f, -1.24646009e-001f, 0.00000000e+000f } }
908 }, AmbiMatrixHOA[][2][MAX_AMBI_COEFFS] = {
909 { { 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 } },
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.40852210e-001f, 1.09057783e-001f, 1.09208910e-001f, -1.09057783e-001f, -7.58818830e-002f, 7.66295578e-002f, -3.28314629e-004f, -7.66295578e-002f, 0.00000000e+000f }, { 7.14251066e-002f, 7.13950780e-002f, 7.14940135e-002f, -7.13950780e-002f, -9.61978444e-002f, 9.71456952e-002f, -4.16214759e-004f, -9.71456952e-002f, 0.00000000e+000f } },
914 { { 1.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 } },
915 { { 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 } },
916 { { 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 } },
917 { { 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 } },
918 { { 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 } },
919 { { 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 } },
920 { { 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 } },
921 { { 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 } },
922 { { 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 } },
924 const ALfloat (*AmbiMatrix)[2][MAX_AMBI_COEFFS] = device->AmbiUp ? AmbiMatrixHOA :
925 AmbiMatrixFOA;
926 ALsizei count = device->AmbiUp ? 9 : 4;
927 ALsizei i;
929 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
931 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
933 for(i = 0;i < count;i++)
935 device->Dry.Ambi.Map[i].Scale = 1.0f;
936 device->Dry.Ambi.Map[i].Index = i;
938 device->Dry.CoeffCount = 0;
939 device->Dry.NumChannels = count;
941 if(device->AmbiUp)
943 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
944 for(i = 0;i < 4;i++)
946 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
947 device->FOAOut.Ambi.Map[i].Index = i;
949 device->FOAOut.CoeffCount = 0;
950 device->FOAOut.NumChannels = 4;
952 ambiup_reset(device->AmbiUp, device);
954 else
956 device->FOAOut.Ambi = device->Dry.Ambi;
957 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
958 device->FOAOut.NumChannels = 0;
961 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
963 device->Hrtf->IrSize = BuildBFormatHrtf(device->HrtfHandle,
964 device->Hrtf, device->Dry.NumChannels,
965 AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints)
969 static void InitUhjPanning(ALCdevice *device)
971 ALsizei count = 3;
972 ALsizei i;
974 for(i = 0;i < count;i++)
976 ALsizei acn = FuMa2ACN[i];
977 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
978 device->Dry.Ambi.Map[i].Index = acn;
980 device->Dry.CoeffCount = 0;
981 device->Dry.NumChannels = count;
983 device->FOAOut.Ambi = device->Dry.Ambi;
984 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
985 device->FOAOut.NumChannels = 0;
987 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
990 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
992 /* Hold the HRTF the device last used, in case it's used again. */
993 struct Hrtf *old_hrtf = device->HrtfHandle;
994 const char *mode;
995 bool headphones;
996 int bs2blevel;
997 size_t i;
999 al_free(device->Hrtf);
1000 device->Hrtf = NULL;
1001 device->HrtfHandle = NULL;
1002 alstr_clear(&device->HrtfName);
1003 device->Render_Mode = NormalRender;
1005 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
1006 device->Dry.CoeffCount = 0;
1007 device->Dry.NumChannels = 0;
1008 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
1009 device->Dry.NumChannelsPerOrder[i] = 0;
1011 device->AvgSpeakerDist = 0.0f;
1012 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
1013 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1015 device->ChannelDelay[i].Gain = 1.0f;
1016 device->ChannelDelay[i].Length = 0;
1019 if(device->FmtChans != DevFmtStereo)
1021 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1022 const char *devname, *layout = NULL;
1023 AmbDecConf conf, *pconf = NULL;
1025 if(old_hrtf)
1026 Hrtf_DecRef(old_hrtf);
1027 old_hrtf = NULL;
1028 if(hrtf_appreq == Hrtf_Enable)
1029 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1031 ambdec_init(&conf);
1033 devname = alstr_get_cstr(device->DeviceName);
1034 switch(device->FmtChans)
1036 case DevFmtQuad: layout = "quad"; break;
1037 case DevFmtX51: /* fall-through */
1038 case DevFmtX51Rear: layout = "surround51"; break;
1039 case DevFmtX61: layout = "surround61"; break;
1040 case DevFmtX71: layout = "surround71"; break;
1041 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1042 case DevFmtMono:
1043 case DevFmtStereo:
1044 case DevFmtAmbi3D:
1045 break;
1047 if(layout)
1049 const char *fname;
1050 if(ConfigValueStr(devname, "decoder", layout, &fname))
1052 if(!ambdec_load(&conf, fname))
1053 ERR("Failed to load layout file %s\n", fname);
1054 else
1056 if(conf.ChanMask > 0xffff)
1057 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1058 else
1060 if(MakeSpeakerMap(device, &conf, speakermap))
1061 pconf = &conf;
1067 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1069 ambiup_free(device->AmbiUp);
1070 device->AmbiUp = NULL;
1071 if(!device->AmbiDecoder)
1072 device->AmbiDecoder = bformatdec_alloc();
1074 else
1076 bformatdec_free(device->AmbiDecoder);
1077 device->AmbiDecoder = NULL;
1078 if(device->FmtChans == DevFmtAmbi3D && device->AmbiOrder > 1)
1080 if(!device->AmbiUp)
1081 device->AmbiUp = ambiup_alloc();
1083 else
1085 ambiup_free(device->AmbiUp);
1086 device->AmbiUp = NULL;
1090 if(!pconf)
1091 InitPanning(device);
1092 else if(device->AmbiDecoder)
1093 InitHQPanning(device, pconf, speakermap);
1094 else
1095 InitCustomPanning(device, pconf, speakermap);
1097 ambdec_deinit(&conf);
1098 return;
1101 bformatdec_free(device->AmbiDecoder);
1102 device->AmbiDecoder = NULL;
1104 headphones = device->IsHeadphones;
1105 if(device->Type != Loopback)
1107 const char *mode;
1108 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1110 if(strcasecmp(mode, "headphones") == 0)
1111 headphones = true;
1112 else if(strcasecmp(mode, "speakers") == 0)
1113 headphones = false;
1114 else if(strcasecmp(mode, "auto") != 0)
1115 ERR("Unexpected stereo-mode: %s\n", mode);
1119 if(hrtf_userreq == Hrtf_Default)
1121 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1122 (hrtf_appreq == Hrtf_Enable);
1123 if(!usehrtf) goto no_hrtf;
1125 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1126 if(headphones && hrtf_appreq != Hrtf_Disable)
1127 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1129 else
1131 if(hrtf_userreq != Hrtf_Enable)
1133 if(hrtf_appreq == Hrtf_Enable)
1134 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1135 goto no_hrtf;
1137 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1140 if(VECTOR_SIZE(device->HrtfList) == 0)
1142 VECTOR_DEINIT(device->HrtfList);
1143 device->HrtfList = EnumerateHrtf(device->DeviceName);
1146 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1148 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1149 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1150 if(hrtf && hrtf->sampleRate == device->Frequency)
1152 device->HrtfHandle = hrtf;
1153 alstr_copy(&device->HrtfName, entry->name);
1155 else if(hrtf)
1156 Hrtf_DecRef(hrtf);
1159 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1161 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1162 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1163 if(hrtf && hrtf->sampleRate == device->Frequency)
1165 device->HrtfHandle = hrtf;
1166 alstr_copy(&device->HrtfName, entry->name);
1168 else if(hrtf)
1169 Hrtf_DecRef(hrtf);
1172 if(device->HrtfHandle)
1174 if(old_hrtf)
1175 Hrtf_DecRef(old_hrtf);
1176 old_hrtf = NULL;
1178 device->Render_Mode = HrtfRender;
1179 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1181 if(strcasecmp(mode, "full") == 0)
1182 device->Render_Mode = HrtfRender;
1183 else if(strcasecmp(mode, "basic") == 0)
1184 device->Render_Mode = NormalRender;
1185 else
1186 ERR("Unexpected hrtf-mode: %s\n", mode);
1189 if(device->Render_Mode == HrtfRender)
1191 /* Don't bother with HOA when using full HRTF rendering. Nothing
1192 * needs it, and it eases the CPU/memory load.
1194 ambiup_free(device->AmbiUp);
1195 device->AmbiUp = NULL;
1197 else
1199 if(!device->AmbiUp)
1200 device->AmbiUp = ambiup_alloc();
1203 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1204 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1205 alstr_get_cstr(device->HrtfName)
1207 InitHrtfPanning(device);
1208 return;
1210 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1212 no_hrtf:
1213 if(old_hrtf)
1214 Hrtf_DecRef(old_hrtf);
1215 old_hrtf = NULL;
1216 TRACE("HRTF disabled\n");
1218 device->Render_Mode = StereoPair;
1220 ambiup_free(device->AmbiUp);
1221 device->AmbiUp = NULL;
1223 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1224 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1225 if(device->Type != Loopback)
1226 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1227 if(bs2blevel > 0 && bs2blevel <= 6)
1229 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1230 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1231 TRACE("BS2B enabled\n");
1232 InitPanning(device);
1233 return;
1236 TRACE("BS2B disabled\n");
1238 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1240 if(strcasecmp(mode, "uhj") == 0)
1241 device->Render_Mode = NormalRender;
1242 else if(strcasecmp(mode, "panpot") != 0)
1243 ERR("Unexpected stereo-encoding: %s\n", mode);
1245 if(device->Render_Mode == NormalRender)
1247 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1248 TRACE("UHJ enabled\n");
1249 InitUhjPanning(device);
1250 return;
1253 TRACE("UHJ disabled\n");
1254 InitPanning(device);
1258 void aluInitEffectPanning(ALeffectslot *slot)
1260 ALsizei i;
1262 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1263 slot->NumChannels = 0;
1265 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1267 slot->ChanMap[i].Scale = 1.0f;
1268 slot->ChanMap[i].Index = i;
1270 slot->NumChannels = i;