Merge pull request #204 from jhasse/android-byte-order
[openal-soft.git] / Alc / panning.c
blobe4562387af2ea4cdad1930b537307d0030b89963
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 "filters/splitter.h"
37 #include "uhjfilter.h"
38 #include "bs2b.h"
41 extern inline void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
42 extern inline void CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
43 extern inline void ComputeDryPanGains(const DryMixParams *dry, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
44 extern inline void ComputeFirstOrderGains(const BFMixParams *foa, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
47 static const ALsizei FuMa2ACN[MAX_AMBI_COEFFS] = {
48 0, /* W */
49 3, /* X */
50 1, /* Y */
51 2, /* Z */
52 6, /* R */
53 7, /* S */
54 5, /* T */
55 8, /* U */
56 4, /* V */
57 12, /* K */
58 13, /* L */
59 11, /* M */
60 14, /* N */
61 10, /* O */
62 15, /* P */
63 9, /* Q */
65 static const ALsizei ACN2ACN[MAX_AMBI_COEFFS] = {
66 0, 1, 2, 3, 4, 5, 6, 7,
67 8, 9, 10, 11, 12, 13, 14, 15
71 void CalcAmbiCoeffs(const ALfloat y, const ALfloat z, const ALfloat x, const ALfloat spread,
72 ALfloat coeffs[MAX_AMBI_COEFFS])
74 /* Zeroth-order */
75 coeffs[0] = 1.0f; /* ACN 0 = 1 */
76 /* First-order */
77 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
78 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
79 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
80 /* Second-order */
81 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
82 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
83 coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
84 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
85 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
86 /* Third-order */
87 coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
88 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
89 coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
90 coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
91 coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
92 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
93 coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
95 if(spread > 0.0f)
97 /* Implement the spread by using a spherical source that subtends the
98 * angle spread. See:
99 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
101 * When adjusted for N3D normalization instead of SN3D, these
102 * calculations are:
104 * ZH0 = -sqrt(pi) * (-1+ca);
105 * ZH1 = 0.5*sqrt(pi) * sa*sa;
106 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
107 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
108 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
109 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
111 * The gain of the source is compensated for size, so that the
112 * loundness doesn't depend on the spread. Thus:
114 * ZH0 = 1.0f;
115 * ZH1 = 0.5f * (ca+1.0f);
116 * ZH2 = 0.5f * (ca+1.0f)*ca;
117 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
118 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
119 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
121 ALfloat ca = cosf(spread * 0.5f);
122 /* Increase the source volume by up to +3dB for a full spread. */
123 ALfloat scale = sqrtf(1.0f + spread/F_TAU);
125 ALfloat ZH0_norm = scale;
126 ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
127 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
128 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
130 /* Zeroth-order */
131 coeffs[0] *= ZH0_norm;
132 /* First-order */
133 coeffs[1] *= ZH1_norm;
134 coeffs[2] *= ZH1_norm;
135 coeffs[3] *= ZH1_norm;
136 /* Second-order */
137 coeffs[4] *= ZH2_norm;
138 coeffs[5] *= ZH2_norm;
139 coeffs[6] *= ZH2_norm;
140 coeffs[7] *= ZH2_norm;
141 coeffs[8] *= ZH2_norm;
142 /* Third-order */
143 coeffs[9] *= ZH3_norm;
144 coeffs[10] *= ZH3_norm;
145 coeffs[11] *= ZH3_norm;
146 coeffs[12] *= ZH3_norm;
147 coeffs[13] *= ZH3_norm;
148 coeffs[14] *= ZH3_norm;
149 coeffs[15] *= ZH3_norm;
153 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
155 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
156 if(!(fabsf(azimuth) > F_PI_2))
157 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
158 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
162 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
164 ALsizei i, j;
166 for(i = 0;i < numchans;i++)
168 float gain = 0.0f;
169 for(j = 0;j < numcoeffs;j++)
170 gain += chancoeffs[i][j]*coeffs[j];
171 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
173 for(;i < MAX_OUTPUT_CHANNELS;i++)
174 gains[i] = 0.0f;
177 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
179 ALsizei i;
181 for(i = 0;i < numchans;i++)
182 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
183 for(;i < MAX_OUTPUT_CHANNELS;i++)
184 gains[i] = 0.0f;
187 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
189 ALsizei i, j;
191 for(i = 0;i < numchans;i++)
193 float gain = 0.0f;
194 for(j = 0;j < 4;j++)
195 gain += chancoeffs[i][j] * mtx[j];
196 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
198 for(;i < MAX_OUTPUT_CHANNELS;i++)
199 gains[i] = 0.0f;
202 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
204 ALsizei i;
206 for(i = 0;i < numchans;i++)
207 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
208 for(;i < MAX_OUTPUT_CHANNELS;i++)
209 gains[i] = 0.0f;
213 static inline const char *GetLabelFromChannel(enum Channel channel)
215 switch(channel)
217 case FrontLeft: return "front-left";
218 case FrontRight: return "front-right";
219 case FrontCenter: return "front-center";
220 case LFE: return "lfe";
221 case BackLeft: return "back-left";
222 case BackRight: return "back-right";
223 case BackCenter: return "back-center";
224 case SideLeft: return "side-left";
225 case SideRight: return "side-right";
227 case UpperFrontLeft: return "upper-front-left";
228 case UpperFrontRight: return "upper-front-right";
229 case UpperBackLeft: return "upper-back-left";
230 case UpperBackRight: return "upper-back-right";
231 case LowerFrontLeft: return "lower-front-left";
232 case LowerFrontRight: return "lower-front-right";
233 case LowerBackLeft: return "lower-back-left";
234 case LowerBackRight: return "lower-back-right";
236 case Aux0: return "aux-0";
237 case Aux1: return "aux-1";
238 case Aux2: return "aux-2";
239 case Aux3: return "aux-3";
240 case Aux4: return "aux-4";
241 case Aux5: return "aux-5";
242 case Aux6: return "aux-6";
243 case Aux7: return "aux-7";
244 case Aux8: return "aux-8";
245 case Aux9: return "aux-9";
246 case Aux10: return "aux-10";
247 case Aux11: return "aux-11";
248 case Aux12: return "aux-12";
249 case Aux13: return "aux-13";
250 case Aux14: return "aux-14";
251 case Aux15: return "aux-15";
253 case InvalidChannel: break;
255 return "(unknown)";
259 typedef struct ChannelMap {
260 enum Channel ChanName;
261 ChannelConfig Config;
262 } ChannelMap;
264 static void SetChannelMap(const enum Channel devchans[MAX_OUTPUT_CHANNELS],
265 ChannelConfig *ambicoeffs, const ChannelMap *chanmap,
266 ALsizei count, ALsizei *outcount)
268 ALsizei maxchans = 0;
269 ALsizei i, j;
271 for(i = 0;i < count;i++)
273 ALint idx = GetChannelIndex(devchans, chanmap[i].ChanName);
274 if(idx < 0)
276 ERR("Failed to find %s channel in device\n",
277 GetLabelFromChannel(chanmap[i].ChanName));
278 continue;
281 maxchans = maxi(maxchans, idx+1);
282 for(j = 0;j < MAX_AMBI_COEFFS;j++)
283 ambicoeffs[idx][j] = chanmap[i].Config[j];
285 *outcount = mini(maxchans, MAX_OUTPUT_CHANNELS);
288 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
290 ALsizei i;
292 for(i = 0;i < conf->NumSpeakers;i++)
294 enum Channel ch;
295 int chidx = -1;
297 /* NOTE: AmbDec does not define any standard speaker names, however
298 * for this to work we have to by able to find the output channel
299 * the speaker definition corresponds to. Therefore, OpenAL Soft
300 * requires these channel labels to be recognized:
302 * LF = Front left
303 * RF = Front right
304 * LS = Side left
305 * RS = Side right
306 * LB = Back left
307 * RB = Back right
308 * CE = Front center
309 * CB = Back center
311 * Additionally, surround51 will acknowledge back speakers for side
312 * channels, and surround51rear will acknowledge side speakers for
313 * back channels, to avoid issues with an ambdec expecting 5.1 to
314 * use the side channels when the device is configured for back,
315 * and vice-versa.
317 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
318 ch = FrontLeft;
319 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
320 ch = FrontRight;
321 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
322 ch = FrontCenter;
323 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
325 if(device->FmtChans == DevFmtX51Rear)
326 ch = BackLeft;
327 else
328 ch = SideLeft;
330 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
332 if(device->FmtChans == DevFmtX51Rear)
333 ch = BackRight;
334 else
335 ch = SideRight;
337 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
339 if(device->FmtChans == DevFmtX51)
340 ch = SideLeft;
341 else
342 ch = BackLeft;
344 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
346 if(device->FmtChans == DevFmtX51)
347 ch = SideRight;
348 else
349 ch = BackRight;
351 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
352 ch = BackCenter;
353 else
355 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
356 unsigned int n;
357 char c;
359 if(sscanf(name, "AUX%u%c", &n, &c) == 1 && n < 16)
360 ch = Aux0+n;
361 else
363 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
364 return false;
367 chidx = GetChannelIdxByName(&device->RealOut, ch);
368 if(chidx == -1)
370 ERR("Failed to lookup AmbDec speaker label %s\n",
371 alstr_get_cstr(conf->Speakers[i].Name));
372 return false;
374 speakermap[i] = chidx;
377 return true;
381 static const ChannelMap MonoCfg[1] = {
382 { FrontCenter, { 1.0f } },
383 }, StereoCfg[2] = {
384 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 5.52305643e-2f } },
385 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 5.52305643e-2f } },
386 }, QuadCfg[4] = {
387 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
388 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
389 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
390 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
391 }, X51SideCfg[4] = {
392 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
393 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
394 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
395 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
396 }, X51RearCfg[4] = {
397 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
398 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
399 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
400 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
401 }, X61Cfg[6] = {
402 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, 0.0f, -4.39996780e-2f, -2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
403 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 0.0f, 1.59658796e-1f, 6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
404 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 0.0f, 1.59658796e-1f, -6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
405 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, 0.0f, -4.39996780e-2f, 2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
406 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
407 }, X71Cfg[6] = {
408 { 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 } },
409 { 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 } },
410 { 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 } },
411 { 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 } },
412 { 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 } },
413 { 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 } },
416 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order,
417 const ALsizei *restrict chans_per_order)
419 const char *devname = alstr_get_cstr(device->DeviceName);
420 ALsizei i;
422 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
424 /* NFC is only used when AvgSpeakerDist is greater than 0, and can only
425 * be used when rendering to an ambisonic buffer.
427 device->AvgSpeakerDist = minf(ctrl_dist, 10.0f);
429 for(i = 0;i < order+1;i++)
430 device->Dry.NumChannelsPerOrder[i] = chans_per_order[i];
431 for(;i < MAX_AMBI_ORDER+1;i++)
432 device->Dry.NumChannelsPerOrder[i] = 0;
436 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
438 const char *devname = alstr_get_cstr(device->DeviceName);
439 ALfloat maxdist = 0.0f;
440 size_t total = 0;
441 ALsizei i;
443 for(i = 0;i < conf->NumSpeakers;i++)
444 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
446 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
448 ALfloat srate = (ALfloat)device->Frequency;
449 for(i = 0;i < conf->NumSpeakers;i++)
451 ALsizei chan = speakermap[i];
452 ALfloat delay;
454 /* Distance compensation only delays in steps of the sample rate.
455 * This is a bit less accurate since the delay time falls to the
456 * nearest sample time, but it's far simpler as it doesn't have to
457 * deal with phase offsets. This means at 48khz, for instance, the
458 * distance delay will be in steps of about 7 millimeters.
460 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
461 srate + 0.5f);
462 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
463 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
464 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
466 device->ChannelDelay[chan].Length = (ALsizei)clampf(
467 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
469 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
470 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
471 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
472 device->ChannelDelay[chan].Gain
475 /* Round up to the next 4th sample, so each channel buffer starts
476 * 16-byte aligned.
478 total += RoundUp(device->ChannelDelay[chan].Length, 4);
482 if(total > 0)
484 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
485 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
487 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
488 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
493 static void InitPanning(ALCdevice *device)
495 const ChannelMap *chanmap = NULL;
496 ALsizei coeffcount = 0;
497 ALsizei count = 0;
498 ALsizei i, j;
500 switch(device->FmtChans)
502 case DevFmtMono:
503 count = COUNTOF(MonoCfg);
504 chanmap = MonoCfg;
505 coeffcount = 1;
506 break;
508 case DevFmtStereo:
509 count = COUNTOF(StereoCfg);
510 chanmap = StereoCfg;
511 coeffcount = 4;
512 break;
514 case DevFmtQuad:
515 count = COUNTOF(QuadCfg);
516 chanmap = QuadCfg;
517 coeffcount = 4;
518 break;
520 case DevFmtX51:
521 count = COUNTOF(X51SideCfg);
522 chanmap = X51SideCfg;
523 coeffcount = 9;
524 break;
526 case DevFmtX51Rear:
527 count = COUNTOF(X51RearCfg);
528 chanmap = X51RearCfg;
529 coeffcount = 9;
530 break;
532 case DevFmtX61:
533 count = COUNTOF(X61Cfg);
534 chanmap = X61Cfg;
535 coeffcount = 9;
536 break;
538 case DevFmtX71:
539 count = COUNTOF(X71Cfg);
540 chanmap = X71Cfg;
541 coeffcount = 16;
542 break;
544 case DevFmtAmbi3D:
545 break;
548 if(device->FmtChans == DevFmtAmbi3D)
550 const char *devname = alstr_get_cstr(device->DeviceName);
551 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
552 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
553 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
554 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ N3D2N3DScale;
555 ALfloat nfc_delay = 0.0f;
557 count = (device->AmbiOrder == 3) ? 16 :
558 (device->AmbiOrder == 2) ? 9 :
559 (device->AmbiOrder == 1) ? 4 : 1;
560 for(i = 0;i < count;i++)
562 ALsizei acn = acnmap[i];
563 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
564 device->Dry.Ambi.Map[i].Index = acn;
566 device->Dry.CoeffCount = 0;
567 device->Dry.NumChannels = count;
569 if(device->AmbiOrder < 2)
571 device->FOAOut.Ambi = device->Dry.Ambi;
572 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
573 device->FOAOut.NumChannels = 0;
575 else
577 ALfloat w_scale=1.0f, xyz_scale=1.0f;
579 /* FOA output is always ACN+N3D for higher-order ambisonic output.
580 * The upsampler expects this and will convert it for output.
582 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
583 for(i = 0;i < 4;i++)
585 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
586 device->FOAOut.Ambi.Map[i].Index = i;
588 device->FOAOut.CoeffCount = 0;
589 device->FOAOut.NumChannels = 4;
591 if(device->AmbiOrder >= 3)
593 w_scale = W_SCALE_3H3P;
594 xyz_scale = XYZ_SCALE_3H3P;
596 else
598 w_scale = W_SCALE_2H2P;
599 xyz_scale = XYZ_SCALE_2H2P;
601 ambiup_reset(device->AmbiUp, device, w_scale, xyz_scale);
604 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
606 static const ALsizei chans_per_order[MAX_AMBI_ORDER+1] = {
607 1, 3, 5, 7
609 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
610 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
611 device->AmbiOrder, chans_per_order);
614 else
616 ALfloat w_scale, xyz_scale;
618 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
619 chanmap, count, &device->Dry.NumChannels);
620 device->Dry.CoeffCount = coeffcount;
622 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE_3H0P :
623 (device->Dry.CoeffCount > 4) ? W_SCALE_2H0P : 1.0f;
624 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE_3H0P :
625 (device->Dry.CoeffCount > 4) ? XYZ_SCALE_2H0P : 1.0f;
627 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
628 for(i = 0;i < device->Dry.NumChannels;i++)
630 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
631 for(j = 1;j < 4;j++)
632 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
634 device->FOAOut.CoeffCount = 4;
635 device->FOAOut.NumChannels = 0;
637 device->RealOut.NumChannels = 0;
640 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
642 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
643 const ALfloat *coeff_scale = N3D2N3DScale;
644 ALfloat w_scale = 1.0f;
645 ALfloat xyz_scale = 1.0f;
646 ALsizei i, j;
648 if(conf->FreqBands != 1)
649 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
650 conf->XOverFreq);
652 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
654 if(conf->ChanMask > 0x1ff)
656 w_scale = W_SCALE_3H3P;
657 xyz_scale = XYZ_SCALE_3H3P;
659 else if(conf->ChanMask > 0xf)
661 w_scale = W_SCALE_2H2P;
662 xyz_scale = XYZ_SCALE_2H2P;
665 else
667 if(conf->ChanMask > 0x1ff)
669 w_scale = W_SCALE_3H0P;
670 xyz_scale = XYZ_SCALE_3H0P;
672 else if(conf->ChanMask > 0xf)
674 w_scale = W_SCALE_2H0P;
675 xyz_scale = XYZ_SCALE_2H0P;
679 if(conf->CoeffScale == ADS_SN3D)
680 coeff_scale = SN3D2N3DScale;
681 else if(conf->CoeffScale == ADS_FuMa)
682 coeff_scale = FuMa2N3DScale;
684 for(i = 0;i < conf->NumSpeakers;i++)
686 ALsizei chan = speakermap[i];
687 ALfloat gain;
688 ALsizei k = 0;
690 for(j = 0;j < MAX_AMBI_COEFFS;j++)
691 chanmap[i].Config[j] = 0.0f;
693 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
694 for(j = 0;j < MAX_AMBI_COEFFS;j++)
696 if(j == 0) gain = conf->HFOrderGain[0];
697 else if(j == 1) gain = conf->HFOrderGain[1];
698 else if(j == 4) gain = conf->HFOrderGain[2];
699 else if(j == 9) gain = conf->HFOrderGain[3];
700 if((conf->ChanMask&(1<<j)))
701 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
705 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
706 conf->NumSpeakers, &device->Dry.NumChannels);
707 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
708 (conf->ChanMask > 0xf) ? 9 : 4;
710 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
711 for(i = 0;i < device->Dry.NumChannels;i++)
713 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
714 for(j = 1;j < 4;j++)
715 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
717 device->FOAOut.CoeffCount = 4;
718 device->FOAOut.NumChannels = 0;
720 device->RealOut.NumChannels = 0;
722 InitDistanceComp(device, conf, speakermap);
725 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
727 static const ALsizei chans_per_order2d[MAX_AMBI_ORDER+1] = { 1, 2, 2, 2 };
728 static const ALsizei chans_per_order3d[MAX_AMBI_ORDER+1] = { 1, 3, 5, 7 };
729 ALfloat avg_dist;
730 ALsizei count;
731 ALsizei i;
733 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
735 count = (conf->ChanMask > 0x1ff) ? 16 :
736 (conf->ChanMask > 0xf) ? 9 : 4;
737 for(i = 0;i < count;i++)
739 device->Dry.Ambi.Map[i].Scale = 1.0f;
740 device->Dry.Ambi.Map[i].Index = i;
743 else
745 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
747 count = (conf->ChanMask > 0x1ff) ? 7 :
748 (conf->ChanMask > 0xf) ? 5 : 3;
749 for(i = 0;i < count;i++)
751 device->Dry.Ambi.Map[i].Scale = 1.0f;
752 device->Dry.Ambi.Map[i].Index = map[i];
755 device->Dry.CoeffCount = 0;
756 device->Dry.NumChannels = count;
758 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
759 (conf->FreqBands == 1) ? "single" : "dual",
760 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
761 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
763 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
765 if(!(conf->ChanMask > 0xf))
767 device->FOAOut.Ambi = device->Dry.Ambi;
768 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
769 device->FOAOut.NumChannels = 0;
771 else
773 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
774 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
776 count = 4;
777 for(i = 0;i < count;i++)
779 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
780 device->FOAOut.Ambi.Map[i].Index = i;
783 else
785 static const int map[3] = { 0, 1, 3 };
786 count = 3;
787 for(i = 0;i < count;i++)
789 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
790 device->FOAOut.Ambi.Map[i].Index = map[i];
793 device->FOAOut.CoeffCount = 0;
794 device->FOAOut.NumChannels = count;
797 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
799 avg_dist = 0.0f;
800 for(i = 0;i < conf->NumSpeakers;i++)
801 avg_dist += conf->Speakers[i].Distance;
802 avg_dist /= (ALfloat)conf->NumSpeakers;
803 InitNearFieldCtrl(device, avg_dist,
804 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
805 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? chans_per_order3d : chans_per_order2d
808 InitDistanceComp(device, conf, speakermap);
811 static void InitHrtfPanning(ALCdevice *device)
813 /* NOTE: azimuth goes clockwise. */
814 static const struct AngularPoint AmbiPoints[] = {
815 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
816 { DEG2RAD( 35.2643897f), DEG2RAD( 45.0f) },
817 { DEG2RAD( 35.2643897f), DEG2RAD( 135.0f) },
818 { DEG2RAD( 35.2643897f), DEG2RAD(-135.0f) },
819 { DEG2RAD( 35.2643897f), DEG2RAD( -45.0f) },
820 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
821 { DEG2RAD( 0.0f), DEG2RAD( 45.0f) },
822 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
823 { DEG2RAD( 0.0f), DEG2RAD( 135.0f) },
824 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
825 { DEG2RAD( 0.0f), DEG2RAD(-135.0f) },
826 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
827 { DEG2RAD( 0.0f), DEG2RAD( -45.0f) },
828 { DEG2RAD(-35.2643897f), DEG2RAD( 45.0f) },
829 { DEG2RAD(-35.2643897f), DEG2RAD( 135.0f) },
830 { DEG2RAD(-35.2643897f), DEG2RAD(-135.0f) },
831 { DEG2RAD(-35.2643897f), DEG2RAD( -45.0f) },
832 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
834 static const ALfloat AmbiMatrixFOA[][MAX_AMBI_COEFFS] = {
835 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f },
836 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f },
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, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f },
841 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
842 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
843 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
844 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f },
845 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
846 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
847 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
848 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, 5.00000000e-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, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f },
853 }, AmbiMatrixHOA[][MAX_AMBI_COEFFS] = {
854 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
855 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f },
856 { 5.55555556e-02f, -5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
857 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, -5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f },
858 { 5.55555556e-02f, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
859 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f, 0.00000000e+00f, 1.29099445e-01f },
860 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, -6.83467648e-02f, 0.00000000e+00f },
861 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
862 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f, 6.83467648e-02f, 0.00000000e+00f },
863 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f, 0.00000000e+00f, 1.29099445e-01f },
864 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f, -6.83467648e-02f, 0.00000000e+00f },
865 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
866 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, 6.83467648e-02f, 0.00000000e+00f },
867 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f },
868 { 5.55555556e-02f, -5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
869 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, -5.00000000e-02f, -4.55645099e-02f, 0.00000000e+00f },
870 { 5.55555556e-02f, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
871 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
873 static const ALfloat AmbiOrderHFGainFOA[MAX_AMBI_ORDER+1] = {
874 3.00000000e+00f, 1.73205081e+00f
875 }, AmbiOrderHFGainHOA[MAX_AMBI_ORDER+1] = {
876 2.40192231e+00f, 1.86052102e+00f, 9.60768923e-01f
878 static const ALsizei IndexMap[6] = { 0, 1, 2, 3, 4, 8 };
879 static const ALsizei ChansPerOrder[MAX_AMBI_ORDER+1] = { 1, 3, 2, 0 };
880 const ALfloat (*restrict AmbiMatrix)[MAX_AMBI_COEFFS] = AmbiMatrixFOA;
881 const ALfloat *restrict AmbiOrderHFGain = AmbiOrderHFGainFOA;
882 ALsizei count = 4;
883 ALsizei i;
885 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixFOA), "FOA Ambisonic HRTF mismatch");
886 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixHOA), "HOA Ambisonic HRTF mismatch");
888 if(device->AmbiUp)
890 AmbiMatrix = AmbiMatrixHOA;
891 AmbiOrderHFGain = AmbiOrderHFGainHOA;
892 count = COUNTOF(IndexMap);
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 = IndexMap[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,
934 ChansPerOrder);
937 static void InitUhjPanning(ALCdevice *device)
939 ALsizei count = 3;
940 ALsizei i;
942 for(i = 0;i < count;i++)
944 ALsizei acn = FuMa2ACN[i];
945 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
946 device->Dry.Ambi.Map[i].Index = acn;
948 device->Dry.CoeffCount = 0;
949 device->Dry.NumChannels = count;
951 device->FOAOut.Ambi = device->Dry.Ambi;
952 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
953 device->FOAOut.NumChannels = 0;
955 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
958 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
960 /* Hold the HRTF the device last used, in case it's used again. */
961 struct Hrtf *old_hrtf = device->HrtfHandle;
962 const char *mode;
963 bool headphones;
964 int bs2blevel;
965 size_t i;
967 al_free(device->Hrtf);
968 device->Hrtf = NULL;
969 device->HrtfHandle = NULL;
970 alstr_clear(&device->HrtfName);
971 device->Render_Mode = NormalRender;
973 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
974 device->Dry.CoeffCount = 0;
975 device->Dry.NumChannels = 0;
976 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
977 device->Dry.NumChannelsPerOrder[i] = 0;
979 device->AvgSpeakerDist = 0.0f;
980 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
981 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
983 device->ChannelDelay[i].Gain = 1.0f;
984 device->ChannelDelay[i].Length = 0;
987 al_free(device->Stablizer);
988 device->Stablizer = NULL;
990 if(device->FmtChans != DevFmtStereo)
992 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
993 const char *devname, *layout = NULL;
994 AmbDecConf conf, *pconf = NULL;
996 if(old_hrtf)
997 Hrtf_DecRef(old_hrtf);
998 old_hrtf = NULL;
999 if(hrtf_appreq == Hrtf_Enable)
1000 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1002 ambdec_init(&conf);
1004 devname = alstr_get_cstr(device->DeviceName);
1005 switch(device->FmtChans)
1007 case DevFmtQuad: layout = "quad"; break;
1008 case DevFmtX51: /* fall-through */
1009 case DevFmtX51Rear: layout = "surround51"; break;
1010 case DevFmtX61: layout = "surround61"; break;
1011 case DevFmtX71: layout = "surround71"; break;
1012 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1013 case DevFmtMono:
1014 case DevFmtStereo:
1015 case DevFmtAmbi3D:
1016 break;
1018 if(layout)
1020 const char *fname;
1021 if(ConfigValueStr(devname, "decoder", layout, &fname))
1023 if(!ambdec_load(&conf, fname))
1024 ERR("Failed to load layout file %s\n", fname);
1025 else
1027 if(conf.ChanMask > 0xffff)
1028 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1029 else
1031 if(MakeSpeakerMap(device, &conf, speakermap))
1032 pconf = &conf;
1038 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1040 ambiup_free(&device->AmbiUp);
1041 if(!device->AmbiDecoder)
1042 device->AmbiDecoder = bformatdec_alloc();
1044 else
1046 bformatdec_free(&device->AmbiDecoder);
1047 if(device->FmtChans != DevFmtAmbi3D || device->AmbiOrder < 2)
1048 ambiup_free(&device->AmbiUp);
1049 else
1051 if(!device->AmbiUp)
1052 device->AmbiUp = ambiup_alloc();
1056 if(!pconf)
1057 InitPanning(device);
1058 else if(device->AmbiDecoder)
1059 InitHQPanning(device, pconf, speakermap);
1060 else
1061 InitCustomPanning(device, pconf, speakermap);
1063 /* Enable the stablizer only for formats that have front-left, front-
1064 * right, and front-center outputs.
1066 switch(device->FmtChans)
1068 case DevFmtX51:
1069 case DevFmtX51Rear:
1070 case DevFmtX61:
1071 case DevFmtX71:
1072 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1074 /* Initialize band-splitting filters for the front-left and
1075 * front-right channels, with a crossover at 5khz (could be
1076 * higher).
1078 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1079 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1081 bandsplit_init(&stablizer->LFilter, scale);
1082 stablizer->RFilter = stablizer->LFilter;
1084 /* Initialize all-pass filters for all other channels. */
1085 splitterap_init(&stablizer->APFilter[0], scale);
1086 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1087 stablizer->APFilter[i] = stablizer->APFilter[0];
1089 device->Stablizer = stablizer;
1091 break;
1092 case DevFmtMono:
1093 case DevFmtStereo:
1094 case DevFmtQuad:
1095 case DevFmtAmbi3D:
1096 break;
1098 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1100 ambdec_deinit(&conf);
1101 return;
1104 bformatdec_free(&device->AmbiDecoder);
1106 headphones = device->IsHeadphones;
1107 if(device->Type != Loopback)
1109 const char *mode;
1110 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1112 if(strcasecmp(mode, "headphones") == 0)
1113 headphones = true;
1114 else if(strcasecmp(mode, "speakers") == 0)
1115 headphones = false;
1116 else if(strcasecmp(mode, "auto") != 0)
1117 ERR("Unexpected stereo-mode: %s\n", mode);
1121 if(hrtf_userreq == Hrtf_Default)
1123 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1124 (hrtf_appreq == Hrtf_Enable);
1125 if(!usehrtf) goto no_hrtf;
1127 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1128 if(headphones && hrtf_appreq != Hrtf_Disable)
1129 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1131 else
1133 if(hrtf_userreq != Hrtf_Enable)
1135 if(hrtf_appreq == Hrtf_Enable)
1136 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1137 goto no_hrtf;
1139 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1142 if(VECTOR_SIZE(device->HrtfList) == 0)
1144 VECTOR_DEINIT(device->HrtfList);
1145 device->HrtfList = EnumerateHrtf(device->DeviceName);
1148 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1150 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1151 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1152 if(hrtf && hrtf->sampleRate == device->Frequency)
1154 device->HrtfHandle = hrtf;
1155 alstr_copy(&device->HrtfName, entry->name);
1157 else if(hrtf)
1158 Hrtf_DecRef(hrtf);
1161 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1163 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1164 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1165 if(hrtf && hrtf->sampleRate == device->Frequency)
1167 device->HrtfHandle = hrtf;
1168 alstr_copy(&device->HrtfName, entry->name);
1170 else if(hrtf)
1171 Hrtf_DecRef(hrtf);
1174 if(device->HrtfHandle)
1176 if(old_hrtf)
1177 Hrtf_DecRef(old_hrtf);
1178 old_hrtf = NULL;
1180 device->Render_Mode = HrtfRender;
1181 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1183 if(strcasecmp(mode, "full") == 0)
1184 device->Render_Mode = HrtfRender;
1185 else if(strcasecmp(mode, "basic") == 0)
1186 device->Render_Mode = NormalRender;
1187 else
1188 ERR("Unexpected hrtf-mode: %s\n", mode);
1191 if(device->Render_Mode == HrtfRender)
1193 /* Don't bother with HOA when using full HRTF rendering. Nothing
1194 * needs it, and it eases the CPU/memory load.
1196 ambiup_free(&device->AmbiUp);
1198 else
1200 if(!device->AmbiUp)
1201 device->AmbiUp = ambiup_alloc();
1204 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1205 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1206 alstr_get_cstr(device->HrtfName)
1208 InitHrtfPanning(device);
1209 return;
1211 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1213 no_hrtf:
1214 if(old_hrtf)
1215 Hrtf_DecRef(old_hrtf);
1216 old_hrtf = NULL;
1217 TRACE("HRTF disabled\n");
1219 device->Render_Mode = StereoPair;
1221 ambiup_free(&device->AmbiUp);
1223 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1224 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1225 if(device->Type != Loopback)
1226 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1227 if(bs2blevel > 0 && bs2blevel <= 6)
1229 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1230 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1231 TRACE("BS2B enabled\n");
1232 InitPanning(device);
1233 return;
1236 TRACE("BS2B disabled\n");
1238 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1240 if(strcasecmp(mode, "uhj") == 0)
1241 device->Render_Mode = NormalRender;
1242 else if(strcasecmp(mode, "panpot") != 0)
1243 ERR("Unexpected stereo-encoding: %s\n", mode);
1245 if(device->Render_Mode == NormalRender)
1247 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1248 TRACE("UHJ enabled\n");
1249 InitUhjPanning(device);
1250 return;
1253 TRACE("UHJ disabled\n");
1254 InitPanning(device);
1258 void aluInitEffectPanning(ALeffectslot *slot)
1260 ALsizei i;
1262 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1263 slot->NumChannels = 0;
1265 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1267 slot->ChanMap[i].Scale = 1.0f;
1268 slot->ChanMap[i].Index = i;
1270 slot->NumChannels = i;