Fix retrieved update size from pulseaudio
[openal-soft.git] / Alc / ALu.c
blobce0f841c8a2fb868b3fb200a2b5226ed1151dc6d
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 "alSource.h"
33 #include "alBuffer.h"
34 #include "alThunk.h"
35 #include "alListener.h"
36 #include "alAuxEffectSlot.h"
37 #include "alu.h"
38 #include "bs2b.h"
40 #if defined(HAVE_STDINT_H)
41 #include <stdint.h>
42 typedef int64_t ALint64;
43 #elif defined(HAVE___INT64)
44 typedef __int64 ALint64;
45 #elif (SIZEOF_LONG == 8)
46 typedef long ALint64;
47 #elif (SIZEOF_LONG_LONG == 8)
48 typedef long long ALint64;
49 #endif
51 #define FRACTIONBITS 14
52 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
53 #define MAX_PITCH 65536
55 /* Minimum ramp length in milliseconds. The value below was chosen to
56 * adequately reduce clicks and pops from harsh gain changes. */
57 #define MIN_RAMP_LENGTH 16
59 ALboolean DuplicateStereo = AL_FALSE;
62 static __inline ALfloat aluF2F(ALfloat Value)
64 if(Value < 0.f) return Value/32768.f;
65 if(Value > 0.f) return Value/32767.f;
66 return 0.f;
69 static __inline ALshort aluF2S(ALfloat Value)
71 ALint i;
73 i = (ALint)Value;
74 i = __min( 32767, i);
75 i = __max(-32768, i);
76 return ((ALshort)i);
79 static __inline ALubyte aluF2UB(ALfloat Value)
81 ALshort i = aluF2S(Value);
82 return (i>>8)+128;
86 static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
88 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
89 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
90 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
93 static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
95 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
96 inVector1[2]*inVector2[2];
99 static __inline ALvoid aluNormalize(ALfloat *inVector)
101 ALfloat length, inverse_length;
103 length = aluSqrt(aluDotproduct(inVector, inVector));
104 if(length != 0.0f)
106 inverse_length = 1.0f/length;
107 inVector[0] *= inverse_length;
108 inVector[1] *= inverse_length;
109 inVector[2] *= inverse_length;
113 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
115 ALfloat temp[4] = {
116 vector[0], vector[1], vector[2], w
119 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
120 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
121 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
124 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
125 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
127 const char *confkey;
128 const char *next;
129 const char *sep;
130 const char *end;
131 int i, val;
133 confkey = GetConfigValue(NULL, name, "");
134 next = confkey;
135 while(next && *next)
137 confkey = next;
138 next = strchr(confkey, ',');
139 if(next)
141 do {
142 next++;
143 } while(isspace(*next));
146 sep = strchr(confkey, '=');
147 if(!sep || confkey == sep)
148 continue;
150 end = sep - 1;
151 while(isspace(*end) && end != confkey)
152 end--;
153 end++;
155 if(strncmp(confkey, "fl", end-confkey) == 0)
156 val = FRONT_LEFT;
157 else if(strncmp(confkey, "fr", end-confkey) == 0)
158 val = FRONT_RIGHT;
159 else if(strncmp(confkey, "fc", end-confkey) == 0)
160 val = FRONT_CENTER;
161 else if(strncmp(confkey, "bl", end-confkey) == 0)
162 val = BACK_LEFT;
163 else if(strncmp(confkey, "br", end-confkey) == 0)
164 val = BACK_RIGHT;
165 else if(strncmp(confkey, "bc", end-confkey) == 0)
166 val = BACK_CENTER;
167 else if(strncmp(confkey, "sl", end-confkey) == 0)
168 val = SIDE_LEFT;
169 else if(strncmp(confkey, "sr", end-confkey) == 0)
170 val = SIDE_RIGHT;
171 else
173 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
174 continue;
177 sep++;
178 while(isspace(*sep))
179 sep++;
181 for(i = 0;i < chans;i++)
183 if(Speaker2Chan[i] == val)
185 val = strtol(sep, NULL, 10);
186 if(val >= -180 && val <= 180)
187 SpeakerAngle[i] = val * M_PI/180.0f;
188 else
189 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
190 break;
195 for(i = 1;i < chans;i++)
197 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
199 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
200 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
201 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
206 static __inline ALfloat aluLUTpos2Angle(ALint pos)
208 if(pos < QUADRANT_NUM)
209 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
210 if(pos < 2 * QUADRANT_NUM)
211 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
212 if(pos < 3 * QUADRANT_NUM)
213 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
214 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
217 ALvoid aluInitPanning(ALCcontext *Context)
219 ALint pos, offset, s;
220 ALfloat Alpha, Theta;
221 ALfloat SpeakerAngle[OUTPUTCHANNELS];
222 ALint Speaker2Chan[OUTPUTCHANNELS];
224 for(s = 0;s < OUTPUTCHANNELS;s++)
226 int s2;
227 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
228 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
231 switch(Context->Device->Format)
233 /* Mono is rendered as stereo, then downmixed during post-process */
234 case AL_FORMAT_MONO8:
235 case AL_FORMAT_MONO16:
236 case AL_FORMAT_MONO_FLOAT32:
237 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
238 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
239 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
240 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
241 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
242 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
243 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
244 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
245 Context->NumChan = 2;
246 Speaker2Chan[0] = FRONT_LEFT;
247 Speaker2Chan[1] = FRONT_RIGHT;
248 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
249 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
250 break;
252 case AL_FORMAT_STEREO8:
253 case AL_FORMAT_STEREO16:
254 case AL_FORMAT_STEREO_FLOAT32:
255 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
256 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
257 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
258 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
259 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
260 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
261 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
262 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
263 Context->NumChan = 2;
264 Speaker2Chan[0] = FRONT_LEFT;
265 Speaker2Chan[1] = FRONT_RIGHT;
266 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
267 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
268 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
269 break;
271 case AL_FORMAT_QUAD8:
272 case AL_FORMAT_QUAD16:
273 case AL_FORMAT_QUAD32:
274 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
275 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
276 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
277 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
278 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
279 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
280 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
281 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
282 Context->NumChan = 4;
283 Speaker2Chan[0] = BACK_LEFT;
284 Speaker2Chan[1] = FRONT_LEFT;
285 Speaker2Chan[2] = FRONT_RIGHT;
286 Speaker2Chan[3] = BACK_RIGHT;
287 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
288 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
289 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
290 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
291 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
292 break;
294 case AL_FORMAT_51CHN8:
295 case AL_FORMAT_51CHN16:
296 case AL_FORMAT_51CHN32:
297 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
298 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
299 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
300 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
301 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
302 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
303 Context->NumChan = 5;
304 Speaker2Chan[0] = BACK_LEFT;
305 Speaker2Chan[1] = FRONT_LEFT;
306 Speaker2Chan[2] = FRONT_CENTER;
307 Speaker2Chan[3] = FRONT_RIGHT;
308 Speaker2Chan[4] = BACK_RIGHT;
309 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
310 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
311 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
312 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
313 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
314 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
315 break;
317 case AL_FORMAT_61CHN8:
318 case AL_FORMAT_61CHN16:
319 case AL_FORMAT_61CHN32:
320 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
321 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
322 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
323 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
324 Context->NumChan = 6;
325 Speaker2Chan[0] = SIDE_LEFT;
326 Speaker2Chan[1] = FRONT_LEFT;
327 Speaker2Chan[2] = FRONT_CENTER;
328 Speaker2Chan[3] = FRONT_RIGHT;
329 Speaker2Chan[4] = SIDE_RIGHT;
330 Speaker2Chan[5] = BACK_CENTER;
331 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
332 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
333 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
334 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
335 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
336 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
337 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
338 break;
340 case AL_FORMAT_71CHN8:
341 case AL_FORMAT_71CHN16:
342 case AL_FORMAT_71CHN32:
343 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
344 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
345 Context->NumChan = 7;
346 Speaker2Chan[0] = BACK_LEFT;
347 Speaker2Chan[1] = SIDE_LEFT;
348 Speaker2Chan[2] = FRONT_LEFT;
349 Speaker2Chan[3] = FRONT_CENTER;
350 Speaker2Chan[4] = FRONT_RIGHT;
351 Speaker2Chan[5] = SIDE_RIGHT;
352 Speaker2Chan[6] = BACK_RIGHT;
353 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
354 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
355 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
356 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
357 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
358 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
359 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
360 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
361 break;
363 default:
364 assert(0);
367 for(pos = 0; pos < LUT_NUM; pos++)
369 /* source angle */
370 Theta = aluLUTpos2Angle(pos);
372 /* clear all values */
373 offset = OUTPUTCHANNELS * pos;
374 for(s = 0; s < OUTPUTCHANNELS; s++)
375 Context->PanningLUT[offset+s] = 0.0f;
377 /* set panning values */
378 for(s = 0; s < Context->NumChan - 1; s++)
380 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
382 /* source between speaker s and speaker s+1 */
383 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
384 (SpeakerAngle[s+1]-SpeakerAngle[s]);
385 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
386 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
387 break;
390 if(s == Context->NumChan - 1)
392 /* source between last and first speaker */
393 if(Theta < SpeakerAngle[0])
394 Theta += 2.0f * M_PI;
395 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
396 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
397 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
398 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
403 static ALvoid CalcSourceParams(const ALCcontext *ALContext, ALsource *ALSource,
404 ALboolean isMono)
406 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix;
407 ALfloat Direction[3],Position[3],SourceToListener[3];
408 ALfloat Velocity[3],ListenerVel[3];
409 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
410 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
411 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound;
412 ALfloat Matrix[4][4];
413 ALfloat flAttenuation;
414 ALfloat RoomAttenuation[MAX_SENDS];
415 ALfloat MetersPerUnit;
416 ALfloat RoomRolloff[MAX_SENDS];
417 ALfloat DryGainHF = 1.0f;
418 ALfloat WetGain[MAX_SENDS];
419 ALfloat WetGainHF[MAX_SENDS];
420 ALfloat DirGain, AmbientGain;
421 ALfloat length;
422 const ALfloat *SpeakerGain;
423 ALuint Frequency;
424 ALint NumSends;
425 ALint pos, s, i;
426 ALfloat cw, a, g;
428 for(i = 0;i < MAX_SENDS;i++)
429 WetGainHF[i] = 1.0f;
431 //Get context properties
432 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
433 DopplerVelocity = ALContext->DopplerVelocity;
434 flSpeedOfSound = ALContext->flSpeedOfSound;
435 NumSends = ALContext->Device->NumAuxSends;
436 Frequency = ALContext->Device->Frequency;
438 //Get listener properties
439 ListenerGain = ALContext->Listener.Gain;
440 MetersPerUnit = ALContext->Listener.MetersPerUnit;
441 memcpy(ListenerVel, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
443 //Get source properties
444 SourceVolume = ALSource->flGain;
445 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
446 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
447 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
448 MinVolume = ALSource->flMinGain;
449 MaxVolume = ALSource->flMaxGain;
450 MinDist = ALSource->flRefDistance;
451 MaxDist = ALSource->flMaxDistance;
452 Rolloff = ALSource->flRollOffFactor;
453 InnerAngle = ALSource->flInnerAngle;
454 OuterAngle = ALSource->flOuterAngle;
455 OuterGainHF = ALSource->OuterGainHF;
457 //Only apply 3D calculations for mono buffers
458 if(isMono == AL_FALSE)
460 //1. Multi-channel buffers always play "normal"
461 ALSource->Params.Pitch = ALSource->flPitch;
463 DryMix = SourceVolume;
464 DryMix = __min(DryMix,MaxVolume);
465 DryMix = __max(DryMix,MinVolume);
467 switch(ALSource->DirectFilter.type)
469 case AL_FILTER_LOWPASS:
470 DryMix *= ALSource->DirectFilter.Gain;
471 DryGainHF *= ALSource->DirectFilter.GainHF;
472 break;
475 ALSource->Params.DryGains[FRONT_LEFT] = DryMix * ListenerGain;
476 ALSource->Params.DryGains[FRONT_RIGHT] = DryMix * ListenerGain;
477 ALSource->Params.DryGains[SIDE_LEFT] = DryMix * ListenerGain;
478 ALSource->Params.DryGains[SIDE_RIGHT] = DryMix * ListenerGain;
479 ALSource->Params.DryGains[BACK_LEFT] = DryMix * ListenerGain;
480 ALSource->Params.DryGains[BACK_RIGHT] = DryMix * ListenerGain;
481 ALSource->Params.DryGains[FRONT_CENTER] = DryMix * ListenerGain;
482 ALSource->Params.DryGains[BACK_CENTER] = DryMix * ListenerGain;
483 ALSource->Params.DryGains[LFE] = DryMix * ListenerGain;
485 for(i = 0;i < NumSends;i++)
487 WetGain[i] = SourceVolume;
488 WetGain[i] = __min(WetGain[i],MaxVolume);
489 WetGain[i] = __max(WetGain[i],MinVolume);
490 WetGainHF[i] = 1.0f;
492 switch(ALSource->Send[i].WetFilter.type)
494 case AL_FILTER_LOWPASS:
495 WetGain[i] *= ALSource->Send[i].WetFilter.Gain;
496 WetGainHF[i] *= ALSource->Send[i].WetFilter.GainHF;
497 break;
500 ALSource->Params.WetGains[i] = WetGain[i] * ListenerGain;
502 for(i = NumSends;i < MAX_SENDS;i++)
504 ALSource->Params.WetGains[i] = 0.0f;
505 WetGainHF[i] = 1.0f;
508 /* Update filter coefficients. Calculations based on the I3DL2
509 * spec. */
510 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency);
511 /* We use two chained one-pole filters, so we need to take the
512 * square root of the squared gain, which is the same as the base
513 * gain. */
514 g = __max(DryGainHF, 0.01f);
515 a = 0.0f;
516 /* Be careful with gains < 0.0001, as that causes the coefficient
517 * head towards 1, which will flatten the signal */
518 if(g < 0.9999f) /* 1-epsilon */
519 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
520 (1 - g);
521 ALSource->Params.iirFilter.coeff = a;
523 for(i = 0;i < NumSends;i++)
525 /* We use a one-pole filter, so we need to take the squared gain */
526 g = __max(WetGainHF[i], 0.1f);
527 g *= g;
528 a = 0.0f;
529 if(g < 0.9999f) /* 1-epsilon */
530 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
531 (1 - g);
532 ALSource->Params.Send[i].iirFilter.coeff = a;
535 return;
538 //1. Translate Listener to origin (convert to head relative)
539 if(ALSource->bHeadRelative==AL_FALSE)
541 ALfloat U[3],V[3],N[3],P[3];
543 // Build transform matrix
544 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
545 aluNormalize(N); // Normalized At-vector
546 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
547 aluNormalize(V); // Normalized Up-vector
548 aluCrossproduct(N, V, U); // Right-vector
549 aluNormalize(U); // Normalized Right-vector
550 P[0] = -(ALContext->Listener.Position[0]*U[0] + // Translation
551 ALContext->Listener.Position[1]*U[1] +
552 ALContext->Listener.Position[2]*U[2]);
553 P[1] = -(ALContext->Listener.Position[0]*V[0] +
554 ALContext->Listener.Position[1]*V[1] +
555 ALContext->Listener.Position[2]*V[2]);
556 P[2] = -(ALContext->Listener.Position[0]*-N[0] +
557 ALContext->Listener.Position[1]*-N[1] +
558 ALContext->Listener.Position[2]*-N[2]);
559 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0]; Matrix[0][3] = 0.0f;
560 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1]; Matrix[1][3] = 0.0f;
561 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2]; Matrix[2][3] = 0.0f;
562 Matrix[3][0] = P[0]; Matrix[3][1] = P[1]; Matrix[3][2] = P[2]; Matrix[3][3] = 1.0f;
564 // Transform source position and direction into listener space
565 aluMatrixVector(Position, 1.0f, Matrix);
566 aluMatrixVector(Direction, 0.0f, Matrix);
567 // Transform source and listener velocity into listener space
568 aluMatrixVector(Velocity, 0.0f, Matrix);
569 aluMatrixVector(ListenerVel, 0.0f, Matrix);
571 else
572 ListenerVel[0] = ListenerVel[1] = ListenerVel[2] = 0.0f;
574 SourceToListener[0] = -Position[0];
575 SourceToListener[1] = -Position[1];
576 SourceToListener[2] = -Position[2];
577 aluNormalize(SourceToListener);
578 aluNormalize(Direction);
580 //2. Calculate distance attenuation
581 Distance = aluSqrt(aluDotproduct(Position, Position));
583 flAttenuation = 1.0f;
584 for(i = 0;i < MAX_SENDS;i++)
586 RoomAttenuation[i] = 1.0f;
588 RoomRolloff[i] = ALSource->RoomRolloffFactor;
589 if(ALSource->Send[i].Slot &&
590 (ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB ||
591 ALSource->Send[i].Slot->effect.type == AL_EFFECT_EAXREVERB))
592 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
595 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
596 ALContext->DistanceModel)
598 case AL_INVERSE_DISTANCE_CLAMPED:
599 Distance=__max(Distance,MinDist);
600 Distance=__min(Distance,MaxDist);
601 if(MaxDist < MinDist)
602 break;
603 //fall-through
604 case AL_INVERSE_DISTANCE:
605 if(MinDist > 0.0f)
607 if((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
608 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
609 for(i = 0;i < NumSends;i++)
611 if((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
612 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
615 break;
617 case AL_LINEAR_DISTANCE_CLAMPED:
618 Distance=__max(Distance,MinDist);
619 Distance=__min(Distance,MaxDist);
620 if(MaxDist < MinDist)
621 break;
622 //fall-through
623 case AL_LINEAR_DISTANCE:
624 Distance=__min(Distance,MaxDist);
625 if(MaxDist != MinDist)
627 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
628 for(i = 0;i < NumSends;i++)
629 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
631 break;
633 case AL_EXPONENT_DISTANCE_CLAMPED:
634 Distance=__max(Distance,MinDist);
635 Distance=__min(Distance,MaxDist);
636 if(MaxDist < MinDist)
637 break;
638 //fall-through
639 case AL_EXPONENT_DISTANCE:
640 if(Distance > 0.0f && MinDist > 0.0f)
642 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
643 for(i = 0;i < NumSends;i++)
644 RoomAttenuation[i] = (ALfloat)pow(Distance/MinDist, -RoomRolloff[i]);
646 break;
648 case AL_NONE:
649 break;
652 // Source Gain + Attenuation
653 DryMix = SourceVolume * flAttenuation;
654 for(i = 0;i < NumSends;i++)
655 WetGain[i] = SourceVolume * RoomAttenuation[i];
657 // Distance-based air absorption
658 if(ALSource->AirAbsorptionFactor > 0.0f && flAttenuation < 1.0f)
660 ALfloat absorb = 0.0f;
662 // Absorption calculation is done in dB
663 if(flAttenuation > 0.0f)
665 absorb = (MinDist/flAttenuation - MinDist)*MetersPerUnit *
666 (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF);
667 // Convert dB to linear gain before applying
668 absorb = pow(10.0, absorb/20.0);
670 DryGainHF *= absorb;
671 for(i = 0;i < MAX_SENDS;i++)
672 WetGainHF[i] *= absorb;
675 //3. Apply directional soundcones
676 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
677 if(Angle >= InnerAngle && Angle <= OuterAngle)
679 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
680 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
681 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
682 DryMix *= ConeVolume;
683 if(ALSource->DryGainHFAuto)
684 DryGainHF *= ConeHF;
686 else if(Angle > OuterAngle)
688 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
689 ConeHF = (1.0f+(OuterGainHF-1.0f));
690 DryMix *= ConeVolume;
691 if(ALSource->DryGainHFAuto)
692 DryGainHF *= ConeHF;
694 else
696 ConeVolume = 1.0f;
697 ConeHF = 1.0f;
700 // Clamp to Min/Max Gain
701 DryMix = __min(DryMix,MaxVolume);
702 DryMix = __max(DryMix,MinVolume);
704 for(i = 0;i < NumSends;i++)
706 if(ALSource->Send[i].Slot &&
707 ALSource->Send[i].Slot->effect.type != AL_EFFECT_NULL)
709 if(ALSource->Send[i].Slot->AuxSendAuto)
711 if(ALSource->WetGainAuto)
712 WetGain[i] *= ConeVolume;
713 if(ALSource->WetGainHFAuto)
714 WetGainHF[i] *= ConeHF;
716 // Clamp to Min/Max Gain
717 WetGain[i] = __min(WetGain[i],MaxVolume);
718 WetGain[i] = __max(WetGain[i],MinVolume);
720 if(ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB ||
721 ALSource->Send[i].Slot->effect.type == AL_EFFECT_EAXREVERB)
723 /* Apply a decay-time transformation to the wet path,
724 * based on the attenuation of the dry path. This should
725 * better approximate the statistical attenuation model
726 * for the reverb effect.
728 * This simple equation converts the distance attenuation
729 * into the time it would take to reach -60 dB. From
730 * there it establishes an origin (0.333s; the decay time
731 * that will produce equal attenuation) and applies the
732 * current decay time. Finally, it converts the result
733 * back to an attenuation for the reverb path.
735 WetGain[i] *= pow(10.0f, log10(flAttenuation) * 0.333f /
736 ALSource->Send[i].Slot->effect.Reverb.DecayTime);
739 else
741 // If the slot's auxiliary send auto is off, the data sent to
742 // the effect slot is the same as the dry path, sans filter
743 // effects
744 WetGain[i] = DryMix;
745 WetGainHF[i] = DryGainHF;
748 switch(ALSource->Send[i].WetFilter.type)
750 case AL_FILTER_LOWPASS:
751 WetGain[i] *= ALSource->Send[i].WetFilter.Gain;
752 WetGainHF[i] *= ALSource->Send[i].WetFilter.GainHF;
753 break;
755 ALSource->Params.WetGains[i] = WetGain[i] * ListenerGain;
757 else
759 ALSource->Params.WetGains[i] = 0.0f;
760 WetGainHF[i] = 1.0f;
763 for(i = NumSends;i < MAX_SENDS;i++)
765 ALSource->Params.WetGains[i] = 0.0f;
766 WetGainHF[i] = 1.0f;
769 // Apply filter gains and filters
770 switch(ALSource->DirectFilter.type)
772 case AL_FILTER_LOWPASS:
773 DryMix *= ALSource->DirectFilter.Gain;
774 DryGainHF *= ALSource->DirectFilter.GainHF;
775 break;
777 DryMix *= ListenerGain;
779 // Calculate Velocity
780 if(DopplerFactor != 0.0f)
782 ALfloat flVSS, flVLS;
783 ALfloat flMaxVelocity = (DopplerVelocity * flSpeedOfSound) /
784 DopplerFactor;
786 flVSS = aluDotproduct(Velocity, SourceToListener);
787 if(flVSS >= flMaxVelocity)
788 flVSS = (flMaxVelocity - 1.0f);
789 else if(flVSS <= -flMaxVelocity)
790 flVSS = -flMaxVelocity + 1.0f;
792 flVLS = aluDotproduct(ListenerVel, SourceToListener);
793 if(flVLS >= flMaxVelocity)
794 flVLS = (flMaxVelocity - 1.0f);
795 else if(flVLS <= -flMaxVelocity)
796 flVLS = -flMaxVelocity + 1.0f;
798 ALSource->Params.Pitch = ALSource->flPitch *
799 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
800 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
802 else
803 ALSource->Params.Pitch = ALSource->flPitch;
805 // Use energy-preserving panning algorithm for multi-speaker playback
806 length = aluSqrt(Position[0]*Position[0] + Position[1]*Position[1] +
807 Position[2]*Position[2]);
808 length = __max(length, MinDist);
809 if(length > 0.0f)
811 ALfloat invlen = 1.0f/length;
812 Position[0] *= invlen;
813 Position[1] *= invlen;
814 Position[2] *= invlen;
817 pos = aluCart2LUTpos(-Position[2], Position[0]);
818 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
820 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
821 // elevation adjustment for directional gain. this sucks, but
822 // has low complexity
823 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
824 for(s = 0; s < OUTPUTCHANNELS; s++)
826 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
827 ALSource->Params.DryGains[s] = DryMix * gain;
830 /* Update filter coefficients. */
831 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency);
832 /* Spatialized sources use four chained one-pole filters, so we need to
833 * take the fourth root of the squared gain, which is the same as the
834 * square root of the base gain. */
835 g = aluSqrt(__max(DryGainHF, 0.0001f));
836 a = 0.0f;
837 if(g < 0.9999f) /* 1-epsilon */
838 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
839 (1 - g);
840 ALSource->Params.iirFilter.coeff = a;
842 for(i = 0;i < NumSends;i++)
844 /* The wet path uses two chained one-pole filters, so take the
845 * base gain (square root of the squared gain) */
846 g = __max(WetGainHF[i], 0.01f);
847 a = 0.0f;
848 if(g < 0.9999f) /* 1-epsilon */
849 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
850 (1 - g);
851 ALSource->Params.Send[i].iirFilter.coeff = a;
855 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
857 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
860 static void MixSomeSources(ALCcontext *ALContext, float (*DryBuffer)[OUTPUTCHANNELS], ALuint SamplesToDo)
862 static float DummyBuffer[BUFFERSIZE];
863 ALfloat *WetBuffer[MAX_SENDS];
864 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
865 ALfloat DrySend[OUTPUTCHANNELS];
866 ALfloat dryGainStep[OUTPUTCHANNELS];
867 ALfloat wetGainStep[MAX_SENDS];
868 ALuint i, j, k, out;
869 ALsource *ALSource;
870 ALfloat value, outsamp;
871 ALbufferlistitem *BufferListItem;
872 ALint64 DataSize64,DataPos64;
873 FILTER *DryFilter, *WetFilter[MAX_SENDS];
874 ALfloat WetSend[MAX_SENDS];
875 ALuint rampLength;
876 ALuint DeviceFreq;
877 ALint increment;
878 ALuint DataPosInt, DataPosFrac;
879 ALuint Channels, Bytes;
880 ALuint Frequency;
881 ALuint BuffersPlayed;
882 ALfloat Pitch;
883 ALenum State;
885 if(!(ALSource=ALContext->Source))
886 return;
888 DeviceFreq = ALContext->Device->Frequency;
890 rampLength = DeviceFreq * MIN_RAMP_LENGTH / 1000;
891 rampLength = max(rampLength, SamplesToDo);
893 another_source:
894 State = ALSource->state;
895 if(State != AL_PLAYING)
897 if((ALSource=ALSource->next) != NULL)
898 goto another_source;
899 return;
901 j = 0;
903 /* Find buffer format */
904 Frequency = 0;
905 Channels = 0;
906 Bytes = 0;
907 BufferListItem = ALSource->queue;
908 while(BufferListItem != NULL)
910 ALbuffer *ALBuffer;
911 if((ALBuffer=BufferListItem->buffer) != NULL)
913 Channels = aluChannelsFromFormat(ALBuffer->format);
914 Bytes = aluBytesFromFormat(ALBuffer->format);
915 Frequency = ALBuffer->frequency;
916 break;
918 BufferListItem = BufferListItem->next;
921 /* Get source info */
922 BuffersPlayed = ALSource->BuffersPlayed;
923 DataPosInt = ALSource->position;
924 DataPosFrac = ALSource->position_fraction;
926 if(ALSource->NeedsUpdate)
928 CalcSourceParams(ALContext, ALSource, (Channels==1)?AL_TRUE:AL_FALSE);
929 ALSource->NeedsUpdate = AL_FALSE;
932 /* Compute 18.14 fixed point step */
933 Pitch = (ALSource->Params.Pitch*Frequency) / DeviceFreq;
934 if(Pitch > (float)MAX_PITCH) Pitch = (float)MAX_PITCH;
935 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
936 if(increment <= 0) increment = (1<<FRACTIONBITS);
938 /* Compute the gain steps for each output channel */
939 if(ALSource->FirstStart)
941 for(i = 0;i < OUTPUTCHANNELS;i++)
942 DrySend[i] = ALSource->Params.DryGains[i];
943 for(i = 0;i < MAX_SENDS;i++)
944 WetSend[i] = ALSource->Params.WetGains[i];
946 else
948 for(i = 0;i < OUTPUTCHANNELS;i++)
949 DrySend[i] = ALSource->DryGains[i];
950 for(i = 0;i < MAX_SENDS;i++)
951 WetSend[i] = ALSource->WetGains[i];
954 DryFilter = &ALSource->Params.iirFilter;
955 for(i = 0;i < MAX_SENDS;i++)
957 WetFilter[i] = &ALSource->Params.Send[i].iirFilter;
958 WetBuffer[i] = (ALSource->Send[i].Slot ?
959 ALSource->Send[i].Slot->WetBuffer :
960 DummyBuffer);
963 if(DuplicateStereo && Channels == 2)
965 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
966 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
967 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
968 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
970 else if(DuplicateStereo)
972 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
973 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
974 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
975 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
978 /* Get current buffer queue item */
979 BufferListItem = ALSource->queue;
980 for(i = 0;i < BuffersPlayed && BufferListItem;i++)
981 BufferListItem = BufferListItem->next;
983 while(State == AL_PLAYING && j < SamplesToDo)
985 ALuint DataSize = 0;
986 ALbuffer *ALBuffer;
987 ALshort *Data;
988 ALuint BufferSize;
990 /* Get buffer info */
991 if((ALBuffer=BufferListItem->buffer) != NULL)
993 Data = ALBuffer->data;
994 DataSize = ALBuffer->size;
995 DataSize /= Channels * Bytes;
997 if(DataPosInt >= DataSize)
998 goto skipmix;
1000 if(BufferListItem->next)
1002 ALbuffer *NextBuf = BufferListItem->next->buffer;
1003 if(NextBuf && NextBuf->data)
1005 ALint ulExtraSamples = BUFFER_PADDING*Channels*Bytes;
1006 ulExtraSamples = min(NextBuf->size, ulExtraSamples);
1007 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1010 else if(ALSource->bLooping)
1012 ALbuffer *NextBuf = ALSource->queue->buffer;
1013 if(NextBuf && NextBuf->data)
1015 ALint ulExtraSamples = BUFFER_PADDING*Channels*Bytes;
1016 ulExtraSamples = min(NextBuf->size, ulExtraSamples);
1017 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1020 else
1021 memset(&Data[DataSize*Channels], 0, (BUFFER_PADDING*Channels*Bytes));
1023 /* Compute the gain steps for each output channel */
1024 for(i = 0;i < OUTPUTCHANNELS;i++)
1025 dryGainStep[i] = (ALSource->Params.DryGains[i]-
1026 DrySend[i]) / rampLength;
1027 for(i = 0;i < MAX_SENDS;i++)
1028 wetGainStep[i] = (ALSource->Params.WetGains[i]-
1029 WetSend[i]) / rampLength;
1031 /* Figure out how many samples we can mix. */
1032 DataSize64 = DataSize;
1033 DataSize64 <<= FRACTIONBITS;
1034 DataPos64 = DataPosInt;
1035 DataPos64 <<= FRACTIONBITS;
1036 DataPos64 += DataPosFrac;
1037 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1039 BufferSize = min(BufferSize, (SamplesToDo-j));
1041 /* Actual sample mixing loop */
1042 k = 0;
1043 Data += DataPosInt*Channels;
1045 if(Channels == 1) /* Mono */
1047 while(BufferSize--)
1049 for(i = 0;i < OUTPUTCHANNELS;i++)
1050 DrySend[i] += dryGainStep[i];
1051 for(i = 0;i < MAX_SENDS;i++)
1052 WetSend[i] += wetGainStep[i];
1054 /* First order interpolator */
1055 value = lerp(Data[k], Data[k+1], DataPosFrac);
1057 /* Direct path final mix buffer and panning */
1058 outsamp = lpFilter4P(DryFilter, 0, value);
1059 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1060 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1061 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1062 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1063 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1064 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1065 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1066 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1068 /* Room path final mix buffer and panning */
1069 for(i = 0;i < MAX_SENDS;i++)
1071 outsamp = lpFilter2P(WetFilter[i], 0, value);
1072 WetBuffer[i][j] += outsamp*WetSend[i];
1075 DataPosFrac += increment;
1076 k += DataPosFrac>>FRACTIONBITS;
1077 DataPosFrac &= FRACTIONMASK;
1078 j++;
1081 else if(Channels == 2) /* Stereo */
1083 const int chans[] = {
1084 FRONT_LEFT, FRONT_RIGHT
1086 const ALfloat scaler = aluSqrt(1.0f/Channels);
1088 #define DO_MIX() do { \
1089 while(BufferSize--) \
1091 for(i = 0;i < OUTPUTCHANNELS;i++) \
1092 DrySend[i] += dryGainStep[i]; \
1093 for(i = 0;i < MAX_SENDS;i++) \
1094 WetSend[i] += wetGainStep[i]; \
1096 for(i = 0;i < Channels;i++) \
1098 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1099 outsamp = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1100 for(out = 0;out < OUTPUTCHANNELS;out++) \
1101 DryBuffer[j][out] += outsamp*Matrix[chans[i]][out]; \
1102 for(out = 0;out < MAX_SENDS;out++) \
1104 outsamp = lpFilter1P(WetFilter[out], chans[out], value); \
1105 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1109 DataPosFrac += increment; \
1110 k += DataPosFrac>>FRACTIONBITS; \
1111 DataPosFrac &= FRACTIONMASK; \
1112 j++; \
1114 } while(0)
1116 DO_MIX();
1118 else if(Channels == 4) /* Quad */
1120 const int chans[] = {
1121 FRONT_LEFT, FRONT_RIGHT,
1122 BACK_LEFT, BACK_RIGHT
1124 const ALfloat scaler = aluSqrt(1.0f/Channels);
1126 DO_MIX();
1128 else if(Channels == 6) /* 5.1 */
1130 const int chans[] = {
1131 FRONT_LEFT, FRONT_RIGHT,
1132 FRONT_CENTER, LFE,
1133 BACK_LEFT, BACK_RIGHT
1135 const ALfloat scaler = aluSqrt(1.0f/Channels);
1137 DO_MIX();
1139 else if(Channels == 7) /* 6.1 */
1141 const int chans[] = {
1142 FRONT_LEFT, FRONT_RIGHT,
1143 FRONT_CENTER, LFE,
1144 BACK_CENTER,
1145 SIDE_LEFT, SIDE_RIGHT
1147 const ALfloat scaler = aluSqrt(1.0f/Channels);
1149 DO_MIX();
1151 else if(Channels == 8) /* 7.1 */
1153 const int chans[] = {
1154 FRONT_LEFT, FRONT_RIGHT,
1155 FRONT_CENTER, LFE,
1156 BACK_LEFT, BACK_RIGHT,
1157 SIDE_LEFT, SIDE_RIGHT
1159 const ALfloat scaler = aluSqrt(1.0f/Channels);
1161 DO_MIX();
1162 #undef DO_MIX
1164 else /* Unknown? */
1166 for(i = 0;i < OUTPUTCHANNELS;i++)
1167 DrySend[i] += dryGainStep[i]*BufferSize;
1168 for(i = 0;i < MAX_SENDS;i++)
1169 WetSend[i] += wetGainStep[i]*BufferSize;
1170 while(BufferSize--)
1172 DataPosFrac += increment;
1173 k += DataPosFrac>>FRACTIONBITS;
1174 DataPosFrac &= FRACTIONMASK;
1175 j++;
1178 DataPosInt += k;
1180 skipmix:
1181 /* Handle looping sources */
1182 if(DataPosInt >= DataSize)
1184 if(BuffersPlayed < (ALSource->BuffersInQueue-1))
1186 BufferListItem = BufferListItem->next;
1187 BuffersPlayed++;
1188 DataPosInt -= DataSize;
1190 else
1192 if(!ALSource->bLooping)
1194 State = AL_STOPPED;
1195 BufferListItem = ALSource->queue;
1196 BuffersPlayed = ALSource->BuffersInQueue;
1197 DataPosInt = 0;
1198 DataPosFrac = 0;
1200 else
1202 BufferListItem = ALSource->queue;
1203 BuffersPlayed = 0;
1204 if(ALSource->BuffersInQueue == 1)
1205 DataPosInt %= DataSize;
1206 else
1207 DataPosInt -= DataSize;
1213 /* Update source info */
1214 ALSource->state = State;
1215 ALSource->BuffersPlayed = BuffersPlayed;
1216 ALSource->position = DataPosInt;
1217 ALSource->position_fraction = DataPosFrac;
1218 ALSource->Buffer = BufferListItem->buffer;
1220 for(i = 0;i < OUTPUTCHANNELS;i++)
1221 ALSource->DryGains[i] = DrySend[i];
1222 for(i = 0;i < MAX_SENDS;i++)
1223 ALSource->WetGains[i] = WetSend[i];
1225 ALSource->FirstStart = AL_FALSE;
1227 if((ALSource=ALSource->next) != NULL)
1228 goto another_source;
1231 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1233 float (*DryBuffer)[OUTPUTCHANNELS];
1234 ALuint SamplesToDo;
1235 ALeffectslot *ALEffectSlot;
1236 ALCcontext *ALContext;
1237 int fpuState;
1238 ALuint i, c;
1240 SuspendContext(NULL);
1242 #if defined(HAVE_FESETROUND)
1243 fpuState = fegetround();
1244 fesetround(FE_TOWARDZERO);
1245 #elif defined(HAVE__CONTROLFP)
1246 fpuState = _controlfp(0, 0);
1247 _controlfp(_RC_CHOP, _MCW_RC);
1248 #else
1249 (void)fpuState;
1250 #endif
1252 DryBuffer = device->DryBuffer;
1253 while(size > 0)
1255 /* Setup variables */
1256 SamplesToDo = min(size, BUFFERSIZE);
1258 /* Clear mixing buffer */
1259 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
1261 for(c = 0;c < device->NumContexts;c++)
1263 ALContext = device->Contexts[c];
1264 SuspendContext(ALContext);
1266 MixSomeSources(ALContext, DryBuffer, SamplesToDo);
1268 /* effect slot processing */
1269 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
1270 while(ALEffectSlot)
1272 if(ALEffectSlot->EffectState)
1273 ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1275 for(i = 0;i < SamplesToDo;i++)
1276 ALEffectSlot->WetBuffer[i] = 0.0f;
1277 ALEffectSlot = ALEffectSlot->next;
1279 ProcessContext(ALContext);
1282 //Post processing loop
1283 switch(device->Format)
1285 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1286 case AL_FORMAT_MONO##bits: \
1287 for(i = 0;i < SamplesToDo;i++) \
1289 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1290 DryBuffer[i][FRONT_RIGHT]); \
1291 buffer = ((type*)buffer) + 1; \
1293 break; \
1294 case AL_FORMAT_STEREO##bits: \
1295 if(device->Bs2b) \
1297 for(i = 0;i < SamplesToDo;i++) \
1299 float samples[2]; \
1300 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1301 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1302 bs2b_cross_feed(device->Bs2b, samples); \
1303 ((type*)buffer)[0] = (func)(samples[0]); \
1304 ((type*)buffer)[1] = (func)(samples[1]); \
1305 buffer = ((type*)buffer) + 2; \
1308 else \
1310 for(i = 0;i < SamplesToDo;i++) \
1312 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1313 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1314 buffer = ((type*)buffer) + 2; \
1317 break; \
1318 case AL_FORMAT_QUAD##bits: \
1319 for(i = 0;i < SamplesToDo;i++) \
1321 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1322 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1323 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1324 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1325 buffer = ((type*)buffer) + 4; \
1327 break; \
1328 case AL_FORMAT_51CHN##bits: \
1329 for(i = 0;i < SamplesToDo;i++) \
1331 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1332 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1333 if(isWin) { \
1334 /* Of course, Windows can't use the same ordering... */ \
1335 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1336 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1337 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1338 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1339 } else { \
1340 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1341 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1342 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1343 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1345 buffer = ((type*)buffer) + 6; \
1347 break; \
1348 case AL_FORMAT_61CHN##bits: \
1349 for(i = 0;i < SamplesToDo;i++) \
1351 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1352 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1353 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1354 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1355 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1356 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1357 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1358 buffer = ((type*)buffer) + 7; \
1360 break; \
1361 case AL_FORMAT_71CHN##bits: \
1362 for(i = 0;i < SamplesToDo;i++) \
1364 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1365 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1366 if(isWin) { \
1367 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1368 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1369 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1370 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1371 } else { \
1372 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1373 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1374 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1375 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1377 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1378 ((type*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1379 buffer = ((type*)buffer) + 8; \
1381 break;
1383 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1384 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1385 #ifdef _WIN32
1386 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB, 1)
1387 CHECK_WRITE_FORMAT(16, ALshort, aluF2S, 1)
1388 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F, 1)
1389 #else
1390 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB, 0)
1391 CHECK_WRITE_FORMAT(16, ALshort, aluF2S, 0)
1392 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F, 0)
1393 #endif
1394 #undef AL_FORMAT_STEREO32
1395 #undef AL_FORMAT_MONO32
1396 #undef CHECK_WRITE_FORMAT
1398 default:
1399 break;
1402 size -= SamplesToDo;
1405 #if defined(HAVE_FESETROUND)
1406 fesetround(fpuState);
1407 #elif defined(HAVE__CONTROLFP)
1408 _controlfp(fpuState, 0xfffff);
1409 #endif
1411 ProcessContext(NULL);
1414 ALvoid aluHandleDisconnect(ALCdevice *device)
1416 ALuint i;
1418 SuspendContext(NULL);
1419 for(i = 0;i < device->NumContexts;i++)
1421 ALsource *source;
1423 SuspendContext(device->Contexts[i]);
1425 source = device->Contexts[i]->Source;
1426 while(source)
1428 if(source->state == AL_PLAYING)
1430 source->state = AL_STOPPED;
1431 source->BuffersPlayed = source->BuffersInQueue;
1432 source->position = 0;
1433 source->position_fraction = 0;
1435 source = source->next;
1437 ProcessContext(device->Contexts[i]);
1440 device->Connected = ALC_FALSE;
1441 ProcessContext(NULL);