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 CalcXYZCoeffs(ALfloat x
, ALfloat y
, ALfloat z
, ALfloat spread
, ALfloat coeffs
[MAX_AMBI_COEFFS
]);
42 #define ZERO_ORDER_SCALE 0.0f
43 #define FIRST_ORDER_SCALE 1.0f
44 #define SECOND_ORDER_SCALE (1.0f / 1.22474f)
45 #define THIRD_ORDER_SCALE (1.0f / 1.30657f)
48 static const ALuint FuMa2ACN
[MAX_AMBI_COEFFS
] = {
66 static const ALuint ACN2ACN
[MAX_AMBI_COEFFS
] = {
67 0, 1, 2, 3, 4, 5, 6, 7,
68 8, 9, 10, 11, 12, 13, 14, 15
71 /* NOTE: These are scale factors as applied to Ambisonics content. Decoder
72 * coefficients should be divided by these values to get proper N3D scalings.
74 static const ALfloat UnitScale
[MAX_AMBI_COEFFS
] = {
75 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
,
76 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
78 static const ALfloat SN3D2N3DScale
[MAX_AMBI_COEFFS
] = {
79 1.000000000f
, /* ACN 0 (W), sqrt(1) */
80 1.732050808f
, /* ACN 1 (Y), sqrt(3) */
81 1.732050808f
, /* ACN 2 (Z), sqrt(3) */
82 1.732050808f
, /* ACN 3 (X), sqrt(3) */
83 2.236067978f
, /* ACN 4 (V), sqrt(5) */
84 2.236067978f
, /* ACN 5 (T), sqrt(5) */
85 2.236067978f
, /* ACN 6 (R), sqrt(5) */
86 2.236067978f
, /* ACN 7 (S), sqrt(5) */
87 2.236067978f
, /* ACN 8 (U), sqrt(5) */
88 2.645751311f
, /* ACN 9 (Q), sqrt(7) */
89 2.645751311f
, /* ACN 10 (O), sqrt(7) */
90 2.645751311f
, /* ACN 11 (M), sqrt(7) */
91 2.645751311f
, /* ACN 12 (K), sqrt(7) */
92 2.645751311f
, /* ACN 13 (L), sqrt(7) */
93 2.645751311f
, /* ACN 14 (N), sqrt(7) */
94 2.645751311f
, /* ACN 15 (P), sqrt(7) */
96 static const ALfloat FuMa2N3DScale
[MAX_AMBI_COEFFS
] = {
97 1.414213562f
, /* ACN 0 (W), sqrt(2) */
98 1.732050808f
, /* ACN 1 (Y), sqrt(3) */
99 1.732050808f
, /* ACN 2 (Z), sqrt(3) */
100 1.732050808f
, /* ACN 3 (X), sqrt(3) */
101 1.936491673f
, /* ACN 4 (V), sqrt(15)/2 */
102 1.936491673f
, /* ACN 5 (T), sqrt(15)/2 */
103 2.236067978f
, /* ACN 6 (R), sqrt(5) */
104 1.936491673f
, /* ACN 7 (S), sqrt(15)/2 */
105 1.936491673f
, /* ACN 8 (U), sqrt(15)/2 */
106 2.091650066f
, /* ACN 9 (Q), sqrt(35/8) */
107 1.972026594f
, /* ACN 10 (O), sqrt(35)/3 */
108 2.231093404f
, /* ACN 11 (M), sqrt(224/45) */
109 2.645751311f
, /* ACN 12 (K), sqrt(7) */
110 2.231093404f
, /* ACN 13 (L), sqrt(224/45) */
111 1.972026594f
, /* ACN 14 (N), sqrt(35)/3 */
112 2.091650066f
, /* ACN 15 (P), sqrt(35/8) */
116 void CalcDirectionCoeffs(const ALfloat dir
[3], ALfloat spread
, ALfloat coeffs
[MAX_AMBI_COEFFS
])
118 /* Convert from OpenAL coords to Ambisonics. */
124 coeffs
[0] = 1.0f
; /* ACN 0 = 1 */
126 coeffs
[1] = 1.732050808f
* y
; /* ACN 1 = sqrt(3) * Y */
127 coeffs
[2] = 1.732050808f
* z
; /* ACN 2 = sqrt(3) * Z */
128 coeffs
[3] = 1.732050808f
* x
; /* ACN 3 = sqrt(3) * X */
130 coeffs
[4] = 3.872983346f
* x
* y
; /* ACN 4 = sqrt(15) * X * Y */
131 coeffs
[5] = 3.872983346f
* y
* z
; /* ACN 5 = sqrt(15) * Y * Z */
132 coeffs
[6] = 1.118033989f
* (3.0f
*z
*z
- 1.0f
); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
133 coeffs
[7] = 3.872983346f
* x
* z
; /* ACN 7 = sqrt(15) * X * Z */
134 coeffs
[8] = 1.936491673f
* (x
*x
- y
*y
); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
136 coeffs
[9] = 2.091650066f
* y
* (3.0f
*x
*x
- y
*y
); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
137 coeffs
[10] = 10.246950766f
* z
* x
* y
; /* ACN 10 = sqrt(105) * Z * X * Y */
138 coeffs
[11] = 1.620185175f
* y
* (5.0f
*z
*z
- 1.0f
); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
139 coeffs
[12] = 1.322875656f
* z
* (5.0f
*z
*z
- 3.0f
); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
140 coeffs
[13] = 1.620185175f
* x
* (5.0f
*z
*z
- 1.0f
); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
141 coeffs
[14] = 5.123475383f
* z
* (x
*x
- y
*y
); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
142 coeffs
[15] = 2.091650066f
* x
* (x
*x
- 3.0f
*y
*y
); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
146 /* Implement the spread by using a spherical source that subtends the
148 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
150 * When adjusted for N3D normalization instead of SN3D, these
153 * ZH0 = -sqrt(pi) * (-1+ca);
154 * ZH1 = 0.5*sqrt(pi) * sa*sa;
155 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
156 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
157 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
158 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
160 * The gain of the source is compensated for size, so that the
161 * loundness doesn't depend on the spread. That is, the factors are
162 * scaled so that ZH0 remains 1 regardless of the spread. Thus:
165 * ZH1 = 0.5f * (ca+1.0f);
166 * ZH2 = 0.5f * (ca+1.0f)*ca;
167 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
168 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
169 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
171 ALfloat ca
= cosf(spread
* 0.5f
);
173 ALfloat ZH0_norm
= 1.0f
;
174 ALfloat ZH1_norm
= 0.5f
* (ca
+1.f
);
175 ALfloat ZH2_norm
= 0.5f
* (ca
+1.f
)*ca
;
176 ALfloat ZH3_norm
= 0.125f
* (ca
+1.f
)*(5.f
*ca
*ca
-1.f
);
179 coeffs
[0] *= ZH0_norm
;
181 coeffs
[1] *= ZH1_norm
;
182 coeffs
[2] *= ZH1_norm
;
183 coeffs
[3] *= ZH1_norm
;
185 coeffs
[4] *= ZH2_norm
;
186 coeffs
[5] *= ZH2_norm
;
187 coeffs
[6] *= ZH2_norm
;
188 coeffs
[7] *= ZH2_norm
;
189 coeffs
[8] *= ZH2_norm
;
191 coeffs
[9] *= ZH3_norm
;
192 coeffs
[10] *= ZH3_norm
;
193 coeffs
[11] *= ZH3_norm
;
194 coeffs
[12] *= ZH3_norm
;
195 coeffs
[13] *= ZH3_norm
;
196 coeffs
[14] *= ZH3_norm
;
197 coeffs
[15] *= ZH3_norm
;
201 void CalcAngleCoeffs(ALfloat azimuth
, ALfloat elevation
, ALfloat spread
, ALfloat coeffs
[MAX_AMBI_COEFFS
])
204 sinf(azimuth
) * cosf(elevation
),
206 -cosf(azimuth
) * cosf(elevation
)
208 CalcDirectionCoeffs(dir
, spread
, coeffs
);
212 void ComputeAmbientGainsMC(const ChannelConfig
*chancoeffs
, ALuint numchans
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
216 for(i
= 0;i
< numchans
;i
++)
218 // The W coefficients are based on a mathematical average of the
219 // output. The square root of the base average provides for a more
220 // perceptual average volume, better suited to non-directional gains.
221 gains
[i
] = sqrtf(chancoeffs
[i
][0]) * ingain
;
223 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
227 void ComputeAmbientGainsBF(const BFChannelConfig
*chanmap
, ALuint numchans
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
232 for(i
= 0;i
< numchans
;i
++)
234 if(chanmap
[i
].Index
== 0)
235 gain
+= chanmap
[i
].Scale
;
237 gains
[0] = gain
* 1.414213562f
* ingain
;
238 for(i
= 1;i
< MAX_OUTPUT_CHANNELS
;i
++)
242 void ComputePanningGainsMC(const ChannelConfig
*chancoeffs
, ALuint numchans
, ALuint numcoeffs
, const ALfloat coeffs
[MAX_AMBI_COEFFS
], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
246 for(i
= 0;i
< numchans
;i
++)
249 for(j
= 0;j
< numcoeffs
;j
++)
250 gain
+= chancoeffs
[i
][j
]*coeffs
[j
];
251 gains
[i
] = gain
* ingain
;
253 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
257 void ComputePanningGainsBF(const BFChannelConfig
*chanmap
, ALuint numchans
, const ALfloat coeffs
[MAX_AMBI_COEFFS
], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
261 for(i
= 0;i
< numchans
;i
++)
262 gains
[i
] = chanmap
[i
].Scale
* coeffs
[chanmap
[i
].Index
] * ingain
;
263 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
267 void ComputeFirstOrderGainsMC(const ChannelConfig
*chancoeffs
, ALuint numchans
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
271 for(i
= 0;i
< numchans
;i
++)
275 gain
+= chancoeffs
[i
][j
] * mtx
[j
];
276 gains
[i
] = gain
* ingain
;
278 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
282 void ComputeFirstOrderGainsBF(const BFChannelConfig
*chanmap
, ALuint numchans
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
])
286 for(i
= 0;i
< numchans
;i
++)
287 gains
[i
] = chanmap
[i
].Scale
* mtx
[chanmap
[i
].Index
] * ingain
;
288 for(;i
< MAX_OUTPUT_CHANNELS
;i
++)
293 DECL_CONST
static inline const char *GetLabelFromChannel(enum Channel channel
)
297 case FrontLeft
: return "front-left";
298 case FrontRight
: return "front-right";
299 case FrontCenter
: return "front-center";
300 case LFE
: return "lfe";
301 case BackLeft
: return "back-left";
302 case BackRight
: return "back-right";
303 case BackCenter
: return "back-center";
304 case SideLeft
: return "side-left";
305 case SideRight
: return "side-right";
307 case UpperFrontLeft
: return "upper-front-left";
308 case UpperFrontRight
: return "upper-front-right";
309 case UpperBackLeft
: return "upper-back-left";
310 case UpperBackRight
: return "upper-back-right";
311 case LowerFrontLeft
: return "lower-front-left";
312 case LowerFrontRight
: return "lower-front-right";
313 case LowerBackLeft
: return "lower-back-left";
314 case LowerBackRight
: return "lower-back-right";
316 case Aux0
: return "aux-0";
317 case Aux1
: return "aux-1";
318 case Aux2
: return "aux-2";
319 case Aux3
: return "aux-3";
320 case Aux4
: return "aux-4";
321 case Aux5
: return "aux-5";
322 case Aux6
: return "aux-6";
323 case Aux7
: return "aux-7";
324 case Aux8
: return "aux-8";
325 case Aux9
: return "aux-9";
326 case Aux10
: return "aux-10";
327 case Aux11
: return "aux-11";
328 case Aux12
: return "aux-12";
329 case Aux13
: return "aux-13";
330 case Aux14
: return "aux-14";
331 case Aux15
: return "aux-15";
333 case InvalidChannel
: break;
339 typedef struct ChannelMap
{
340 enum Channel ChanName
;
341 ChannelConfig Config
;
344 static void SetChannelMap(const enum Channel
*devchans
, ChannelConfig
*ambicoeffs
,
345 const ChannelMap
*chanmap
, size_t count
, ALuint
*outcount
,
351 for(i
= 0;i
< MAX_OUTPUT_CHANNELS
&& devchans
[i
] != InvalidChannel
;i
++)
353 if(devchans
[i
] == LFE
)
355 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
356 ambicoeffs
[i
][j
] = 0.0f
;
360 for(j
= 0;j
< count
;j
++)
362 if(devchans
[i
] != chanmap
[j
].ChanName
)
367 /* Reformat FuMa -> ACN/N3D */
368 for(k
= 0;k
< MAX_AMBI_COEFFS
;++k
)
370 ALuint acn
= FuMa2ACN
[k
];
371 ambicoeffs
[i
][acn
] = chanmap
[j
].Config
[k
] / FuMa2N3DScale
[acn
];
376 for(k
= 0;k
< MAX_AMBI_COEFFS
;++k
)
377 ambicoeffs
[i
][k
] = chanmap
[j
].Config
[k
];
382 ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans
[i
]), i
);
387 static bool MakeSpeakerMap(ALCdevice
*device
, const AmbDecConf
*conf
, ALuint speakermap
[MAX_OUTPUT_CHANNELS
])
391 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
395 /* NOTE: AmbDec does not define any standard speaker names, however
396 * for this to work we have to by able to find the output channel
397 * the speaker definition corresponds to. Therefore, OpenAL Soft
398 * requires these channel labels to be recognized:
409 * Additionally, surround51 will acknowledge back speakers for side
410 * channels, and surround51rear will acknowledge side speakers for
411 * back channels, to avoid issues with an ambdec expecting 5.1 to
412 * use the side channels when the device is configured for back,
415 if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "LF") == 0)
416 c
= GetChannelIdxByName(device
->RealOut
, FrontLeft
);
417 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "RF") == 0)
418 c
= GetChannelIdxByName(device
->RealOut
, FrontRight
);
419 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "CE") == 0)
420 c
= GetChannelIdxByName(device
->RealOut
, FrontCenter
);
421 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "LS") == 0)
423 if(device
->FmtChans
== DevFmtX51Rear
)
424 c
= GetChannelIdxByName(device
->RealOut
, BackLeft
);
426 c
= GetChannelIdxByName(device
->RealOut
, SideLeft
);
428 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "RS") == 0)
430 if(device
->FmtChans
== DevFmtX51Rear
)
431 c
= GetChannelIdxByName(device
->RealOut
, BackRight
);
433 c
= GetChannelIdxByName(device
->RealOut
, SideRight
);
435 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "LB") == 0)
437 if(device
->FmtChans
== DevFmtX51
)
438 c
= GetChannelIdxByName(device
->RealOut
, SideLeft
);
440 c
= GetChannelIdxByName(device
->RealOut
, BackLeft
);
442 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "RB") == 0)
444 if(device
->FmtChans
== DevFmtX51
)
445 c
= GetChannelIdxByName(device
->RealOut
, SideRight
);
447 c
= GetChannelIdxByName(device
->RealOut
, BackRight
);
449 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "CB") == 0)
450 c
= GetChannelIdxByName(device
->RealOut
, BackCenter
);
453 const char *name
= al_string_get_cstr(conf
->Speakers
[i
].Name
);
457 if(sscanf(name
, "AUX%u%c", &n
, &ch
) == 1 && n
< 16)
458 c
= GetChannelIdxByName(device
->RealOut
, Aux0
+n
);
461 ERR("AmbDec speaker label \"%s\" not recognized\n", name
);
467 ERR("Failed to lookup AmbDec speaker label %s\n",
468 al_string_get_cstr(conf
->Speakers
[i
].Name
));
478 /* NOTE: These decoder coefficients are using FuMa channel ordering and
479 * normalization, since that's what was produced by the Ambisonic Decoder
480 * Toolbox. SetChannelMap will convert them to N3D.
482 static const ChannelMap MonoCfg
[1] = {
483 { FrontCenter
, { 1.414213562f
} },
485 { FrontLeft
, { 0.707106781f
, 0.0f
, 0.5f
, 0.0f
} },
486 { FrontRight
, { 0.707106781f
, 0.0f
, -0.5f
, 0.0f
} },
488 { FrontLeft
, { 0.353553f
, 0.306186f
, 0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.125000f
} },
489 { FrontRight
, { 0.353553f
, 0.306186f
, -0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, -0.125000f
} },
490 { BackLeft
, { 0.353553f
, -0.306186f
, 0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, -0.125000f
} },
491 { BackRight
, { 0.353553f
, -0.306186f
, -0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.125000f
} },
493 { FrontLeft
, { 0.208954f
, 0.199518f
, 0.223424f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012543f
, 0.144260f
} },
494 { FrontRight
, { 0.208950f
, 0.199514f
, -0.223425f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012544f
, -0.144258f
} },
495 { FrontCenter
, { 0.109403f
, 0.168250f
, -0.000002f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.100431f
, -0.000001f
} },
496 { SideLeft
, { 0.470934f
, -0.346484f
, 0.327504f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022188f
, -0.041113f
} },
497 { SideRight
, { 0.470936f
, -0.346480f
, -0.327507f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022186f
, 0.041114f
} },
499 { FrontLeft
, { 0.208954f
, 0.199518f
, 0.223424f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012543f
, 0.144260f
} },
500 { FrontRight
, { 0.208950f
, 0.199514f
, -0.223425f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012544f
, -0.144258f
} },
501 { FrontCenter
, { 0.109403f
, 0.168250f
, -0.000002f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.100431f
, -0.000001f
} },
502 { BackLeft
, { 0.470934f
, -0.346484f
, 0.327504f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022188f
, -0.041113f
} },
503 { BackRight
, { 0.470936f
, -0.346480f
, -0.327507f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022186f
, 0.041114f
} },
505 { FrontLeft
, { 0.167065f
, 0.200583f
, 0.172695f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.029855f
, 0.186407f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.039241f
, 0.068910f
} },
506 { FrontRight
, { 0.167065f
, 0.200583f
, -0.172695f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.029855f
, -0.186407f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.039241f
, -0.068910f
} },
507 { FrontCenter
, { 0.109403f
, 0.179490f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.142031f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.072024f
, 0.000000f
} },
508 { BackCenter
, { 0.353556f
, -0.461940f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.165723f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.000000f
} },
509 { SideLeft
, { 0.289151f
, -0.081301f
, 0.401292f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.188208f
, -0.071420f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.010099f
, -0.032897f
} },
510 { SideRight
, { 0.289151f
, -0.081301f
, -0.401292f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.188208f
, 0.071420f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.010099f
, 0.032897f
} },
512 { FrontLeft
, { 0.167065f
, 0.200583f
, 0.172695f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.029855f
, 0.186407f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.039241f
, 0.068910f
} },
513 { FrontRight
, { 0.167065f
, 0.200583f
, -0.172695f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.029855f
, -0.186407f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.039241f
, -0.068910f
} },
514 { FrontCenter
, { 0.109403f
, 0.179490f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.142031f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.072024f
, 0.000000f
} },
515 { BackLeft
, { 0.224752f
, -0.295009f
, 0.170325f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.105349f
, -0.182473f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.065799f
} },
516 { BackRight
, { 0.224752f
, -0.295009f
, -0.170325f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.105349f
, 0.182473f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, -0.065799f
} },
517 { SideLeft
, { 0.224739f
, 0.000000f
, 0.340644f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.210697f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, -0.065795f
} },
518 { SideRight
, { 0.224739f
, 0.000000f
, -0.340644f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.210697f
, 0.000000f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.065795f
} },
521 static void InitPanning(ALCdevice
*device
)
523 const ChannelMap
*chanmap
= NULL
;
524 ALuint coeffcount
= 0;
530 switch(device
->FmtChans
)
533 count
= COUNTOF(MonoCfg
);
535 ambiscale
= ZERO_ORDER_SCALE
;
540 count
= COUNTOF(StereoCfg
);
542 ambiscale
= FIRST_ORDER_SCALE
;
547 count
= COUNTOF(QuadCfg
);
549 ambiscale
= SECOND_ORDER_SCALE
;
554 count
= COUNTOF(X51SideCfg
);
555 chanmap
= X51SideCfg
;
556 ambiscale
= SECOND_ORDER_SCALE
;
561 count
= COUNTOF(X51RearCfg
);
562 chanmap
= X51RearCfg
;
563 ambiscale
= SECOND_ORDER_SCALE
;
568 count
= COUNTOF(X61Cfg
);
570 ambiscale
= THIRD_ORDER_SCALE
;
575 count
= COUNTOF(X71Cfg
);
577 ambiscale
= THIRD_ORDER_SCALE
;
587 if(device
->FmtChans
>= DevFmtAmbi1
&& device
->FmtChans
<= DevFmtAmbi3
)
589 const ALuint
*acnmap
= (device
->AmbiFmt
== AmbiFormat_FuMa
) ? FuMa2ACN
: ACN2ACN
;
590 const ALfloat
*n3dcale
= (device
->AmbiFmt
== AmbiFormat_FuMa
) ? FuMa2N3DScale
:
591 (device
->AmbiFmt
== AmbiFormat_ACN_SN3D
) ? SN3D2N3DScale
:
592 /*(device->AmbiFmt == AmbiFormat_ACN_N3D) ?*/ UnitScale
;
594 count
= (device
->FmtChans
== DevFmtAmbi3
) ? 16 :
595 (device
->FmtChans
== DevFmtAmbi2
) ? 9 :
596 (device
->FmtChans
== DevFmtAmbi1
) ? 4 : 1;
597 for(i
= 0;i
< count
;i
++)
599 ALuint acn
= acnmap
[i
];
600 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
/n3dcale
[acn
];
601 device
->Dry
.Ambi
.Map
[i
].Index
= acn
;
603 device
->Dry
.CoeffCount
= 0;
604 device
->Dry
.NumChannels
= count
;
606 if(device
->FmtChans
== DevFmtAmbi1
)
608 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
609 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
613 /* FOA output is always ACN+N3D for higher-order ambisonic output.
614 * The upsampler expects this and will convert it for output.
616 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
619 device
->FOAOut
.Ambi
.Map
[i
].Scale
= 1.0f
;
620 device
->FOAOut
.Ambi
.Map
[i
].Index
= i
;
622 device
->FOAOut
.CoeffCount
= 0;
624 ambiup_reset(device
->AmbiUp
, device
);
629 SetChannelMap(device
->RealOut
.ChannelName
, device
->Dry
.Ambi
.Coeffs
,
630 chanmap
, count
, &device
->Dry
.NumChannels
, AL_TRUE
);
631 device
->Dry
.CoeffCount
= coeffcount
;
633 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
634 for(i
= 0;i
< device
->Dry
.NumChannels
;i
++)
636 device
->FOAOut
.Ambi
.Coeffs
[i
][0] = device
->Dry
.Ambi
.Coeffs
[i
][0];
638 device
->FOAOut
.Ambi
.Coeffs
[i
][j
] = device
->Dry
.Ambi
.Coeffs
[i
][j
] * ambiscale
;
640 device
->FOAOut
.CoeffCount
= 4;
644 static void InitCustomPanning(ALCdevice
*device
, const AmbDecConf
*conf
, const ALuint speakermap
[MAX_OUTPUT_CHANNELS
])
646 ChannelMap chanmap
[MAX_OUTPUT_CHANNELS
];
647 const ALfloat
*coeff_scale
= UnitScale
;
648 ALfloat ambiscale
= 1.0f
;
651 if(conf
->FreqBands
!= 1)
652 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
655 if(conf
->ChanMask
> 0x1ff)
656 ambiscale
= THIRD_ORDER_SCALE
;
657 else if(conf
->ChanMask
> 0xf)
658 ambiscale
= SECOND_ORDER_SCALE
;
659 else if(conf
->ChanMask
> 0x1)
660 ambiscale
= FIRST_ORDER_SCALE
;
664 if(conf
->CoeffScale
== ADS_SN3D
)
665 coeff_scale
= SN3D2N3DScale
;
666 else if(conf
->CoeffScale
== ADS_FuMa
)
667 coeff_scale
= FuMa2N3DScale
;
669 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
671 ALuint chan
= speakermap
[i
];
675 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
676 chanmap
[i
].Config
[j
] = 0.0f
;
678 chanmap
[i
].ChanName
= device
->RealOut
.ChannelName
[chan
];
679 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
681 if(j
== 0) gain
= conf
->HFOrderGain
[0];
682 else if(j
== 1) gain
= conf
->HFOrderGain
[1];
683 else if(j
== 4) gain
= conf
->HFOrderGain
[2];
684 else if(j
== 9) gain
= conf
->HFOrderGain
[3];
685 if((conf
->ChanMask
&(1<<j
)))
686 chanmap
[i
].Config
[j
] = conf
->HFMatrix
[i
][k
++] / coeff_scale
[j
] * gain
;
690 SetChannelMap(device
->RealOut
.ChannelName
, device
->Dry
.Ambi
.Coeffs
, chanmap
,
691 conf
->NumSpeakers
, &device
->Dry
.NumChannels
, AL_FALSE
);
692 device
->Dry
.CoeffCount
= (conf
->ChanMask
> 0x1ff) ? 16 :
693 (conf
->ChanMask
> 0xf) ? 9 : 4;
695 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
696 for(i
= 0;i
< device
->Dry
.NumChannels
;i
++)
698 device
->FOAOut
.Ambi
.Coeffs
[i
][0] = device
->Dry
.Ambi
.Coeffs
[i
][0];
700 device
->FOAOut
.Ambi
.Coeffs
[i
][j
] = device
->Dry
.Ambi
.Coeffs
[i
][j
] * ambiscale
;
702 device
->FOAOut
.CoeffCount
= 4;
705 static void InitHQPanning(ALCdevice
*device
, const AmbDecConf
*conf
, const ALuint speakermap
[MAX_OUTPUT_CHANNELS
])
712 devname
= al_string_get_cstr(device
->DeviceName
);
713 if(GetConfigValueBool(devname
, "decoder", "distance-comp", 1))
714 decflags
|= BFDF_DistanceComp
;
716 if((conf
->ChanMask
&AMBI_PERIPHONIC_MASK
))
718 count
= (conf
->ChanMask
> 0x1ff) ? 16 :
719 (conf
->ChanMask
> 0xf) ? 9 : 4;
720 for(i
= 0;i
< count
;i
++)
722 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
;
723 device
->Dry
.Ambi
.Map
[i
].Index
= i
;
728 static const int map
[MAX_AMBI2D_COEFFS
] = { 0, 1, 3, 4, 8, 9, 15 };
730 count
= (conf
->ChanMask
> 0x1ff) ? 7 :
731 (conf
->ChanMask
> 0xf) ? 5 : 3;
732 for(i
= 0;i
< count
;i
++)
734 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
;
735 device
->Dry
.Ambi
.Map
[i
].Index
= map
[i
];
738 device
->Dry
.CoeffCount
= 0;
739 device
->Dry
.NumChannels
= count
;
741 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
742 (conf
->FreqBands
== 1) ? "single" : "dual",
743 (conf
->ChanMask
> 0xf) ? (conf
->ChanMask
> 0x1ff) ? "third" : "second" : "first",
744 (conf
->ChanMask
&AMBI_PERIPHONIC_MASK
) ? " periphonic" : ""
746 bformatdec_reset(device
->AmbiDecoder
, conf
, count
, device
->Frequency
,
747 speakermap
, decflags
);
749 if(bformatdec_getOrder(device
->AmbiDecoder
) < 2)
751 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
752 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
756 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
759 device
->FOAOut
.Ambi
.Map
[i
].Scale
= 1.0f
;
760 device
->FOAOut
.Ambi
.Map
[i
].Index
= i
;
762 device
->FOAOut
.CoeffCount
= 0;
766 static void InitHrtfPanning(ALCdevice
*device
)
768 static const enum Channel CubeChannels
[MAX_OUTPUT_CHANNELS
] = {
769 UpperFrontLeft
, UpperFrontRight
, UpperBackLeft
, UpperBackRight
,
770 LowerFrontLeft
, LowerFrontRight
, LowerBackLeft
, LowerBackRight
,
771 InvalidChannel
, InvalidChannel
, InvalidChannel
, InvalidChannel
,
772 InvalidChannel
, InvalidChannel
, InvalidChannel
, InvalidChannel
774 static const ChannelMap Cube8Cfg
[8] = {
775 { UpperFrontLeft
, { 0.176776695f
, 0.072168784f
, 0.072168784f
, 0.072168784f
} },
776 { UpperFrontRight
, { 0.176776695f
, 0.072168784f
, -0.072168784f
, 0.072168784f
} },
777 { UpperBackLeft
, { 0.176776695f
, -0.072168784f
, 0.072168784f
, 0.072168784f
} },
778 { UpperBackRight
, { 0.176776695f
, -0.072168784f
, -0.072168784f
, 0.072168784f
} },
779 { LowerFrontLeft
, { 0.176776695f
, 0.072168784f
, 0.072168784f
, -0.072168784f
} },
780 { LowerFrontRight
, { 0.176776695f
, 0.072168784f
, -0.072168784f
, -0.072168784f
} },
781 { LowerBackLeft
, { 0.176776695f
, -0.072168784f
, 0.072168784f
, -0.072168784f
} },
782 { LowerBackRight
, { 0.176776695f
, -0.072168784f
, -0.072168784f
, -0.072168784f
} },
784 static const struct {
785 enum Channel Channel
;
789 { UpperFrontLeft
, DEG2RAD( -45.0f
), DEG2RAD( 45.0f
) },
790 { UpperFrontRight
, DEG2RAD( 45.0f
), DEG2RAD( 45.0f
) },
791 { UpperBackLeft
, DEG2RAD(-135.0f
), DEG2RAD( 45.0f
) },
792 { UpperBackRight
, DEG2RAD( 135.0f
), DEG2RAD( 45.0f
) },
793 { LowerFrontLeft
, DEG2RAD( -45.0f
), DEG2RAD(-45.0f
) },
794 { LowerFrontRight
, DEG2RAD( 45.0f
), DEG2RAD(-45.0f
) },
795 { LowerBackLeft
, DEG2RAD(-135.0f
), DEG2RAD(-45.0f
) },
796 { LowerBackRight
, DEG2RAD( 135.0f
), DEG2RAD(-45.0f
) },
798 const ChannelMap
*chanmap
= Cube8Cfg
;
799 size_t count
= COUNTOF(Cube8Cfg
);
802 SetChannelMap(CubeChannels
, device
->Dry
.Ambi
.Coeffs
, chanmap
, count
,
803 &device
->Dry
.NumChannels
, AL_TRUE
);
804 device
->Dry
.CoeffCount
= 4;
806 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
807 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
809 for(i
= 0;i
< device
->Dry
.NumChannels
;i
++)
811 int chan
= GetChannelIndex(CubeChannels
, CubeInfo
[i
].Channel
);
812 GetLerpedHrtfCoeffs(device
->Hrtf
, CubeInfo
[i
].Elevation
, CubeInfo
[i
].Angle
, 1.0f
, 0.0f
,
813 device
->Hrtf_Params
[chan
].Coeffs
, device
->Hrtf_Params
[chan
].Delay
);
817 static void InitUhjPanning(ALCdevice
*device
)
822 for(i
= 0;i
< count
;i
++)
824 ALuint acn
= FuMa2ACN
[i
];
825 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
/FuMa2N3DScale
[acn
];
826 device
->Dry
.Ambi
.Map
[i
].Index
= acn
;
828 device
->Dry
.CoeffCount
= 0;
829 device
->Dry
.NumChannels
= count
;
831 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
832 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
835 void aluInitRenderer(ALCdevice
*device
, ALint hrtf_id
, enum HrtfRequestMode hrtf_appreq
, enum HrtfRequestMode hrtf_userreq
)
843 al_string_clear(&device
->Hrtf_Name
);
844 device
->Render_Mode
= NormalRender
;
846 memset(&device
->Dry
.Ambi
, 0, sizeof(device
->Dry
.Ambi
));
847 device
->Dry
.CoeffCount
= 0;
848 device
->Dry
.NumChannels
= 0;
850 if(device
->FmtChans
!= DevFmtStereo
)
852 ALuint speakermap
[MAX_OUTPUT_CHANNELS
];
853 const char *devname
, *layout
= NULL
;
854 AmbDecConf conf
, *pconf
= NULL
;
856 if(hrtf_appreq
== Hrtf_Enable
)
857 device
->Hrtf_Status
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
861 devname
= al_string_get_cstr(device
->DeviceName
);
862 switch(device
->FmtChans
)
864 case DevFmtQuad
: layout
= "quad"; break;
865 case DevFmtX51
: layout
= "surround51"; break;
866 case DevFmtX51Rear
: layout
= "surround51rear"; break;
867 case DevFmtX61
: layout
= "surround61"; break;
868 case DevFmtX71
: layout
= "surround71"; break;
869 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
880 if(ConfigValueStr(devname
, "decoder", layout
, &fname
))
882 if(!ambdec_load(&conf
, fname
))
883 ERR("Failed to load layout file %s\n", fname
);
886 if(conf
.ChanMask
> 0xffff)
887 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf
.ChanMask
);
890 if(MakeSpeakerMap(device
, &conf
, speakermap
))
897 if(pconf
&& GetConfigValueBool(devname
, "decoder", "hq-mode", 0))
899 ambiup_free(device
->AmbiUp
);
900 device
->AmbiUp
= NULL
;
901 if(!device
->AmbiDecoder
)
902 device
->AmbiDecoder
= bformatdec_alloc();
906 bformatdec_free(device
->AmbiDecoder
);
907 device
->AmbiDecoder
= NULL
;
908 if(device
->FmtChans
> DevFmtAmbi1
&& device
->FmtChans
<= DevFmtAmbi3
)
911 device
->AmbiUp
= ambiup_alloc();
915 ambiup_free(device
->AmbiUp
);
916 device
->AmbiUp
= NULL
;
922 else if(device
->AmbiDecoder
)
923 InitHQPanning(device
, pconf
, speakermap
);
925 InitCustomPanning(device
, pconf
, speakermap
);
927 ambdec_deinit(&conf
);
931 ambiup_free(device
->AmbiUp
);
932 device
->AmbiUp
= NULL
;
933 bformatdec_free(device
->AmbiDecoder
);
934 device
->AmbiDecoder
= NULL
;
936 headphones
= device
->IsHeadphones
;
937 if(device
->Type
!= Loopback
)
940 if(ConfigValueStr(al_string_get_cstr(device
->DeviceName
), NULL
, "stereo-mode", &mode
))
942 if(strcasecmp(mode
, "headphones") == 0)
944 else if(strcasecmp(mode
, "speakers") == 0)
946 else if(strcasecmp(mode
, "auto") != 0)
947 ERR("Unexpected stereo-mode: %s\n", mode
);
951 if(hrtf_userreq
== Hrtf_Default
)
953 bool usehrtf
= (headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
954 (hrtf_appreq
== Hrtf_Enable
);
955 if(!usehrtf
) goto no_hrtf
;
957 device
->Hrtf_Status
= ALC_HRTF_ENABLED_SOFT
;
958 if(headphones
&& hrtf_appreq
!= Hrtf_Disable
)
959 device
->Hrtf_Status
= ALC_HRTF_HEADPHONES_DETECTED_SOFT
;
963 if(hrtf_userreq
!= Hrtf_Enable
)
965 if(hrtf_appreq
== Hrtf_Enable
)
966 device
->Hrtf_Status
= ALC_HRTF_DENIED_SOFT
;
969 device
->Hrtf_Status
= ALC_HRTF_REQUIRED_SOFT
;
972 if(VECTOR_SIZE(device
->Hrtf_List
) == 0)
974 VECTOR_DEINIT(device
->Hrtf_List
);
975 device
->Hrtf_List
= EnumerateHrtf(device
->DeviceName
);
978 if(hrtf_id
>= 0 && (size_t)hrtf_id
< VECTOR_SIZE(device
->Hrtf_List
))
980 const HrtfEntry
*entry
= &VECTOR_ELEM(device
->Hrtf_List
, hrtf_id
);
981 if(entry
->hrtf
->sampleRate
== device
->Frequency
)
983 device
->Hrtf
= entry
->hrtf
;
984 al_string_copy(&device
->Hrtf_Name
, entry
->name
);
988 for(i
= 0;!device
->Hrtf
&& i
< VECTOR_SIZE(device
->Hrtf_List
);i
++)
990 const HrtfEntry
*entry
= &VECTOR_ELEM(device
->Hrtf_List
, i
);
991 if(entry
->hrtf
->sampleRate
== device
->Frequency
)
993 device
->Hrtf
= entry
->hrtf
;
994 al_string_copy(&device
->Hrtf_Name
, entry
->name
);
1000 device
->Render_Mode
= HrtfRender
;
1001 if(ConfigValueStr(al_string_get_cstr(device
->DeviceName
), NULL
, "hrtf-mode", &mode
))
1003 if(strcasecmp(mode
, "full") == 0)
1004 device
->Render_Mode
= HrtfRender
;
1005 else if(strcasecmp(mode
, "basic") == 0)
1006 device
->Render_Mode
= NormalRender
;
1008 ERR("Unexpected hrtf-mode: %s\n", mode
);
1011 TRACE("HRTF enabled, \"%s\"\n", al_string_get_cstr(device
->Hrtf_Name
));
1012 InitHrtfPanning(device
);
1015 device
->Hrtf_Status
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
1018 TRACE("HRTF disabled\n");
1020 bs2blevel
= ((headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
1021 (hrtf_appreq
== Hrtf_Enable
)) ? 5 : 0;
1022 if(device
->Type
!= Loopback
)
1023 ConfigValueInt(al_string_get_cstr(device
->DeviceName
), NULL
, "cf_level", &bs2blevel
);
1024 if(bs2blevel
> 0 && bs2blevel
<= 6)
1026 device
->Bs2b
= al_calloc(16, sizeof(*device
->Bs2b
));
1027 bs2b_set_params(device
->Bs2b
, bs2blevel
, device
->Frequency
);
1028 device
->Render_Mode
= StereoPair
;
1029 TRACE("BS2B enabled\n");
1030 InitPanning(device
);
1034 TRACE("BS2B disabled\n");
1036 device
->Render_Mode
= NormalRender
;
1037 if(ConfigValueStr(al_string_get_cstr(device
->DeviceName
), NULL
, "stereo-panning", &mode
))
1039 if(strcasecmp(mode
, "paired") == 0)
1040 device
->Render_Mode
= StereoPair
;
1041 else if(strcasecmp(mode
, "uhj") != 0)
1042 ERR("Unexpected stereo-panning: %s\n", mode
);
1044 if(device
->Render_Mode
== NormalRender
)
1046 device
->Uhj_Encoder
= al_calloc(16, sizeof(Uhj2Encoder
));
1047 TRACE("UHJ enabled\n");
1048 InitUhjPanning(device
);
1052 TRACE("UHJ disabled\n");
1053 InitPanning(device
);
1057 void aluInitEffectPanning(ALeffectslot
*slot
)
1061 memset(slot
->ChanMap
, 0, sizeof(slot
->ChanMap
));
1062 slot
->NumChannels
= 0;
1064 for(i
= 0;i
< MAX_EFFECT_CHANNELS
;i
++)
1066 slot
->ChanMap
[i
].Scale
= 1.0f
;
1067 slot
->ChanMap
[i
].Index
= i
;
1069 slot
->NumChannels
= i
;