Fix stereo device configuration
[openal-soft.git] / Alc / panning.c
blobe73c99aa0b3132e4e25dd72274c5d834d7276efd
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 "AL/al.h"
31 #include "AL/alc.h"
32 #include "alu.h"
34 extern inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels]);
37 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat elevation, ALfloat ingain, ALfloat gains[MaxChannels])
39 ALfloat dir[3] = {
40 -sinf(angle) * cosf(elevation),
41 sinf(elevation),
42 -cosf(angle) * cosf(elevation)
44 ComputeDirectionalGains(device, dir, ingain, gains);
47 void ComputeDirectionalGains(const ALCdevice *device, const ALfloat dir[3], ALfloat ingain, ALfloat gains[MaxChannels])
49 ALfloat coeffs[MAX_AMBI_COEFFS];
50 ALuint i, j;
51 /* Convert from OpenAL coords to Ambisonics. */
52 ALfloat x = -dir[2];
53 ALfloat y = -dir[0];
54 ALfloat z = dir[1];
56 coeffs[0] = 0.7071f; /* sqrt(1.0 / 2.0) */
57 coeffs[1] = x; /* X */
58 coeffs[2] = y; /* Y */
59 coeffs[3] = z; /* Z */
60 coeffs[4] = 0.5f * (3.0f*z*z - 1.0f); /* 0.5 * (3*Z*Z - 1) */
61 coeffs[5] = 2.0f * z * x; /* 2*Z*X */
62 coeffs[6] = 2.0f * y * z; /* 2*Y*Z */
63 coeffs[7] = x*x - y*y; /* X*X - Y*Y */
64 coeffs[8] = 2.0f * x * y; /* 2*X*Y */
65 coeffs[9] = 0.5f * z * (5.0f*z*z - 3.0f); /* 0.5 * Z * (5*Z*Z - 3) */
66 coeffs[10] = 0.7262f * x * (5.0f*z*z - 1.0f); /* sqrt(135.0 / 256.0) * X * (5*Z*Z - 1) */
67 coeffs[11] = 0.7262f * y * (5.0f*z*z - 1.0f); /* sqrt(135.0 / 256.0) * Y * (5*Z*Z - 1) */
68 coeffs[12] = 2.5981f * z * (x*x - y*y); /* sqrt(27.0 / 4.0) * Z * (X*X - Y*Y) */
69 coeffs[13] = 5.1962f * x * y * z; /* sqrt(27) * X * Y * Z */
70 coeffs[14] = x * (x*x - 3.0f*y*y); /* X * (X*X - 3*Y*Y) */
71 coeffs[15] = y * (3.0f*x*x - y*y); /* Y * (3*X*X - Y*Y) */
73 for(i = 0;i < MaxChannels;i++)
74 gains[i] = 0.0f;
75 for(i = 0;i < device->NumSpeakers;i++)
77 enum Channel chan = device->Speaker[i].ChanName;
78 for(j = 0;j < MAX_AMBI_COEFFS;j++)
79 gains[chan] += device->Speaker[i].Coeff[j]*coeffs[j];
80 gains[chan] = maxf(gains[chan], 0.0f) * ingain;
85 ALvoid aluInitPanning(ALCdevice *device)
87 static const ChannelConfig MonoCfg[1] = {
88 { FrontCenter, DEG2RAD(0.0f), DEG2RAD(0.0f), { 1.4142f } }
89 }, StereoCfg[2] = {
90 { FrontLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f), { 0.7071f, 0.0f, 0.5f, 0.0f } },
91 { FrontRight, DEG2RAD( 90.0f), DEG2RAD(0.0f), { 0.7071f, 0.0f, -0.5f, 0.0f } }
92 }, QuadCfg[4] = {
93 { FrontLeft, DEG2RAD( -45.0f), DEG2RAD(0.0f), { 0.353558f, 0.306181f, 0.306192f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.117183f } },
94 { FrontRight, DEG2RAD( 45.0f), DEG2RAD(0.0f), { 0.353543f, 0.306181f, -0.306192f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.117193f } },
95 { BackLeft, DEG2RAD(-135.0f), DEG2RAD(0.0f), { 0.353543f, -0.306192f, 0.306181f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.117193f } },
96 { BackRight, DEG2RAD( 135.0f), DEG2RAD(0.0f), { 0.353558f, -0.306192f, -0.306181f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.117183f } }
97 }, X51Cfg[5] = {
98 { FrontLeft, DEG2RAD( -30.0f), DEG2RAD(0.0f), { 0.208954f, 0.212846f, 0.238350f, 0.0f, 0.0f, 0.0f, 0.0f, -0.017738f, 0.204014f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.051023f, 0.047490f } },
99 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f), { 0.208950f, 0.212842f, -0.238350f, 0.0f, 0.0f, 0.0f, 0.0f, -0.017740f, -0.204011f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.051022f, -0.047489f } },
100 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f), { 0.109403f, 0.179490f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.142031f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.072024f, -0.000001f } },
101 { BackLeft, DEG2RAD(-110.0f), DEG2RAD(0.0f), { 0.470934f, -0.369630f, 0.349383f, 0.0f, 0.0f, 0.0f, 0.0f, -0.031379f, -0.058143f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.007116f, -0.043968f } },
102 { BackRight, DEG2RAD( 110.0f), DEG2RAD(0.0f), { 0.470936f, -0.369626f, -0.349386f, 0.0f, 0.0f, 0.0f, 0.0f, -0.031375f, 0.058144f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.007119f, 0.043968f } }
103 }, X51SideCfg[5] = {
104 { FrontLeft, DEG2RAD(-30.0f), DEG2RAD(0.0f), { 0.167065f, 0.200583f, 0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, 0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, 0.068910f } },
105 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f), { 0.167058f, 0.200580f, -0.172701f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029846f, -0.186405f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, -0.068904f } },
106 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f), { 0.109403f, 0.179490f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.142031f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.072024f, -0.000001f } },
107 { SideLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f), { 0.289151f, -0.081301f, 0.401292f, 0.0f, 0.0f, 0.0f, 0.0f, -0.188208f, -0.071420f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.010099f, -0.032897f } },
108 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f), { 0.289157f, -0.081298f, -0.401295f, 0.0f, 0.0f, 0.0f, 0.0f, -0.188208f, 0.071419f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.010099f, 0.032897f } }
109 }, X61Cfg[6] = {
110 { FrontLeft, DEG2RAD(-30.0f), DEG2RAD(0.0f), { 0.167065f, 0.200583f, 0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, 0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, 0.068910f } },
111 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f), { 0.167058f, 0.200580f, -0.172701f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029846f, -0.186405f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, -0.068904f } },
112 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f), { 0.109403f, 0.179490f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.142031f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.072024f, -0.000001f } },
113 { BackCenter, DEG2RAD(180.0f), DEG2RAD(0.0f), { 0.353556f, -0.461940f, -0.000006f, 0.0f, 0.0f, 0.0f, 0.0f, 0.165723f, -0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, 0.000005f } },
114 { SideLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f), { 0.289151f, -0.081301f, 0.401292f, 0.0f, 0.0f, 0.0f, 0.0f, -0.188208f, -0.071420f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.010099f, -0.032897f } },
115 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f), { 0.289157f, -0.081298f, -0.401295f, 0.0f, 0.0f, 0.0f, 0.0f, -0.188208f, 0.071419f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.010099f, 0.032897f } }
116 }, X71Cfg[7] = {
117 { FrontLeft, DEG2RAD( -30.0f), DEG2RAD(0.0f), { 0.167065f, 0.200583f, 0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, 0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, 0.068910f } },
118 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f), { 0.167058f, 0.200580f, -0.172701f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029846f, -0.186405f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, -0.068904f } },
119 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f), { 0.109403f, 0.179490f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.142031f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.072024f, -0.000001f } },
120 { BackLeft, DEG2RAD(-150.0f), DEG2RAD(0.0f), { 0.224752f, -0.295009f, 0.170325f, 0.0f, 0.0f, 0.0f, 0.0f, 0.105349f, -0.182473f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.000000f, 0.065799f } },
121 { BackRight, DEG2RAD( 150.0f), DEG2RAD(0.0f), { 0.224739f, -0.295005f, -0.170331f, 0.0f, 0.0f, 0.0f, 0.0f, 0.105342f, 0.182470f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.000000f, -0.065792f } },
122 { SideLeft, DEG2RAD( -90.0f), DEG2RAD(0.0f), { 0.224739f, 0.000002f, 0.340644f, 0.0f, 0.0f, 0.0f, 0.0f, -0.210697f, 0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.000000f, -0.065795f } },
123 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f), { 0.224754f, 0.000004f, -0.340647f, 0.0f, 0.0f, 0.0f, 0.0f, -0.210697f, -0.000004f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.000000f, 0.065796f } }
125 const ChannelConfig *config;
126 ALuint i;
128 memset(device->Speaker, 0, sizeof(device->Speaker));
129 device->NumSpeakers = 0;
131 switch(device->FmtChans)
133 case DevFmtMono:
134 device->NumSpeakers = 1;
135 config = MonoCfg;
136 break;
138 case DevFmtStereo:
139 device->NumSpeakers = 2;
140 config = StereoCfg;
141 break;
143 case DevFmtQuad:
144 device->NumSpeakers = 4;
145 config = QuadCfg;
146 break;
148 case DevFmtX51:
149 device->NumSpeakers = 5;
150 config = X51Cfg;
151 break;
153 case DevFmtX51Side:
154 device->NumSpeakers = 5;
155 config = X51SideCfg;
156 break;
158 case DevFmtX61:
159 device->NumSpeakers = 6;
160 config = X61Cfg;
161 break;
163 case DevFmtX71:
164 device->NumSpeakers = 7;
165 config = X71Cfg;
166 break;
168 for(i = 0;i < device->NumSpeakers;i++)
169 device->Speaker[i] = config[i];