Trace the NFC reference distance when set
[openal-soft.git] / Alc / panning.c
blob51d65a24f87c7960ec70f1a8b841123ccfb76462
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);
428 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
430 for(i = 0;i < order+1;i++)
431 device->Dry.NumChannelsPerOrder[i] = chans_per_order[i];
432 for(;i < MAX_AMBI_ORDER+1;i++)
433 device->Dry.NumChannelsPerOrder[i] = 0;
437 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
439 const char *devname = alstr_get_cstr(device->DeviceName);
440 ALfloat maxdist = 0.0f;
441 size_t total = 0;
442 ALsizei i;
444 for(i = 0;i < conf->NumSpeakers;i++)
445 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
447 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
449 ALfloat srate = (ALfloat)device->Frequency;
450 for(i = 0;i < conf->NumSpeakers;i++)
452 ALsizei chan = speakermap[i];
453 ALfloat delay;
455 /* Distance compensation only delays in steps of the sample rate.
456 * This is a bit less accurate since the delay time falls to the
457 * nearest sample time, but it's far simpler as it doesn't have to
458 * deal with phase offsets. This means at 48khz, for instance, the
459 * distance delay will be in steps of about 7 millimeters.
461 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
462 srate + 0.5f);
463 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
464 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
465 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
467 device->ChannelDelay[chan].Length = (ALsizei)clampf(
468 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
470 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
471 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
472 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
473 device->ChannelDelay[chan].Gain
476 /* Round up to the next 4th sample, so each channel buffer starts
477 * 16-byte aligned.
479 total += RoundUp(device->ChannelDelay[chan].Length, 4);
483 if(total > 0)
485 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
486 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
488 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
489 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
494 static void InitPanning(ALCdevice *device)
496 const ChannelMap *chanmap = NULL;
497 ALsizei coeffcount = 0;
498 ALsizei count = 0;
499 ALsizei i, j;
501 switch(device->FmtChans)
503 case DevFmtMono:
504 count = COUNTOF(MonoCfg);
505 chanmap = MonoCfg;
506 coeffcount = 1;
507 break;
509 case DevFmtStereo:
510 count = COUNTOF(StereoCfg);
511 chanmap = StereoCfg;
512 coeffcount = 4;
513 break;
515 case DevFmtQuad:
516 count = COUNTOF(QuadCfg);
517 chanmap = QuadCfg;
518 coeffcount = 4;
519 break;
521 case DevFmtX51:
522 count = COUNTOF(X51SideCfg);
523 chanmap = X51SideCfg;
524 coeffcount = 9;
525 break;
527 case DevFmtX51Rear:
528 count = COUNTOF(X51RearCfg);
529 chanmap = X51RearCfg;
530 coeffcount = 9;
531 break;
533 case DevFmtX61:
534 count = COUNTOF(X61Cfg);
535 chanmap = X61Cfg;
536 coeffcount = 9;
537 break;
539 case DevFmtX71:
540 count = COUNTOF(X71Cfg);
541 chanmap = X71Cfg;
542 coeffcount = 16;
543 break;
545 case DevFmtAmbi3D:
546 break;
549 if(device->FmtChans == DevFmtAmbi3D)
551 const char *devname = alstr_get_cstr(device->DeviceName);
552 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
553 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
554 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
555 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ N3D2N3DScale;
556 ALfloat nfc_delay = 0.0f;
558 count = (device->AmbiOrder == 3) ? 16 :
559 (device->AmbiOrder == 2) ? 9 :
560 (device->AmbiOrder == 1) ? 4 : 1;
561 for(i = 0;i < count;i++)
563 ALsizei acn = acnmap[i];
564 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
565 device->Dry.Ambi.Map[i].Index = acn;
567 device->Dry.CoeffCount = 0;
568 device->Dry.NumChannels = count;
570 if(device->AmbiOrder < 2)
572 device->FOAOut.Ambi = device->Dry.Ambi;
573 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
574 device->FOAOut.NumChannels = 0;
576 else
578 ALfloat w_scale=1.0f, xyz_scale=1.0f;
580 /* FOA output is always ACN+N3D for higher-order ambisonic output.
581 * The upsampler expects this and will convert it for output.
583 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
584 for(i = 0;i < 4;i++)
586 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
587 device->FOAOut.Ambi.Map[i].Index = i;
589 device->FOAOut.CoeffCount = 0;
590 device->FOAOut.NumChannels = 4;
592 if(device->AmbiOrder >= 3)
594 w_scale = W_SCALE_3H3P;
595 xyz_scale = XYZ_SCALE_3H3P;
597 else
599 w_scale = W_SCALE_2H2P;
600 xyz_scale = XYZ_SCALE_2H2P;
602 ambiup_reset(device->AmbiUp, device, w_scale, xyz_scale);
605 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
607 static const ALsizei chans_per_order[MAX_AMBI_ORDER+1] = {
608 1, 3, 5, 7
610 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
611 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
612 device->AmbiOrder, chans_per_order);
615 else
617 ALfloat w_scale, xyz_scale;
619 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
620 chanmap, count, &device->Dry.NumChannels);
621 device->Dry.CoeffCount = coeffcount;
623 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE_3H0P :
624 (device->Dry.CoeffCount > 4) ? W_SCALE_2H0P : 1.0f;
625 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE_3H0P :
626 (device->Dry.CoeffCount > 4) ? XYZ_SCALE_2H0P : 1.0f;
628 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
629 for(i = 0;i < device->Dry.NumChannels;i++)
631 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
632 for(j = 1;j < 4;j++)
633 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
635 device->FOAOut.CoeffCount = 4;
636 device->FOAOut.NumChannels = 0;
638 device->RealOut.NumChannels = 0;
641 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
643 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
644 const ALfloat *coeff_scale = N3D2N3DScale;
645 ALfloat w_scale = 1.0f;
646 ALfloat xyz_scale = 1.0f;
647 ALsizei i, j;
649 if(conf->FreqBands != 1)
650 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
651 conf->XOverFreq);
653 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
655 if(conf->ChanMask > 0x1ff)
657 w_scale = W_SCALE_3H3P;
658 xyz_scale = XYZ_SCALE_3H3P;
660 else if(conf->ChanMask > 0xf)
662 w_scale = W_SCALE_2H2P;
663 xyz_scale = XYZ_SCALE_2H2P;
666 else
668 if(conf->ChanMask > 0x1ff)
670 w_scale = W_SCALE_3H0P;
671 xyz_scale = XYZ_SCALE_3H0P;
673 else if(conf->ChanMask > 0xf)
675 w_scale = W_SCALE_2H0P;
676 xyz_scale = XYZ_SCALE_2H0P;
680 if(conf->CoeffScale == ADS_SN3D)
681 coeff_scale = SN3D2N3DScale;
682 else if(conf->CoeffScale == ADS_FuMa)
683 coeff_scale = FuMa2N3DScale;
685 for(i = 0;i < conf->NumSpeakers;i++)
687 ALsizei chan = speakermap[i];
688 ALfloat gain;
689 ALsizei k = 0;
691 for(j = 0;j < MAX_AMBI_COEFFS;j++)
692 chanmap[i].Config[j] = 0.0f;
694 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
695 for(j = 0;j < MAX_AMBI_COEFFS;j++)
697 if(j == 0) gain = conf->HFOrderGain[0];
698 else if(j == 1) gain = conf->HFOrderGain[1];
699 else if(j == 4) gain = conf->HFOrderGain[2];
700 else if(j == 9) gain = conf->HFOrderGain[3];
701 if((conf->ChanMask&(1<<j)))
702 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
706 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
707 conf->NumSpeakers, &device->Dry.NumChannels);
708 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
709 (conf->ChanMask > 0xf) ? 9 : 4;
711 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
712 for(i = 0;i < device->Dry.NumChannels;i++)
714 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
715 for(j = 1;j < 4;j++)
716 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
718 device->FOAOut.CoeffCount = 4;
719 device->FOAOut.NumChannels = 0;
721 device->RealOut.NumChannels = 0;
723 InitDistanceComp(device, conf, speakermap);
726 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
728 static const ALsizei chans_per_order2d[MAX_AMBI_ORDER+1] = { 1, 2, 2, 2 };
729 static const ALsizei chans_per_order3d[MAX_AMBI_ORDER+1] = { 1, 3, 5, 7 };
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) ? chans_per_order3d : chans_per_order2d
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.2643897f), DEG2RAD( 45.0f) },
818 { DEG2RAD( 35.2643897f), DEG2RAD( 135.0f) },
819 { DEG2RAD( 35.2643897f), DEG2RAD(-135.0f) },
820 { DEG2RAD( 35.2643897f), 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.2643897f), DEG2RAD( 45.0f) },
830 { DEG2RAD(-35.2643897f), DEG2RAD( 135.0f) },
831 { DEG2RAD(-35.2643897f), DEG2RAD(-135.0f) },
832 { DEG2RAD(-35.2643897f), 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 },
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, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
860 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f, 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 },
862 { 5.55555556e-02f, -8.66025404e-02f, 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 },
864 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f, 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 },
866 { 5.55555556e-02f, 8.66025404e-02f, 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 },
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, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
872 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
874 static const ALfloat AmbiOrderHFGainFOA[MAX_AMBI_ORDER+1] = {
875 3.00000000e+00f, 1.73205081e+00f
876 }, AmbiOrderHFGainHOA[MAX_AMBI_ORDER+1] = {
877 2.40192231e+00f, 1.86052102e+00f, 9.60768923e-01f
879 static const ALsizei IndexMap[6] = { 0, 1, 2, 3, 4, 8 };
880 static const ALsizei ChansPerOrder[MAX_AMBI_ORDER+1] = { 1, 3, 2, 0 };
881 const ALfloat (*restrict AmbiMatrix)[MAX_AMBI_COEFFS] = AmbiMatrixFOA;
882 const ALfloat *restrict AmbiOrderHFGain = AmbiOrderHFGainFOA;
883 ALsizei count = 4;
884 ALsizei i;
886 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixFOA), "FOA Ambisonic HRTF mismatch");
887 static_assert(COUNTOF(AmbiPoints) == COUNTOF(AmbiMatrixHOA), "HOA Ambisonic HRTF mismatch");
889 if(device->AmbiUp)
891 AmbiMatrix = AmbiMatrixHOA;
892 AmbiOrderHFGain = AmbiOrderHFGainHOA;
893 count = COUNTOF(IndexMap);
896 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
898 for(i = 0;i < count;i++)
900 device->Dry.Ambi.Map[i].Scale = 1.0f;
901 device->Dry.Ambi.Map[i].Index = IndexMap[i];
903 device->Dry.CoeffCount = 0;
904 device->Dry.NumChannels = count;
906 if(device->AmbiUp)
908 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
909 for(i = 0;i < 4;i++)
911 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
912 device->FOAOut.Ambi.Map[i].Index = i;
914 device->FOAOut.CoeffCount = 0;
915 device->FOAOut.NumChannels = 4;
917 ambiup_reset(device->AmbiUp, device, AmbiOrderHFGainFOA[0] / AmbiOrderHFGain[0],
918 AmbiOrderHFGainFOA[1] / AmbiOrderHFGain[1]);
920 else
922 device->FOAOut.Ambi = device->Dry.Ambi;
923 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
924 device->FOAOut.NumChannels = 0;
927 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
929 BuildBFormatHrtf(device->HrtfHandle,
930 device->Hrtf, device->Dry.NumChannels, AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints),
931 AmbiOrderHFGain
934 InitNearFieldCtrl(device, device->HrtfHandle->distance, device->AmbiUp ? 2 : 1,
935 ChansPerOrder);
938 static void InitUhjPanning(ALCdevice *device)
940 ALsizei count = 3;
941 ALsizei i;
943 for(i = 0;i < count;i++)
945 ALsizei acn = FuMa2ACN[i];
946 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
947 device->Dry.Ambi.Map[i].Index = acn;
949 device->Dry.CoeffCount = 0;
950 device->Dry.NumChannels = count;
952 device->FOAOut.Ambi = device->Dry.Ambi;
953 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
954 device->FOAOut.NumChannels = 0;
956 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
959 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
961 /* Hold the HRTF the device last used, in case it's used again. */
962 struct Hrtf *old_hrtf = device->HrtfHandle;
963 const char *mode;
964 bool headphones;
965 int bs2blevel;
966 size_t i;
968 al_free(device->Hrtf);
969 device->Hrtf = NULL;
970 device->HrtfHandle = NULL;
971 alstr_clear(&device->HrtfName);
972 device->Render_Mode = NormalRender;
974 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
975 device->Dry.CoeffCount = 0;
976 device->Dry.NumChannels = 0;
977 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
978 device->Dry.NumChannelsPerOrder[i] = 0;
980 device->AvgSpeakerDist = 0.0f;
981 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
982 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
984 device->ChannelDelay[i].Gain = 1.0f;
985 device->ChannelDelay[i].Length = 0;
988 al_free(device->Stablizer);
989 device->Stablizer = NULL;
991 if(device->FmtChans != DevFmtStereo)
993 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
994 const char *devname, *layout = NULL;
995 AmbDecConf conf, *pconf = NULL;
997 if(old_hrtf)
998 Hrtf_DecRef(old_hrtf);
999 old_hrtf = NULL;
1000 if(hrtf_appreq == Hrtf_Enable)
1001 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1003 ambdec_init(&conf);
1005 devname = alstr_get_cstr(device->DeviceName);
1006 switch(device->FmtChans)
1008 case DevFmtQuad: layout = "quad"; break;
1009 case DevFmtX51: /* fall-through */
1010 case DevFmtX51Rear: layout = "surround51"; break;
1011 case DevFmtX61: layout = "surround61"; break;
1012 case DevFmtX71: layout = "surround71"; break;
1013 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1014 case DevFmtMono:
1015 case DevFmtStereo:
1016 case DevFmtAmbi3D:
1017 break;
1019 if(layout)
1021 const char *fname;
1022 if(ConfigValueStr(devname, "decoder", layout, &fname))
1024 if(!ambdec_load(&conf, fname))
1025 ERR("Failed to load layout file %s\n", fname);
1026 else
1028 if(conf.ChanMask > 0xffff)
1029 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1030 else
1032 if(MakeSpeakerMap(device, &conf, speakermap))
1033 pconf = &conf;
1039 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1041 ambiup_free(&device->AmbiUp);
1042 if(!device->AmbiDecoder)
1043 device->AmbiDecoder = bformatdec_alloc();
1045 else
1047 bformatdec_free(&device->AmbiDecoder);
1048 if(device->FmtChans != DevFmtAmbi3D || device->AmbiOrder < 2)
1049 ambiup_free(&device->AmbiUp);
1050 else
1052 if(!device->AmbiUp)
1053 device->AmbiUp = ambiup_alloc();
1057 if(!pconf)
1058 InitPanning(device);
1059 else if(device->AmbiDecoder)
1060 InitHQPanning(device, pconf, speakermap);
1061 else
1062 InitCustomPanning(device, pconf, speakermap);
1064 /* Enable the stablizer only for formats that have front-left, front-
1065 * right, and front-center outputs.
1067 switch(device->FmtChans)
1069 case DevFmtX51:
1070 case DevFmtX51Rear:
1071 case DevFmtX61:
1072 case DevFmtX71:
1073 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1075 /* Initialize band-splitting filters for the front-left and
1076 * front-right channels, with a crossover at 5khz (could be
1077 * higher).
1079 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1080 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1082 bandsplit_init(&stablizer->LFilter, scale);
1083 stablizer->RFilter = stablizer->LFilter;
1085 /* Initialize all-pass filters for all other channels. */
1086 splitterap_init(&stablizer->APFilter[0], scale);
1087 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1088 stablizer->APFilter[i] = stablizer->APFilter[0];
1090 device->Stablizer = stablizer;
1092 break;
1093 case DevFmtMono:
1094 case DevFmtStereo:
1095 case DevFmtQuad:
1096 case DevFmtAmbi3D:
1097 break;
1099 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1101 ambdec_deinit(&conf);
1102 return;
1105 bformatdec_free(&device->AmbiDecoder);
1107 headphones = device->IsHeadphones;
1108 if(device->Type != Loopback)
1110 const char *mode;
1111 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1113 if(strcasecmp(mode, "headphones") == 0)
1114 headphones = true;
1115 else if(strcasecmp(mode, "speakers") == 0)
1116 headphones = false;
1117 else if(strcasecmp(mode, "auto") != 0)
1118 ERR("Unexpected stereo-mode: %s\n", mode);
1122 if(hrtf_userreq == Hrtf_Default)
1124 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1125 (hrtf_appreq == Hrtf_Enable);
1126 if(!usehrtf) goto no_hrtf;
1128 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1129 if(headphones && hrtf_appreq != Hrtf_Disable)
1130 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1132 else
1134 if(hrtf_userreq != Hrtf_Enable)
1136 if(hrtf_appreq == Hrtf_Enable)
1137 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1138 goto no_hrtf;
1140 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1143 if(VECTOR_SIZE(device->HrtfList) == 0)
1145 VECTOR_DEINIT(device->HrtfList);
1146 device->HrtfList = EnumerateHrtf(device->DeviceName);
1149 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1151 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1152 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1153 if(hrtf && hrtf->sampleRate == device->Frequency)
1155 device->HrtfHandle = hrtf;
1156 alstr_copy(&device->HrtfName, entry->name);
1158 else if(hrtf)
1159 Hrtf_DecRef(hrtf);
1162 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1164 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1165 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1166 if(hrtf && hrtf->sampleRate == device->Frequency)
1168 device->HrtfHandle = hrtf;
1169 alstr_copy(&device->HrtfName, entry->name);
1171 else if(hrtf)
1172 Hrtf_DecRef(hrtf);
1175 if(device->HrtfHandle)
1177 if(old_hrtf)
1178 Hrtf_DecRef(old_hrtf);
1179 old_hrtf = NULL;
1181 device->Render_Mode = HrtfRender;
1182 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1184 if(strcasecmp(mode, "full") == 0)
1185 device->Render_Mode = HrtfRender;
1186 else if(strcasecmp(mode, "basic") == 0)
1187 device->Render_Mode = NormalRender;
1188 else
1189 ERR("Unexpected hrtf-mode: %s\n", mode);
1192 if(device->Render_Mode == HrtfRender)
1194 /* Don't bother with HOA when using full HRTF rendering. Nothing
1195 * needs it, and it eases the CPU/memory load.
1197 ambiup_free(&device->AmbiUp);
1199 else
1201 if(!device->AmbiUp)
1202 device->AmbiUp = ambiup_alloc();
1205 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1206 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1207 alstr_get_cstr(device->HrtfName)
1209 InitHrtfPanning(device);
1210 return;
1212 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1214 no_hrtf:
1215 if(old_hrtf)
1216 Hrtf_DecRef(old_hrtf);
1217 old_hrtf = NULL;
1218 TRACE("HRTF disabled\n");
1220 device->Render_Mode = StereoPair;
1222 ambiup_free(&device->AmbiUp);
1224 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1225 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1226 if(device->Type != Loopback)
1227 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1228 if(bs2blevel > 0 && bs2blevel <= 6)
1230 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1231 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1232 TRACE("BS2B enabled\n");
1233 InitPanning(device);
1234 return;
1237 TRACE("BS2B disabled\n");
1239 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1241 if(strcasecmp(mode, "uhj") == 0)
1242 device->Render_Mode = NormalRender;
1243 else if(strcasecmp(mode, "panpot") != 0)
1244 ERR("Unexpected stereo-encoding: %s\n", mode);
1246 if(device->Render_Mode == NormalRender)
1248 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1249 TRACE("UHJ enabled\n");
1250 InitUhjPanning(device);
1251 return;
1254 TRACE("UHJ disabled\n");
1255 InitPanning(device);
1259 void aluInitEffectPanning(ALeffectslot *slot)
1261 ALsizei i;
1263 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1264 slot->NumChannels = 0;
1266 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1268 slot->ChanMap[i].Scale = 1.0f;
1269 slot->ChanMap[i].Index = i;
1271 slot->NumChannels = i;