Use transposed direct form 2 for the UHJ all-pass filters
[openal-soft.git] / Alc / panning.c
blob5ff37d0a2c59cb3faabfdbd73375519f384de071
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 CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
42 extern inline void ComputeDryPanGains(const DryMixParams *dry, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
43 extern inline void ComputeFirstOrderGains(const BFMixParams *foa, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
46 static const ALsizei FuMa2ACN[MAX_AMBI_COEFFS] = {
47 0, /* W */
48 3, /* X */
49 1, /* Y */
50 2, /* Z */
51 6, /* R */
52 7, /* S */
53 5, /* T */
54 8, /* U */
55 4, /* V */
56 12, /* K */
57 13, /* L */
58 11, /* M */
59 14, /* N */
60 10, /* O */
61 15, /* P */
62 9, /* Q */
64 static const ALsizei ACN2ACN[MAX_AMBI_COEFFS] = {
65 0, 1, 2, 3, 4, 5, 6, 7,
66 8, 9, 10, 11, 12, 13, 14, 15
70 void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
72 /* Convert from OpenAL coords to Ambisonics. */
73 ALfloat x = -dir[2];
74 ALfloat y = -dir[0];
75 ALfloat z = dir[1];
77 /* Zeroth-order */
78 coeffs[0] = 1.0f; /* ACN 0 = 1 */
79 /* First-order */
80 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
81 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
82 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
83 /* Second-order */
84 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
85 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
86 coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
87 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
88 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
89 /* Third-order */
90 coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
91 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
92 coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
93 coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
94 coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
95 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
96 coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
98 if(spread > 0.0f)
100 /* Implement the spread by using a spherical source that subtends the
101 * angle spread. See:
102 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
104 * When adjusted for N3D normalization instead of SN3D, these
105 * calculations are:
107 * ZH0 = -sqrt(pi) * (-1+ca);
108 * ZH1 = 0.5*sqrt(pi) * sa*sa;
109 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
110 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
111 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
112 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
114 * The gain of the source is compensated for size, so that the
115 * loundness doesn't depend on the spread. Thus:
117 * ZH0 = 1.0f;
118 * ZH1 = 0.5f * (ca+1.0f);
119 * ZH2 = 0.5f * (ca+1.0f)*ca;
120 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
121 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
122 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
124 ALfloat ca = cosf(spread * 0.5f);
125 /* Increase the source volume by up to +3dB for a full spread. */
126 ALfloat scale = sqrtf(1.0f + spread/F_TAU);
128 ALfloat ZH0_norm = scale;
129 ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
130 ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
131 ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
133 /* Zeroth-order */
134 coeffs[0] *= ZH0_norm;
135 /* First-order */
136 coeffs[1] *= ZH1_norm;
137 coeffs[2] *= ZH1_norm;
138 coeffs[3] *= ZH1_norm;
139 /* Second-order */
140 coeffs[4] *= ZH2_norm;
141 coeffs[5] *= ZH2_norm;
142 coeffs[6] *= ZH2_norm;
143 coeffs[7] *= ZH2_norm;
144 coeffs[8] *= ZH2_norm;
145 /* Third-order */
146 coeffs[9] *= ZH3_norm;
147 coeffs[10] *= ZH3_norm;
148 coeffs[11] *= ZH3_norm;
149 coeffs[12] *= ZH3_norm;
150 coeffs[13] *= ZH3_norm;
151 coeffs[14] *= ZH3_norm;
152 coeffs[15] *= ZH3_norm;
156 void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
158 ALfloat sign = (azimuth < 0.0f) ? -1.0f : 1.0f;
159 if(!(fabsf(azimuth) > F_PI_2))
160 azimuth = minf(fabsf(azimuth) * F_PI_2 / (F_PI/6.0f), F_PI_2) * sign;
161 CalcAngleCoeffs(azimuth, elevation, spread, coeffs);
165 void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
167 ALsizei i, j;
169 for(i = 0;i < numchans;i++)
171 float gain = 0.0f;
172 for(j = 0;j < numcoeffs;j++)
173 gain += chancoeffs[i][j]*coeffs[j];
174 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
176 for(;i < MAX_OUTPUT_CHANNELS;i++)
177 gains[i] = 0.0f;
180 void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
182 ALsizei i;
184 for(i = 0;i < numchans;i++)
185 gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
186 for(;i < MAX_OUTPUT_CHANNELS;i++)
187 gains[i] = 0.0f;
190 void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
192 ALsizei i, j;
194 for(i = 0;i < numchans;i++)
196 float gain = 0.0f;
197 for(j = 0;j < 4;j++)
198 gain += chancoeffs[i][j] * mtx[j];
199 gains[i] = clampf(gain, 0.0f, 1.0f) * ingain;
201 for(;i < MAX_OUTPUT_CHANNELS;i++)
202 gains[i] = 0.0f;
205 void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
207 ALsizei i;
209 for(i = 0;i < numchans;i++)
210 gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
211 for(;i < MAX_OUTPUT_CHANNELS;i++)
212 gains[i] = 0.0f;
216 static inline const char *GetLabelFromChannel(enum Channel channel)
218 switch(channel)
220 case FrontLeft: return "front-left";
221 case FrontRight: return "front-right";
222 case FrontCenter: return "front-center";
223 case LFE: return "lfe";
224 case BackLeft: return "back-left";
225 case BackRight: return "back-right";
226 case BackCenter: return "back-center";
227 case SideLeft: return "side-left";
228 case SideRight: return "side-right";
230 case UpperFrontLeft: return "upper-front-left";
231 case UpperFrontRight: return "upper-front-right";
232 case UpperBackLeft: return "upper-back-left";
233 case UpperBackRight: return "upper-back-right";
234 case LowerFrontLeft: return "lower-front-left";
235 case LowerFrontRight: return "lower-front-right";
236 case LowerBackLeft: return "lower-back-left";
237 case LowerBackRight: return "lower-back-right";
239 case Aux0: return "aux-0";
240 case Aux1: return "aux-1";
241 case Aux2: return "aux-2";
242 case Aux3: return "aux-3";
243 case Aux4: return "aux-4";
244 case Aux5: return "aux-5";
245 case Aux6: return "aux-6";
246 case Aux7: return "aux-7";
247 case Aux8: return "aux-8";
248 case Aux9: return "aux-9";
249 case Aux10: return "aux-10";
250 case Aux11: return "aux-11";
251 case Aux12: return "aux-12";
252 case Aux13: return "aux-13";
253 case Aux14: return "aux-14";
254 case Aux15: return "aux-15";
256 case InvalidChannel: break;
258 return "(unknown)";
262 typedef struct ChannelMap {
263 enum Channel ChanName;
264 ChannelConfig Config;
265 } ChannelMap;
267 static void SetChannelMap(const enum Channel devchans[MAX_OUTPUT_CHANNELS],
268 ChannelConfig *ambicoeffs, const ChannelMap *chanmap,
269 ALsizei count, ALsizei *outcount)
271 ALsizei maxchans = 0;
272 ALsizei i, j;
274 for(i = 0;i < count;i++)
276 ALint idx = GetChannelIndex(devchans, chanmap[i].ChanName);
277 if(idx < 0)
279 ERR("Failed to find %s channel in device\n",
280 GetLabelFromChannel(chanmap[i].ChanName));
281 continue;
284 maxchans = maxi(maxchans, idx+1);
285 for(j = 0;j < MAX_AMBI_COEFFS;j++)
286 ambicoeffs[idx][j] = chanmap[i].Config[j];
288 *outcount = mini(maxchans, MAX_OUTPUT_CHANNELS);
291 static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei speakermap[MAX_OUTPUT_CHANNELS])
293 ALsizei i;
295 for(i = 0;i < conf->NumSpeakers;i++)
297 enum Channel ch;
298 int chidx = -1;
300 /* NOTE: AmbDec does not define any standard speaker names, however
301 * for this to work we have to by able to find the output channel
302 * the speaker definition corresponds to. Therefore, OpenAL Soft
303 * requires these channel labels to be recognized:
305 * LF = Front left
306 * RF = Front right
307 * LS = Side left
308 * RS = Side right
309 * LB = Back left
310 * RB = Back right
311 * CE = Front center
312 * CB = Back center
314 * Additionally, surround51 will acknowledge back speakers for side
315 * channels, and surround51rear will acknowledge side speakers for
316 * back channels, to avoid issues with an ambdec expecting 5.1 to
317 * use the side channels when the device is configured for back,
318 * and vice-versa.
320 if(alstr_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
321 ch = FrontLeft;
322 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
323 ch = FrontRight;
324 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
325 ch = FrontCenter;
326 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
328 if(device->FmtChans == DevFmtX51Rear)
329 ch = BackLeft;
330 else
331 ch = SideLeft;
333 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
335 if(device->FmtChans == DevFmtX51Rear)
336 ch = BackRight;
337 else
338 ch = SideRight;
340 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
342 if(device->FmtChans == DevFmtX51)
343 ch = SideLeft;
344 else
345 ch = BackLeft;
347 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
349 if(device->FmtChans == DevFmtX51)
350 ch = SideRight;
351 else
352 ch = BackRight;
354 else if(alstr_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
355 ch = BackCenter;
356 else
358 const char *name = alstr_get_cstr(conf->Speakers[i].Name);
359 unsigned int n;
360 char c;
362 if(sscanf(name, "AUX%u%c", &n, &c) == 1 && n < 16)
363 ch = Aux0+n;
364 else
366 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
367 return false;
370 chidx = GetChannelIdxByName(&device->RealOut, ch);
371 if(chidx == -1)
373 ERR("Failed to lookup AmbDec speaker label %s\n",
374 alstr_get_cstr(conf->Speakers[i].Name));
375 return false;
377 speakermap[i] = chidx;
380 return true;
384 static const ChannelMap MonoCfg[1] = {
385 { FrontCenter, { 1.0f } },
386 }, StereoCfg[2] = {
387 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 0.0f, 5.52305643e-2f } },
388 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 0.0f, 5.52305643e-2f } },
389 }, QuadCfg[4] = {
390 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, -2.04124145e-1f } },
391 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 0.0f, 2.04124145e-1f } },
392 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, 2.04124145e-1f } },
393 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, 0.0f, -2.04124145e-1f } },
394 }, X51SideCfg[4] = {
395 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
396 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
397 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
398 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
399 }, X51RearCfg[4] = {
400 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, 0.0f, -2.00042375e-1f, -2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
401 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 0.0f, 1.66295695e-1f, 7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
402 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 0.0f, 1.66295695e-1f, -7.30571517e-2f, 0.0f, 0.0f, 0.0f, 2.10901184e-2f } },
403 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, 0.0f, -2.00042375e-1f, 2.12307769e-2f, 0.0f, 0.0f, 0.0f, -1.14579885e-2f } },
404 }, X61Cfg[6] = {
405 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, 0.0f, -4.39996780e-2f, -2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
406 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 0.0f, 1.59658796e-1f, 6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
407 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 0.0f, 1.59658796e-1f, -6.66278083e-2f, 0.0f, 0.0f, 0.0f, 3.84686854e-2f } },
408 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, 0.0f, -4.39996780e-2f, 2.60790269e-2f, 0.0f, 0.0f, 0.0f, -6.87239792e-2f } },
409 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, 0.0f, -2.50000094e-1f, 0.00000000e+0f, 0.0f, 0.0f, 0.0f, 6.05133395e-2f } },
410 }, X71Cfg[6] = {
411 { 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 } },
412 { 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 } },
413 { 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 } },
414 { 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 } },
415 { 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 } },
416 { 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 } },
419 static void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, bool periphonic)
421 const char *devname = alstr_get_cstr(device->DeviceName);
422 ALsizei i;
424 if(GetConfigValueBool(devname, "decoder", "nfc", 1) && ctrl_dist > 0.0f)
426 /* NFC is only used when AvgSpeakerDist is greater than 0, and can only
427 * be used when rendering to an ambisonic buffer.
429 device->AvgSpeakerDist = minf(ctrl_dist, 10.0f);
431 device->Dry.NumChannelsPerOrder[0] = 1;
432 if(periphonic)
433 for(i = 1;i < order+1;i++)
434 device->Dry.NumChannelsPerOrder[i] = (i+1)*(i+1) - i*i;
435 else
436 for(i = 1;i < order+1;i++)
437 device->Dry.NumChannelsPerOrder[i] = (i*2+1) - ((i-1)*2+1);
438 for(;i < MAX_AMBI_ORDER+1;i++)
439 device->Dry.NumChannelsPerOrder[i] = 0;
443 static void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
445 const char *devname = alstr_get_cstr(device->DeviceName);
446 ALfloat maxdist = 0.0f;
447 size_t total = 0;
448 ALsizei i;
450 for(i = 0;i < conf->NumSpeakers;i++)
451 maxdist = maxf(maxdist, conf->Speakers[i].Distance);
453 if(GetConfigValueBool(devname, "decoder", "distance-comp", 1) && maxdist > 0.0f)
455 ALfloat srate = (ALfloat)device->Frequency;
456 for(i = 0;i < conf->NumSpeakers;i++)
458 ALsizei chan = speakermap[i];
459 ALfloat delay;
461 /* Distance compensation only delays in steps of the sample rate.
462 * This is a bit less accurate since the delay time falls to the
463 * nearest sample time, but it's far simpler as it doesn't have to
464 * deal with phase offsets. This means at 48khz, for instance, the
465 * distance delay will be in steps of about 7 millimeters.
467 delay = floorf((maxdist-conf->Speakers[i].Distance) / SPEEDOFSOUNDMETRESPERSEC *
468 srate + 0.5f);
469 if(delay >= (ALfloat)MAX_DELAY_LENGTH)
470 ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %u)\n",
471 alstr_get_cstr(conf->Speakers[i].Name), delay, MAX_DELAY_LENGTH);
473 device->ChannelDelay[chan].Length = (ALsizei)clampf(
474 delay, 0.0f, (ALfloat)(MAX_DELAY_LENGTH-1)
476 device->ChannelDelay[chan].Gain = conf->Speakers[i].Distance / maxdist;
477 TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
478 alstr_get_cstr(conf->Speakers[i].Name), device->ChannelDelay[chan].Length,
479 device->ChannelDelay[chan].Gain
482 /* Round up to the next 4th sample, so each channel buffer starts
483 * 16-byte aligned.
485 total += RoundUp(device->ChannelDelay[chan].Length, 4);
489 if(total > 0)
491 device->ChannelDelay[0].Buffer = al_calloc(16, total * sizeof(ALfloat));
492 for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
494 size_t len = RoundUp(device->ChannelDelay[i-1].Length, 4);
495 device->ChannelDelay[i].Buffer = device->ChannelDelay[i-1].Buffer + len;
500 static void InitPanning(ALCdevice *device)
502 const ChannelMap *chanmap = NULL;
503 ALsizei coeffcount = 0;
504 ALsizei count = 0;
505 ALsizei i, j;
507 switch(device->FmtChans)
509 case DevFmtMono:
510 count = COUNTOF(MonoCfg);
511 chanmap = MonoCfg;
512 coeffcount = 1;
513 break;
515 case DevFmtStereo:
516 count = COUNTOF(StereoCfg);
517 chanmap = StereoCfg;
518 coeffcount = 4;
519 break;
521 case DevFmtQuad:
522 count = COUNTOF(QuadCfg);
523 chanmap = QuadCfg;
524 coeffcount = 4;
525 break;
527 case DevFmtX51:
528 count = COUNTOF(X51SideCfg);
529 chanmap = X51SideCfg;
530 coeffcount = 9;
531 break;
533 case DevFmtX51Rear:
534 count = COUNTOF(X51RearCfg);
535 chanmap = X51RearCfg;
536 coeffcount = 9;
537 break;
539 case DevFmtX61:
540 count = COUNTOF(X61Cfg);
541 chanmap = X61Cfg;
542 coeffcount = 9;
543 break;
545 case DevFmtX71:
546 count = COUNTOF(X71Cfg);
547 chanmap = X71Cfg;
548 coeffcount = 16;
549 break;
551 case DevFmtAmbi3D:
552 break;
555 if(device->FmtChans == DevFmtAmbi3D)
557 const char *devname = alstr_get_cstr(device->DeviceName);
558 const ALsizei *acnmap = (device->AmbiLayout == AmbiLayout_FuMa) ? FuMa2ACN : ACN2ACN;
559 const ALfloat *n3dscale = (device->AmbiScale == AmbiNorm_FuMa) ? FuMa2N3DScale :
560 (device->AmbiScale == AmbiNorm_SN3D) ? SN3D2N3DScale :
561 /*(device->AmbiScale == AmbiNorm_N3D) ?*/ N3D2N3DScale;
562 ALfloat nfc_delay = 0.0f;
564 count = (device->AmbiOrder == 3) ? 16 :
565 (device->AmbiOrder == 2) ? 9 :
566 (device->AmbiOrder == 1) ? 4 : 1;
567 for(i = 0;i < count;i++)
569 ALsizei acn = acnmap[i];
570 device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
571 device->Dry.Ambi.Map[i].Index = acn;
573 device->Dry.CoeffCount = 0;
574 device->Dry.NumChannels = count;
576 if(device->AmbiOrder < 2)
578 device->FOAOut.Ambi = device->Dry.Ambi;
579 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
580 device->FOAOut.NumChannels = 0;
582 else
584 ALfloat w_scale=1.0f, xyz_scale=1.0f;
586 /* FOA output is always ACN+N3D for higher-order ambisonic output.
587 * The upsampler expects this and will convert it for output.
589 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
590 for(i = 0;i < 4;i++)
592 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
593 device->FOAOut.Ambi.Map[i].Index = i;
595 device->FOAOut.CoeffCount = 0;
596 device->FOAOut.NumChannels = 4;
598 if(device->AmbiOrder >= 3)
600 w_scale = W_SCALE_3H3P;
601 xyz_scale = XYZ_SCALE_3H3P;
603 else
605 w_scale = W_SCALE_2H2P;
606 xyz_scale = XYZ_SCALE_2H2P;
608 ambiup_reset(device->AmbiUp, device, w_scale, xyz_scale);
611 if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
613 nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
614 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
615 device->AmbiOrder, true);
618 else
620 ALfloat w_scale, xyz_scale;
622 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
623 chanmap, count, &device->Dry.NumChannels);
624 device->Dry.CoeffCount = coeffcount;
626 w_scale = (device->Dry.CoeffCount > 9) ? W_SCALE_3H0P :
627 (device->Dry.CoeffCount > 4) ? W_SCALE_2H0P : 1.0f;
628 xyz_scale = (device->Dry.CoeffCount > 9) ? XYZ_SCALE_3H0P :
629 (device->Dry.CoeffCount > 4) ? XYZ_SCALE_2H0P : 1.0f;
631 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
632 for(i = 0;i < device->Dry.NumChannels;i++)
634 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
635 for(j = 1;j < 4;j++)
636 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
638 device->FOAOut.CoeffCount = 4;
639 device->FOAOut.NumChannels = 0;
641 device->RealOut.NumChannels = 0;
644 static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
646 ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
647 const ALfloat *coeff_scale = N3D2N3DScale;
648 ALfloat w_scale = 1.0f;
649 ALfloat xyz_scale = 1.0f;
650 ALsizei i, j;
652 if(conf->FreqBands != 1)
653 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
654 conf->XOverFreq);
656 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
658 if(conf->ChanMask > 0x1ff)
660 w_scale = W_SCALE_3H3P;
661 xyz_scale = XYZ_SCALE_3H3P;
663 else if(conf->ChanMask > 0xf)
665 w_scale = W_SCALE_2H2P;
666 xyz_scale = XYZ_SCALE_2H2P;
669 else
671 if(conf->ChanMask > 0x1ff)
673 w_scale = W_SCALE_3H0P;
674 xyz_scale = XYZ_SCALE_3H0P;
676 else if(conf->ChanMask > 0xf)
678 w_scale = W_SCALE_2H0P;
679 xyz_scale = XYZ_SCALE_2H0P;
683 if(conf->CoeffScale == ADS_SN3D)
684 coeff_scale = SN3D2N3DScale;
685 else if(conf->CoeffScale == ADS_FuMa)
686 coeff_scale = FuMa2N3DScale;
688 for(i = 0;i < conf->NumSpeakers;i++)
690 ALsizei chan = speakermap[i];
691 ALfloat gain;
692 ALsizei k = 0;
694 for(j = 0;j < MAX_AMBI_COEFFS;j++)
695 chanmap[i].Config[j] = 0.0f;
697 chanmap[i].ChanName = device->RealOut.ChannelName[chan];
698 for(j = 0;j < MAX_AMBI_COEFFS;j++)
700 if(j == 0) gain = conf->HFOrderGain[0];
701 else if(j == 1) gain = conf->HFOrderGain[1];
702 else if(j == 4) gain = conf->HFOrderGain[2];
703 else if(j == 9) gain = conf->HFOrderGain[3];
704 if((conf->ChanMask&(1<<j)))
705 chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
709 SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
710 conf->NumSpeakers, &device->Dry.NumChannels);
711 device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
712 (conf->ChanMask > 0xf) ? 9 : 4;
714 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
715 for(i = 0;i < device->Dry.NumChannels;i++)
717 device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0] * w_scale;
718 for(j = 1;j < 4;j++)
719 device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * xyz_scale;
721 device->FOAOut.CoeffCount = 4;
722 device->FOAOut.NumChannels = 0;
724 device->RealOut.NumChannels = 0;
726 InitDistanceComp(device, conf, speakermap);
729 static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALsizei speakermap[MAX_OUTPUT_CHANNELS])
731 ALfloat avg_dist;
732 ALsizei count;
733 ALsizei i;
735 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
737 count = (conf->ChanMask > 0x1ff) ? 16 :
738 (conf->ChanMask > 0xf) ? 9 : 4;
739 for(i = 0;i < count;i++)
741 device->Dry.Ambi.Map[i].Scale = 1.0f;
742 device->Dry.Ambi.Map[i].Index = i;
745 else
747 static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
749 count = (conf->ChanMask > 0x1ff) ? 7 :
750 (conf->ChanMask > 0xf) ? 5 : 3;
751 for(i = 0;i < count;i++)
753 device->Dry.Ambi.Map[i].Scale = 1.0f;
754 device->Dry.Ambi.Map[i].Index = map[i];
757 device->Dry.CoeffCount = 0;
758 device->Dry.NumChannels = count;
760 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
761 (conf->FreqBands == 1) ? "single" : "dual",
762 (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
763 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
765 bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency, speakermap);
767 if(!(conf->ChanMask > 0xf))
769 device->FOAOut.Ambi = device->Dry.Ambi;
770 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
771 device->FOAOut.NumChannels = 0;
773 else
775 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
776 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
778 count = 4;
779 for(i = 0;i < count;i++)
781 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
782 device->FOAOut.Ambi.Map[i].Index = i;
785 else
787 static const int map[3] = { 0, 1, 3 };
788 count = 3;
789 for(i = 0;i < count;i++)
791 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
792 device->FOAOut.Ambi.Map[i].Index = map[i];
795 device->FOAOut.CoeffCount = 0;
796 device->FOAOut.NumChannels = count;
799 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
801 avg_dist = 0.0f;
802 for(i = 0;i < conf->NumSpeakers;i++)
803 avg_dist += conf->Speakers[i].Distance;
804 avg_dist /= (ALfloat)conf->NumSpeakers;
805 InitNearFieldCtrl(device, avg_dist,
806 (conf->ChanMask > 0x1ff) ? 3 : (conf->ChanMask > 0xf) ? 2 : 1,
807 !!(conf->ChanMask&AMBI_PERIPHONIC_MASK)
810 InitDistanceComp(device, conf, speakermap);
813 static void InitHrtfPanning(ALCdevice *device)
815 /* NOTE: azimuth goes clockwise. */
816 static const struct AngularPoint AmbiPoints[] = {
817 { DEG2RAD( 90.0f), DEG2RAD( 0.0f) },
818 { DEG2RAD( 35.0f), DEG2RAD( 45.0f) },
819 { DEG2RAD( 35.0f), DEG2RAD( 135.0f) },
820 { DEG2RAD( 35.0f), DEG2RAD(-135.0f) },
821 { DEG2RAD( 35.0f), DEG2RAD( -45.0f) },
822 { DEG2RAD( 0.0f), DEG2RAD( 0.0f) },
823 { DEG2RAD( 0.0f), DEG2RAD( 45.0f) },
824 { DEG2RAD( 0.0f), DEG2RAD( 90.0f) },
825 { DEG2RAD( 0.0f), DEG2RAD( 135.0f) },
826 { DEG2RAD( 0.0f), DEG2RAD( 180.0f) },
827 { DEG2RAD( 0.0f), DEG2RAD(-135.0f) },
828 { DEG2RAD( 0.0f), DEG2RAD( -90.0f) },
829 { DEG2RAD( 0.0f), DEG2RAD( -45.0f) },
830 { DEG2RAD(-35.0f), DEG2RAD( 45.0f) },
831 { DEG2RAD(-35.0f), DEG2RAD( 135.0f) },
832 { DEG2RAD(-35.0f), DEG2RAD(-135.0f) },
833 { DEG2RAD(-35.0f), DEG2RAD( -45.0f) },
834 { DEG2RAD(-90.0f), DEG2RAD( 0.0f) },
836 static const ALfloat AmbiMatrixFOA[][MAX_AMBI_COEFFS] = {
837 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 0.00000000e+00f },
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, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f },
842 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f },
843 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f },
844 { 5.55555556e-02f, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
845 { 5.55555556e-02f, -6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
846 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f },
847 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, -6.12372435e-02f },
848 { 5.55555556e-02f, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f },
849 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-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, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f },
854 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f },
855 }, AmbiMatrixHOA[][MAX_AMBI_COEFFS] = {
856 { 5.55555556e-02f, 0.00000000e+00f, 1.23717915e-01f, 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 },
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, 5.00000000e-02f, 7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
861 { 5.55555556e-02f, 0.00000000e+00f, 0.00000000e+00f, 8.66025404e-02f, 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, -8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 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, 0.00000000e+00f, 0.00000000e+00f, -8.66025404e-02f, 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, 8.66025404e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -1.29099445e-01f },
868 { 5.55555556e-02f, 6.12372435e-02f, 0.00000000e+00f, 6.12372435e-02f, 6.83467648e-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, 5.00000000e-02f, -7.14285715e-02f, 5.00000000e-02f, 4.55645099e-02f, 0.00000000e+00f },
873 { 5.55555556e-02f, 0.00000000e+00f, -1.23717915e-01f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f },
875 static const ALfloat AmbiOrderHFGainFOA[MAX_AMBI_ORDER+1] = {
876 3.00000000e+00f, 1.73205081e+00f
877 }, AmbiOrderHFGainHOA[MAX_AMBI_ORDER+1] = {
878 2.40192231e+00f, 1.86052102e+00f, 9.60768923e-01f
880 static const ALsizei IndexMap[6] = { 0, 1, 2, 3, 4, 8 };
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");
888 static_assert(COUNTOF(AmbiPoints) <= HRTF_AMBI_MAX_CHANNELS, "HRTF_AMBI_MAX_CHANNELS is too small");
890 if(device->AmbiUp)
892 AmbiMatrix = AmbiMatrixHOA;
893 AmbiOrderHFGain = AmbiOrderHFGainHOA;
894 count = COUNTOF(IndexMap);
897 device->Hrtf = al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count));
899 for(i = 0;i < count;i++)
901 device->Dry.Ambi.Map[i].Scale = 1.0f;
902 device->Dry.Ambi.Map[i].Index = IndexMap[i];
904 device->Dry.CoeffCount = 0;
905 device->Dry.NumChannels = count;
907 if(device->AmbiUp)
909 memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
910 for(i = 0;i < 4;i++)
912 device->FOAOut.Ambi.Map[i].Scale = 1.0f;
913 device->FOAOut.Ambi.Map[i].Index = i;
915 device->FOAOut.CoeffCount = 0;
916 device->FOAOut.NumChannels = 4;
918 ambiup_reset(device->AmbiUp, device, AmbiOrderHFGainFOA[0] / AmbiOrderHFGain[0],
919 AmbiOrderHFGainFOA[1] / AmbiOrderHFGain[1]);
921 else
923 device->FOAOut.Ambi = device->Dry.Ambi;
924 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
925 device->FOAOut.NumChannels = 0;
928 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
930 BuildBFormatHrtf(device->HrtfHandle,
931 device->Hrtf, device->Dry.NumChannels, AmbiPoints, AmbiMatrix, COUNTOF(AmbiPoints),
932 AmbiOrderHFGain
935 if(GetConfigValueBool(alstr_get_cstr(device->DeviceName), "decoder", "nfc", 1) &&
936 device->HrtfHandle->distance > 0.0f)
938 /* NFC is only used when AvgSpeakerDist is greater than 0, and can only
939 * be used when rendering to an ambisonic buffer.
941 device->AvgSpeakerDist = minf(device->HrtfHandle->distance, 10.0f);
943 i = 0;
944 device->Dry.NumChannelsPerOrder[i++] = 1;
945 device->Dry.NumChannelsPerOrder[i++] = 3;
946 if(device->AmbiUp)
947 device->Dry.NumChannelsPerOrder[i++] = 2;
948 while(i < MAX_AMBI_ORDER+1)
949 device->Dry.NumChannelsPerOrder[i++] = 0;
953 static void InitUhjPanning(ALCdevice *device)
955 ALsizei count = 3;
956 ALsizei i;
958 for(i = 0;i < count;i++)
960 ALsizei acn = FuMa2ACN[i];
961 device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
962 device->Dry.Ambi.Map[i].Index = acn;
964 device->Dry.CoeffCount = 0;
965 device->Dry.NumChannels = count;
967 device->FOAOut.Ambi = device->Dry.Ambi;
968 device->FOAOut.CoeffCount = device->Dry.CoeffCount;
969 device->FOAOut.NumChannels = 0;
971 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
974 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
976 /* Hold the HRTF the device last used, in case it's used again. */
977 struct Hrtf *old_hrtf = device->HrtfHandle;
978 const char *mode;
979 bool headphones;
980 int bs2blevel;
981 size_t i;
983 al_free(device->Hrtf);
984 device->Hrtf = NULL;
985 device->HrtfHandle = NULL;
986 alstr_clear(&device->HrtfName);
987 device->Render_Mode = NormalRender;
989 memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
990 device->Dry.CoeffCount = 0;
991 device->Dry.NumChannels = 0;
992 for(i = 0;i < MAX_AMBI_ORDER+1;i++)
993 device->Dry.NumChannelsPerOrder[i] = 0;
995 device->AvgSpeakerDist = 0.0f;
996 memset(device->ChannelDelay, 0, sizeof(device->ChannelDelay));
997 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
999 device->ChannelDelay[i].Gain = 1.0f;
1000 device->ChannelDelay[i].Length = 0;
1003 al_free(device->Stablizer);
1004 device->Stablizer = NULL;
1006 if(device->FmtChans != DevFmtStereo)
1008 ALsizei speakermap[MAX_OUTPUT_CHANNELS];
1009 const char *devname, *layout = NULL;
1010 AmbDecConf conf, *pconf = NULL;
1012 if(old_hrtf)
1013 Hrtf_DecRef(old_hrtf);
1014 old_hrtf = NULL;
1015 if(hrtf_appreq == Hrtf_Enable)
1016 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1018 ambdec_init(&conf);
1020 devname = alstr_get_cstr(device->DeviceName);
1021 switch(device->FmtChans)
1023 case DevFmtQuad: layout = "quad"; break;
1024 case DevFmtX51: /* fall-through */
1025 case DevFmtX51Rear: layout = "surround51"; break;
1026 case DevFmtX61: layout = "surround61"; break;
1027 case DevFmtX71: layout = "surround71"; break;
1028 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1029 case DevFmtMono:
1030 case DevFmtStereo:
1031 case DevFmtAmbi3D:
1032 break;
1034 if(layout)
1036 const char *fname;
1037 if(ConfigValueStr(devname, "decoder", layout, &fname))
1039 if(!ambdec_load(&conf, fname))
1040 ERR("Failed to load layout file %s\n", fname);
1041 else
1043 if(conf.ChanMask > 0xffff)
1044 ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
1045 else
1047 if(MakeSpeakerMap(device, &conf, speakermap))
1048 pconf = &conf;
1054 if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
1056 ambiup_free(&device->AmbiUp);
1057 if(!device->AmbiDecoder)
1058 device->AmbiDecoder = bformatdec_alloc();
1060 else
1062 bformatdec_free(&device->AmbiDecoder);
1063 if(device->FmtChans != DevFmtAmbi3D || device->AmbiOrder < 2)
1064 ambiup_free(&device->AmbiUp);
1065 else
1067 if(!device->AmbiUp)
1068 device->AmbiUp = ambiup_alloc();
1072 if(!pconf)
1073 InitPanning(device);
1074 else if(device->AmbiDecoder)
1075 InitHQPanning(device, pconf, speakermap);
1076 else
1077 InitCustomPanning(device, pconf, speakermap);
1079 /* Enable the stablizer only for formats that have front-left, front-
1080 * right, and front-center outputs.
1082 switch(device->FmtChans)
1084 case DevFmtX51:
1085 case DevFmtX51Rear:
1086 case DevFmtX61:
1087 case DevFmtX71:
1088 if(GetConfigValueBool(devname, NULL, "front-stablizer", 0))
1090 /* Initialize band-splitting filters for the front-left and
1091 * front-right channels, with a crossover at 5khz (could be
1092 * higher).
1094 ALfloat scale = (ALfloat)(5000.0 / device->Frequency);
1095 FrontStablizer *stablizer = al_calloc(16, sizeof(*stablizer));
1097 bandsplit_init(&stablizer->LFilter, scale);
1098 stablizer->RFilter = stablizer->LFilter;
1100 /* Initialize all-pass filters for all other channels. */
1101 splitterap_init(&stablizer->APFilter[0], scale);
1102 for(i = 1;i < (size_t)device->RealOut.NumChannels;i++)
1103 stablizer->APFilter[i] = stablizer->APFilter[0];
1105 device->Stablizer = stablizer;
1107 break;
1108 case DevFmtMono:
1109 case DevFmtStereo:
1110 case DevFmtQuad:
1111 case DevFmtAmbi3D:
1112 break;
1114 TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
1116 ambdec_deinit(&conf);
1117 return;
1120 bformatdec_free(&device->AmbiDecoder);
1122 headphones = device->IsHeadphones;
1123 if(device->Type != Loopback)
1125 const char *mode;
1126 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
1128 if(strcasecmp(mode, "headphones") == 0)
1129 headphones = true;
1130 else if(strcasecmp(mode, "speakers") == 0)
1131 headphones = false;
1132 else if(strcasecmp(mode, "auto") != 0)
1133 ERR("Unexpected stereo-mode: %s\n", mode);
1137 if(hrtf_userreq == Hrtf_Default)
1139 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
1140 (hrtf_appreq == Hrtf_Enable);
1141 if(!usehrtf) goto no_hrtf;
1143 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
1144 if(headphones && hrtf_appreq != Hrtf_Disable)
1145 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
1147 else
1149 if(hrtf_userreq != Hrtf_Enable)
1151 if(hrtf_appreq == Hrtf_Enable)
1152 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
1153 goto no_hrtf;
1155 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
1158 if(VECTOR_SIZE(device->HrtfList) == 0)
1160 VECTOR_DEINIT(device->HrtfList);
1161 device->HrtfList = EnumerateHrtf(device->DeviceName);
1164 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
1166 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, hrtf_id);
1167 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1168 if(hrtf && hrtf->sampleRate == device->Frequency)
1170 device->HrtfHandle = hrtf;
1171 alstr_copy(&device->HrtfName, entry->name);
1173 else if(hrtf)
1174 Hrtf_DecRef(hrtf);
1177 for(i = 0;!device->HrtfHandle && i < VECTOR_SIZE(device->HrtfList);i++)
1179 const EnumeratedHrtf *entry = &VECTOR_ELEM(device->HrtfList, i);
1180 struct Hrtf *hrtf = GetLoadedHrtf(entry->hrtf);
1181 if(hrtf && hrtf->sampleRate == device->Frequency)
1183 device->HrtfHandle = hrtf;
1184 alstr_copy(&device->HrtfName, entry->name);
1186 else if(hrtf)
1187 Hrtf_DecRef(hrtf);
1190 if(device->HrtfHandle)
1192 if(old_hrtf)
1193 Hrtf_DecRef(old_hrtf);
1194 old_hrtf = NULL;
1196 device->Render_Mode = HrtfRender;
1197 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
1199 if(strcasecmp(mode, "full") == 0)
1200 device->Render_Mode = HrtfRender;
1201 else if(strcasecmp(mode, "basic") == 0)
1202 device->Render_Mode = NormalRender;
1203 else
1204 ERR("Unexpected hrtf-mode: %s\n", mode);
1207 if(device->Render_Mode == HrtfRender)
1209 /* Don't bother with HOA when using full HRTF rendering. Nothing
1210 * needs it, and it eases the CPU/memory load.
1212 ambiup_free(&device->AmbiUp);
1214 else
1216 if(!device->AmbiUp)
1217 device->AmbiUp = ambiup_alloc();
1220 TRACE("%s HRTF rendering enabled, using \"%s\"\n",
1221 ((device->Render_Mode == HrtfRender) ? "Full" : "Basic"),
1222 alstr_get_cstr(device->HrtfName)
1224 InitHrtfPanning(device);
1225 return;
1227 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1229 no_hrtf:
1230 if(old_hrtf)
1231 Hrtf_DecRef(old_hrtf);
1232 old_hrtf = NULL;
1233 TRACE("HRTF disabled\n");
1235 device->Render_Mode = StereoPair;
1237 ambiup_free(&device->AmbiUp);
1239 bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
1240 (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
1241 if(device->Type != Loopback)
1242 ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
1243 if(bs2blevel > 0 && bs2blevel <= 6)
1245 device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
1246 bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
1247 TRACE("BS2B enabled\n");
1248 InitPanning(device);
1249 return;
1252 TRACE("BS2B disabled\n");
1254 if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "stereo-encoding", &mode))
1256 if(strcasecmp(mode, "uhj") == 0)
1257 device->Render_Mode = NormalRender;
1258 else if(strcasecmp(mode, "panpot") != 0)
1259 ERR("Unexpected stereo-encoding: %s\n", mode);
1261 if(device->Render_Mode == NormalRender)
1263 device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
1264 TRACE("UHJ enabled\n");
1265 InitUhjPanning(device);
1266 return;
1269 TRACE("UHJ disabled\n");
1270 InitPanning(device);
1274 void aluInitEffectPanning(ALeffectslot *slot)
1276 ALsizei i;
1278 memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
1279 slot->NumChannels = 0;
1281 for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
1283 slot->ChanMap[i].Scale = 1.0f;
1284 slot->ChanMap[i].Index = i;
1286 slot->NumChannels = i;