Update queued buffer states when source looping is toggled
[openal-soft.git] / Alc / ALu.c
blobd153a2f02ed103fca5200276c6f9726c269d3ba5
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 ALshort aluF2S(ALfloat Value)
64 ALint i;
66 i = (ALint)Value;
67 i = __min( 32767, i);
68 i = __max(-32768, i);
69 return ((ALshort)i);
72 static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
74 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
75 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
76 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
79 static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
81 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
82 inVector1[2]*inVector2[2];
85 static __inline ALvoid aluNormalize(ALfloat *inVector)
87 ALfloat length, inverse_length;
89 length = aluSqrt(aluDotproduct(inVector, inVector));
90 if(length != 0.0f)
92 inverse_length = 1.0f/length;
93 inVector[0] *= inverse_length;
94 inVector[1] *= inverse_length;
95 inVector[2] *= inverse_length;
99 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
101 ALfloat result[3];
103 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
104 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
105 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
106 memcpy(vector, result, sizeof(result));
109 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
110 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
112 const char *confkey;
113 const char *next;
114 const char *sep;
115 const char *end;
116 int i, val;
118 confkey = GetConfigValue(NULL, name, "");
119 next = confkey;
120 while(next && *next)
122 confkey = next;
123 next = strchr(confkey, ',');
124 if(next)
126 do {
127 next++;
128 } while(isspace(*next));
131 sep = strchr(confkey, '=');
132 if(!sep || confkey == sep)
133 continue;
135 end = sep - 1;
136 while(isspace(*end) && end != confkey)
137 end--;
138 end++;
140 if(strncmp(confkey, "fl", end-confkey) == 0)
141 val = FRONT_LEFT;
142 else if(strncmp(confkey, "fr", end-confkey) == 0)
143 val = FRONT_RIGHT;
144 else if(strncmp(confkey, "fc", end-confkey) == 0)
145 val = FRONT_CENTER;
146 else if(strncmp(confkey, "bl", end-confkey) == 0)
147 val = BACK_LEFT;
148 else if(strncmp(confkey, "br", end-confkey) == 0)
149 val = BACK_RIGHT;
150 else if(strncmp(confkey, "bc", end-confkey) == 0)
151 val = BACK_CENTER;
152 else if(strncmp(confkey, "sl", end-confkey) == 0)
153 val = SIDE_LEFT;
154 else if(strncmp(confkey, "sr", end-confkey) == 0)
155 val = SIDE_RIGHT;
156 else
158 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
159 continue;
162 sep++;
163 while(isspace(*sep))
164 sep++;
166 for(i = 0;i < chans;i++)
168 if(Speaker2Chan[i] == val)
170 val = strtol(sep, NULL, 10);
171 if(val >= -180 && val <= 180)
172 SpeakerAngle[i] = val * M_PI/180.0f;
173 else
174 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
175 break;
180 for(i = 1;i < chans;i++)
182 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
184 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
185 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
186 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
191 static __inline ALfloat aluLUTpos2Angle(ALint pos)
193 if(pos < QUADRANT_NUM)
194 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
195 if(pos < 2 * QUADRANT_NUM)
196 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
197 if(pos < 3 * QUADRANT_NUM)
198 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
199 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
202 ALvoid aluInitPanning(ALCcontext *Context)
204 ALint pos, offset, s;
205 ALfloat Alpha, Theta;
206 ALfloat SpeakerAngle[OUTPUTCHANNELS];
207 ALint Speaker2Chan[OUTPUTCHANNELS];
209 for(s = 0;s < OUTPUTCHANNELS;s++)
211 int s2;
212 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
213 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
216 switch(Context->Device->Format)
218 /* Mono is rendered as stereo, then downmixed during post-process */
219 case AL_FORMAT_MONO8:
220 case AL_FORMAT_MONO16:
221 case AL_FORMAT_MONO_FLOAT32:
222 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
223 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
224 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
225 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
226 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
227 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
228 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
229 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
230 Context->NumChan = 2;
231 Speaker2Chan[0] = FRONT_LEFT;
232 Speaker2Chan[1] = FRONT_RIGHT;
233 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
234 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
235 break;
237 case AL_FORMAT_STEREO8:
238 case AL_FORMAT_STEREO16:
239 case AL_FORMAT_STEREO_FLOAT32:
240 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
241 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
242 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
243 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
244 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
245 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
246 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
247 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
248 Context->NumChan = 2;
249 Speaker2Chan[0] = FRONT_LEFT;
250 Speaker2Chan[1] = FRONT_RIGHT;
251 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
252 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
253 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
254 break;
256 case AL_FORMAT_QUAD8:
257 case AL_FORMAT_QUAD16:
258 case AL_FORMAT_QUAD32:
259 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
260 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
261 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
262 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
263 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
264 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
265 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
266 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
267 Context->NumChan = 4;
268 Speaker2Chan[0] = BACK_LEFT;
269 Speaker2Chan[1] = FRONT_LEFT;
270 Speaker2Chan[2] = FRONT_RIGHT;
271 Speaker2Chan[3] = BACK_RIGHT;
272 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
273 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
274 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
275 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
276 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
277 break;
279 case AL_FORMAT_51CHN8:
280 case AL_FORMAT_51CHN16:
281 case AL_FORMAT_51CHN32:
282 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
283 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
284 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
285 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
286 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
287 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
288 Context->NumChan = 5;
289 Speaker2Chan[0] = BACK_LEFT;
290 Speaker2Chan[1] = FRONT_LEFT;
291 Speaker2Chan[2] = FRONT_CENTER;
292 Speaker2Chan[3] = FRONT_RIGHT;
293 Speaker2Chan[4] = BACK_RIGHT;
294 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
295 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
296 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
297 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
298 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
299 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
300 break;
302 case AL_FORMAT_61CHN8:
303 case AL_FORMAT_61CHN16:
304 case AL_FORMAT_61CHN32:
305 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
306 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
307 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
308 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
309 Context->NumChan = 6;
310 Speaker2Chan[0] = SIDE_LEFT;
311 Speaker2Chan[1] = FRONT_LEFT;
312 Speaker2Chan[2] = FRONT_CENTER;
313 Speaker2Chan[3] = FRONT_RIGHT;
314 Speaker2Chan[4] = SIDE_RIGHT;
315 Speaker2Chan[5] = BACK_CENTER;
316 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
317 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
318 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
319 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
320 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
321 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
322 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
323 break;
325 case AL_FORMAT_71CHN8:
326 case AL_FORMAT_71CHN16:
327 case AL_FORMAT_71CHN32:
328 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
329 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
330 Context->NumChan = 7;
331 Speaker2Chan[0] = BACK_LEFT;
332 Speaker2Chan[1] = SIDE_LEFT;
333 Speaker2Chan[2] = FRONT_LEFT;
334 Speaker2Chan[3] = FRONT_CENTER;
335 Speaker2Chan[4] = FRONT_RIGHT;
336 Speaker2Chan[5] = SIDE_RIGHT;
337 Speaker2Chan[6] = BACK_RIGHT;
338 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
339 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
340 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
341 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
342 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
343 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
344 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
345 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
346 break;
348 default:
349 assert(0);
352 for(pos = 0; pos < LUT_NUM; pos++)
354 /* source angle */
355 Theta = aluLUTpos2Angle(pos);
357 /* clear all values */
358 offset = OUTPUTCHANNELS * pos;
359 for(s = 0; s < OUTPUTCHANNELS; s++)
360 Context->PanningLUT[offset+s] = 0.0f;
362 /* set panning values */
363 for(s = 0; s < Context->NumChan - 1; s++)
365 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
367 /* source between speaker s and speaker s+1 */
368 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
369 (SpeakerAngle[s+1]-SpeakerAngle[s]);
370 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
371 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
372 break;
375 if(s == Context->NumChan - 1)
377 /* source between last and first speaker */
378 if(Theta < SpeakerAngle[0])
379 Theta += 2.0f * M_PI;
380 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
381 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
382 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
383 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
388 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
390 ALint pos = 0;
391 ALfloat denom = aluFabs(re) + aluFabs(im);
392 if(denom > 0.0f)
393 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
395 if(re < 0.0)
396 pos = 2 * QUADRANT_NUM - pos;
397 if(im < 0.0)
398 pos = LUT_NUM - pos;
399 return pos%LUT_NUM;
402 static ALvoid CalcSourceParams(const ALCcontext *ALContext,
403 const ALsource *ALSource, ALenum isMono,
404 ALfloat *drysend, ALfloat *wetsend,
405 ALfloat *pitch, ALfloat *drygainhf,
406 ALfloat *wetgainhf)
408 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix;
409 ALfloat Direction[3],Position[3],SourceToListener[3];
410 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
411 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
412 ALfloat U[3],V[3],N[3];
413 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
414 ALfloat Matrix[3][3];
415 ALfloat flAttenuation;
416 ALfloat RoomAttenuation[MAX_SENDS];
417 ALfloat MetersPerUnit;
418 ALfloat RoomRolloff[MAX_SENDS];
419 ALfloat DryGainHF = 1.0f;
420 ALfloat DirGain, AmbientGain;
421 ALfloat length;
422 const ALfloat *SpeakerGain;
423 ALint NumSends;
424 ALint pos, s, i;
426 //Get context properties
427 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
428 DopplerVelocity = ALContext->DopplerVelocity;
429 flSpeedOfSound = ALContext->flSpeedOfSound;
430 NumSends = ALContext->Device->NumAuxSends;
432 //Get listener properties
433 ListenerGain = ALContext->Listener.Gain;
434 MetersPerUnit = ALContext->Listener.MetersPerUnit;
436 //Get source properties
437 SourceVolume = ALSource->flGain;
438 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
439 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
440 MinVolume = ALSource->flMinGain;
441 MaxVolume = ALSource->flMaxGain;
442 MinDist = ALSource->flRefDistance;
443 MaxDist = ALSource->flMaxDistance;
444 Rolloff = ALSource->flRollOffFactor;
445 InnerAngle = ALSource->flInnerAngle;
446 OuterAngle = ALSource->flOuterAngle;
447 OuterGainHF = ALSource->OuterGainHF;
449 //Only apply 3D calculations for mono buffers
450 if(isMono != AL_FALSE)
452 //1. Translate Listener to origin (convert to head relative)
453 // Note that Direction and SourceToListener are *not* transformed.
454 // SourceToListener is used with the source and listener velocities,
455 // which are untransformed, and Direction is used with SourceToListener
456 // for the sound cone
457 if(ALSource->bHeadRelative==AL_FALSE)
459 // Build transform matrix
460 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
461 aluNormalize(U); // Normalized Right-vector
462 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
463 aluNormalize(V); // Normalized Up-vector
464 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
465 aluNormalize(N); // Normalized At-vector
466 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
467 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
468 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
470 // Translate source position into listener space
471 Position[0] -= ALContext->Listener.Position[0];
472 Position[1] -= ALContext->Listener.Position[1];
473 Position[2] -= ALContext->Listener.Position[2];
475 SourceToListener[0] = -Position[0];
476 SourceToListener[1] = -Position[1];
477 SourceToListener[2] = -Position[2];
479 // Transform source position into listener space
480 aluMatrixVector(Position, Matrix);
482 else
484 SourceToListener[0] = -Position[0];
485 SourceToListener[1] = -Position[1];
486 SourceToListener[2] = -Position[2];
488 aluNormalize(SourceToListener);
489 aluNormalize(Direction);
491 //2. Calculate distance attenuation
492 Distance = aluSqrt(aluDotproduct(Position, Position));
494 flAttenuation = 1.0f;
495 for(i = 0;i < MAX_SENDS;i++)
497 RoomAttenuation[i] = 1.0f;
499 RoomRolloff[i] = ALSource->RoomRolloffFactor;
500 if(ALSource->Send[i].Slot &&
501 ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB)
502 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
505 switch (ALSource->DistanceModel)
507 case AL_INVERSE_DISTANCE_CLAMPED:
508 Distance=__max(Distance,MinDist);
509 Distance=__min(Distance,MaxDist);
510 if (MaxDist < MinDist)
511 break;
512 //fall-through
513 case AL_INVERSE_DISTANCE:
514 if (MinDist > 0.0f)
516 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
517 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
518 for(i = 0;i < NumSends;i++)
520 if ((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
521 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
524 break;
526 case AL_LINEAR_DISTANCE_CLAMPED:
527 Distance=__max(Distance,MinDist);
528 Distance=__min(Distance,MaxDist);
529 if (MaxDist < MinDist)
530 break;
531 //fall-through
532 case AL_LINEAR_DISTANCE:
533 Distance=__min(Distance,MaxDist);
534 if (MaxDist != MinDist)
536 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
537 for(i = 0;i < NumSends;i++)
538 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
540 break;
542 case AL_EXPONENT_DISTANCE_CLAMPED:
543 Distance=__max(Distance,MinDist);
544 Distance=__min(Distance,MaxDist);
545 if (MaxDist < MinDist)
546 break;
547 //fall-through
548 case AL_EXPONENT_DISTANCE:
549 if ((Distance > 0.0f) && (MinDist > 0.0f))
551 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
552 for(i = 0;i < NumSends;i++)
553 RoomAttenuation[i] = (ALfloat)pow(Distance/MinDist, -RoomRolloff[i]);
555 break;
557 case AL_NONE:
558 break;
561 // Source Gain + Attenuation and clamp to Min/Max Gain
562 DryMix = SourceVolume * flAttenuation;
563 DryMix = __min(DryMix,MaxVolume);
564 DryMix = __max(DryMix,MinVolume);
566 for(i = 0;i < NumSends;i++)
568 ALfloat WetMix = SourceVolume * RoomAttenuation[i];
569 WetMix = __min(WetMix,MaxVolume);
570 wetsend[i] = __max(WetMix,MinVolume);
571 wetgainhf[i] = 1.0f;
574 // Distance-based air absorption
575 if(ALSource->AirAbsorptionFactor > 0.0f && ALSource->DistanceModel != AL_NONE)
577 ALfloat dist = Distance-MinDist;
578 ALfloat absorb;
580 if(dist < 0.0f) dist = 0.0f;
581 // Absorption calculation is done in dB
582 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
583 (dist*MetersPerUnit);
584 // Convert dB to linear gain before applying
585 absorb = pow(10.0, absorb/20.0);
586 DryGainHF *= absorb;
587 for(i = 0;i < MAX_SENDS;i++)
588 wetgainhf[i] *= absorb;
591 //3. Apply directional soundcones
592 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
593 if(Angle >= InnerAngle && Angle <= OuterAngle)
595 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
596 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
597 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
598 DryMix *= ConeVolume;
599 if(ALSource->DryGainHFAuto)
600 DryGainHF *= ConeHF;
602 else if(Angle > OuterAngle)
604 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
605 ConeHF = (1.0f+(OuterGainHF-1.0f));
606 DryMix *= ConeVolume;
607 if(ALSource->DryGainHFAuto)
608 DryGainHF *= ConeHF;
610 else
612 ConeVolume = 1.0f;
613 ConeHF = 1.0f;
616 //4. Calculate Velocity
617 if(DopplerFactor != 0.0f)
619 ALfloat flVSS, flVLS = 0.0f;
621 if(ALSource->bHeadRelative==AL_FALSE)
622 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
623 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
625 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
627 if (flVSS >= flMaxVelocity)
628 flVSS = (flMaxVelocity - 1.0f);
629 else if (flVSS <= -flMaxVelocity)
630 flVSS = -flMaxVelocity + 1.0f;
632 if (flVLS >= flMaxVelocity)
633 flVLS = (flMaxVelocity - 1.0f);
634 else if (flVLS <= -flMaxVelocity)
635 flVLS = -flMaxVelocity + 1.0f;
637 pitch[0] = ALSource->flPitch *
638 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
639 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
641 else
642 pitch[0] = ALSource->flPitch;
644 for(i = 0;i < NumSends;i++)
646 if(ALSource->Send[i].Slot &&
647 ALSource->Send[i].Slot->effect.type != AL_EFFECT_NULL)
649 if(ALSource->WetGainAuto)
650 wetsend[i] *= ConeVolume;
651 if(ALSource->WetGainHFAuto)
652 wetgainhf[i] *= ConeHF;
654 if(ALSource->Send[i].Slot->AuxSendAuto)
656 // Apply minimal attenuation in place of missing
657 // statistical reverb model.
658 wetsend[i] *= pow(DryMix, 1.0f / 2.0f);
660 else
662 // If the slot's auxilliary send auto is off, the data sent to the
663 // effect slot is the same as the dry path, sans filter effects
664 wetsend[i] = DryMix;
665 wetgainhf[i] = DryGainHF;
668 switch(ALSource->Send[i].WetFilter.type)
670 case AL_FILTER_LOWPASS:
671 wetsend[i] *= ALSource->Send[i].WetFilter.Gain;
672 wetgainhf[i] *= ALSource->Send[i].WetFilter.GainHF;
673 break;
675 wetsend[i] *= ListenerGain;
677 else
679 wetsend[i] = 0.0f;
680 wetgainhf[i] = 1.0f;
683 for(i = NumSends;i < MAX_SENDS;i++)
685 wetsend[i] = 0.0f;
686 wetgainhf[i] = 1.0f;
689 //5. Apply filter gains and filters
690 switch(ALSource->DirectFilter.type)
692 case AL_FILTER_LOWPASS:
693 DryMix *= ALSource->DirectFilter.Gain;
694 DryGainHF *= ALSource->DirectFilter.GainHF;
695 break;
697 DryMix *= ListenerGain;
699 // Use energy-preserving panning algorithm for multi-speaker playback
700 length = aluSqrt(Position[0]*Position[0] + Position[1]*Position[1] +
701 Position[2]*Position[2]);
702 length = __max(length, MinDist);
703 if(length > 0.0f)
705 ALfloat invlen = 1.0f/length;
706 Position[0] *= invlen;
707 Position[1] *= invlen;
708 Position[2] *= invlen;
711 pos = aluCart2LUTpos(-Position[2], Position[0]);
712 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
714 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
715 // elevation adjustment for directional gain. this sucks, but
716 // has low complexity
717 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
718 for(s = 0; s < OUTPUTCHANNELS; s++)
720 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
721 drysend[s] = DryMix * gain;
723 *drygainhf = DryGainHF;
725 else
727 //1. Multi-channel buffers always play "normal"
728 pitch[0] = ALSource->flPitch;
730 DryMix = SourceVolume;
731 DryMix = __min(DryMix,MaxVolume);
732 DryMix = __max(DryMix,MinVolume);
734 switch(ALSource->DirectFilter.type)
736 case AL_FILTER_LOWPASS:
737 DryMix *= ALSource->DirectFilter.Gain;
738 DryGainHF *= ALSource->DirectFilter.GainHF;
739 break;
742 drysend[FRONT_LEFT] = DryMix * ListenerGain;
743 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
744 drysend[SIDE_LEFT] = DryMix * ListenerGain;
745 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
746 drysend[BACK_LEFT] = DryMix * ListenerGain;
747 drysend[BACK_RIGHT] = DryMix * ListenerGain;
748 drysend[FRONT_CENTER] = DryMix * ListenerGain;
749 drysend[BACK_CENTER] = DryMix * ListenerGain;
750 drysend[LFE] = DryMix * ListenerGain;
751 *drygainhf = DryGainHF;
753 for(i = 0;i < MAX_SENDS;i++)
755 wetsend[i] = 0.0f;
756 wetgainhf[i] = 1.0f;
761 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
763 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
766 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
768 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
769 static float DummyBuffer[BUFFERSIZE];
770 ALfloat *WetBuffer[MAX_SENDS];
771 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
772 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
773 ALfloat WetSend[MAX_SENDS];
774 ALfloat DryGainHF = 0.0f;
775 ALfloat WetGainHF[MAX_SENDS];
776 ALuint rampLength;
777 ALfloat dryGainStep[OUTPUTCHANNELS];
778 ALfloat wetGainStep[MAX_SENDS];
779 ALuint BlockAlign,BufferSize;
780 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
781 ALuint Channels,Frequency,ulExtraSamples;
782 ALfloat Pitch;
783 ALint Looping,State;
784 ALint increment;
785 ALuint Buffer;
786 ALuint SamplesToDo;
787 ALsource *ALSource;
788 ALbuffer *ALBuffer;
789 ALeffectslot *ALEffectSlot;
790 ALfloat values[OUTPUTCHANNELS];
791 ALfloat value;
792 ALshort *Data;
793 ALuint i,j,k,out;
794 ALfloat cw, a, g;
795 ALbufferlistitem *BufferListItem;
796 ALuint loop;
797 ALint64 DataSize64,DataPos64;
798 FILTER *DryFilter, *WetFilter[MAX_SENDS];
799 int fpuState;
801 SuspendContext(ALContext);
803 #if defined(HAVE_FESETROUND)
804 fpuState = fegetround();
805 fesetround(FE_TOWARDZERO);
806 #elif defined(HAVE__CONTROLFP)
807 fpuState = _controlfp(0, 0);
808 _controlfp(_RC_CHOP, _MCW_RC);
809 #else
810 (void)fpuState;
811 #endif
813 //Figure output format variables
814 BlockAlign = aluChannelsFromFormat(format);
815 BlockAlign *= aluBytesFromFormat(format);
817 size /= BlockAlign;
818 while(size > 0)
820 //Setup variables
821 SamplesToDo = min(size, BUFFERSIZE);
822 if(ALContext)
824 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
825 ALSource = ALContext->Source;
826 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
828 else
830 ALEffectSlot = NULL;
831 ALSource = NULL;
832 rampLength = 0;
834 rampLength = max(rampLength, SamplesToDo);
836 //Clear mixing buffer
837 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
839 //Actual mixing loop
840 while(ALSource)
842 j = 0;
843 State = ALSource->state;
845 while(State == AL_PLAYING && j < SamplesToDo)
847 DataSize = 0;
848 DataPosInt = 0;
849 DataPosFrac = 0;
851 //Get buffer info
852 if((Buffer = ALSource->ulBufferID))
854 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
856 Data = ALBuffer->data;
857 Channels = aluChannelsFromFormat(ALBuffer->format);
858 DataSize = ALBuffer->size;
859 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
860 Frequency = ALBuffer->frequency;
861 DataPosInt = ALSource->position;
862 DataPosFrac = ALSource->position_fraction;
864 if(DataPosInt >= DataSize)
865 goto skipmix;
867 //Get source info
868 DryFilter = &ALSource->iirFilter;
869 for(i = 0;i < MAX_SENDS;i++)
871 WetFilter[i] = &ALSource->Send[i].iirFilter;
872 WetBuffer[i] = (ALSource->Send[i].Slot ?
873 ALSource->Send[i].Slot->WetBuffer :
874 DummyBuffer);
877 CalcSourceParams(ALContext, ALSource,
878 (Channels==1) ? AL_TRUE : AL_FALSE,
879 DrySend, WetSend, &Pitch,
880 &DryGainHF, WetGainHF);
881 Pitch = (Pitch*Frequency) / ALContext->Frequency;
883 if(Channels == 1)
885 // Update filter coefficients. Calculations based on
886 // the I3DL2 spec.
887 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
888 // We use four chained one-pole filters, so we need to
889 // take the fourth root of the squared gain, which is
890 // the same as the square root of the base gain.
891 // Be careful with gains < 0.0001, as that causes the
892 // coefficient to head towards 1, which will flatten
893 // the signal
894 g = aluSqrt(__max(DryGainHF, 0.0001f));
895 a = 0.0f;
896 if(g < 0.9999f) // 1-epsilon
897 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
898 DryFilter->coeff = a;
900 for(i = 0;i < MAX_SENDS;i++)
902 // The wet path uses two chained one-pole filters,
903 // so take the base gain (square root of the
904 // squared gain)
905 g = __max(WetGainHF[i], 0.01f);
906 a = 0.0f;
907 if(g < 0.9999f) // 1-epsilon
908 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
909 WetFilter[i]->coeff = a;
912 else
914 // Multi-channel sources use two chained one-pole
915 // filters
916 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
917 g = __max(DryGainHF, 0.01f);
918 a = 0.0f;
919 if(g < 0.9999f) // 1-epsilon
920 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
921 DryFilter->coeff = a;
922 for(i = 0;i < MAX_SENDS;i++)
923 WetFilter[i]->coeff = 0.0f;
925 if(DuplicateStereo && Channels == 2)
927 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
928 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
929 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
930 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
932 else if(DuplicateStereo)
934 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
935 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
936 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
937 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
941 //Compute the gain steps for each output channel
942 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
944 for(i = 0;i < OUTPUTCHANNELS;i++)
945 dryGainStep[i] = 0.0f;
946 for(i = 0;i < MAX_SENDS;i++)
947 wetGainStep[i] = 0.0f;
949 else
951 for(i = 0;i < OUTPUTCHANNELS;i++)
953 dryGainStep[i] = (DrySend[i]-ALSource->DryGains[i]) / rampLength;
954 DrySend[i] = ALSource->DryGains[i];
956 for(i = 0;i < MAX_SENDS;i++)
958 wetGainStep[i] = (WetSend[i]-ALSource->WetGains[i]) / rampLength;
959 WetSend[i] = ALSource->WetGains[i];
962 ALSource->FirstStart = AL_FALSE;
964 //Compute 18.14 fixed point step
965 if(Pitch > (float)MAX_PITCH)
966 Pitch = (float)MAX_PITCH;
967 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
968 if(increment <= 0)
969 increment = (1<<FRACTIONBITS);
971 //Figure out how many samples we can mix.
972 DataSize64 = DataSize;
973 DataSize64 <<= FRACTIONBITS;
974 DataPos64 = DataPosInt;
975 DataPos64 <<= FRACTIONBITS;
976 DataPos64 += DataPosFrac;
977 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
979 BufferListItem = ALSource->queue;
980 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
982 if(BufferListItem)
983 BufferListItem = BufferListItem->next;
985 if (BufferListItem)
987 if (BufferListItem->next)
989 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
990 if(NextBuf && NextBuf->data)
992 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
993 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
996 else if (ALSource->bLooping)
998 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
999 if (NextBuf && NextBuf->data)
1001 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1002 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1005 else
1006 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1008 BufferSize = min(BufferSize, (SamplesToDo-j));
1010 //Actual sample mixing loop
1011 k = 0;
1012 Data += DataPosInt*Channels;
1014 if(Channels == 1) /* Mono */
1016 ALfloat outsamp;
1018 while(BufferSize--)
1020 for(i = 0;i < OUTPUTCHANNELS;i++)
1021 DrySend[i] += dryGainStep[i];
1022 for(i = 0;i < MAX_SENDS;i++)
1023 WetSend[i] += wetGainStep[i];
1025 //First order interpolator
1026 value = lerp(Data[k], Data[k+1], DataPosFrac);
1028 //Direct path final mix buffer and panning
1029 outsamp = lpFilter4P(DryFilter, 0, value);
1030 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1031 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1032 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1033 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1034 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1035 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1036 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1037 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1039 //Room path final mix buffer and panning
1040 for(i = 0;i < MAX_SENDS;i++)
1042 outsamp = lpFilter2P(WetFilter[i], 0, value);
1043 WetBuffer[i][j] += outsamp*WetSend[i];
1046 DataPosFrac += increment;
1047 k += DataPosFrac>>FRACTIONBITS;
1048 DataPosFrac &= FRACTIONMASK;
1049 j++;
1052 else if(Channels == 2) /* Stereo */
1054 const int chans[] = {
1055 FRONT_LEFT, FRONT_RIGHT
1058 #define DO_MIX() do { \
1059 for(i = 0;i < MAX_SENDS;i++) \
1060 WetSend[i] += wetGainStep[i]*BufferSize; \
1061 while(BufferSize--) \
1063 for(i = 0;i < OUTPUTCHANNELS;i++) \
1064 DrySend[i] += dryGainStep[i]; \
1066 for(i = 0;i < Channels;i++) \
1068 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1069 values[i] = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1071 for(out = 0;out < OUTPUTCHANNELS;out++) \
1073 ALfloat sum = 0.0f; \
1074 for(i = 0;i < Channels;i++) \
1075 sum += values[i]*Matrix[chans[i]][out]; \
1076 DryBuffer[j][out] += sum; \
1079 DataPosFrac += increment; \
1080 k += DataPosFrac>>FRACTIONBITS; \
1081 DataPosFrac &= FRACTIONMASK; \
1082 j++; \
1084 } while(0)
1086 DO_MIX();
1088 else if(Channels == 4) /* Quad */
1090 const int chans[] = {
1091 FRONT_LEFT, FRONT_RIGHT,
1092 BACK_LEFT, BACK_RIGHT
1095 DO_MIX();
1097 else if(Channels == 6) /* 5.1 */
1099 const int chans[] = {
1100 FRONT_LEFT, FRONT_RIGHT,
1101 FRONT_CENTER, LFE,
1102 BACK_LEFT, BACK_RIGHT
1105 DO_MIX();
1107 else if(Channels == 7) /* 6.1 */
1109 const int chans[] = {
1110 FRONT_LEFT, FRONT_RIGHT,
1111 FRONT_CENTER, LFE,
1112 BACK_CENTER,
1113 SIDE_LEFT, SIDE_RIGHT
1116 DO_MIX();
1118 else if(Channels == 8) /* 7.1 */
1120 const int chans[] = {
1121 FRONT_LEFT, FRONT_RIGHT,
1122 FRONT_CENTER, LFE,
1123 BACK_LEFT, BACK_RIGHT,
1124 SIDE_LEFT, SIDE_RIGHT
1127 DO_MIX();
1128 #undef DO_MIX
1130 else /* Unknown? */
1132 for(i = 0;i < OUTPUTCHANNELS;i++)
1133 DrySend[i] += dryGainStep[i]*BufferSize;
1134 for(i = 0;i < MAX_SENDS;i++)
1135 WetSend[i] += wetGainStep[i]*BufferSize;
1136 while(BufferSize--)
1138 DataPosFrac += increment;
1139 k += DataPosFrac>>FRACTIONBITS;
1140 DataPosFrac &= FRACTIONMASK;
1141 j++;
1144 DataPosInt += k;
1146 //Update source info
1147 ALSource->position = DataPosInt;
1148 ALSource->position_fraction = DataPosFrac;
1149 for(i = 0;i < OUTPUTCHANNELS;i++)
1150 ALSource->DryGains[i] = DrySend[i];
1151 for(i = 0;i < MAX_SENDS;i++)
1152 ALSource->WetGains[i] = WetSend[i];
1154 skipmix: ;
1157 //Handle looping sources
1158 if(!Buffer || DataPosInt >= DataSize)
1160 //queueing
1161 if(ALSource->queue)
1163 Looping = ALSource->bLooping;
1164 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1166 BufferListItem = ALSource->queue;
1167 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1169 if(BufferListItem)
1171 if(!Looping)
1172 BufferListItem->bufferstate = PROCESSED;
1173 BufferListItem = BufferListItem->next;
1176 if(BufferListItem)
1177 ALSource->ulBufferID = BufferListItem->buffer;
1178 ALSource->position = DataPosInt-DataSize;
1179 ALSource->position_fraction = DataPosFrac;
1180 ALSource->BuffersPlayed++;
1182 else
1184 if(!Looping)
1186 /* alSourceStop */
1187 ALSource->state = AL_STOPPED;
1188 ALSource->inuse = AL_FALSE;
1189 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1190 BufferListItem = ALSource->queue;
1191 while(BufferListItem != NULL)
1193 BufferListItem->bufferstate = PROCESSED;
1194 BufferListItem = BufferListItem->next;
1196 ALSource->position = DataSize;
1197 ALSource->position_fraction = 0;
1199 else
1201 /* alSourceRewind */
1202 /* alSourcePlay */
1203 ALSource->state = AL_PLAYING;
1204 ALSource->inuse = AL_TRUE;
1205 ALSource->play = AL_TRUE;
1206 ALSource->BuffersPlayed = 0;
1207 BufferListItem = ALSource->queue;
1208 while(BufferListItem != NULL)
1210 BufferListItem->bufferstate = PENDING;
1211 BufferListItem = BufferListItem->next;
1213 ALSource->ulBufferID = ALSource->queue->buffer;
1215 if(ALSource->BuffersInQueue == 1)
1216 ALSource->position = DataPosInt%DataSize;
1217 else
1218 ALSource->position = DataPosInt-DataSize;
1219 ALSource->position_fraction = DataPosFrac;
1225 //Get source state
1226 State = ALSource->state;
1229 ALSource = ALSource->next;
1232 // effect slot processing
1233 while(ALEffectSlot)
1235 if(ALEffectSlot->EffectState)
1236 ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1238 for(i = 0;i < SamplesToDo;i++)
1239 ALEffectSlot->WetBuffer[i] = 0.0f;
1240 ALEffectSlot = ALEffectSlot->next;
1243 //Post processing loop
1244 switch(format)
1246 case AL_FORMAT_MONO8:
1247 for(i = 0;i < SamplesToDo;i++)
1249 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1250 buffer = ((ALubyte*)buffer) + 1;
1252 break;
1253 case AL_FORMAT_STEREO8:
1254 if(ALContext && ALContext->bs2b)
1256 for(i = 0;i < SamplesToDo;i++)
1258 float samples[2];
1259 samples[0] = DryBuffer[i][FRONT_LEFT];
1260 samples[1] = DryBuffer[i][FRONT_RIGHT];
1261 bs2b_cross_feed(ALContext->bs2b, samples);
1262 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1263 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1264 buffer = ((ALubyte*)buffer) + 2;
1267 else
1269 for(i = 0;i < SamplesToDo;i++)
1271 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1272 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1273 buffer = ((ALubyte*)buffer) + 2;
1276 break;
1277 case AL_FORMAT_QUAD8:
1278 for(i = 0;i < SamplesToDo;i++)
1280 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1281 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1282 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1283 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1284 buffer = ((ALubyte*)buffer) + 4;
1286 break;
1287 case AL_FORMAT_51CHN8:
1288 for(i = 0;i < SamplesToDo;i++)
1290 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1291 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1292 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1293 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1294 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1295 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1296 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1297 #else
1298 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1299 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1300 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1301 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1302 #endif
1303 buffer = ((ALubyte*)buffer) + 6;
1305 break;
1306 case AL_FORMAT_61CHN8:
1307 for(i = 0;i < SamplesToDo;i++)
1309 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1310 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1311 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1312 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1313 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1314 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1315 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1316 buffer = ((ALubyte*)buffer) + 7;
1318 break;
1319 case AL_FORMAT_71CHN8:
1320 for(i = 0;i < SamplesToDo;i++)
1322 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1323 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1324 #ifdef _WIN32
1325 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1326 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1327 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1328 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1329 #else
1330 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1331 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1332 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1333 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1334 #endif
1335 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1336 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1337 buffer = ((ALubyte*)buffer) + 8;
1339 break;
1341 case AL_FORMAT_MONO16:
1342 for(i = 0;i < SamplesToDo;i++)
1344 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1345 buffer = ((ALshort*)buffer) + 1;
1347 break;
1348 case AL_FORMAT_STEREO16:
1349 if(ALContext && ALContext->bs2b)
1351 for(i = 0;i < SamplesToDo;i++)
1353 float samples[2];
1354 samples[0] = DryBuffer[i][FRONT_LEFT];
1355 samples[1] = DryBuffer[i][FRONT_RIGHT];
1356 bs2b_cross_feed(ALContext->bs2b, samples);
1357 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1358 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1359 buffer = ((ALshort*)buffer) + 2;
1362 else
1364 for(i = 0;i < SamplesToDo;i++)
1366 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1367 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1368 buffer = ((ALshort*)buffer) + 2;
1371 break;
1372 case AL_FORMAT_QUAD16:
1373 for(i = 0;i < SamplesToDo;i++)
1375 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1376 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1377 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1378 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1379 buffer = ((ALshort*)buffer) + 4;
1381 break;
1382 case AL_FORMAT_51CHN16:
1383 for(i = 0;i < SamplesToDo;i++)
1385 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1386 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1387 #ifdef _WIN32
1388 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1389 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1390 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1391 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1392 #else
1393 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1394 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1395 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1396 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1397 #endif
1398 buffer = ((ALshort*)buffer) + 6;
1400 break;
1401 case AL_FORMAT_61CHN16:
1402 for(i = 0;i < SamplesToDo;i++)
1404 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1405 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1406 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1407 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1408 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1409 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1410 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1411 buffer = ((ALshort*)buffer) + 7;
1413 break;
1414 case AL_FORMAT_71CHN16:
1415 for(i = 0;i < SamplesToDo;i++)
1417 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1418 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1419 #ifdef _WIN32
1420 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1421 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1422 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1423 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1424 #else
1425 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1426 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1427 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1428 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1429 #endif
1430 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1431 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1432 buffer = ((ALshort*)buffer) + 8;
1434 break;
1436 default:
1437 break;
1440 size -= SamplesToDo;
1443 #if defined(HAVE_FESETROUND)
1444 fesetround(fpuState);
1445 #elif defined(HAVE__CONTROLFP)
1446 _controlfp(fpuState, 0xfffff);
1447 #endif
1449 ProcessContext(ALContext);