EFX: Align some arrays used in intrinsics (#180)
[openal-soft.git] / Alc / panning.c
blobae8315f32f7a66d00f7512ff3a4a5aa1cfcaee98
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 ComputeDryPanGains(const DryMixParams *dry, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
42 extern inline void ComputeFirstOrderGains(const BFMixParams *foa, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
45 static const ALsizei FuMa2ACN[MAX_AMBI_COEFFS] = {
46 0, /* W */
47 3, /* X */
48 1, /* Y */
49 2, /* Z */
50 6, /* R */
51 7, /* S */
52 5, /* T */
53 8, /* U */
54 4, /* V */
55 12, /* K */
56 13, /* L */
57 11, /* M */
58 14, /* N */
59 10, /* O */
60 15, /* P */
61 9, /* Q */
63 static const ALsizei ACN2ACN[MAX_AMBI_COEFFS] = {
64 0, 1, 2, 3, 4, 5, 6, 7,
65 8, 9, 10, 11, 12, 13, 14, 15
69 void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
71 /* Convert from OpenAL coords to Ambisonics. */
72 ALfloat x = -dir[2];
73 ALfloat y = -dir[0];
74 ALfloat z = dir[1];
76 /* Zeroth-order */
77 coeffs[0] = 1.0f; /* ACN 0 = 1 */
78 /* First-order */
79 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
80 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
81 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
82 /* Second-order */
83 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
84 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
85 coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
86 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
87 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
88 /* Third-order */
89 coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
90 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
91 coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
92 coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
93 coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
94 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
95 coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
97 if(spread > 0.0f)
99 /* Implement the spread by using a spherical source that subtends the
100 * angle spread. See:
101 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
103 * When adjusted for N3D normalization instead of SN3D, these
104 * calculations are:
106 * ZH0 = -sqrt(pi) * (-1+ca);
107 * ZH1 = 0.5*sqrt(pi) * sa*sa;
108 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
109 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
110 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
111 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
113 * The gain of the source is compensated for size, so that the
114 * loundness doesn't depend on the spread. Thus:
116 * ZH0 = 1.0f;
117 * ZH1 = 0.5f * (ca+1.0f);
118 * ZH2 = 0.5f * (ca+1.0f)*ca;
119 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
120 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
121 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
123 ALfloat ca = cosf(spread * 0.5f);
124 /* Increase the source volume by up to +3dB for a full spread. */
125 ALfloat scale = sqrtf(1.0f + spread/F_TAU);
127 ALfloat ZH0_norm = scale;
128 ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
129 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
130 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
132 /* Zeroth-order */
133 coeffs[0] *= ZH0_norm;
134 /* First-order */
135 coeffs[1] *= ZH1_norm;
136 coeffs[2] *= ZH1_norm;
137 coeffs[3] *= ZH1_norm;
138 /* Second-order */
139 coeffs[4] *= ZH2_norm;
140 coeffs[5] *= ZH2_norm;
141 coeffs[6] *= ZH2_norm;
142 coeffs[7] *= ZH2_norm;
143 coeffs[8] *= ZH2_norm;
144 /* Third-order */
145 coeffs[9] *= ZH3_norm;
146 coeffs[10] *= ZH3_norm;
147 coeffs[11] *= ZH3_norm;
148 coeffs[12] *= ZH3_norm;
149 coeffs[13] *= ZH3_norm;
150 coeffs[14] *= ZH3_norm;
151 coeffs[15] *= ZH3_norm;
155 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
157 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
158 if(!(fabsf(azimuth) > F_PI_2))
159 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
160 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
164 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
166 ALsizei i, j;
168 for(i = 0;i < numchans;i++)
170 float gain = 0.0f;
171 for(j = 0;j < numcoeffs;j++)
172 gain += chancoeffs[i][j]*coeffs[j];
173 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
175 for(;i < MAX_OUTPUT_CHANNELS;i++)
176 gains[i] = 0.0f;
179 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
181 ALsizei i;
183 for(i = 0;i < numchans;i++)
184 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
185 for(;i < MAX_OUTPUT_CHANNELS;i++)
186 gains[i] = 0.0f;
189 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
191 ALsizei i, j;
193 for(i = 0;i < numchans;i++)
195 float gain = 0.0f;
196 for(j = 0;j < 4;j++)
197 gain += chancoeffs[i][j] * mtx[j];
198 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
200 for(;i < MAX_OUTPUT_CHANNELS;i++)
201 gains[i] = 0.0f;
204 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
206 ALsizei i;
208 for(i = 0;i < numchans;i++)
209 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
210 for(;i < MAX_OUTPUT_CHANNELS;i++)
211 gains[i] = 0.0f;
215 static inline const char *GetLabelFromChannel(enum Channel channel)
217 switch(channel)
219 case FrontLeft: return "front-left";
220 case FrontRight: return "front-right";
221 case FrontCenter: return "front-center";
222 case LFE: return "lfe";
223 case BackLeft: return "back-left";
224 case BackRight: return "back-right";
225 case BackCenter: return "back-center";
226 case SideLeft: return "side-left";
227 case SideRight: return "side-right";
229 case UpperFrontLeft: return "upper-front-left";
230 case UpperFrontRight: return "upper-front-right";
231 case UpperBackLeft: return "upper-back-left";
232 case UpperBackRight: return "upper-back-right";
233 case LowerFrontLeft: return "lower-front-left";
234 case LowerFrontRight: return "lower-front-right";
235 case LowerBackLeft: return "lower-back-left";
236 case LowerBackRight: return "lower-back-right";
238 case Aux0: return "aux-0";
239 case Aux1: return "aux-1";
240 case Aux2: return "aux-2";
241 case Aux3: return "aux-3";
242 case Aux4: return "aux-4";
243 case Aux5: return "aux-5";
244 case Aux6: return "aux-6";
245 case Aux7: return "aux-7";
246 case Aux8: return "aux-8";
247 case Aux9: return "aux-9";
248 case Aux10: return "aux-10";
249 case Aux11: return "aux-11";
250 case Aux12: return "aux-12";
251 case Aux13: return "aux-13";
252 case Aux14: return "aux-14";
253 case Aux15: return "aux-15";
255 case InvalidChannel: break;
257 return "(unknown)";
261 typedef struct ChannelMap {
262 enum Channel ChanName;
263 ChannelConfig Config;
264 } ChannelMap;
266 static void SetChannelMap(const enum Channel devchans[MAX_OUTPUT_CHANNELS],
267 ChannelConfig *ambicoeffs, const ChannelMap *chanmap,
268 ALsizei count, ALsizei *outcount)
270 ALsizei maxchans = 0;
271 ALsizei i, j;
273 for(i = 0;i < count;i++)
275 ALint idx = GetChannelIndex(devchans, chanmap[i].ChanName);
276 if(idx < 0)
278 ERR("Failed to find %s channel in device\n",
279 GetLabelFromChannel(chanmap[i].ChanName));
280 continue;
283 maxchans = maxi(maxchans, idx+1);
284 for(j = 0;j < MAX_AMBI_COEFFS;j++)
285 ambicoeffs[idx][j] = chanmap[i].Config[j];
287 *outcount = mini(maxchans, MAX_OUTPUT_CHANNELS);
290 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
292 ALsizei i;
294 for(i = 0;i < conf->NumSpeakers;i++)
296 enum Channel ch;
297 int chidx = -1;
299 /* NOTE: AmbDec does not define any standard speaker names, however
300 * for this to work we have to by able to find the output channel
301 * the speaker definition corresponds to. Therefore, OpenAL Soft
302 * requires these channel labels to be recognized:
304 * LF = Front left
305 * RF = Front right
306 * LS = Side left
307 * RS = Side right
308 * LB = Back left
309 * RB = Back right
310 * CE = Front center
311 * CB = Back center
313 * Additionally, surround51 will acknowledge back speakers for side
314 * channels, and surround51rear will acknowledge side speakers for
315 * back channels, to avoid issues with an ambdec expecting 5.1 to
316 * use the side channels when the device is configured for back,
317 * and vice-versa.
319 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
320 ch = FrontLeft;
321 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
322 ch = FrontRight;
323 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
324 ch = FrontCenter;
325 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
327 if(device->FmtChans == DevFmtX51Rear)
328 ch = BackLeft;
329 else
330 ch = SideLeft;
332 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
334 if(device->FmtChans == DevFmtX51Rear)
335 ch = BackRight;
336 else
337 ch = SideRight;
339 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
341 if(device->FmtChans == DevFmtX51)
342 ch = SideLeft;
343 else
344 ch = BackLeft;
346 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
348 if(device->FmtChans == DevFmtX51)
349 ch = SideRight;
350 else
351 ch = BackRight;
353 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
354 ch = BackCenter;
355 else
357 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
358 unsigned int n;
359 char c;
361 if(sscanf(name, "AUX%u%c", &n, &c) == 1 && n < 16)
362 ch = Aux0+n;
363 else
365 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
366 return false;
369 chidx = GetChannelIdxByName(&device->RealOut, ch);
370 if(chidx == -1)
372 ERR("Failed to lookup AmbDec speaker label %s\n",
373 alstr_get_cstr(conf->Speakers[i].Name));
374 return false;
376 speakermap[i] = chidx;
379 return true;
383 static const ChannelMap MonoCfg[1] = {
384 { FrontCenter, { 1.0f } },
385 }, StereoCfg[2] = {
386 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 1.19573156e-1f } },
387 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 1.19573156e-1f } },
388 }, QuadCfg[4] = {
389 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
390 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
391 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
392 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
393 }, X51SideCfg[4] = {
394 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
395 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
396 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
397 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
398 }, X51RearCfg[4] = {
399 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
400 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
401 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
402 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
403 }, X61Cfg[6] = {
404 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, 0.0f, -4.39996780e-2f, -2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
405 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 0.0f, 1.59658796e-1f, 6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
406 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 0.0f, 1.59658796e-1f, -6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
407 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, 0.0f, -4.39996780e-2f, 2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
408 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
409 }, X71Cfg[6] = {
410 { 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 } },
411 { 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 } },
412 { 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 } },
413 { 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 } },
414 { 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 } },
415 { 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 } },
418 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
420 const char *devname = alstr_get_cstr(device->DeviceName);
421 ALsizei i;
423 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
425 /* NFC is only used when AvgSpeakerDist is greater than 0, and can only
426 * be used when rendering to an ambisonic buffer.
428 device->AvgSpeakerDist = ctrl_dist;
430 device->Dry.NumChannelsPerOrder[0] = 1;
431 if(periphonic)
432 for(i = 1;i < order+1;i++)
433 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
434 else
435 for(i = 1;i < order+1;i++)
436 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
437 for(;i < MAX_AMBI_ORDER+1;i++)
438 device->Dry.NumChannelsPerOrder[i] = 0;
442 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
444 const char *devname = alstr_get_cstr(device->DeviceName);
445 ALfloat maxdist = 0.0f;
446 size_t total = 0;
447 ALsizei i;
449 for(i = 0;i < conf->NumSpeakers;i++)
450 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
452 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
454 ALfloat srate = (ALfloat)device->Frequency;
455 for(i = 0;i < conf->NumSpeakers;i++)
457 ALsizei chan = speakermap[i];
458 ALfloat delay;
460 /* Distance compensation only delays in steps of the sample rate.
461 * This is a bit less accurate since the delay time falls to the
462 * nearest sample time, but it's far simpler as it doesn't have to
463 * deal with phase offsets. This means at 48khz, for instance, the
464 * distance delay will be in steps of about 7 millimeters.
466 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
467 srate + 0.5f);
468 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
469 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
470 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
472 device->ChannelDelay[chan].Length = (ALsizei)clampf(
473 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
475 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
476 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
477 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
478 device->ChannelDelay[chan].Gain
481 /* Round up to the next 4th sample, so each channel buffer starts
482 * 16-byte aligned.
484 total += RoundUp(device->ChannelDelay[chan].Length, 4);
488 if(total > 0)
490 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
491 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
493 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
494 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
499 static void InitPanning(ALCdevice *device)
501 const ChannelMap *chanmap = NULL;
502 ALsizei coeffcount = 0;
503 ALsizei count = 0;
504 ALsizei i, j;
506 switch(device->FmtChans)
508 case DevFmtMono:
509 count = COUNTOF(MonoCfg);
510 chanmap = MonoCfg;
511 coeffcount = 1;
512 break;
514 case DevFmtStereo:
515 count = COUNTOF(StereoCfg);
516 chanmap = StereoCfg;
517 coeffcount = 4;
518 break;
520 case DevFmtQuad:
521 count = COUNTOF(QuadCfg);
522 chanmap = QuadCfg;
523 coeffcount = 4;
524 break;
526 case DevFmtX51:
527 count = COUNTOF(X51SideCfg);
528 chanmap = X51SideCfg;
529 coeffcount = 9;
530 break;
532 case DevFmtX51Rear:
533 count = COUNTOF(X51RearCfg);
534 chanmap = X51RearCfg;
535 coeffcount = 9;
536 break;
538 case DevFmtX61:
539 count = COUNTOF(X61Cfg);
540 chanmap = X61Cfg;
541 coeffcount = 9;
542 break;
544 case DevFmtX71:
545 count = COUNTOF(X71Cfg);
546 chanmap = X71Cfg;
547 coeffcount = 16;
548 break;
550 case DevFmtAmbi3D:
551 break;
554 if(device->FmtChans == DevFmtAmbi3D)
556 const char *devname = alstr_get_cstr(device->DeviceName);
557 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
558 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
559 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
560 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ N3D2N3DScale;
561 ALfloat nfc_delay = 0.0f;
563 count = (device->AmbiOrder == 3) ? 16 :
564 (device->AmbiOrder == 2) ? 9 :
565 (device->AmbiOrder == 1) ? 4 : 1;
566 for(i = 0;i < count;i++)
568 ALsizei acn = acnmap[i];
569 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
570 device->Dry.Ambi.Map[i].Index = acn;
572 device->Dry.CoeffCount = 0;
573 device->Dry.NumChannels = count;
575 if(device->AmbiOrder < 2)
577 device->FOAOut.Ambi = device->Dry.Ambi;
578 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
579 device->FOAOut.NumChannels = 0;
581 else
583 ALfloat w_scale=1.0f, xyz_scale=1.0f;
585 /* FOA output is always ACN+N3D for higher-order ambisonic output.
586 * The upsampler expects this and will convert it for output.
588 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
589 for(i = 0;i < 4;i++)
591 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
592 device->FOAOut.Ambi.Map[i].Index = i;
594 device->FOAOut.CoeffCount = 0;
595 device->FOAOut.NumChannels = 4;
597 if(device->AmbiOrder >= 3)
599 w_scale = W_SCALE_3H3P;
600 xyz_scale = XYZ_SCALE_3H3P;
602 else
604 w_scale = W_SCALE_2H2P;
605 xyz_scale = XYZ_SCALE_2H2P;
607 ambiup_reset(device->AmbiUp, device, w_scale, xyz_scale);
610 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
612 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
613 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
614 device->AmbiOrder, true);
617 else
619 ALfloat w_scale, xyz_scale;
621 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
622 chanmap, count, &device->Dry.NumChannels);
623 device->Dry.CoeffCount = coeffcount;
625 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE_3H0P :
626 (device->Dry.CoeffCount > 4) ? W_SCALE_2H0P : 1.0f;
627 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE_3H0P :
628 (device->Dry.CoeffCount > 4) ? XYZ_SCALE_2H0P : 1.0f;
630 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
631 for(i = 0;i < device->Dry.NumChannels;i++)
633 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
634 for(j = 1;j < 4;j++)
635 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
637 device->FOAOut.CoeffCount = 4;
638 device->FOAOut.NumChannels = 0;
640 device->RealOut.NumChannels = 0;
643 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
645 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
646 const ALfloat *coeff_scale = N3D2N3DScale;
647 ALfloat w_scale = 1.0f;
648 ALfloat xyz_scale = 1.0f;
649 ALsizei i, j;
651 if(conf->FreqBands != 1)
652 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
653 conf->XOverFreq);
655 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
657 if(conf->ChanMask > 0x1ff)
659 w_scale = W_SCALE_3H3P;
660 xyz_scale = XYZ_SCALE_3H3P;
662 else if(conf->ChanMask > 0xf)
664 w_scale = W_SCALE_2H2P;
665 xyz_scale = XYZ_SCALE_2H2P;
668 else
670 if(conf->ChanMask > 0x1ff)
672 w_scale = W_SCALE_3H0P;
673 xyz_scale = XYZ_SCALE_3H0P;
675 else if(conf->ChanMask > 0xf)
677 w_scale = W_SCALE_2H0P;
678 xyz_scale = XYZ_SCALE_2H0P;
682 if(conf->CoeffScale == ADS_SN3D)
683 coeff_scale = SN3D2N3DScale;
684 else if(conf->CoeffScale == ADS_FuMa)
685 coeff_scale = FuMa2N3DScale;
687 for(i = 0;i < conf->NumSpeakers;i++)
689 ALsizei chan = speakermap[i];
690 ALfloat gain;
691 ALsizei k = 0;
693 for(j = 0;j < MAX_AMBI_COEFFS;j++)
694 chanmap[i].Config[j] = 0.0f;
696 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
697 for(j = 0;j < MAX_AMBI_COEFFS;j++)
699 if(j == 0) gain = conf->HFOrderGain[0];
700 else if(j == 1) gain = conf->HFOrderGain[1];
701 else if(j == 4) gain = conf->HFOrderGain[2];
702 else if(j == 9) gain = conf->HFOrderGain[3];
703 if((conf->ChanMask&(1<<j)))
704 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
708 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
709 conf->NumSpeakers, &device->Dry.NumChannels);
710 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
711 (conf->ChanMask > 0xf) ? 9 : 4;
713 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
714 for(i = 0;i < device->Dry.NumChannels;i++)
716 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
717 for(j = 1;j < 4;j++)
718 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
720 device->FOAOut.CoeffCount = 4;
721 device->FOAOut.NumChannels = 0;
723 device->RealOut.NumChannels = 0;
725 InitDistanceComp(device, conf, speakermap);
728 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
730 ALfloat avg_dist;
731 ALsizei count;
732 ALsizei i;
734 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
736 count = (conf->ChanMask > 0x1ff) ? 16 :
737 (conf->ChanMask > 0xf) ? 9 : 4;
738 for(i = 0;i < count;i++)
740 device->Dry.Ambi.Map[i].Scale = 1.0f;
741 device->Dry.Ambi.Map[i].Index = i;
744 else
746 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
748 count = (conf->ChanMask > 0x1ff) ? 7 :
749 (conf->ChanMask > 0xf) ? 5 : 3;
750 for(i = 0;i < count;i++)
752 device->Dry.Ambi.Map[i].Scale = 1.0f;
753 device->Dry.Ambi.Map[i].Index = map[i];
756 device->Dry.CoeffCount = 0;
757 device->Dry.NumChannels = count;
759 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
760 (conf->FreqBands == 1) ? "single" : "dual",
761 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
762 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
764 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
766 if(!(conf->ChanMask > 0xf))
768 device->FOAOut.Ambi = device->Dry.Ambi;
769 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
770 device->FOAOut.NumChannels = 0;
772 else
774 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
775 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
777 count = 4;
778 for(i = 0;i < count;i++)
780 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
781 device->FOAOut.Ambi.Map[i].Index = i;
784 else
786 static const int map[3] = { 0, 1, 3 };
787 count = 3;
788 for(i = 0;i < count;i++)
790 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
791 device->FOAOut.Ambi.Map[i].Index = map[i];
794 device->FOAOut.CoeffCount = 0;
795 device->FOAOut.NumChannels = count;
798 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
800 avg_dist = 0.0f;
801 for(i = 0;i < conf->NumSpeakers;i++)
802 avg_dist += conf->Speakers[i].Distance;
803 avg_dist /= (ALfloat)conf->NumSpeakers;
804 InitNearFieldCtrl(device, avg_dist,
805 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
806 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
809 InitDistanceComp(device, conf, speakermap);
812 static void InitHrtfPanning(ALCdevice *device)
814 /* NOTE: azimuth goes clockwise. */
815 static const struct AngularPoint AmbiPoints[] = {
816 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
817 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
818 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
819 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
820 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
821 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
822 { DEG2RAD( 0.0f), DEG2RAD( 45.0f) },
823 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
824 { DEG2RAD( 0.0f), DEG2RAD( 135.0f) },
825 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
826 { DEG2RAD( 0.0f), DEG2RAD(-135.0f) },
827 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
828 { DEG2RAD( 0.0f), DEG2RAD( -45.0f) },
829 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
830 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
831 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
832 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
833 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
835 static const ALfloat AmbiMatrixFOA[][MAX_AMBI_COEFFS] = {
836 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f },
837 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f },
838 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f },
839 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f },
840 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f },
841 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f },
842 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
843 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
844 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
845 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f },
846 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
847 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
848 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
849 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f },
850 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f },
851 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f },
852 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f },
853 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f },
854 }, AmbiMatrixHOA[][MAX_AMBI_COEFFS] = {
855 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
856 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
857 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
858 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
859 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
860 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 1.29099445e-01f },
861 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, -6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
862 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
863 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f, 6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
864 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 1.29099445e-01f },
865 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f, -6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
866 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
867 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, 6.83467648e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
868 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
869 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
870 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
871 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
872 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
874 static const ALfloat AmbiOrderHFGainFOA[MAX_AMBI_ORDER+1] = {
875 1.00000000e+00f, 5.77350269e-01f
876 }, AmbiOrderHFGainHOA[MAX_AMBI_ORDER+1] = {
877 9.80580676e-01f, 7.59554525e-01f, 3.92232270e-01f
879 const ALfloat (*restrict AmbiMatrix)[MAX_AMBI_COEFFS] = AmbiMatrixFOA;
880 const ALfloat *restrict AmbiOrderHFGain = AmbiOrderHFGainFOA;
881 ALsizei count = 4;
882 ALsizei i;
884 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixFOA), "FOA Ambisonic HRTF mismatch");
885 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixHOA), "HOA Ambisonic HRTF mismatch");
886 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
888 if(device->AmbiUp)
890 AmbiMatrix = AmbiMatrixHOA;
891 AmbiOrderHFGain = AmbiOrderHFGainHOA;
892 count = 9;
895 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
897 for(i = 0;i < count;i++)
899 device->Dry.Ambi.Map[i].Scale = 1.0f;
900 device->Dry.Ambi.Map[i].Index = i;
902 device->Dry.CoeffCount = 0;
903 device->Dry.NumChannels = count;
905 if(device->AmbiUp)
907 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
908 for(i = 0;i < 4;i++)
910 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
911 device->FOAOut.Ambi.Map[i].Index = i;
913 device->FOAOut.CoeffCount = 0;
914 device->FOAOut.NumChannels = 4;
916 ambiup_reset(device->AmbiUp, device, AmbiOrderHFGainFOA[0] / AmbiOrderHFGain[0],
917 AmbiOrderHFGainFOA[1] / AmbiOrderHFGain[1]);
919 else
921 device->FOAOut.Ambi = device->Dry.Ambi;
922 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
923 device->FOAOut.NumChannels = 0;
926 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
928 BuildBFormatHrtf(device->HrtfHandle,
929 device->Hrtf, device->Dry.NumChannels, AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints),
930 AmbiOrderHFGain
933 InitNearFieldCtrl(device, device->HrtfHandle->distance, device->AmbiUp ? 2 : 1, true);
936 static void InitUhjPanning(ALCdevice *device)
938 ALsizei count = 3;
939 ALsizei i;
941 for(i = 0;i < count;i++)
943 ALsizei acn = FuMa2ACN[i];
944 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
945 device->Dry.Ambi.Map[i].Index = acn;
947 device->Dry.CoeffCount = 0;
948 device->Dry.NumChannels = count;
950 device->FOAOut.Ambi = device->Dry.Ambi;
951 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
952 device->FOAOut.NumChannels = 0;
954 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
957 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
959 /* Hold the HRTF the device last used, in case it's used again. */
960 struct Hrtf *old_hrtf = device->HrtfHandle;
961 const char *mode;
962 bool headphones;
963 int bs2blevel;
964 size_t i;
966 al_free(device->Hrtf);
967 device->Hrtf = NULL;
968 device->HrtfHandle = NULL;
969 alstr_clear(&device->HrtfName);
970 device->Render_Mode = NormalRender;
972 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
973 device->Dry.CoeffCount = 0;
974 device->Dry.NumChannels = 0;
975 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
976 device->Dry.NumChannelsPerOrder[i] = 0;
978 device->AvgSpeakerDist = 0.0f;
979 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
980 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
982 device->ChannelDelay[i].Gain = 1.0f;
983 device->ChannelDelay[i].Length = 0;
986 al_free(device->Stablizer);
987 device->Stablizer = NULL;
989 if(device->FmtChans != DevFmtStereo)
991 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
992 const char *devname, *layout = NULL;
993 AmbDecConf conf, *pconf = NULL;
995 if(old_hrtf)
996 Hrtf_DecRef(old_hrtf);
997 old_hrtf = NULL;
998 if(hrtf_appreq == Hrtf_Enable)
999 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1001 ambdec_init(&conf);
1003 devname = alstr_get_cstr(device->DeviceName);
1004 switch(device->FmtChans)
1006 case DevFmtQuad: layout = "quad"; break;
1007 case DevFmtX51: /* fall-through */
1008 case DevFmtX51Rear: layout = "surround51"; break;
1009 case DevFmtX61: layout = "surround61"; break;
1010 case DevFmtX71: layout = "surround71"; break;
1011 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1012 case DevFmtMono:
1013 case DevFmtStereo:
1014 case DevFmtAmbi3D:
1015 break;
1017 if(layout)
1019 const char *fname;
1020 if(ConfigValueStr(devname, "decoder", layout, &fname))
1022 if(!ambdec_load(&conf, fname))
1023 ERR("Failed to load layout file %s\n", fname);
1024 else
1026 if(conf.ChanMask > 0xffff)
1027 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1028 else
1030 if(MakeSpeakerMap(device, &conf, speakermap))
1031 pconf = &conf;
1037 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1039 ambiup_free(&device->AmbiUp);
1040 if(!device->AmbiDecoder)
1041 device->AmbiDecoder = bformatdec_alloc();
1043 else
1045 bformatdec_free(&device->AmbiDecoder);
1046 if(device->FmtChans != DevFmtAmbi3D || device->AmbiOrder < 2)
1047 ambiup_free(&device->AmbiUp);
1048 else
1050 if(!device->AmbiUp)
1051 device->AmbiUp = ambiup_alloc();
1055 if(!pconf)
1056 InitPanning(device);
1057 else if(device->AmbiDecoder)
1058 InitHQPanning(device, pconf, speakermap);
1059 else
1060 InitCustomPanning(device, pconf, speakermap);
1062 /* Enable the stablizer only for formats that have front-left, front-
1063 * right, and front-center outputs.
1065 switch(device->FmtChans)
1067 case DevFmtX51:
1068 case DevFmtX51Rear:
1069 case DevFmtX61:
1070 case DevFmtX71:
1071 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1073 /* Initialize band-splitting filters for the front-left and
1074 * front-right channels, with a crossover at 5khz (could be
1075 * higher).
1077 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1078 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1080 bandsplit_init(&stablizer->LFilter, scale);
1081 stablizer->RFilter = stablizer->LFilter;
1083 /* Initialize all-pass filters for all other channels. */
1084 splitterap_init(&stablizer->APFilter[0], scale);
1085 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1086 stablizer->APFilter[i] = stablizer->APFilter[0];
1088 device->Stablizer = stablizer;
1090 break;
1091 case DevFmtMono:
1092 case DevFmtStereo:
1093 case DevFmtQuad:
1094 case DevFmtAmbi3D:
1095 break;
1097 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1099 ambdec_deinit(&conf);
1100 return;
1103 bformatdec_free(&device->AmbiDecoder);
1105 headphones = device->IsHeadphones;
1106 if(device->Type != Loopback)
1108 const char *mode;
1109 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1111 if(strcasecmp(mode, "headphones") == 0)
1112 headphones = true;
1113 else if(strcasecmp(mode, "speakers") == 0)
1114 headphones = false;
1115 else if(strcasecmp(mode, "auto") != 0)
1116 ERR("Unexpected stereo-mode: %s\n", mode);
1120 if(hrtf_userreq == Hrtf_Default)
1122 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1123 (hrtf_appreq == Hrtf_Enable);
1124 if(!usehrtf) goto no_hrtf;
1126 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1127 if(headphones && hrtf_appreq != Hrtf_Disable)
1128 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1130 else
1132 if(hrtf_userreq != Hrtf_Enable)
1134 if(hrtf_appreq == Hrtf_Enable)
1135 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1136 goto no_hrtf;
1138 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1141 if(VECTOR_SIZE(device->HrtfList) == 0)
1143 VECTOR_DEINIT(device->HrtfList);
1144 device->HrtfList = EnumerateHrtf(device->DeviceName);
1147 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1149 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1150 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1151 if(hrtf && hrtf->sampleRate == device->Frequency)
1153 device->HrtfHandle = hrtf;
1154 alstr_copy(&device->HrtfName, entry->name);
1156 else if(hrtf)
1157 Hrtf_DecRef(hrtf);
1160 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1162 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1163 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1164 if(hrtf && hrtf->sampleRate == device->Frequency)
1166 device->HrtfHandle = hrtf;
1167 alstr_copy(&device->HrtfName, entry->name);
1169 else if(hrtf)
1170 Hrtf_DecRef(hrtf);
1173 if(device->HrtfHandle)
1175 if(old_hrtf)
1176 Hrtf_DecRef(old_hrtf);
1177 old_hrtf = NULL;
1179 device->Render_Mode = HrtfRender;
1180 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1182 if(strcasecmp(mode, "full") == 0)
1183 device->Render_Mode = HrtfRender;
1184 else if(strcasecmp(mode, "basic") == 0)
1185 device->Render_Mode = NormalRender;
1186 else
1187 ERR("Unexpected hrtf-mode: %s\n", mode);
1190 if(device->Render_Mode == HrtfRender)
1192 /* Don't bother with HOA when using full HRTF rendering. Nothing
1193 * needs it, and it eases the CPU/memory load.
1195 ambiup_free(&device->AmbiUp);
1197 else
1199 if(!device->AmbiUp)
1200 device->AmbiUp = ambiup_alloc();
1203 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1204 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1205 alstr_get_cstr(device->HrtfName)
1207 InitHrtfPanning(device);
1208 return;
1210 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1212 no_hrtf:
1213 if(old_hrtf)
1214 Hrtf_DecRef(old_hrtf);
1215 old_hrtf = NULL;
1216 TRACE("HRTF disabled\n");
1218 device->Render_Mode = StereoPair;
1220 ambiup_free(&device->AmbiUp);
1222 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1223 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1224 if(device->Type != Loopback)
1225 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1226 if(bs2blevel > 0 && bs2blevel <= 6)
1228 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1229 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1230 TRACE("BS2B enabled\n");
1231 InitPanning(device);
1232 return;
1235 TRACE("BS2B disabled\n");
1237 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1239 if(strcasecmp(mode, "uhj") == 0)
1240 device->Render_Mode = NormalRender;
1241 else if(strcasecmp(mode, "panpot") != 0)
1242 ERR("Unexpected stereo-encoding: %s\n", mode);
1244 if(device->Render_Mode == NormalRender)
1246 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1247 TRACE("UHJ enabled\n");
1248 InitUhjPanning(device);
1249 return;
1252 TRACE("UHJ disabled\n");
1253 InitPanning(device);
1257 void aluInitEffectPanning(ALeffectslot *slot)
1259 ALsizei i;
1261 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1262 slot->NumChannels = 0;
1264 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1266 slot->ChanMap[i].Scale = 1.0f;
1267 slot->ChanMap[i].Index = i;
1269 slot->NumChannels = i;