Add some helper wrappers to mmap files
[openal-soft.git] / Alc / panning.c
blob172861da70d5460e99c48fe66e182337ce7058cb
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 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] = {
49 0, /* W */
50 3, /* X */
51 1, /* Y */
52 2, /* Z */
53 6, /* R */
54 7, /* S */
55 5, /* T */
56 8, /* U */
57 4, /* V */
58 12, /* K */
59 13, /* L */
60 11, /* M */
61 14, /* N */
62 10, /* O */
63 15, /* P */
64 9, /* Q */
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. */
119 ALfloat x = -dir[2];
120 ALfloat y = -dir[0];
121 ALfloat z = dir[1];
123 /* Zeroth-order */
124 coeffs[0] = 1.0f; /* ACN 0 = 1 */
125 /* First-order */
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 */
129 /* Second-order */
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) */
135 /* Third-order */
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) */
144 if(spread > 0.0f)
146 /* Implement the spread by using a spherical source that subtends the
147 * angle spread. See:
148 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
150 * When adjusted for N3D normalization instead of SN3D, these
151 * calculations are:
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:
164 * ZH0 = 1.0f;
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);
178 /* Zeroth-order */
179 coeffs[0] *= ZH0_norm;
180 /* First-order */
181 coeffs[1] *= ZH1_norm;
182 coeffs[2] *= ZH1_norm;
183 coeffs[3] *= ZH1_norm;
184 /* Second-order */
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;
190 /* Third-order */
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])
203 ALfloat dir[3] = {
204 sinf(azimuth) * cosf(elevation),
205 sinf(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])
214 ALuint i;
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++)
224 gains[i] = 0.0f;
227 void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALuint numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
229 ALfloat gain = 0.0f;
230 ALuint i;
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++)
239 gains[i] = 0.0f;
242 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALuint numchans, ALuint numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
244 ALuint i, j;
246 for(i = 0;i < numchans;i++)
248 float gain = 0.0f;
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++)
254 gains[i] = 0.0f;
257 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALuint numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
259 ALuint i;
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++)
264 gains[i] = 0.0f;
267 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALuint numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
269 ALuint i, j;
271 for(i = 0;i < numchans;i++)
273 float gain = 0.0f;
274 for(j = 0;j < 4;j++)
275 gain += chancoeffs[i][j] * mtx[j];
276 gains[i] = gain * ingain;
278 for(;i < MAX_OUTPUT_CHANNELS;i++)
279 gains[i] = 0.0f;
282 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALuint numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
284 ALuint i;
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++)
289 gains[i] = 0.0f;
293 DECL_CONST static inline const char *GetLabelFromChannel(enum Channel channel)
295 switch(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;
335 return "(unknown)";
339 typedef struct ChannelMap {
340 enum Channel ChanName;
341 ChannelConfig Config;
342 } ChannelMap;
344 static void SetChannelMap(const enum Channel *devchans, ChannelConfig *ambicoeffs,
345 const ChannelMap *chanmap, size_t count, ALuint *outcount,
346 ALboolean isfuma)
348 const ALuint *acnmap = isfuma ? FuMa2ACN : ACN2ACN;
349 const ALfloat *n3dscale = isfuma ? FuMa2N3DScale : UnitScale;
350 size_t j, k;
351 ALuint i;
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;
359 continue;
362 for(j = 0;j < count;j++)
364 if(devchans[i] != chanmap[j].ChanName)
365 continue;
367 for(k = 0;k < MAX_AMBI_COEFFS;++k)
369 ALuint acn = acnmap[k];
370 ambicoeffs[i][acn] = chanmap[j].Config[k] / n3dscale[acn];
372 break;
374 if(j == count)
375 ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans[i]), i);
377 *outcount = i;
380 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALuint speakermap[MAX_OUTPUT_CHANNELS])
382 ALuint i;
384 for(i = 0;i < conf->NumSpeakers;i++)
386 int c = -1;
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:
393 * LF = Front left
394 * RF = Front right
395 * LS = Side left
396 * RS = Side right
397 * LB = Back left
398 * RB = Back right
399 * CE = Front center
400 * CB = Back center
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,
406 * and vice-versa.
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);
418 else
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);
425 else
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);
432 else
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);
439 else
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);
444 else
446 const char *name = al_string_get_cstr(conf->Speakers[i].Name);
447 unsigned int n;
448 char ch;
450 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
451 c = GetChannelIdxByName(device->RealOut, Aux0+n);
452 else
454 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
455 return false;
458 if(c == -1)
460 ERR("Failed to lookup AmbDec speaker label %s\n",
461 al_string_get_cstr(conf->Speakers[i].Name));
462 return false;
464 speakermap[i] = c;
467 return true;
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 } },
477 }, StereoCfg[2] = {
478 { FrontLeft, { 0.707106781f, 0.0f, 0.5f, 0.0f } },
479 { FrontRight, { 0.707106781f, 0.0f, -0.5f, 0.0f } },
480 }, QuadCfg[4] = {
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 } },
485 }, X51SideCfg[5] = {
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 } },
491 }, X51RearCfg[5] = {
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 } },
497 }, X61Cfg[6] = {
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 } },
504 }, X71Cfg[7] = {
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;
518 ALfloat ambiscale;
519 size_t count = 0;
520 ALuint i, j;
522 ambiscale = 1.0f;
523 switch(device->FmtChans)
525 case DevFmtMono:
526 count = COUNTOF(MonoCfg);
527 chanmap = MonoCfg;
528 ambiscale = ZERO_ORDER_SCALE;
529 coeffcount = 1;
530 break;
532 case DevFmtStereo:
533 count = COUNTOF(StereoCfg);
534 chanmap = StereoCfg;
535 ambiscale = FIRST_ORDER_SCALE;
536 coeffcount = 4;
537 break;
539 case DevFmtQuad:
540 count = COUNTOF(QuadCfg);
541 chanmap = QuadCfg;
542 ambiscale = SECOND_ORDER_SCALE;
543 coeffcount = 9;
544 break;
546 case DevFmtX51:
547 count = COUNTOF(X51SideCfg);
548 chanmap = X51SideCfg;
549 ambiscale = SECOND_ORDER_SCALE;
550 coeffcount = 9;
551 break;
553 case DevFmtX51Rear:
554 count = COUNTOF(X51RearCfg);
555 chanmap = X51RearCfg;
556 ambiscale = SECOND_ORDER_SCALE;
557 coeffcount = 9;
558 break;
560 case DevFmtX61:
561 count = COUNTOF(X61Cfg);
562 chanmap = X61Cfg;
563 ambiscale = THIRD_ORDER_SCALE;
564 coeffcount = 16;
565 break;
567 case DevFmtX71:
568 count = COUNTOF(X71Cfg);
569 chanmap = X71Cfg;
570 ambiscale = THIRD_ORDER_SCALE;
571 coeffcount = 16;
572 break;
574 case DevFmtAmbi1:
575 case DevFmtAmbi2:
576 case DevFmtAmbi3:
577 break;
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;
604 else
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));
610 for(i = 0;i < 4;i++)
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);
620 else
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];
630 for(j = 1;j < 4;j++)
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;
642 ALuint i, j;
644 if(conf->FreqBands != 1)
645 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
646 conf->XOverFreq);
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;
654 else
655 ambiscale = 0.0f;
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];
665 ALfloat gain;
666 ALuint k = 0;
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];
692 for(j = 1;j < 4;j++)
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])
700 const char *devname;
701 int decflags = 0;
702 size_t count;
703 ALuint i;
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;
719 else
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;
747 else
749 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
750 for(i = 0;i < 4;i++)
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)
761 size_t count = 4;
762 ALuint i;
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)
786 size_t count = 3;
787 ALuint i;
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)
804 const char *mode;
805 bool headphones;
806 int bs2blevel;
807 size_t i;
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;
826 ambdec_init(&conf);
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. */
837 case DevFmtMono:
838 case DevFmtStereo:
839 case DevFmtAmbi1:
840 case DevFmtAmbi2:
841 case DevFmtAmbi3:
842 break;
844 if(layout)
846 const char *fname;
847 if(ConfigValueStr(devname, "decoder", layout, &fname))
849 if(!ambdec_load(&conf, fname))
850 ERR("Failed to load layout file %s\n", fname);
851 else
853 if(conf.ChanMask > 0xffff)
854 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
855 else
857 if(MakeSpeakerMap(device, &conf, speakermap))
858 pconf = &conf;
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();
871 else
873 bformatdec_free(device->AmbiDecoder);
874 device->AmbiDecoder = NULL;
875 if(device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
877 if(!device->AmbiUp)
878 device->AmbiUp = ambiup_alloc();
880 else
882 ambiup_free(device->AmbiUp);
883 device->AmbiUp = NULL;
887 if(!pconf)
888 InitPanning(device);
889 else if(device->AmbiDecoder)
890 InitHQPanning(device, pconf, speakermap);
891 else
892 InitCustomPanning(device, pconf, speakermap);
894 ambdec_deinit(&conf);
895 return;
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)
906 const char *mode;
907 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
909 if(strcasecmp(mode, "headphones") == 0)
910 headphones = true;
911 else if(strcasecmp(mode, "speakers") == 0)
912 headphones = false;
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;
928 else
930 if(hrtf_userreq != Hrtf_Enable)
932 if(hrtf_appreq == Hrtf_Enable)
933 device->Hrtf.Status = ALC_HRTF_DENIED_SOFT;
934 goto no_hrtf;
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;
974 else
975 ERR("Unexpected hrtf-mode: %s\n", mode);
978 TRACE("HRTF enabled, \"%s\"\n", al_string_get_cstr(device->Hrtf.Name));
979 InitHrtfPanning(device);
980 return;
982 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
984 no_hrtf:
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");
997 InitPanning(device);
998 return;
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);
1016 return;
1019 TRACE("UHJ disabled\n");
1020 InitPanning(device);
1024 void aluInitEffectPanning(ALeffectslot *slot)
1026 ALuint i;
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;