Store the buffer handle directly in the source and buffer queue list
[openal-soft.git] / Alc / ALu.c
blobd69285f1723d72de758f958e9a418e9c64615e20
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 matrix[3][3])
115 ALfloat result[3];
117 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
118 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
119 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
120 memcpy(vector, result, sizeof(result));
123 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
124 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
126 const char *confkey;
127 const char *next;
128 const char *sep;
129 const char *end;
130 int i, val;
132 confkey = GetConfigValue(NULL, name, "");
133 next = confkey;
134 while(next && *next)
136 confkey = next;
137 next = strchr(confkey, ',');
138 if(next)
140 do {
141 next++;
142 } while(isspace(*next));
145 sep = strchr(confkey, '=');
146 if(!sep || confkey == sep)
147 continue;
149 end = sep - 1;
150 while(isspace(*end) && end != confkey)
151 end--;
152 end++;
154 if(strncmp(confkey, "fl", end-confkey) == 0)
155 val = FRONT_LEFT;
156 else if(strncmp(confkey, "fr", end-confkey) == 0)
157 val = FRONT_RIGHT;
158 else if(strncmp(confkey, "fc", end-confkey) == 0)
159 val = FRONT_CENTER;
160 else if(strncmp(confkey, "bl", end-confkey) == 0)
161 val = BACK_LEFT;
162 else if(strncmp(confkey, "br", end-confkey) == 0)
163 val = BACK_RIGHT;
164 else if(strncmp(confkey, "bc", end-confkey) == 0)
165 val = BACK_CENTER;
166 else if(strncmp(confkey, "sl", end-confkey) == 0)
167 val = SIDE_LEFT;
168 else if(strncmp(confkey, "sr", end-confkey) == 0)
169 val = SIDE_RIGHT;
170 else
172 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
173 continue;
176 sep++;
177 while(isspace(*sep))
178 sep++;
180 for(i = 0;i < chans;i++)
182 if(Speaker2Chan[i] == val)
184 val = strtol(sep, NULL, 10);
185 if(val >= -180 && val <= 180)
186 SpeakerAngle[i] = val * M_PI/180.0f;
187 else
188 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
189 break;
194 for(i = 1;i < chans;i++)
196 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
198 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
199 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
200 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
205 static __inline ALfloat aluLUTpos2Angle(ALint pos)
207 if(pos < QUADRANT_NUM)
208 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
209 if(pos < 2 * QUADRANT_NUM)
210 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
211 if(pos < 3 * QUADRANT_NUM)
212 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
213 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
216 ALvoid aluInitPanning(ALCcontext *Context)
218 ALint pos, offset, s;
219 ALfloat Alpha, Theta;
220 ALfloat SpeakerAngle[OUTPUTCHANNELS];
221 ALint Speaker2Chan[OUTPUTCHANNELS];
223 for(s = 0;s < OUTPUTCHANNELS;s++)
225 int s2;
226 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
227 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
230 switch(Context->Device->Format)
232 /* Mono is rendered as stereo, then downmixed during post-process */
233 case AL_FORMAT_MONO8:
234 case AL_FORMAT_MONO16:
235 case AL_FORMAT_MONO_FLOAT32:
236 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
237 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
238 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
239 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
240 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
241 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
242 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
243 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
244 Context->NumChan = 2;
245 Speaker2Chan[0] = FRONT_LEFT;
246 Speaker2Chan[1] = FRONT_RIGHT;
247 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
248 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
249 break;
251 case AL_FORMAT_STEREO8:
252 case AL_FORMAT_STEREO16:
253 case AL_FORMAT_STEREO_FLOAT32:
254 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
255 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
256 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
257 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
258 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
259 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
260 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
261 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
262 Context->NumChan = 2;
263 Speaker2Chan[0] = FRONT_LEFT;
264 Speaker2Chan[1] = FRONT_RIGHT;
265 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
266 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
267 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
268 break;
270 case AL_FORMAT_QUAD8:
271 case AL_FORMAT_QUAD16:
272 case AL_FORMAT_QUAD32:
273 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
274 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
275 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
276 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
277 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
278 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
279 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
280 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
281 Context->NumChan = 4;
282 Speaker2Chan[0] = BACK_LEFT;
283 Speaker2Chan[1] = FRONT_LEFT;
284 Speaker2Chan[2] = FRONT_RIGHT;
285 Speaker2Chan[3] = BACK_RIGHT;
286 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
287 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
288 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
289 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
290 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
291 break;
293 case AL_FORMAT_51CHN8:
294 case AL_FORMAT_51CHN16:
295 case AL_FORMAT_51CHN32:
296 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
297 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
298 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
299 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
300 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
301 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
302 Context->NumChan = 5;
303 Speaker2Chan[0] = BACK_LEFT;
304 Speaker2Chan[1] = FRONT_LEFT;
305 Speaker2Chan[2] = FRONT_CENTER;
306 Speaker2Chan[3] = FRONT_RIGHT;
307 Speaker2Chan[4] = BACK_RIGHT;
308 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
309 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
310 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
311 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
312 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
313 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
314 break;
316 case AL_FORMAT_61CHN8:
317 case AL_FORMAT_61CHN16:
318 case AL_FORMAT_61CHN32:
319 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
320 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
321 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
322 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
323 Context->NumChan = 6;
324 Speaker2Chan[0] = SIDE_LEFT;
325 Speaker2Chan[1] = FRONT_LEFT;
326 Speaker2Chan[2] = FRONT_CENTER;
327 Speaker2Chan[3] = FRONT_RIGHT;
328 Speaker2Chan[4] = SIDE_RIGHT;
329 Speaker2Chan[5] = BACK_CENTER;
330 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
331 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
332 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
333 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
334 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
335 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
336 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
337 break;
339 case AL_FORMAT_71CHN8:
340 case AL_FORMAT_71CHN16:
341 case AL_FORMAT_71CHN32:
342 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
343 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
344 Context->NumChan = 7;
345 Speaker2Chan[0] = BACK_LEFT;
346 Speaker2Chan[1] = SIDE_LEFT;
347 Speaker2Chan[2] = FRONT_LEFT;
348 Speaker2Chan[3] = FRONT_CENTER;
349 Speaker2Chan[4] = FRONT_RIGHT;
350 Speaker2Chan[5] = SIDE_RIGHT;
351 Speaker2Chan[6] = BACK_RIGHT;
352 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
353 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
354 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
355 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
356 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
357 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
358 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
359 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
360 break;
362 default:
363 assert(0);
366 for(pos = 0; pos < LUT_NUM; pos++)
368 /* source angle */
369 Theta = aluLUTpos2Angle(pos);
371 /* clear all values */
372 offset = OUTPUTCHANNELS * pos;
373 for(s = 0; s < OUTPUTCHANNELS; s++)
374 Context->PanningLUT[offset+s] = 0.0f;
376 /* set panning values */
377 for(s = 0; s < Context->NumChan - 1; s++)
379 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
381 /* source between speaker s and speaker s+1 */
382 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
383 (SpeakerAngle[s+1]-SpeakerAngle[s]);
384 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
385 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
386 break;
389 if(s == Context->NumChan - 1)
391 /* source between last and first speaker */
392 if(Theta < SpeakerAngle[0])
393 Theta += 2.0f * M_PI;
394 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
395 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
396 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
397 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
402 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
404 ALint pos = 0;
405 ALfloat denom = aluFabs(re) + aluFabs(im);
406 if(denom > 0.0f)
407 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
409 if(re < 0.0)
410 pos = 2 * QUADRANT_NUM - pos;
411 if(im < 0.0)
412 pos = LUT_NUM - pos;
413 return pos%LUT_NUM;
416 static ALvoid CalcSourceParams(const ALCcontext *ALContext, ALsource *ALSource,
417 ALboolean isMono)
419 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix;
420 ALfloat Direction[3],Position[3],SourceToListener[3];
421 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
422 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
423 ALfloat U[3],V[3],N[3];
424 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound;
425 ALfloat Matrix[3][3];
426 ALfloat flAttenuation;
427 ALfloat RoomAttenuation[MAX_SENDS];
428 ALfloat MetersPerUnit;
429 ALfloat RoomRolloff[MAX_SENDS];
430 ALfloat DryGainHF = 1.0f;
431 ALfloat WetGain[MAX_SENDS];
432 ALfloat WetGainHF[MAX_SENDS];
433 ALfloat DirGain, AmbientGain;
434 ALfloat length;
435 const ALfloat *SpeakerGain;
436 ALuint Frequency;
437 ALint NumSends;
438 ALint pos, s, i;
439 ALfloat cw, a, g;
441 //Get context properties
442 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
443 DopplerVelocity = ALContext->DopplerVelocity;
444 flSpeedOfSound = ALContext->flSpeedOfSound;
445 NumSends = ALContext->Device->NumAuxSends;
446 Frequency = ALContext->Device->Frequency;
448 //Get listener properties
449 ListenerGain = ALContext->Listener.Gain;
450 MetersPerUnit = ALContext->Listener.MetersPerUnit;
452 //Get source properties
453 SourceVolume = ALSource->flGain;
454 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
455 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
456 MinVolume = ALSource->flMinGain;
457 MaxVolume = ALSource->flMaxGain;
458 MinDist = ALSource->flRefDistance;
459 MaxDist = ALSource->flMaxDistance;
460 Rolloff = ALSource->flRollOffFactor;
461 InnerAngle = ALSource->flInnerAngle;
462 OuterAngle = ALSource->flOuterAngle;
463 OuterGainHF = ALSource->OuterGainHF;
465 //Only apply 3D calculations for mono buffers
466 if(isMono == AL_FALSE)
468 //1. Multi-channel buffers always play "normal"
469 ALSource->Params.Pitch = ALSource->flPitch;
471 DryMix = SourceVolume;
472 DryMix = __min(DryMix,MaxVolume);
473 DryMix = __max(DryMix,MinVolume);
475 switch(ALSource->DirectFilter.type)
477 case AL_FILTER_LOWPASS:
478 DryMix *= ALSource->DirectFilter.Gain;
479 DryGainHF *= ALSource->DirectFilter.GainHF;
480 break;
483 ALSource->Params.DryGains[FRONT_LEFT] = DryMix * ListenerGain;
484 ALSource->Params.DryGains[FRONT_RIGHT] = DryMix * ListenerGain;
485 ALSource->Params.DryGains[SIDE_LEFT] = DryMix * ListenerGain;
486 ALSource->Params.DryGains[SIDE_RIGHT] = DryMix * ListenerGain;
487 ALSource->Params.DryGains[BACK_LEFT] = DryMix * ListenerGain;
488 ALSource->Params.DryGains[BACK_RIGHT] = DryMix * ListenerGain;
489 ALSource->Params.DryGains[FRONT_CENTER] = DryMix * ListenerGain;
490 ALSource->Params.DryGains[BACK_CENTER] = DryMix * ListenerGain;
491 ALSource->Params.DryGains[LFE] = DryMix * ListenerGain;
492 for(i = 0;i < MAX_SENDS;i++)
493 ALSource->Params.WetGains[i] = 0.0f;
495 /* Update filter coefficients. Calculations based on the I3DL2
496 * spec. */
497 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency);
498 /* We use two chained one-pole filters, so we need to take the
499 * square root of the squared gain, which is the same as the base
500 * gain. */
501 g = __max(DryGainHF, 0.01f);
502 a = 0.0f;
503 /* Be careful with gains < 0.0001, as that causes the coefficient
504 * head towards 1, which will flatten the signal */
505 if(g < 0.9999f) /* 1-epsilon */
506 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
507 (1 - g);
508 ALSource->Params.iirFilter.coeff = a;
509 for(i = 0;i < MAX_SENDS;i++)
510 ALSource->Params.Send[i].iirFilter.coeff = 0.0f;
512 return;
515 //1. Translate Listener to origin (convert to head relative)
516 // Note that Direction and SourceToListener are *not* transformed.
517 // SourceToListener is used with the source and listener velocities,
518 // which are untransformed, and Direction is used with SourceToListener
519 // for the sound cone
520 if(ALSource->bHeadRelative==AL_FALSE)
522 // Build transform matrix
523 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
524 aluNormalize(U); // Normalized Right-vector
525 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
526 aluNormalize(V); // Normalized Up-vector
527 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
528 aluNormalize(N); // Normalized At-vector
529 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
530 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
531 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
533 // Translate source position into listener space
534 Position[0] -= ALContext->Listener.Position[0];
535 Position[1] -= ALContext->Listener.Position[1];
536 Position[2] -= ALContext->Listener.Position[2];
538 SourceToListener[0] = -Position[0];
539 SourceToListener[1] = -Position[1];
540 SourceToListener[2] = -Position[2];
542 // Transform source position into listener space
543 aluMatrixVector(Position, Matrix);
545 else
547 SourceToListener[0] = -Position[0];
548 SourceToListener[1] = -Position[1];
549 SourceToListener[2] = -Position[2];
551 aluNormalize(SourceToListener);
552 aluNormalize(Direction);
554 //2. Calculate distance attenuation
555 Distance = aluSqrt(aluDotproduct(Position, Position));
557 flAttenuation = 1.0f;
558 for(i = 0;i < MAX_SENDS;i++)
560 RoomAttenuation[i] = 1.0f;
562 RoomRolloff[i] = ALSource->RoomRolloffFactor;
563 if(ALSource->Send[i].Slot &&
564 ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB)
565 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
568 switch(ALSource->DistanceModel)
570 case AL_INVERSE_DISTANCE_CLAMPED:
571 Distance=__max(Distance,MinDist);
572 Distance=__min(Distance,MaxDist);
573 if(MaxDist < MinDist)
574 break;
575 //fall-through
576 case AL_INVERSE_DISTANCE:
577 if(MinDist > 0.0f)
579 if((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
580 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
581 for(i = 0;i < NumSends;i++)
583 if((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
584 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
587 break;
589 case AL_LINEAR_DISTANCE_CLAMPED:
590 Distance=__max(Distance,MinDist);
591 Distance=__min(Distance,MaxDist);
592 if(MaxDist < MinDist)
593 break;
594 //fall-through
595 case AL_LINEAR_DISTANCE:
596 Distance=__min(Distance,MaxDist);
597 if(MaxDist != MinDist)
599 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
600 for(i = 0;i < NumSends;i++)
601 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
603 break;
605 case AL_EXPONENT_DISTANCE_CLAMPED:
606 Distance=__max(Distance,MinDist);
607 Distance=__min(Distance,MaxDist);
608 if(MaxDist < MinDist)
609 break;
610 //fall-through
611 case AL_EXPONENT_DISTANCE:
612 if(Distance > 0.0f && MinDist > 0.0f)
614 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
615 for(i = 0;i < NumSends;i++)
616 RoomAttenuation[i] = (ALfloat)pow(Distance/MinDist, -RoomRolloff[i]);
618 break;
620 case AL_NONE:
621 break;
624 // Source Gain + Attenuation and clamp to Min/Max Gain
625 DryMix = SourceVolume * flAttenuation;
626 DryMix = __min(DryMix,MaxVolume);
627 DryMix = __max(DryMix,MinVolume);
629 for(i = 0;i < NumSends;i++)
631 ALfloat WetMix = SourceVolume * RoomAttenuation[i];
632 WetMix = __min(WetMix,MaxVolume);
633 WetGain[i] = __max(WetMix,MinVolume);
634 WetGainHF[i] = 1.0f;
637 // Distance-based air absorption
638 if(ALSource->AirAbsorptionFactor > 0.0f && ALSource->DistanceModel != AL_NONE)
640 ALfloat dist = Distance-MinDist;
641 ALfloat absorb;
643 if(dist < 0.0f) dist = 0.0f;
644 // Absorption calculation is done in dB
645 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
646 (dist*MetersPerUnit);
647 // Convert dB to linear gain before applying
648 absorb = pow(10.0, absorb/20.0);
649 DryGainHF *= absorb;
650 for(i = 0;i < MAX_SENDS;i++)
651 WetGainHF[i] *= absorb;
654 //3. Apply directional soundcones
655 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
656 if(Angle >= InnerAngle && Angle <= OuterAngle)
658 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
659 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
660 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
661 DryMix *= ConeVolume;
662 if(ALSource->DryGainHFAuto)
663 DryGainHF *= ConeHF;
665 else if(Angle > OuterAngle)
667 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
668 ConeHF = (1.0f+(OuterGainHF-1.0f));
669 DryMix *= ConeVolume;
670 if(ALSource->DryGainHFAuto)
671 DryGainHF *= ConeHF;
673 else
675 ConeVolume = 1.0f;
676 ConeHF = 1.0f;
679 //4. Calculate Velocity
680 if(DopplerFactor != 0.0f)
682 ALfloat flVSS, flVLS = 0.0f;
683 ALfloat flMaxVelocity = (DopplerVelocity * flSpeedOfSound) /
684 DopplerFactor;
686 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
687 if(flVSS >= flMaxVelocity)
688 flVSS = (flMaxVelocity - 1.0f);
689 else if(flVSS <= -flMaxVelocity)
690 flVSS = -flMaxVelocity + 1.0f;
692 if(ALSource->bHeadRelative == AL_FALSE)
694 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
695 if(flVLS >= flMaxVelocity)
696 flVLS = (flMaxVelocity - 1.0f);
697 else if(flVLS <= -flMaxVelocity)
698 flVLS = -flMaxVelocity + 1.0f;
701 ALSource->Params.Pitch = ALSource->flPitch *
702 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
703 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
705 else
706 ALSource->Params.Pitch = ALSource->flPitch;
708 for(i = 0;i < NumSends;i++)
710 if(ALSource->Send[i].Slot &&
711 ALSource->Send[i].Slot->effect.type != AL_EFFECT_NULL)
713 if(ALSource->Send[i].Slot->AuxSendAuto)
715 if(ALSource->WetGainAuto)
716 WetGain[i] *= ConeVolume;
717 if(ALSource->WetGainHFAuto)
718 WetGainHF[i] *= ConeHF;
720 // Apply minimal attenuation in place of missing
721 // statistical reverb model.
722 WetGain[i] *= pow(DryMix, 1.0f / 2.0f);
724 else
726 // If the slot's auxiliary send auto is off, the data sent to
727 // the effect slot is the same as the dry path, sans filter
728 // effects
729 WetGain[i] = DryMix;
730 WetGainHF[i] = DryGainHF;
733 switch(ALSource->Send[i].WetFilter.type)
735 case AL_FILTER_LOWPASS:
736 WetGain[i] *= ALSource->Send[i].WetFilter.Gain;
737 WetGainHF[i] *= ALSource->Send[i].WetFilter.GainHF;
738 break;
740 ALSource->Params.WetGains[i] = WetGain[i] * ListenerGain;
742 else
744 ALSource->Params.WetGains[i] = 0.0f;
745 WetGainHF[i] = 1.0f;
748 for(i = NumSends;i < MAX_SENDS;i++)
750 ALSource->Params.WetGains[i] = 0.0f;
751 WetGainHF[i] = 1.0f;
754 //5. Apply filter gains and filters
755 switch(ALSource->DirectFilter.type)
757 case AL_FILTER_LOWPASS:
758 DryMix *= ALSource->DirectFilter.Gain;
759 DryGainHF *= ALSource->DirectFilter.GainHF;
760 break;
762 DryMix *= ListenerGain;
764 // Use energy-preserving panning algorithm for multi-speaker playback
765 length = aluSqrt(Position[0]*Position[0] + Position[1]*Position[1] +
766 Position[2]*Position[2]);
767 length = __max(length, MinDist);
768 if(length > 0.0f)
770 ALfloat invlen = 1.0f/length;
771 Position[0] *= invlen;
772 Position[1] *= invlen;
773 Position[2] *= invlen;
776 pos = aluCart2LUTpos(-Position[2], Position[0]);
777 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
779 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
780 // elevation adjustment for directional gain. this sucks, but
781 // has low complexity
782 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
783 for(s = 0; s < OUTPUTCHANNELS; s++)
785 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
786 ALSource->Params.DryGains[s] = DryMix * gain;
789 /* Update filter coefficients. */
790 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency);
791 /* Spatialized sources use four chained one-pole filters, so we need to
792 * take the fourth root of the squared gain, which is the same as the
793 * square root of the base gain. */
794 g = aluSqrt(__max(DryGainHF, 0.0001f));
795 a = 0.0f;
796 if(g < 0.9999f) /* 1-epsilon */
797 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
798 (1 - g);
799 ALSource->Params.iirFilter.coeff = a;
801 for(i = 0;i < NumSends;i++)
803 /* The wet path uses two chained one-pole filters, so take the
804 * base gain (square root of the squared gain) */
805 g = __max(WetGainHF[i], 0.01f);
806 a = 0.0f;
807 if(g < 0.9999f) /* 1-epsilon */
808 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
809 (1 - g);
810 ALSource->Params.Send[i].iirFilter.coeff = a;
814 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
816 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
819 static void MixSomeSources(ALCcontext *ALContext, float (*DryBuffer)[OUTPUTCHANNELS], ALuint SamplesToDo)
821 static float DummyBuffer[BUFFERSIZE];
822 ALfloat *WetBuffer[MAX_SENDS];
823 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
824 ALfloat DrySend[OUTPUTCHANNELS];
825 ALfloat dryGainStep[OUTPUTCHANNELS];
826 ALfloat wetGainStep[MAX_SENDS];
827 ALuint i, j, k, out;
828 ALsource *ALSource;
829 ALfloat value;
830 ALshort *Data;
831 ALbufferlistitem *BufferListItem;
832 ALint64 DataSize64,DataPos64;
833 FILTER *DryFilter, *WetFilter[MAX_SENDS];
834 ALfloat WetSend[MAX_SENDS];
835 ALuint rampLength;
836 ALuint frequency;
837 ALint Looping,State;
838 ALint increment;
840 if(!(ALSource=ALContext->Source))
841 return;
843 frequency = ALContext->Device->Frequency;
845 rampLength = frequency * MIN_RAMP_LENGTH / 1000;
846 rampLength = max(rampLength, SamplesToDo);
848 another_source:
849 j = 0;
850 State = ALSource->state;
851 while(State == AL_PLAYING && j < SamplesToDo)
853 ALuint DataSize = 0;
854 ALuint DataPosInt = 0;
855 ALuint DataPosFrac = 0;
856 ALbuffer *ALBuffer;
857 ALuint Channels, Bytes;
858 ALuint BufferSize;
859 ALfloat Pitch;
861 /* Get buffer info */
862 if(!(ALBuffer = ALSource->Buffer))
863 goto skipmix;
865 Data = ALBuffer->data;
866 Channels = aluChannelsFromFormat(ALBuffer->format);
867 Bytes = aluBytesFromFormat(ALBuffer->format);
868 DataSize = ALBuffer->size;
869 DataSize /= Channels * Bytes;
871 DataPosInt = ALSource->position;
872 DataPosFrac = ALSource->position_fraction;
874 if(DataPosInt >= DataSize)
875 goto skipmix;
877 /* Get source info */
878 DryFilter = &ALSource->Params.iirFilter;
879 for(i = 0;i < MAX_SENDS;i++)
881 WetFilter[i] = &ALSource->Params.Send[i].iirFilter;
882 WetBuffer[i] = (ALSource->Send[i].Slot ?
883 ALSource->Send[i].Slot->WetBuffer :
884 DummyBuffer);
887 CalcSourceParams(ALContext, ALSource, (Channels==1)?AL_TRUE:AL_FALSE);
888 Pitch = (ALSource->Params.Pitch*ALBuffer->frequency) / frequency;
890 if(DuplicateStereo && Channels == 2)
892 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
893 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
894 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
895 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
897 else if(DuplicateStereo)
899 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
900 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
901 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
902 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
905 /* Compute the gain steps for each output channel */
906 if(ALSource->FirstStart)
908 ALSource->FirstStart = AL_FALSE;
909 for(i = 0;i < OUTPUTCHANNELS;i++)
911 DrySend[i] = ALSource->Params.DryGains[i];
912 dryGainStep[i] = 0.0f;
914 for(i = 0;i < MAX_SENDS;i++)
916 WetSend[i] = ALSource->Params.WetGains[i];
917 wetGainStep[i] = 0.0f;
920 else
922 for(i = 0;i < OUTPUTCHANNELS;i++)
924 dryGainStep[i] = (ALSource->Params.DryGains[i]-
925 ALSource->DryGains[i]) / rampLength;
926 DrySend[i] = ALSource->DryGains[i];
928 for(i = 0;i < MAX_SENDS;i++)
930 wetGainStep[i] = (ALSource->Params.WetGains[i]-
931 ALSource->WetGains[i]) / rampLength;
932 WetSend[i] = ALSource->WetGains[i];
936 /* Compute 18.14 fixed point step */
937 if(Pitch > (float)MAX_PITCH)
938 Pitch = (float)MAX_PITCH;
939 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
940 if(increment <= 0)
941 increment = (1<<FRACTIONBITS);
943 /* Figure out how many samples we can mix. */
944 DataSize64 = DataSize;
945 DataSize64 <<= FRACTIONBITS;
946 DataPos64 = DataPosInt;
947 DataPos64 <<= FRACTIONBITS;
948 DataPos64 += DataPosFrac;
949 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
951 BufferListItem = ALSource->queue;
952 for(i = 0;i < ALSource->BuffersPlayed && BufferListItem;i++)
953 BufferListItem = BufferListItem->next;
954 if(BufferListItem)
956 ALbuffer *NextBuf;
957 ALuint ulExtraSamples;
959 if(BufferListItem->next)
961 NextBuf = BufferListItem->next->buffer;
962 if(NextBuf && NextBuf->data)
964 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*Bytes));
965 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
968 else if(ALSource->bLooping)
970 NextBuf = ALSource->queue->buffer;
971 if(NextBuf && NextBuf->data)
973 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*Bytes));
974 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
977 else
978 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*Bytes));
980 BufferSize = min(BufferSize, (SamplesToDo-j));
982 /* Actual sample mixing loop */
983 k = 0;
984 Data += DataPosInt*Channels;
986 if(Channels == 1) /* Mono */
988 ALfloat outsamp;
990 while(BufferSize--)
992 for(i = 0;i < OUTPUTCHANNELS;i++)
993 DrySend[i] += dryGainStep[i];
994 for(i = 0;i < MAX_SENDS;i++)
995 WetSend[i] += wetGainStep[i];
997 /* First order interpolator */
998 value = lerp(Data[k], Data[k+1], DataPosFrac);
1000 /* Direct path final mix buffer and panning */
1001 outsamp = lpFilter4P(DryFilter, 0, value);
1002 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1003 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1004 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1005 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1006 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1007 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1008 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1009 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1011 /* Room path final mix buffer and panning */
1012 for(i = 0;i < MAX_SENDS;i++)
1014 outsamp = lpFilter2P(WetFilter[i], 0, value);
1015 WetBuffer[i][j] += outsamp*WetSend[i];
1018 DataPosFrac += increment;
1019 k += DataPosFrac>>FRACTIONBITS;
1020 DataPosFrac &= FRACTIONMASK;
1021 j++;
1024 else if(Channels == 2) /* Stereo */
1026 const int chans[] = {
1027 FRONT_LEFT, FRONT_RIGHT
1030 #define DO_MIX() do { \
1031 for(i = 0;i < MAX_SENDS;i++) \
1032 WetSend[i] += wetGainStep[i]*BufferSize; \
1033 while(BufferSize--) \
1035 for(i = 0;i < OUTPUTCHANNELS;i++) \
1036 DrySend[i] += dryGainStep[i]; \
1038 for(i = 0;i < Channels;i++) \
1040 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1041 value = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1042 for(out = 0;out < OUTPUTCHANNELS;out++) \
1043 DryBuffer[j][out] += value*Matrix[chans[i]][out]; \
1046 DataPosFrac += increment; \
1047 k += DataPosFrac>>FRACTIONBITS; \
1048 DataPosFrac &= FRACTIONMASK; \
1049 j++; \
1051 } while(0)
1053 DO_MIX();
1055 else if(Channels == 4) /* Quad */
1057 const int chans[] = {
1058 FRONT_LEFT, FRONT_RIGHT,
1059 BACK_LEFT, BACK_RIGHT
1062 DO_MIX();
1064 else if(Channels == 6) /* 5.1 */
1066 const int chans[] = {
1067 FRONT_LEFT, FRONT_RIGHT,
1068 FRONT_CENTER, LFE,
1069 BACK_LEFT, BACK_RIGHT
1072 DO_MIX();
1074 else if(Channels == 7) /* 6.1 */
1076 const int chans[] = {
1077 FRONT_LEFT, FRONT_RIGHT,
1078 FRONT_CENTER, LFE,
1079 BACK_CENTER,
1080 SIDE_LEFT, SIDE_RIGHT
1083 DO_MIX();
1085 else if(Channels == 8) /* 7.1 */
1087 const int chans[] = {
1088 FRONT_LEFT, FRONT_RIGHT,
1089 FRONT_CENTER, LFE,
1090 BACK_LEFT, BACK_RIGHT,
1091 SIDE_LEFT, SIDE_RIGHT
1094 DO_MIX();
1095 #undef DO_MIX
1097 else /* Unknown? */
1099 for(i = 0;i < OUTPUTCHANNELS;i++)
1100 DrySend[i] += dryGainStep[i]*BufferSize;
1101 for(i = 0;i < MAX_SENDS;i++)
1102 WetSend[i] += wetGainStep[i]*BufferSize;
1103 while(BufferSize--)
1105 DataPosFrac += increment;
1106 k += DataPosFrac>>FRACTIONBITS;
1107 DataPosFrac &= FRACTIONMASK;
1108 j++;
1111 DataPosInt += k;
1113 /* Update source info */
1114 ALSource->position = DataPosInt;
1115 ALSource->position_fraction = DataPosFrac;
1116 for(i = 0;i < OUTPUTCHANNELS;i++)
1117 ALSource->DryGains[i] = DrySend[i];
1118 for(i = 0;i < MAX_SENDS;i++)
1119 ALSource->WetGains[i] = WetSend[i];
1121 skipmix:
1122 /* Handle looping sources */
1123 if(!ALBuffer || DataPosInt >= DataSize)
1125 /* Queueing */
1126 if(ALSource->queue)
1128 Looping = ALSource->bLooping;
1129 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1131 BufferListItem = ALSource->queue;
1132 for(i = 0;i <= ALSource->BuffersPlayed && BufferListItem;i++)
1134 if(!Looping)
1135 BufferListItem->bufferstate = PROCESSED;
1136 BufferListItem = BufferListItem->next;
1138 if(BufferListItem)
1139 ALSource->Buffer = BufferListItem->buffer;
1140 ALSource->position = DataPosInt-DataSize;
1141 ALSource->position_fraction = DataPosFrac;
1142 ALSource->BuffersPlayed++;
1144 else
1146 if(!Looping)
1148 /* alSourceStop */
1149 ALSource->state = AL_STOPPED;
1150 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1151 BufferListItem = ALSource->queue;
1152 while(BufferListItem != NULL)
1154 BufferListItem->bufferstate = PROCESSED;
1155 BufferListItem = BufferListItem->next;
1157 ALSource->position = 0;
1158 ALSource->position_fraction = 0;
1160 else
1162 /* alSourceRewind */
1163 /* alSourcePlay */
1164 ALSource->state = AL_PLAYING;
1165 ALSource->BuffersPlayed = 0;
1166 BufferListItem = ALSource->queue;
1167 while(BufferListItem != NULL)
1169 BufferListItem->bufferstate = PENDING;
1170 BufferListItem = BufferListItem->next;
1172 ALSource->Buffer = ALSource->queue->buffer;
1174 if(ALSource->BuffersInQueue == 1)
1175 ALSource->position = DataPosInt%DataSize;
1176 else
1177 ALSource->position = DataPosInt-DataSize;
1178 ALSource->position_fraction = DataPosFrac;
1184 /* Get source state */
1185 State = ALSource->state;
1188 if((ALSource=ALSource->next) != NULL)
1189 goto another_source;
1192 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1194 float (*DryBuffer)[OUTPUTCHANNELS];
1195 ALuint SamplesToDo;
1196 ALeffectslot *ALEffectSlot;
1197 ALCcontext *ALContext;
1198 int fpuState;
1199 ALuint i, c;
1201 SuspendContext(NULL);
1203 #if defined(HAVE_FESETROUND)
1204 fpuState = fegetround();
1205 fesetround(FE_TOWARDZERO);
1206 #elif defined(HAVE__CONTROLFP)
1207 fpuState = _controlfp(0, 0);
1208 _controlfp(_RC_CHOP, _MCW_RC);
1209 #else
1210 (void)fpuState;
1211 #endif
1213 DryBuffer = device->DryBuffer;
1214 while(size > 0)
1216 /* Setup variables */
1217 SamplesToDo = min(size, BUFFERSIZE);
1219 /* Clear mixing buffer */
1220 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
1222 for(c = 0;c < device->NumContexts;c++)
1224 ALContext = device->Contexts[c];
1225 SuspendContext(ALContext);
1227 MixSomeSources(ALContext, DryBuffer, SamplesToDo);
1229 /* effect slot processing */
1230 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
1231 while(ALEffectSlot)
1233 if(ALEffectSlot->EffectState)
1234 ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1236 for(i = 0;i < SamplesToDo;i++)
1237 ALEffectSlot->WetBuffer[i] = 0.0f;
1238 ALEffectSlot = ALEffectSlot->next;
1240 ProcessContext(ALContext);
1243 //Post processing loop
1244 switch(device->Format)
1246 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1247 case AL_FORMAT_MONO##bits: \
1248 for(i = 0;i < SamplesToDo;i++) \
1250 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1251 DryBuffer[i][FRONT_RIGHT]); \
1252 buffer = ((type*)buffer) + 1; \
1254 break; \
1255 case AL_FORMAT_STEREO##bits: \
1256 if(device->Bs2b) \
1258 for(i = 0;i < SamplesToDo;i++) \
1260 float samples[2]; \
1261 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1262 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1263 bs2b_cross_feed(device->Bs2b, samples); \
1264 ((type*)buffer)[0] = (func)(samples[0]); \
1265 ((type*)buffer)[1] = (func)(samples[1]); \
1266 buffer = ((type*)buffer) + 2; \
1269 else \
1271 for(i = 0;i < SamplesToDo;i++) \
1273 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1274 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1275 buffer = ((type*)buffer) + 2; \
1278 break; \
1279 case AL_FORMAT_QUAD##bits: \
1280 for(i = 0;i < SamplesToDo;i++) \
1282 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1283 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1284 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1285 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1286 buffer = ((type*)buffer) + 4; \
1288 break; \
1289 case AL_FORMAT_51CHN##bits: \
1290 for(i = 0;i < SamplesToDo;i++) \
1292 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1293 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1294 if(isWin) { \
1295 /* Of course, Windows can't use the same ordering... */ \
1296 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1297 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1298 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1299 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1300 } else { \
1301 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1302 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1303 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1304 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1306 buffer = ((type*)buffer) + 6; \
1308 break; \
1309 case AL_FORMAT_61CHN##bits: \
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 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1315 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1316 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1317 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1318 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1319 buffer = ((type*)buffer) + 7; \
1321 break; \
1322 case AL_FORMAT_71CHN##bits: \
1323 for(i = 0;i < SamplesToDo;i++) \
1325 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1326 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1327 if(isWin) { \
1328 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1329 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1330 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1331 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1332 } else { \
1333 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1334 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1335 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1336 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1338 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1339 ((type*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1340 buffer = ((type*)buffer) + 8; \
1342 break;
1344 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1345 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1346 #ifdef _WIN32
1347 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB, 1)
1348 CHECK_WRITE_FORMAT(16, ALshort, aluF2S, 1)
1349 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F, 1)
1350 #else
1351 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB, 0)
1352 CHECK_WRITE_FORMAT(16, ALshort, aluF2S, 0)
1353 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F, 0)
1354 #endif
1355 #undef AL_FORMAT_STEREO32
1356 #undef AL_FORMAT_MONO32
1357 #undef CHECK_WRITE_FORMAT
1359 default:
1360 break;
1363 size -= SamplesToDo;
1366 #if defined(HAVE_FESETROUND)
1367 fesetround(fpuState);
1368 #elif defined(HAVE__CONTROLFP)
1369 _controlfp(fpuState, 0xfffff);
1370 #endif
1372 ProcessContext(NULL);
1375 ALvoid aluHandleDisconnect(ALCdevice *device)
1377 ALuint i;
1379 for(i = 0;i < device->NumContexts;i++)
1381 ALsource *source;
1383 SuspendContext(device->Contexts[i]);
1385 source = device->Contexts[i]->Source;
1386 while(source)
1388 if(source->state == AL_PLAYING)
1390 ALbufferlistitem *BufferListItem;
1392 source->state = AL_STOPPED;
1393 source->BuffersPlayed = source->BuffersInQueue;
1394 BufferListItem = source->queue;
1395 while(BufferListItem != NULL)
1397 BufferListItem->bufferstate = PROCESSED;
1398 BufferListItem = BufferListItem->next;
1400 source->position = 0;
1401 source->position_fraction = 0;
1403 source = source->next;
1405 ProcessContext(device->Contexts[i]);
1408 device->Connected = ALC_FALSE;