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
,
348 const ALuint
*acnmap
= isfuma
? FuMa2ACN
: ACN2ACN
;
349 const ALfloat
*n3dscale
= isfuma
? FuMa2N3DScale
: UnitScale
;
353 for(i
= 0;i
< MAX_OUTPUT_CHANNELS
&& devchans
[i
] != InvalidChannel
;i
++)
355 if(devchans
[i
] == LFE
)
357 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
358 ambicoeffs
[i
][j
] = 0.0f
;
362 for(j
= 0;j
< count
;j
++)
364 if(devchans
[i
] != chanmap
[j
].ChanName
)
367 for(k
= 0;k
< MAX_AMBI_COEFFS
;++k
)
369 ALuint acn
= acnmap
[k
];
370 ambicoeffs
[i
][acn
] = chanmap
[j
].Config
[k
] / n3dscale
[acn
];
375 ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans
[i
]), i
);
380 static bool MakeSpeakerMap(ALCdevice
*device
, const AmbDecConf
*conf
, ALuint speakermap
[MAX_OUTPUT_CHANNELS
])
384 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
388 /* NOTE: AmbDec does not define any standard speaker names, however
389 * for this to work we have to by able to find the output channel
390 * the speaker definition corresponds to. Therefore, OpenAL Soft
391 * requires these channel labels to be recognized:
402 * Additionally, surround51 will acknowledge back speakers for side
403 * channels, and surround51rear will acknowledge side speakers for
404 * back channels, to avoid issues with an ambdec expecting 5.1 to
405 * use the side channels when the device is configured for back,
408 if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "LF") == 0)
409 c
= GetChannelIdxByName(device
->RealOut
, FrontLeft
);
410 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "RF") == 0)
411 c
= GetChannelIdxByName(device
->RealOut
, FrontRight
);
412 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "CE") == 0)
413 c
= GetChannelIdxByName(device
->RealOut
, FrontCenter
);
414 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "LS") == 0)
416 if(device
->FmtChans
== DevFmtX51Rear
)
417 c
= GetChannelIdxByName(device
->RealOut
, BackLeft
);
419 c
= GetChannelIdxByName(device
->RealOut
, SideLeft
);
421 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "RS") == 0)
423 if(device
->FmtChans
== DevFmtX51Rear
)
424 c
= GetChannelIdxByName(device
->RealOut
, BackRight
);
426 c
= GetChannelIdxByName(device
->RealOut
, SideRight
);
428 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "LB") == 0)
430 if(device
->FmtChans
== DevFmtX51
)
431 c
= GetChannelIdxByName(device
->RealOut
, SideLeft
);
433 c
= GetChannelIdxByName(device
->RealOut
, BackLeft
);
435 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "RB") == 0)
437 if(device
->FmtChans
== DevFmtX51
)
438 c
= GetChannelIdxByName(device
->RealOut
, SideRight
);
440 c
= GetChannelIdxByName(device
->RealOut
, BackRight
);
442 else if(al_string_cmp_cstr(conf
->Speakers
[i
].Name
, "CB") == 0)
443 c
= GetChannelIdxByName(device
->RealOut
, BackCenter
);
446 const char *name
= al_string_get_cstr(conf
->Speakers
[i
].Name
);
450 if(sscanf(name
, "AUX%u%c", &n
, &ch
) == 1 && n
< 16)
451 c
= GetChannelIdxByName(device
->RealOut
, Aux0
+n
);
454 ERR("AmbDec speaker label \"%s\" not recognized\n", name
);
460 ERR("Failed to lookup AmbDec speaker label %s\n",
461 al_string_get_cstr(conf
->Speakers
[i
].Name
));
471 /* NOTE: These decoder coefficients are using FuMa channel ordering and
472 * normalization, since that's what was produced by the Ambisonic Decoder
473 * Toolbox. SetChannelMap will convert them to N3D.
475 static const ChannelMap MonoCfg
[1] = {
476 { FrontCenter
, { 1.414213562f
} },
478 { FrontLeft
, { 0.707106781f
, 0.0f
, 0.5f
, 0.0f
} },
479 { FrontRight
, { 0.707106781f
, 0.0f
, -0.5f
, 0.0f
} },
481 { FrontLeft
, { 0.353553f
, 0.306186f
, 0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.125000f
} },
482 { FrontRight
, { 0.353553f
, 0.306186f
, -0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, -0.125000f
} },
483 { BackLeft
, { 0.353553f
, -0.306186f
, 0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, -0.125000f
} },
484 { BackRight
, { 0.353553f
, -0.306186f
, -0.306186f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.000000f
, 0.125000f
} },
486 { FrontLeft
, { 0.208954f
, 0.199518f
, 0.223424f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012543f
, 0.144260f
} },
487 { FrontRight
, { 0.208950f
, 0.199514f
, -0.223425f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012544f
, -0.144258f
} },
488 { FrontCenter
, { 0.109403f
, 0.168250f
, -0.000002f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.100431f
, -0.000001f
} },
489 { SideLeft
, { 0.470934f
, -0.346484f
, 0.327504f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022188f
, -0.041113f
} },
490 { SideRight
, { 0.470936f
, -0.346480f
, -0.327507f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022186f
, 0.041114f
} },
492 { FrontLeft
, { 0.208954f
, 0.199518f
, 0.223424f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012543f
, 0.144260f
} },
493 { FrontRight
, { 0.208950f
, 0.199514f
, -0.223425f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.012544f
, -0.144258f
} },
494 { FrontCenter
, { 0.109403f
, 0.168250f
, -0.000002f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.100431f
, -0.000001f
} },
495 { BackLeft
, { 0.470934f
, -0.346484f
, 0.327504f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022188f
, -0.041113f
} },
496 { BackRight
, { 0.470936f
, -0.346480f
, -0.327507f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, -0.022186f
, 0.041114f
} },
498 { 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
} },
499 { 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
} },
500 { 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
} },
501 { 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
} },
502 { 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
} },
503 { 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
} },
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 { 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
} },
509 { 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
} },
510 { 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
} },
511 { 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
} },
514 static void InitPanning(ALCdevice
*device
)
516 const ChannelMap
*chanmap
= NULL
;
517 ALuint coeffcount
= 0;
523 switch(device
->FmtChans
)
526 count
= COUNTOF(MonoCfg
);
528 ambiscale
= ZERO_ORDER_SCALE
;
533 count
= COUNTOF(StereoCfg
);
535 ambiscale
= FIRST_ORDER_SCALE
;
540 count
= COUNTOF(QuadCfg
);
542 ambiscale
= SECOND_ORDER_SCALE
;
547 count
= COUNTOF(X51SideCfg
);
548 chanmap
= X51SideCfg
;
549 ambiscale
= SECOND_ORDER_SCALE
;
554 count
= COUNTOF(X51RearCfg
);
555 chanmap
= X51RearCfg
;
556 ambiscale
= SECOND_ORDER_SCALE
;
561 count
= COUNTOF(X61Cfg
);
563 ambiscale
= THIRD_ORDER_SCALE
;
568 count
= COUNTOF(X71Cfg
);
570 ambiscale
= THIRD_ORDER_SCALE
;
580 if(device
->FmtChans
>= DevFmtAmbi1
&& device
->FmtChans
<= DevFmtAmbi3
)
582 const ALuint
*acnmap
= (device
->AmbiFmt
== AmbiFormat_FuMa
) ? FuMa2ACN
: ACN2ACN
;
583 const ALfloat
*n3dscale
= (device
->AmbiFmt
== AmbiFormat_FuMa
) ? FuMa2N3DScale
:
584 (device
->AmbiFmt
== AmbiFormat_ACN_SN3D
) ? SN3D2N3DScale
:
585 /*(device->AmbiFmt == AmbiFormat_ACN_N3D) ?*/ UnitScale
;
587 count
= (device
->FmtChans
== DevFmtAmbi3
) ? 16 :
588 (device
->FmtChans
== DevFmtAmbi2
) ? 9 :
589 (device
->FmtChans
== DevFmtAmbi1
) ? 4 : 1;
590 for(i
= 0;i
< count
;i
++)
592 ALuint acn
= acnmap
[i
];
593 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
/n3dscale
[acn
];
594 device
->Dry
.Ambi
.Map
[i
].Index
= acn
;
596 device
->Dry
.CoeffCount
= 0;
597 device
->Dry
.NumChannels
= count
;
599 if(device
->FmtChans
== DevFmtAmbi1
)
601 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
602 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
606 /* FOA output is always ACN+N3D for higher-order ambisonic output.
607 * The upsampler expects this and will convert it for output.
609 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
612 device
->FOAOut
.Ambi
.Map
[i
].Scale
= 1.0f
;
613 device
->FOAOut
.Ambi
.Map
[i
].Index
= i
;
615 device
->FOAOut
.CoeffCount
= 0;
617 ambiup_reset(device
->AmbiUp
, device
);
622 SetChannelMap(device
->RealOut
.ChannelName
, device
->Dry
.Ambi
.Coeffs
,
623 chanmap
, count
, &device
->Dry
.NumChannels
, AL_TRUE
);
624 device
->Dry
.CoeffCount
= coeffcount
;
626 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
627 for(i
= 0;i
< device
->Dry
.NumChannels
;i
++)
629 device
->FOAOut
.Ambi
.Coeffs
[i
][0] = device
->Dry
.Ambi
.Coeffs
[i
][0];
631 device
->FOAOut
.Ambi
.Coeffs
[i
][j
] = device
->Dry
.Ambi
.Coeffs
[i
][j
] * ambiscale
;
633 device
->FOAOut
.CoeffCount
= 4;
637 static void InitCustomPanning(ALCdevice
*device
, const AmbDecConf
*conf
, const ALuint speakermap
[MAX_OUTPUT_CHANNELS
])
639 ChannelMap chanmap
[MAX_OUTPUT_CHANNELS
];
640 const ALfloat
*coeff_scale
= UnitScale
;
641 ALfloat ambiscale
= 1.0f
;
644 if(conf
->FreqBands
!= 1)
645 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
648 if(conf
->ChanMask
> 0x1ff)
649 ambiscale
= THIRD_ORDER_SCALE
;
650 else if(conf
->ChanMask
> 0xf)
651 ambiscale
= SECOND_ORDER_SCALE
;
652 else if(conf
->ChanMask
> 0x1)
653 ambiscale
= FIRST_ORDER_SCALE
;
657 if(conf
->CoeffScale
== ADS_SN3D
)
658 coeff_scale
= SN3D2N3DScale
;
659 else if(conf
->CoeffScale
== ADS_FuMa
)
660 coeff_scale
= FuMa2N3DScale
;
662 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
664 ALuint chan
= speakermap
[i
];
668 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
669 chanmap
[i
].Config
[j
] = 0.0f
;
671 chanmap
[i
].ChanName
= device
->RealOut
.ChannelName
[chan
];
672 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
674 if(j
== 0) gain
= conf
->HFOrderGain
[0];
675 else if(j
== 1) gain
= conf
->HFOrderGain
[1];
676 else if(j
== 4) gain
= conf
->HFOrderGain
[2];
677 else if(j
== 9) gain
= conf
->HFOrderGain
[3];
678 if((conf
->ChanMask
&(1<<j
)))
679 chanmap
[i
].Config
[j
] = conf
->HFMatrix
[i
][k
++] / coeff_scale
[j
] * gain
;
683 SetChannelMap(device
->RealOut
.ChannelName
, device
->Dry
.Ambi
.Coeffs
, chanmap
,
684 conf
->NumSpeakers
, &device
->Dry
.NumChannels
, AL_FALSE
);
685 device
->Dry
.CoeffCount
= (conf
->ChanMask
> 0x1ff) ? 16 :
686 (conf
->ChanMask
> 0xf) ? 9 : 4;
688 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
689 for(i
= 0;i
< device
->Dry
.NumChannels
;i
++)
691 device
->FOAOut
.Ambi
.Coeffs
[i
][0] = device
->Dry
.Ambi
.Coeffs
[i
][0];
693 device
->FOAOut
.Ambi
.Coeffs
[i
][j
] = device
->Dry
.Ambi
.Coeffs
[i
][j
] * ambiscale
;
695 device
->FOAOut
.CoeffCount
= 4;
698 static void InitHQPanning(ALCdevice
*device
, const AmbDecConf
*conf
, const ALuint speakermap
[MAX_OUTPUT_CHANNELS
])
705 devname
= al_string_get_cstr(device
->DeviceName
);
706 if(GetConfigValueBool(devname
, "decoder", "distance-comp", 1))
707 decflags
|= BFDF_DistanceComp
;
709 if((conf
->ChanMask
&AMBI_PERIPHONIC_MASK
))
711 count
= (conf
->ChanMask
> 0x1ff) ? 16 :
712 (conf
->ChanMask
> 0xf) ? 9 : 4;
713 for(i
= 0;i
< count
;i
++)
715 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
;
716 device
->Dry
.Ambi
.Map
[i
].Index
= i
;
721 static const int map
[MAX_AMBI2D_COEFFS
] = { 0, 1, 3, 4, 8, 9, 15 };
723 count
= (conf
->ChanMask
> 0x1ff) ? 7 :
724 (conf
->ChanMask
> 0xf) ? 5 : 3;
725 for(i
= 0;i
< count
;i
++)
727 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
;
728 device
->Dry
.Ambi
.Map
[i
].Index
= map
[i
];
731 device
->Dry
.CoeffCount
= 0;
732 device
->Dry
.NumChannels
= count
;
734 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
735 (conf
->FreqBands
== 1) ? "single" : "dual",
736 (conf
->ChanMask
> 0xf) ? (conf
->ChanMask
> 0x1ff) ? "third" : "second" : "first",
737 (conf
->ChanMask
&AMBI_PERIPHONIC_MASK
) ? " periphonic" : ""
739 bformatdec_reset(device
->AmbiDecoder
, conf
, count
, device
->Frequency
,
740 speakermap
, decflags
);
742 if(bformatdec_getOrder(device
->AmbiDecoder
) < 2)
744 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
745 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
749 memset(&device
->FOAOut
.Ambi
, 0, sizeof(device
->FOAOut
.Ambi
));
752 device
->FOAOut
.Ambi
.Map
[i
].Scale
= 1.0f
;
753 device
->FOAOut
.Ambi
.Map
[i
].Index
= i
;
755 device
->FOAOut
.CoeffCount
= 0;
759 static void InitHrtfPanning(ALCdevice
*device
)
764 for(i
= 0;i
< count
;i
++)
766 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
;
767 device
->Dry
.Ambi
.Map
[i
].Index
= i
;
769 device
->Dry
.CoeffCount
= 0;
770 device
->Dry
.NumChannels
= count
;
772 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
773 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
775 memset(device
->Hrtf
.Coeffs
, 0, sizeof(device
->Hrtf
.Coeffs
));
776 device
->Hrtf
.IrSize
= BuildBFormatHrtf(device
->Hrtf
.Handle
,
777 device
->Hrtf
.Coeffs
, device
->Dry
.NumChannels
780 /* Round up to the nearest multiple of 8 */
781 device
->Hrtf
.IrSize
= (device
->Hrtf
.IrSize
+7)&~7;
784 static void InitUhjPanning(ALCdevice
*device
)
789 for(i
= 0;i
< count
;i
++)
791 ALuint acn
= FuMa2ACN
[i
];
792 device
->Dry
.Ambi
.Map
[i
].Scale
= 1.0f
/FuMa2N3DScale
[acn
];
793 device
->Dry
.Ambi
.Map
[i
].Index
= acn
;
795 device
->Dry
.CoeffCount
= 0;
796 device
->Dry
.NumChannels
= count
;
798 device
->FOAOut
.Ambi
= device
->Dry
.Ambi
;
799 device
->FOAOut
.CoeffCount
= device
->Dry
.CoeffCount
;
802 void aluInitRenderer(ALCdevice
*device
, ALint hrtf_id
, enum HrtfRequestMode hrtf_appreq
, enum HrtfRequestMode hrtf_userreq
)
809 device
->Hrtf
.Handle
= NULL
;
810 al_string_clear(&device
->Hrtf
.Name
);
811 device
->Render_Mode
= NormalRender
;
813 memset(&device
->Dry
.Ambi
, 0, sizeof(device
->Dry
.Ambi
));
814 device
->Dry
.CoeffCount
= 0;
815 device
->Dry
.NumChannels
= 0;
817 if(device
->FmtChans
!= DevFmtStereo
)
819 ALuint speakermap
[MAX_OUTPUT_CHANNELS
];
820 const char *devname
, *layout
= NULL
;
821 AmbDecConf conf
, *pconf
= NULL
;
823 if(hrtf_appreq
== Hrtf_Enable
)
824 device
->Hrtf
.Status
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
828 devname
= al_string_get_cstr(device
->DeviceName
);
829 switch(device
->FmtChans
)
831 case DevFmtQuad
: layout
= "quad"; break;
832 case DevFmtX51
: layout
= "surround51"; break;
833 case DevFmtX51Rear
: layout
= "surround51rear"; break;
834 case DevFmtX61
: layout
= "surround61"; break;
835 case DevFmtX71
: layout
= "surround71"; break;
836 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
847 if(ConfigValueStr(devname
, "decoder", layout
, &fname
))
849 if(!ambdec_load(&conf
, fname
))
850 ERR("Failed to load layout file %s\n", fname
);
853 if(conf
.ChanMask
> 0xffff)
854 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf
.ChanMask
);
857 if(MakeSpeakerMap(device
, &conf
, speakermap
))
864 if(pconf
&& GetConfigValueBool(devname
, "decoder", "hq-mode", 0))
866 ambiup_free(device
->AmbiUp
);
867 device
->AmbiUp
= NULL
;
868 if(!device
->AmbiDecoder
)
869 device
->AmbiDecoder
= bformatdec_alloc();
873 bformatdec_free(device
->AmbiDecoder
);
874 device
->AmbiDecoder
= NULL
;
875 if(device
->FmtChans
> DevFmtAmbi1
&& device
->FmtChans
<= DevFmtAmbi3
)
878 device
->AmbiUp
= ambiup_alloc();
882 ambiup_free(device
->AmbiUp
);
883 device
->AmbiUp
= NULL
;
889 else if(device
->AmbiDecoder
)
890 InitHQPanning(device
, pconf
, speakermap
);
892 InitCustomPanning(device
, pconf
, speakermap
);
894 ambdec_deinit(&conf
);
898 ambiup_free(device
->AmbiUp
);
899 device
->AmbiUp
= NULL
;
900 bformatdec_free(device
->AmbiDecoder
);
901 device
->AmbiDecoder
= NULL
;
903 headphones
= device
->IsHeadphones
;
904 if(device
->Type
!= Loopback
)
907 if(ConfigValueStr(al_string_get_cstr(device
->DeviceName
), NULL
, "stereo-mode", &mode
))
909 if(strcasecmp(mode
, "headphones") == 0)
911 else if(strcasecmp(mode
, "speakers") == 0)
913 else if(strcasecmp(mode
, "auto") != 0)
914 ERR("Unexpected stereo-mode: %s\n", mode
);
918 if(hrtf_userreq
== Hrtf_Default
)
920 bool usehrtf
= (headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
921 (hrtf_appreq
== Hrtf_Enable
);
922 if(!usehrtf
) goto no_hrtf
;
924 device
->Hrtf
.Status
= ALC_HRTF_ENABLED_SOFT
;
925 if(headphones
&& hrtf_appreq
!= Hrtf_Disable
)
926 device
->Hrtf
.Status
= ALC_HRTF_HEADPHONES_DETECTED_SOFT
;
930 if(hrtf_userreq
!= Hrtf_Enable
)
932 if(hrtf_appreq
== Hrtf_Enable
)
933 device
->Hrtf
.Status
= ALC_HRTF_DENIED_SOFT
;
936 device
->Hrtf
.Status
= ALC_HRTF_REQUIRED_SOFT
;
939 if(VECTOR_SIZE(device
->Hrtf
.List
) == 0)
941 VECTOR_DEINIT(device
->Hrtf
.List
);
942 device
->Hrtf
.List
= EnumerateHrtf(device
->DeviceName
);
945 if(hrtf_id
>= 0 && (size_t)hrtf_id
< VECTOR_SIZE(device
->Hrtf
.List
))
947 const HrtfEntry
*entry
= &VECTOR_ELEM(device
->Hrtf
.List
, hrtf_id
);
948 if(entry
->hrtf
->sampleRate
== device
->Frequency
)
950 device
->Hrtf
.Handle
= entry
->hrtf
;
951 al_string_copy(&device
->Hrtf
.Name
, entry
->name
);
955 for(i
= 0;!device
->Hrtf
.Handle
&& i
< VECTOR_SIZE(device
->Hrtf
.List
);i
++)
957 const HrtfEntry
*entry
= &VECTOR_ELEM(device
->Hrtf
.List
, i
);
958 if(entry
->hrtf
->sampleRate
== device
->Frequency
)
960 device
->Hrtf
.Handle
= entry
->hrtf
;
961 al_string_copy(&device
->Hrtf
.Name
, entry
->name
);
965 if(device
->Hrtf
.Handle
)
967 device
->Render_Mode
= HrtfRender
;
968 if(ConfigValueStr(al_string_get_cstr(device
->DeviceName
), NULL
, "hrtf-mode", &mode
))
970 if(strcasecmp(mode
, "full") == 0)
971 device
->Render_Mode
= HrtfRender
;
972 else if(strcasecmp(mode
, "basic") == 0)
973 device
->Render_Mode
= NormalRender
;
975 ERR("Unexpected hrtf-mode: %s\n", mode
);
978 TRACE("HRTF enabled, \"%s\"\n", al_string_get_cstr(device
->Hrtf
.Name
));
979 InitHrtfPanning(device
);
982 device
->Hrtf
.Status
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
985 TRACE("HRTF disabled\n");
987 bs2blevel
= ((headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
988 (hrtf_appreq
== Hrtf_Enable
)) ? 5 : 0;
989 if(device
->Type
!= Loopback
)
990 ConfigValueInt(al_string_get_cstr(device
->DeviceName
), NULL
, "cf_level", &bs2blevel
);
991 if(bs2blevel
> 0 && bs2blevel
<= 6)
993 device
->Bs2b
= al_calloc(16, sizeof(*device
->Bs2b
));
994 bs2b_set_params(device
->Bs2b
, bs2blevel
, device
->Frequency
);
995 device
->Render_Mode
= StereoPair
;
996 TRACE("BS2B enabled\n");
1001 TRACE("BS2B disabled\n");
1003 device
->Render_Mode
= NormalRender
;
1004 if(ConfigValueStr(al_string_get_cstr(device
->DeviceName
), NULL
, "stereo-panning", &mode
))
1006 if(strcasecmp(mode
, "paired") == 0)
1007 device
->Render_Mode
= StereoPair
;
1008 else if(strcasecmp(mode
, "uhj") != 0)
1009 ERR("Unexpected stereo-panning: %s\n", mode
);
1011 if(device
->Render_Mode
== NormalRender
)
1013 device
->Uhj_Encoder
= al_calloc(16, sizeof(Uhj2Encoder
));
1014 TRACE("UHJ enabled\n");
1015 InitUhjPanning(device
);
1019 TRACE("UHJ disabled\n");
1020 InitPanning(device
);
1024 void aluInitEffectPanning(ALeffectslot
*slot
)
1028 memset(slot
->ChanMap
, 0, sizeof(slot
->ChanMap
));
1029 slot
->NumChannels
= 0;
1031 for(i
= 0;i
< MAX_EFFECT_CHANNELS
;i
++)
1033 slot
->ChanMap
[i
].Scale
= 1.0f
;
1034 slot
->ChanMap
[i
].Index
= i
;
1036 slot
->NumChannels
= i
;