Avoid duplicating some scale tables
[openal-soft.git] / Alc / panning.c
blobc8a245da83a3299ff446597653c1bdbd2f4a1ce2
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 "alconfig.h"
33 #include "bool.h"
34 #include "ambdec.h"
35 #include "bformatdec.h"
36 #include "uhjfilter.h"
37 #include "bs2b.h"
40 extern inline void CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
41 extern inline void ComputeAmbientGains(const DryMixParams *dry, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
42 extern inline void ComputeDryPanGains(const DryMixParams *dry, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
43 extern inline void ComputeFirstOrderGains(const BFMixParams *foa, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
46 static const ALsizei FuMa2ACN[MAX_AMBI_COEFFS] = {
47 0, /* W */
48 3, /* X */
49 1, /* Y */
50 2, /* Z */
51 6, /* R */
52 7, /* S */
53 5, /* T */
54 8, /* U */
55 4, /* V */
56 12, /* K */
57 13, /* L */
58 11, /* M */
59 14, /* N */
60 10, /* O */
61 15, /* P */
62 9, /* Q */
64 static const ALsizei ACN2ACN[MAX_AMBI_COEFFS] = {
65 0, 1, 2, 3, 4, 5, 6, 7,
66 8, 9, 10, 11, 12, 13, 14, 15
70 void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
72 /* Convert from OpenAL coords to Ambisonics. */
73 ALfloat x = -dir[2];
74 ALfloat y = -dir[0];
75 ALfloat z = dir[1];
77 /* Zeroth-order */
78 coeffs[0] = 1.0f; /* ACN 0 = 1 */
79 /* First-order */
80 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
81 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
82 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
83 /* Second-order */
84 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
85 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
86 coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
87 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
88 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
89 /* Third-order */
90 coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
91 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
92 coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
93 coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
94 coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
95 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
96 coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
98 if(spread > 0.0f)
100 /* Implement the spread by using a spherical source that subtends the
101 * angle spread. See:
102 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
104 * When adjusted for N3D normalization instead of SN3D, these
105 * calculations are:
107 * ZH0 = -sqrt(pi) * (-1+ca);
108 * ZH1 = 0.5*sqrt(pi) * sa*sa;
109 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
110 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
111 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
112 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
114 * The gain of the source is compensated for size, so that the
115 * loundness doesn't depend on the spread. Thus:
117 * ZH0 = 1.0f;
118 * ZH1 = 0.5f * (ca+1.0f);
119 * ZH2 = 0.5f * (ca+1.0f)*ca;
120 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
121 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
122 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
124 ALfloat ca = cosf(spread * 0.5f);
125 /* Increase the source volume by up to +3dB for a full spread. */
126 ALfloat scale = sqrtf(1.0f + spread/F_TAU);
128 ALfloat ZH0_norm = scale;
129 ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
130 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
131 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
133 /* Zeroth-order */
134 coeffs[0] *= ZH0_norm;
135 /* First-order */
136 coeffs[1] *= ZH1_norm;
137 coeffs[2] *= ZH1_norm;
138 coeffs[3] *= ZH1_norm;
139 /* Second-order */
140 coeffs[4] *= ZH2_norm;
141 coeffs[5] *= ZH2_norm;
142 coeffs[6] *= ZH2_norm;
143 coeffs[7] *= ZH2_norm;
144 coeffs[8] *= ZH2_norm;
145 /* Third-order */
146 coeffs[9] *= ZH3_norm;
147 coeffs[10] *= ZH3_norm;
148 coeffs[11] *= ZH3_norm;
149 coeffs[12] *= ZH3_norm;
150 coeffs[13] *= ZH3_norm;
151 coeffs[14] *= ZH3_norm;
152 coeffs[15] *= ZH3_norm;
156 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
158 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
159 if(!(fabsf(azimuth) > F_PI_2))
160 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
161 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
165 void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
167 ALsizei i;
169 for(i = 0;i < numchans;i++)
170 gains[i] = chancoeffs[i][0] * 1.414213562f * ingain;
171 for(;i < MAX_OUTPUT_CHANNELS;i++)
172 gains[i] = 0.0f;
175 void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
177 ALfloat gain = 0.0f;
178 ALsizei i;
180 for(i = 0;i < numchans;i++)
182 if(chanmap[i].Index == 0)
183 gain += chanmap[i].Scale;
185 gains[0] = gain * 1.414213562f * ingain;
186 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
187 gains[i] = 0.0f;
190 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
192 ALsizei i, j;
194 for(i = 0;i < numchans;i++)
196 float gain = 0.0f;
197 for(j = 0;j < numcoeffs;j++)
198 gain += chancoeffs[i][j]*coeffs[j];
199 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
201 for(;i < MAX_OUTPUT_CHANNELS;i++)
202 gains[i] = 0.0f;
205 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
207 ALsizei i;
209 for(i = 0;i < numchans;i++)
210 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
211 for(;i < MAX_OUTPUT_CHANNELS;i++)
212 gains[i] = 0.0f;
215 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
217 ALsizei i, j;
219 for(i = 0;i < numchans;i++)
221 float gain = 0.0f;
222 for(j = 0;j < 4;j++)
223 gain += chancoeffs[i][j] * mtx[j];
224 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
226 for(;i < MAX_OUTPUT_CHANNELS;i++)
227 gains[i] = 0.0f;
230 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
232 ALsizei i;
234 for(i = 0;i < numchans;i++)
235 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
236 for(;i < MAX_OUTPUT_CHANNELS;i++)
237 gains[i] = 0.0f;
241 static inline const char *GetLabelFromChannel(enum Channel channel)
243 switch(channel)
245 case FrontLeft: return "front-left";
246 case FrontRight: return "front-right";
247 case FrontCenter: return "front-center";
248 case LFE: return "lfe";
249 case BackLeft: return "back-left";
250 case BackRight: return "back-right";
251 case BackCenter: return "back-center";
252 case SideLeft: return "side-left";
253 case SideRight: return "side-right";
255 case UpperFrontLeft: return "upper-front-left";
256 case UpperFrontRight: return "upper-front-right";
257 case UpperBackLeft: return "upper-back-left";
258 case UpperBackRight: return "upper-back-right";
259 case LowerFrontLeft: return "lower-front-left";
260 case LowerFrontRight: return "lower-front-right";
261 case LowerBackLeft: return "lower-back-left";
262 case LowerBackRight: return "lower-back-right";
264 case Aux0: return "aux-0";
265 case Aux1: return "aux-1";
266 case Aux2: return "aux-2";
267 case Aux3: return "aux-3";
268 case Aux4: return "aux-4";
269 case Aux5: return "aux-5";
270 case Aux6: return "aux-6";
271 case Aux7: return "aux-7";
272 case Aux8: return "aux-8";
273 case Aux9: return "aux-9";
274 case Aux10: return "aux-10";
275 case Aux11: return "aux-11";
276 case Aux12: return "aux-12";
277 case Aux13: return "aux-13";
278 case Aux14: return "aux-14";
279 case Aux15: return "aux-15";
281 case InvalidChannel: break;
283 return "(unknown)";
287 typedef struct ChannelMap {
288 enum Channel ChanName;
289 ChannelConfig Config;
290 } ChannelMap;
292 static void SetChannelMap(const enum Channel devchans[MAX_OUTPUT_CHANNELS],
293 ChannelConfig *ambicoeffs, const ChannelMap *chanmap,
294 ALsizei count, ALsizei *outcount)
296 ALsizei maxchans = 0;
297 ALsizei i, j;
299 for(i = 0;i < count;i++)
301 ALint idx = GetChannelIndex(devchans, chanmap[i].ChanName);
302 if(idx < 0)
304 ERR("Failed to find %s channel in device\n",
305 GetLabelFromChannel(chanmap[i].ChanName));
306 continue;
309 maxchans = maxi(maxchans, idx+1);
310 for(j = 0;j < MAX_AMBI_COEFFS;j++)
311 ambicoeffs[idx][j] = chanmap[i].Config[j];
313 *outcount = mini(maxchans, MAX_OUTPUT_CHANNELS);
316 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
318 ALsizei i;
320 for(i = 0;i < conf->NumSpeakers;i++)
322 int c = -1;
324 /* NOTE: AmbDec does not define any standard speaker names, however
325 * for this to work we have to by able to find the output channel
326 * the speaker definition corresponds to. Therefore, OpenAL Soft
327 * requires these channel labels to be recognized:
329 * LF = Front left
330 * RF = Front right
331 * LS = Side left
332 * RS = Side right
333 * LB = Back left
334 * RB = Back right
335 * CE = Front center
336 * CB = Back center
338 * Additionally, surround51 will acknowledge back speakers for side
339 * channels, and surround51rear will acknowledge side speakers for
340 * back channels, to avoid issues with an ambdec expecting 5.1 to
341 * use the side channels when the device is configured for back,
342 * and vice-versa.
344 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
345 c = GetChannelIdxByName(&device->RealOut, FrontLeft);
346 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
347 c = GetChannelIdxByName(&device->RealOut, FrontRight);
348 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
349 c = GetChannelIdxByName(&device->RealOut, FrontCenter);
350 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
352 if(device->FmtChans == DevFmtX51Rear)
353 c = GetChannelIdxByName(&device->RealOut, BackLeft);
354 else
355 c = GetChannelIdxByName(&device->RealOut, SideLeft);
357 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
359 if(device->FmtChans == DevFmtX51Rear)
360 c = GetChannelIdxByName(&device->RealOut, BackRight);
361 else
362 c = GetChannelIdxByName(&device->RealOut, SideRight);
364 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
366 if(device->FmtChans == DevFmtX51)
367 c = GetChannelIdxByName(&device->RealOut, SideLeft);
368 else
369 c = GetChannelIdxByName(&device->RealOut, BackLeft);
371 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
373 if(device->FmtChans == DevFmtX51)
374 c = GetChannelIdxByName(&device->RealOut, SideRight);
375 else
376 c = GetChannelIdxByName(&device->RealOut, BackRight);
378 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
379 c = GetChannelIdxByName(&device->RealOut, BackCenter);
380 else
382 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
383 unsigned int n;
384 char ch;
386 if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
387 c = GetChannelIdxByName(&device->RealOut, Aux0+n);
388 else
390 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
391 return false;
394 if(c == -1)
396 ERR("Failed to lookup AmbDec speaker label %s\n",
397 alstr_get_cstr(conf->Speakers[i].Name));
398 return false;
400 speakermap[i] = c;
403 return true;
407 static const ChannelMap MonoCfg[1] = {
408 { FrontCenter, { 1.0f } },
409 }, StereoCfg[2] = {
410 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
411 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
412 }, QuadCfg[4] = {
413 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
414 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
415 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
416 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
417 }, X51SideCfg[4] = {
418 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
419 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
420 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
421 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
422 }, X51RearCfg[4] = {
423 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
424 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
425 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
426 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
427 }, X61Cfg[6] = {
428 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, 0.0f, -4.39996780e-2f, -2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
429 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 0.0f, 1.59658796e-1f, 6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
430 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 0.0f, 1.59658796e-1f, -6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
431 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, 0.0f, -4.39996780e-2f, 2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
432 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
433 }, X71Cfg[6] = {
434 { BackLeft, { 2.04124145e-1f, 1.08880247e-1f, 0.0f, -1.88586120e-1f, -1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, 3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
435 { SideLeft, { 2.04124145e-1f, 2.17760495e-1f, 0.0f, 0.00000000e+0f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, -1.49071198e-1f, -3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
436 { FrontLeft, { 2.04124145e-1f, 1.08880247e-1f, 0.0f, 1.88586120e-1f, 1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, 3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
437 { FrontRight, { 2.04124145e-1f, -1.08880247e-1f, 0.0f, 1.88586120e-1f, -1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, -3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
438 { SideRight, { 2.04124145e-1f, -2.17760495e-1f, 0.0f, 0.00000000e+0f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, -1.49071198e-1f, 3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
439 { BackRight, { 2.04124145e-1f, -1.08880247e-1f, 0.0f, -1.88586120e-1f, 1.29099444e-1f, 0.0f, 0.0f, 0.0f, 7.45355993e-2f, -3.73460789e-2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00000000e+0f } },
442 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
444 const char *devname = alstr_get_cstr(device->DeviceName);
445 ALsizei i;
447 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
449 /* NFC is only used when AvgSpeakerDist is greater than 0, and
450 * METERS_PER_UNIT is also greater than 0. In addition, NFC can only be
451 * used when rendering to an ambisonic buffer.
453 device->AvgSpeakerDist = ctrl_dist;
455 device->Dry.NumChannelsPerOrder[0] = 1;
456 if(periphonic)
457 for(i = 1;i < order+1;i++)
458 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
459 else
460 for(i = 1;i < order+1;i++)
461 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
462 for(;i < MAX_AMBI_ORDER+1;i++)
463 device->Dry.NumChannelsPerOrder[i] = 0;
467 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
469 const char *devname = alstr_get_cstr(device->DeviceName);
470 ALfloat maxdist = 0.0f;
471 size_t total = 0;
472 ALsizei i;
474 for(i = 0;i < conf->NumSpeakers;i++)
475 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
477 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
479 ALfloat srate = (ALfloat)device->Frequency;
480 for(i = 0;i < conf->NumSpeakers;i++)
482 ALsizei chan = speakermap[i];
483 ALfloat delay;
485 /* Distance compensation only delays in steps of the sample rate.
486 * This is a bit less accurate since the delay time falls to the
487 * nearest sample time, but it's far simpler as it doesn't have to
488 * deal with phase offsets. This means at 48khz, for instance, the
489 * distance delay will be in steps of about 7 millimeters.
491 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
492 srate + 0.5f);
493 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
494 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
495 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
497 device->ChannelDelay[chan].Length = (ALsizei)clampf(
498 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
500 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
501 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
502 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
503 device->ChannelDelay[chan].Gain
506 /* Round up to the next 4th sample, so each channel buffer starts
507 * 16-byte aligned.
509 total += RoundUp(device->ChannelDelay[chan].Length, 4);
513 if(total > 0)
515 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
516 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
518 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
519 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
524 static void InitPanning(ALCdevice *device)
526 const ChannelMap *chanmap = NULL;
527 ALsizei coeffcount = 0;
528 ALsizei count = 0;
529 ALsizei i, j;
531 switch(device->FmtChans)
533 case DevFmtMono:
534 count = COUNTOF(MonoCfg);
535 chanmap = MonoCfg;
536 coeffcount = 1;
537 break;
539 case DevFmtStereo:
540 count = COUNTOF(StereoCfg);
541 chanmap = StereoCfg;
542 coeffcount = 4;
543 break;
545 case DevFmtQuad:
546 count = COUNTOF(QuadCfg);
547 chanmap = QuadCfg;
548 coeffcount = 4;
549 break;
551 case DevFmtX51:
552 count = COUNTOF(X51SideCfg);
553 chanmap = X51SideCfg;
554 coeffcount = 9;
555 break;
557 case DevFmtX51Rear:
558 count = COUNTOF(X51RearCfg);
559 chanmap = X51RearCfg;
560 coeffcount = 9;
561 break;
563 case DevFmtX61:
564 count = COUNTOF(X61Cfg);
565 chanmap = X61Cfg;
566 coeffcount = 9;
567 break;
569 case DevFmtX71:
570 count = COUNTOF(X71Cfg);
571 chanmap = X71Cfg;
572 coeffcount = 16;
573 break;
575 case DevFmtAmbi3D:
576 break;
579 if(device->FmtChans == DevFmtAmbi3D)
581 const char *devname = alstr_get_cstr(device->DeviceName);
582 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
583 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
584 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
585 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ N3D2N3DScale;
586 ALfloat nfc_delay = 0.0f;
588 count = (device->AmbiOrder == 3) ? 16 :
589 (device->AmbiOrder == 2) ? 9 :
590 (device->AmbiOrder == 1) ? 4 : 1;
591 for(i = 0;i < count;i++)
593 ALsizei acn = acnmap[i];
594 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
595 device->Dry.Ambi.Map[i].Index = acn;
597 device->Dry.CoeffCount = 0;
598 device->Dry.NumChannels = count;
600 if(device->AmbiOrder < 2)
602 device->FOAOut.Ambi = device->Dry.Ambi;
603 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
604 device->FOAOut.NumChannels = 0;
606 else
608 ALfloat w_scale=1.0f, xyz_scale=1.0f;
610 /* FOA output is always ACN+N3D for higher-order ambisonic output.
611 * The upsampler expects this and will convert it for output.
613 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
614 for(i = 0;i < 4;i++)
616 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
617 device->FOAOut.Ambi.Map[i].Index = i;
619 device->FOAOut.CoeffCount = 0;
620 device->FOAOut.NumChannels = 4;
622 if(device->AmbiOrder >= 3)
624 w_scale = W_SCALE_3H3P;
625 xyz_scale = XYZ_SCALE_3H3P;
627 else
629 w_scale = W_SCALE_2H2P;
630 xyz_scale = XYZ_SCALE_2H2P;
632 ambiup_reset(device->AmbiUp, device, w_scale, xyz_scale);
635 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
637 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
638 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
639 device->AmbiOrder, true);
642 else
644 ALfloat w_scale, xyz_scale;
646 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
647 chanmap, count, &device->Dry.NumChannels);
648 device->Dry.CoeffCount = coeffcount;
650 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE_3H0P :
651 (device->Dry.CoeffCount > 4) ? W_SCALE_2H0P : 1.0f;
652 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE_3H0P :
653 (device->Dry.CoeffCount > 4) ? XYZ_SCALE_2H0P : 1.0f;
655 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
656 for(i = 0;i < device->Dry.NumChannels;i++)
658 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
659 for(j = 1;j < 4;j++)
660 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
662 device->FOAOut.CoeffCount = 4;
663 device->FOAOut.NumChannels = 0;
665 device->RealOut.NumChannels = 0;
668 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
670 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
671 const ALfloat *coeff_scale = N3D2N3DScale;
672 ALfloat w_scale = 1.0f;
673 ALfloat xyz_scale = 1.0f;
674 ALsizei i, j;
676 if(conf->FreqBands != 1)
677 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
678 conf->XOverFreq);
680 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
682 if(conf->ChanMask > 0x1ff)
684 w_scale = W_SCALE_3H3P;
685 xyz_scale = XYZ_SCALE_3H3P;
687 else if(conf->ChanMask > 0xf)
689 w_scale = W_SCALE_2H2P;
690 xyz_scale = XYZ_SCALE_2H2P;
693 else
695 if(conf->ChanMask > 0x1ff)
697 w_scale = W_SCALE_3H0P;
698 xyz_scale = XYZ_SCALE_3H0P;
700 else if(conf->ChanMask > 0xf)
702 w_scale = W_SCALE_2H0P;
703 xyz_scale = XYZ_SCALE_2H0P;
707 if(conf->CoeffScale == ADS_SN3D)
708 coeff_scale = SN3D2N3DScale;
709 else if(conf->CoeffScale == ADS_FuMa)
710 coeff_scale = FuMa2N3DScale;
712 for(i = 0;i < conf->NumSpeakers;i++)
714 ALsizei chan = speakermap[i];
715 ALfloat gain;
716 ALsizei k = 0;
718 for(j = 0;j < MAX_AMBI_COEFFS;j++)
719 chanmap[i].Config[j] = 0.0f;
721 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
722 for(j = 0;j < MAX_AMBI_COEFFS;j++)
724 if(j == 0) gain = conf->HFOrderGain[0];
725 else if(j == 1) gain = conf->HFOrderGain[1];
726 else if(j == 4) gain = conf->HFOrderGain[2];
727 else if(j == 9) gain = conf->HFOrderGain[3];
728 if((conf->ChanMask&(1<<j)))
729 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
733 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
734 conf->NumSpeakers, &device->Dry.NumChannels);
735 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
736 (conf->ChanMask > 0xf) ? 9 : 4;
738 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
739 for(i = 0;i < device->Dry.NumChannels;i++)
741 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
742 for(j = 1;j < 4;j++)
743 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
745 device->FOAOut.CoeffCount = 4;
746 device->FOAOut.NumChannels = 0;
748 device->RealOut.NumChannels = 0;
750 InitDistanceComp(device, conf, speakermap);
753 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
755 ALfloat avg_dist;
756 ALsizei count;
757 ALsizei i;
759 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
761 count = (conf->ChanMask > 0x1ff) ? 16 :
762 (conf->ChanMask > 0xf) ? 9 : 4;
763 for(i = 0;i < count;i++)
765 device->Dry.Ambi.Map[i].Scale = 1.0f;
766 device->Dry.Ambi.Map[i].Index = i;
769 else
771 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
773 count = (conf->ChanMask > 0x1ff) ? 7 :
774 (conf->ChanMask > 0xf) ? 5 : 3;
775 for(i = 0;i < count;i++)
777 device->Dry.Ambi.Map[i].Scale = 1.0f;
778 device->Dry.Ambi.Map[i].Index = map[i];
781 device->Dry.CoeffCount = 0;
782 device->Dry.NumChannels = count;
784 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
785 (conf->FreqBands == 1) ? "single" : "dual",
786 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
787 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
789 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
791 if(!(conf->ChanMask > 0xf))
793 device->FOAOut.Ambi = device->Dry.Ambi;
794 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
795 device->FOAOut.NumChannels = 0;
797 else
799 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
800 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
802 count = 4;
803 for(i = 0;i < count;i++)
805 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
806 device->FOAOut.Ambi.Map[i].Index = i;
809 else
811 static const int map[3] = { 0, 1, 3 };
812 count = 3;
813 for(i = 0;i < count;i++)
815 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
816 device->FOAOut.Ambi.Map[i].Index = map[i];
819 device->FOAOut.CoeffCount = 0;
820 device->FOAOut.NumChannels = count;
823 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
825 avg_dist = 0.0f;
826 for(i = 0;i < conf->NumSpeakers;i++)
827 avg_dist += conf->Speakers[i].Distance;
828 avg_dist /= (ALfloat)conf->NumSpeakers;
829 InitNearFieldCtrl(device, avg_dist,
830 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
831 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
834 InitDistanceComp(device, conf, speakermap);
837 static void InitHrtfPanning(ALCdevice *device)
839 /* NOTE: azimuth goes clockwise. */
840 static const ALfloat AmbiPoints[][2] = {
841 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
842 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
843 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
844 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
845 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
846 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
847 { DEG2RAD( 0.0f), DEG2RAD( 45.0f) },
848 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
849 { DEG2RAD( 0.0f), DEG2RAD( 135.0f) },
850 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
851 { DEG2RAD( 0.0f), DEG2RAD(-135.0f) },
852 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
853 { DEG2RAD( 0.0f), DEG2RAD( -45.0f) },
854 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
855 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
856 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
857 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
858 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
860 static const ALfloat AmbiMatrixFOA[][MAX_AMBI_COEFFS] = {
861 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f },
862 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f },
863 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f },
864 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f },
865 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f },
866 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f },
867 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
868 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
869 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
870 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f },
871 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
872 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
873 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
874 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f },
875 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f },
876 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f },
877 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f },
878 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f },
879 }, AmbiMatrixHOA[][MAX_AMBI_COEFFS] = {
880 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
881 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
882 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
883 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
884 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
885 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 1.29099445e-01f },
886 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, -6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
887 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
888 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f, 6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
889 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 1.29099445e-01f },
890 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f, -6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
891 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
892 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, 6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
893 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
894 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
895 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
896 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
897 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
899 static const ALfloat AmbiOrderHFGainFOA[MAX_AMBI_ORDER+1] = {
900 3.00000000e+00f, 1.73205081e+00f
901 }, AmbiOrderHFGainHOA[MAX_AMBI_ORDER+1] = {
902 2.40192231e+00f, 1.86052102e+00f, 9.60768923e-01f
904 const ALfloat (*restrict AmbiMatrix)[MAX_AMBI_COEFFS] = AmbiMatrixFOA;
905 const ALfloat *restrict AmbiOrderHFGain = AmbiOrderHFGainFOA;
906 ALsizei count = 4;
907 ALsizei i;
909 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixFOA), "FOA Ambisonic HRTF mismatch");
910 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixHOA), "HOA Ambisonic HRTF mismatch");
911 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
913 if(device->AmbiUp)
915 AmbiMatrix = AmbiMatrixHOA;
916 AmbiOrderHFGain = AmbiOrderHFGainHOA;
917 count = 9;
920 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
922 for(i = 0;i < count;i++)
924 device->Dry.Ambi.Map[i].Scale = 1.0f;
925 device->Dry.Ambi.Map[i].Index = i;
927 device->Dry.CoeffCount = 0;
928 device->Dry.NumChannels = count;
930 if(device->AmbiUp)
932 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
933 for(i = 0;i < 4;i++)
935 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
936 device->FOAOut.Ambi.Map[i].Index = i;
938 device->FOAOut.CoeffCount = 0;
939 device->FOAOut.NumChannels = 4;
941 ambiup_reset(device->AmbiUp, device, AmbiOrderHFGainFOA[0] / AmbiOrderHFGain[0],
942 AmbiOrderHFGainFOA[1] / AmbiOrderHFGain[1]);
944 else
946 device->FOAOut.Ambi = device->Dry.Ambi;
947 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
948 device->FOAOut.NumChannels = 0;
951 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
953 BuildBFormatHrtf(device->HrtfHandle,
954 device->Hrtf, device->Dry.NumChannels, AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints),
955 AmbiOrderHFGain
958 InitNearFieldCtrl(device, device->HrtfHandle->distance, device->AmbiUp ? 2 : 1, true);
961 static void InitUhjPanning(ALCdevice *device)
963 ALsizei count = 3;
964 ALsizei i;
966 for(i = 0;i < count;i++)
968 ALsizei acn = FuMa2ACN[i];
969 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
970 device->Dry.Ambi.Map[i].Index = acn;
972 device->Dry.CoeffCount = 0;
973 device->Dry.NumChannels = count;
975 device->FOAOut.Ambi = device->Dry.Ambi;
976 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
977 device->FOAOut.NumChannels = 0;
979 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
982 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
984 /* Hold the HRTF the device last used, in case it's used again. */
985 struct Hrtf *old_hrtf = device->HrtfHandle;
986 const char *mode;
987 bool headphones;
988 int bs2blevel;
989 size_t i;
991 al_free(device->Hrtf);
992 device->Hrtf = NULL;
993 device->HrtfHandle = NULL;
994 alstr_clear(&device->HrtfName);
995 device->Render_Mode = NormalRender;
997 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
998 device->Dry.CoeffCount = 0;
999 device->Dry.NumChannels = 0;
1000 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
1001 device->Dry.NumChannelsPerOrder[i] = 0;
1003 device->AvgSpeakerDist = 0.0f;
1004 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
1005 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1007 device->ChannelDelay[i].Gain = 1.0f;
1008 device->ChannelDelay[i].Length = 0;
1011 al_free(device->Stablizer);
1012 device->Stablizer = NULL;
1014 if(device->FmtChans != DevFmtStereo)
1016 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1017 const char *devname, *layout = NULL;
1018 AmbDecConf conf, *pconf = NULL;
1020 if(old_hrtf)
1021 Hrtf_DecRef(old_hrtf);
1022 old_hrtf = NULL;
1023 if(hrtf_appreq == Hrtf_Enable)
1024 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1026 ambdec_init(&conf);
1028 devname = alstr_get_cstr(device->DeviceName);
1029 switch(device->FmtChans)
1031 case DevFmtQuad: layout = "quad"; break;
1032 case DevFmtX51: /* fall-through */
1033 case DevFmtX51Rear: layout = "surround51"; break;
1034 case DevFmtX61: layout = "surround61"; break;
1035 case DevFmtX71: layout = "surround71"; break;
1036 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1037 case DevFmtMono:
1038 case DevFmtStereo:
1039 case DevFmtAmbi3D:
1040 break;
1042 if(layout)
1044 const char *fname;
1045 if(ConfigValueStr(devname, "decoder", layout, &fname))
1047 if(!ambdec_load(&conf, fname))
1048 ERR("Failed to load layout file %s\n", fname);
1049 else
1051 if(conf.ChanMask > 0xffff)
1052 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1053 else
1055 if(MakeSpeakerMap(device, &conf, speakermap))
1056 pconf = &conf;
1062 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1064 ambiup_free(&device->AmbiUp);
1065 if(!device->AmbiDecoder)
1066 device->AmbiDecoder = bformatdec_alloc();
1068 else
1070 bformatdec_free(&device->AmbiDecoder);
1071 if(device->FmtChans != DevFmtAmbi3D || device->AmbiOrder < 2)
1072 ambiup_free(&device->AmbiUp);
1073 else
1075 if(!device->AmbiUp)
1076 device->AmbiUp = ambiup_alloc();
1080 if(!pconf)
1081 InitPanning(device);
1082 else if(device->AmbiDecoder)
1083 InitHQPanning(device, pconf, speakermap);
1084 else
1085 InitCustomPanning(device, pconf, speakermap);
1087 /* Enable the stablizer only for formats that have front-left, front-
1088 * right, and front-center outputs.
1090 switch(device->FmtChans)
1092 case DevFmtX51:
1093 case DevFmtX51Rear:
1094 case DevFmtX61:
1095 case DevFmtX71:
1096 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1098 /* Initialize band-splitting filters for the front-left and
1099 * front-right channels, with a crossover at 5khz (could be
1100 * higher).
1102 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1103 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1105 bandsplit_init(&stablizer->LFilter, scale);
1106 stablizer->RFilter = stablizer->LFilter;
1108 /* Initialize all-pass filters for all other channels. */
1109 splitterap_init(&stablizer->APFilter[0], scale);
1110 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1111 stablizer->APFilter[i] = stablizer->APFilter[0];
1113 device->Stablizer = stablizer;
1115 break;
1116 case DevFmtMono:
1117 case DevFmtStereo:
1118 case DevFmtQuad:
1119 case DevFmtAmbi3D:
1120 break;
1122 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1124 ambdec_deinit(&conf);
1125 return;
1128 bformatdec_free(&device->AmbiDecoder);
1130 headphones = device->IsHeadphones;
1131 if(device->Type != Loopback)
1133 const char *mode;
1134 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1136 if(strcasecmp(mode, "headphones") == 0)
1137 headphones = true;
1138 else if(strcasecmp(mode, "speakers") == 0)
1139 headphones = false;
1140 else if(strcasecmp(mode, "auto") != 0)
1141 ERR("Unexpected stereo-mode: %s\n", mode);
1145 if(hrtf_userreq == Hrtf_Default)
1147 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1148 (hrtf_appreq == Hrtf_Enable);
1149 if(!usehrtf) goto no_hrtf;
1151 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1152 if(headphones && hrtf_appreq != Hrtf_Disable)
1153 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1155 else
1157 if(hrtf_userreq != Hrtf_Enable)
1159 if(hrtf_appreq == Hrtf_Enable)
1160 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1161 goto no_hrtf;
1163 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1166 if(VECTOR_SIZE(device->HrtfList) == 0)
1168 VECTOR_DEINIT(device->HrtfList);
1169 device->HrtfList = EnumerateHrtf(device->DeviceName);
1172 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1174 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1175 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1176 if(hrtf && hrtf->sampleRate == device->Frequency)
1178 device->HrtfHandle = hrtf;
1179 alstr_copy(&device->HrtfName, entry->name);
1181 else if(hrtf)
1182 Hrtf_DecRef(hrtf);
1185 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1187 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1188 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1189 if(hrtf && hrtf->sampleRate == device->Frequency)
1191 device->HrtfHandle = hrtf;
1192 alstr_copy(&device->HrtfName, entry->name);
1194 else if(hrtf)
1195 Hrtf_DecRef(hrtf);
1198 if(device->HrtfHandle)
1200 if(old_hrtf)
1201 Hrtf_DecRef(old_hrtf);
1202 old_hrtf = NULL;
1204 device->Render_Mode = HrtfRender;
1205 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1207 if(strcasecmp(mode, "full") == 0)
1208 device->Render_Mode = HrtfRender;
1209 else if(strcasecmp(mode, "basic") == 0)
1210 device->Render_Mode = NormalRender;
1211 else
1212 ERR("Unexpected hrtf-mode: %s\n", mode);
1215 if(device->Render_Mode == HrtfRender)
1217 /* Don't bother with HOA when using full HRTF rendering. Nothing
1218 * needs it, and it eases the CPU/memory load.
1220 ambiup_free(&device->AmbiUp);
1222 else
1224 if(!device->AmbiUp)
1225 device->AmbiUp = ambiup_alloc();
1228 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1229 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1230 alstr_get_cstr(device->HrtfName)
1232 InitHrtfPanning(device);
1233 return;
1235 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1237 no_hrtf:
1238 if(old_hrtf)
1239 Hrtf_DecRef(old_hrtf);
1240 old_hrtf = NULL;
1241 TRACE("HRTF disabled\n");
1243 device->Render_Mode = StereoPair;
1245 ambiup_free(&device->AmbiUp);
1247 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1248 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1249 if(device->Type != Loopback)
1250 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1251 if(bs2blevel > 0 && bs2blevel <= 6)
1253 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1254 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1255 TRACE("BS2B enabled\n");
1256 InitPanning(device);
1257 return;
1260 TRACE("BS2B disabled\n");
1262 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1264 if(strcasecmp(mode, "uhj") == 0)
1265 device->Render_Mode = NormalRender;
1266 else if(strcasecmp(mode, "panpot") != 0)
1267 ERR("Unexpected stereo-encoding: %s\n", mode);
1269 if(device->Render_Mode == NormalRender)
1271 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1272 TRACE("UHJ enabled\n");
1273 InitUhjPanning(device);
1274 return;
1277 TRACE("UHJ disabled\n");
1278 InitPanning(device);
1282 void aluInitEffectPanning(ALeffectslot *slot)
1284 ALsizei i;
1286 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1287 slot->NumChannels = 0;
1289 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1291 slot->ChanMap[i].Scale = 1.0f;
1292 slot->ChanMap[i].Index = i;
1294 slot->NumChannels = i;