comparison ext/openal-soft/Alc/ALu.c @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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.
8 *
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.
13 *
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
19 */
20
21 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
22
23 #include "config.h"
24
25 #include <math.h>
26 #include "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "alSource.h"
30 #include "alBuffer.h"
31 #include "alThunk.h"
32 #include "alListener.h"
33 #include "alAuxEffectSlot.h"
34 #include "bs2b.h"
35
36 #if defined(HAVE_STDINT_H)
37 #include <stdint.h>
38 typedef int64_t ALint64;
39 #elif defined(HAVE___INT64)
40 typedef __int64 ALint64;
41 #elif (SIZEOF_LONG == 8)
42 typedef long ALint64;
43 #elif (SIZEOF_LONG_LONG == 8)
44 typedef long long ALint64;
45 #endif
46
47 #ifdef HAVE_SQRTF
48 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
49 #else
50 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
51 #endif
52
53 #ifdef HAVE_ACOSF
54 #define aluAcos(x) ((ALfloat)acosf((float)(x)))
55 #else
56 #define aluAcos(x) ((ALfloat)acos((double)(x)))
57 #endif
58
59 // fixes for mingw32.
60 #if defined(max) && !defined(__max)
61 #define __max max
62 #endif
63 #if defined(min) && !defined(__min)
64 #define __min min
65 #endif
66
67 #define BUFFERSIZE 48000
68 #define FRACTIONBITS 14
69 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
70 #define MAX_PITCH 4
71
72 enum {
73 FRONT_LEFT = 0,
74 FRONT_RIGHT,
75 SIDE_LEFT,
76 SIDE_RIGHT,
77 BACK_LEFT,
78 BACK_RIGHT,
79 CENTER,
80 LFE,
81
82 OUTPUTCHANNELS
83 };
84
85 ALboolean DuplicateStereo = AL_FALSE;
86
87 /* NOTE: The AL_FORMAT_REAR* enums aren't handled here be cause they're
88 * converted to AL_FORMAT_QUAD* when loaded */
89 __inline ALuint aluBytesFromFormat(ALenum format)
90 {
91 switch(format)
92 {
93 case AL_FORMAT_MONO8:
94 case AL_FORMAT_STEREO8:
95 case AL_FORMAT_QUAD8_LOKI:
96 case AL_FORMAT_QUAD8:
97 case AL_FORMAT_51CHN8:
98 case AL_FORMAT_61CHN8:
99 case AL_FORMAT_71CHN8:
100 return 1;
101
102 case AL_FORMAT_MONO16:
103 case AL_FORMAT_STEREO16:
104 case AL_FORMAT_QUAD16_LOKI:
105 case AL_FORMAT_QUAD16:
106 case AL_FORMAT_51CHN16:
107 case AL_FORMAT_61CHN16:
108 case AL_FORMAT_71CHN16:
109 return 2;
110
111 case AL_FORMAT_MONO_FLOAT32:
112 case AL_FORMAT_STEREO_FLOAT32:
113 case AL_FORMAT_QUAD32:
114 case AL_FORMAT_51CHN32:
115 case AL_FORMAT_61CHN32:
116 case AL_FORMAT_71CHN32:
117 return 4;
118
119 default:
120 return 0;
121 }
122 }
123
124 __inline ALuint aluChannelsFromFormat(ALenum format)
125 {
126 switch(format)
127 {
128 case AL_FORMAT_MONO8:
129 case AL_FORMAT_MONO16:
130 case AL_FORMAT_MONO_FLOAT32:
131 return 1;
132
133 case AL_FORMAT_STEREO8:
134 case AL_FORMAT_STEREO16:
135 case AL_FORMAT_STEREO_FLOAT32:
136 return 2;
137
138 case AL_FORMAT_QUAD8_LOKI:
139 case AL_FORMAT_QUAD16_LOKI:
140 case AL_FORMAT_QUAD8:
141 case AL_FORMAT_QUAD16:
142 case AL_FORMAT_QUAD32:
143 return 4;
144
145 case AL_FORMAT_51CHN8:
146 case AL_FORMAT_51CHN16:
147 case AL_FORMAT_51CHN32:
148 return 6;
149
150 case AL_FORMAT_61CHN8:
151 case AL_FORMAT_61CHN16:
152 case AL_FORMAT_61CHN32:
153 return 7;
154
155 case AL_FORMAT_71CHN8:
156 case AL_FORMAT_71CHN16:
157 case AL_FORMAT_71CHN32:
158 return 8;
159
160 default:
161 return 0;
162 }
163 }
164
165
166 static __inline ALshort aluF2S(ALfloat Value)
167 {
168 ALint i;
169
170 i = (ALint)Value;
171 i = __min( 32767, i);
172 i = __max(-32768, i);
173 return ((ALshort)i);
174 }
175
176 static __inline ALvoid aluCrossproduct(ALfloat *inVector1,ALfloat *inVector2,ALfloat *outVector)
177 {
178 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
179 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
180 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
181 }
182
183 static __inline ALfloat aluDotproduct(ALfloat *inVector1,ALfloat *inVector2)
184 {
185 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
186 inVector1[2]*inVector2[2];
187 }
188
189 static __inline ALvoid aluNormalize(ALfloat *inVector)
190 {
191 ALfloat length, inverse_length;
192
193 length = aluSqrt(aluDotproduct(inVector, inVector));
194 if(length != 0.0f)
195 {
196 inverse_length = 1.0f/length;
197 inVector[0] *= inverse_length;
198 inVector[1] *= inverse_length;
199 inVector[2] *= inverse_length;
200 }
201 }
202
203 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
204 {
205 ALfloat result[3];
206
207 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
208 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
209 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
210 memcpy(vector, result, sizeof(result));
211 }
212
213 static __inline ALfloat aluComputeSample(ALfloat GainHF, ALfloat sample, ALfloat LastSample)
214 {
215 if(GainHF < 1.0f)
216 {
217 if(GainHF > 0.0f)
218 {
219 sample *= GainHF;
220 sample += LastSample * (1.0f-GainHF);
221 }
222 else
223 sample = 0.0f;
224 }
225
226 return sample;
227 }
228
229 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
230 ALenum isMono, ALenum OutputFormat,
231 ALfloat *drysend, ALfloat *wetsend,
232 ALfloat *pitch, ALfloat *drygainhf,
233 ALfloat *wetgainhf)
234 {
235 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix,WetMix=0.0f;
236 ALfloat Direction[3],Position[3],SourceToListener[3];
237 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
238 ALfloat ConeVolume,SourceVolume,PanningFB,PanningLR,ListenerGain;
239 ALfloat U[3],V[3],N[3];
240 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
241 ALfloat Matrix[3][3];
242 ALfloat flAttenuation;
243 ALfloat RoomAttenuation;
244 ALfloat MetersPerUnit;
245 ALfloat RoomRolloff;
246 ALfloat DryGainHF = 1.0f;
247 ALfloat WetGainHF = 1.0f;
248
249 //Get context properties
250 DopplerFactor = ALContext->DopplerFactor;
251 DopplerVelocity = ALContext->DopplerVelocity;
252 flSpeedOfSound = ALContext->flSpeedOfSound;
253
254 //Get listener properties
255 ListenerGain = ALContext->Listener.Gain;
256 MetersPerUnit = ALContext->Listener.MetersPerUnit;
257
258 //Get source properties
259 SourceVolume = ALSource->flGain;
260 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
261 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
262 MinVolume = ALSource->flMinGain;
263 MaxVolume = ALSource->flMaxGain;
264 MinDist = ALSource->flRefDistance;
265 MaxDist = ALSource->flMaxDistance;
266 Rolloff = ALSource->flRollOffFactor;
267 InnerAngle = ALSource->flInnerAngle;
268 OuterAngle = ALSource->flOuterAngle;
269 OuterGainHF = ALSource->OuterGainHF;
270 RoomRolloff = ALSource->RoomRolloffFactor;
271
272 //Only apply 3D calculations for mono buffers
273 if(isMono != AL_FALSE)
274 {
275 //1. Translate Listener to origin (convert to head relative)
276 if(ALSource->bHeadRelative==AL_FALSE)
277 {
278 Position[0] -= ALContext->Listener.Position[0];
279 Position[1] -= ALContext->Listener.Position[1];
280 Position[2] -= ALContext->Listener.Position[2];
281 }
282
283 //2. Calculate distance attenuation
284 Distance = aluSqrt(aluDotproduct(Position, Position));
285
286 if(ALSource->Send[0].Slot && !ALSource->Send[0].Slot->AuxSendAuto)
287 {
288 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
289 RoomRolloff = ALSource->Send[0].Slot->effect.Reverb.RoomRolloffFactor;
290 }
291
292 flAttenuation = 1.0f;
293 RoomAttenuation = 1.0f;
294 switch (ALContext->DistanceModel)
295 {
296 case AL_INVERSE_DISTANCE_CLAMPED:
297 Distance=__max(Distance,MinDist);
298 Distance=__min(Distance,MaxDist);
299 if (MaxDist < MinDist)
300 break;
301 //fall-through
302 case AL_INVERSE_DISTANCE:
303 if (MinDist > 0.0f)
304 {
305 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
306 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
307 if ((MinDist + (RoomRolloff * (Distance - MinDist))) > 0.0f)
308 RoomAttenuation = MinDist / (MinDist + (RoomRolloff * (Distance - MinDist)));
309 }
310 break;
311
312 case AL_LINEAR_DISTANCE_CLAMPED:
313 Distance=__max(Distance,MinDist);
314 Distance=__min(Distance,MaxDist);
315 if (MaxDist < MinDist)
316 break;
317 //fall-through
318 case AL_LINEAR_DISTANCE:
319 Distance=__min(Distance,MaxDist);
320 if (MaxDist != MinDist)
321 {
322 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
323 RoomAttenuation = 1.0f - (RoomRolloff*(Distance-MinDist)/(MaxDist - MinDist));
324 }
325 break;
326
327 case AL_EXPONENT_DISTANCE_CLAMPED:
328 Distance=__max(Distance,MinDist);
329 Distance=__min(Distance,MaxDist);
330 if (MaxDist < MinDist)
331 break;
332 //fall-through
333 case AL_EXPONENT_DISTANCE:
334 if ((Distance > 0.0f) && (MinDist > 0.0f))
335 {
336 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
337 RoomAttenuation = (ALfloat)pow(Distance/MinDist, -RoomRolloff);
338 }
339 break;
340
341 case AL_NONE:
342 default:
343 flAttenuation = 1.0f;
344 RoomAttenuation = 1.0f;
345 break;
346 }
347
348 // Source Gain + Attenuation and clamp to Min/Max Gain
349 DryMix = SourceVolume * flAttenuation;
350 DryMix = __min(DryMix,MaxVolume);
351 DryMix = __max(DryMix,MinVolume);
352
353 WetMix = SourceVolume * (ALSource->WetGainAuto ?
354 RoomAttenuation : 1.0f);
355 WetMix = __min(WetMix,MaxVolume);
356 WetMix = __max(WetMix,MinVolume);
357
358 //3. Apply directional soundcones
359 SourceToListener[0] = -Position[0];
360 SourceToListener[1] = -Position[1];
361 SourceToListener[2] = -Position[2];
362 aluNormalize(Direction);
363 aluNormalize(SourceToListener);
364 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f /
365 3.141592654f;
366 if(Angle >= InnerAngle && Angle <= OuterAngle)
367 {
368 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
369 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
370 if(ALSource->WetGainAuto)
371 WetMix *= ConeVolume;
372 if(ALSource->DryGainHFAuto)
373 DryGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
374 if(ALSource->WetGainHFAuto)
375 WetGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
376 }
377 else if(Angle > OuterAngle)
378 {
379 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
380 if(ALSource->WetGainAuto)
381 WetMix *= ConeVolume;
382 if(ALSource->DryGainHFAuto)
383 DryGainHF *= (1.0f+(OuterGainHF-1.0f));
384 if(ALSource->WetGainHFAuto)
385 WetGainHF *= (1.0f+(OuterGainHF-1.0f));
386 }
387 else
388 ConeVolume = 1.0f;
389
390 //4. Calculate Velocity
391 if(DopplerFactor != 0.0f)
392 {
393 ALfloat flVSS, flVLS;
394
395 flVLS = aluDotproduct(ALContext->Listener.Velocity,
396 SourceToListener);
397 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
398
399 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
400
401 if (flVSS >= flMaxVelocity)
402 flVSS = (flMaxVelocity - 1.0f);
403 else if (flVSS <= -flMaxVelocity)
404 flVSS = -flMaxVelocity + 1.0f;
405
406 if (flVLS >= flMaxVelocity)
407 flVLS = (flMaxVelocity - 1.0f);
408 else if (flVLS <= -flMaxVelocity)
409 flVLS = -flMaxVelocity + 1.0f;
410
411 pitch[0] = ALSource->flPitch *
412 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
413 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
414 }
415 else
416 pitch[0] = ALSource->flPitch;
417
418 //5. Align coordinate system axes
419 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
420 aluNormalize(U); // Normalized Right-vector
421 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
422 aluNormalize(V); // Normalized Up-vector
423 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
424 aluNormalize(N); // Normalized At-vector
425 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
426 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
427 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
428 aluMatrixVector(Position, Matrix);
429
430 //6. Apply filter gains and filters
431 switch(ALSource->DirectFilter.filter)
432 {
433 case AL_FILTER_LOWPASS:
434 DryMix *= ALSource->DirectFilter.Gain;
435 DryGainHF *= ALSource->DirectFilter.GainHF;
436 break;
437 }
438
439 switch(ALSource->Send[0].WetFilter.filter)
440 {
441 case AL_FILTER_LOWPASS:
442 WetMix *= ALSource->Send[0].WetFilter.Gain;
443 WetGainHF *= ALSource->Send[0].WetFilter.GainHF;
444 break;
445 }
446
447 if(ALSource->AirAbsorptionFactor > 0.0f)
448 DryGainHF *= pow(ALSource->AirAbsorptionFactor * AIRABSORBGAINHF,
449 Distance * MetersPerUnit);
450
451 if(ALSource->Send[0].Slot)
452 {
453 WetMix *= ALSource->Send[0].Slot->Gain;
454
455 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
456 {
457 WetGainHF *= ALSource->Send[0].Slot->effect.Reverb.GainHF;
458 WetGainHF *= pow(ALSource->Send[0].Slot->effect.Reverb.AirAbsorptionGainHF,
459 Distance * MetersPerUnit);
460 }
461 }
462 else
463 {
464 WetMix = 0.0f;
465 WetGainHF = 1.0f;
466 }
467
468 DryMix *= ListenerGain * ConeVolume;
469 WetMix *= ListenerGain;
470
471 //7. Convert normalized position into pannings, then into channel volumes
472 aluNormalize(Position);
473 switch(aluChannelsFromFormat(OutputFormat))
474 {
475 case 1:
476 drysend[FRONT_LEFT] = DryMix * aluSqrt(1.0f); //Direct
477 drysend[FRONT_RIGHT] = DryMix * aluSqrt(1.0f); //Direct
478 wetsend[FRONT_LEFT] = WetMix * aluSqrt(1.0f); //Room
479 wetsend[FRONT_RIGHT] = WetMix * aluSqrt(1.0f); //Room
480 break;
481 case 2:
482 PanningLR = 0.5f + 0.5f*Position[0];
483 drysend[FRONT_LEFT] = DryMix * aluSqrt(1.0f-PanningLR); //L Direct
484 drysend[FRONT_RIGHT] = DryMix * aluSqrt( PanningLR); //R Direct
485 wetsend[FRONT_LEFT] = WetMix * aluSqrt(1.0f-PanningLR); //L Room
486 wetsend[FRONT_RIGHT] = WetMix * aluSqrt( PanningLR); //R Room
487 break;
488 case 4:
489 /* TODO: Add center/lfe channel in spatial calculations? */
490 case 6:
491 // Apply a scalar so each individual speaker has more weight
492 PanningLR = 0.5f + (0.5f*Position[0]*1.41421356f);
493 PanningLR = __min(1.0f, PanningLR);
494 PanningLR = __max(0.0f, PanningLR);
495 PanningFB = 0.5f + (0.5f*Position[2]*1.41421356f);
496 PanningFB = __min(1.0f, PanningFB);
497 PanningFB = __max(0.0f, PanningFB);
498 drysend[FRONT_LEFT] = DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
499 drysend[FRONT_RIGHT] = DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
500 drysend[BACK_LEFT] = DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
501 drysend[BACK_RIGHT] = DryMix * aluSqrt(( PanningLR)*( PanningFB));
502 wetsend[FRONT_LEFT] = WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
503 wetsend[FRONT_RIGHT] = WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
504 wetsend[BACK_LEFT] = WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
505 wetsend[BACK_RIGHT] = WetMix * aluSqrt(( PanningLR)*( PanningFB));
506 break;
507 case 7:
508 case 8:
509 PanningFB = 1.0f - fabs(Position[2]*1.15470054f);
510 PanningFB = __min(1.0f, PanningFB);
511 PanningFB = __max(0.0f, PanningFB);
512 PanningLR = 0.5f + (0.5*Position[0]*((1.0f-PanningFB)*2.0f));
513 PanningLR = __min(1.0f, PanningLR);
514 PanningLR = __max(0.0f, PanningLR);
515 if(Position[2] > 0.0f)
516 {
517 drysend[BACK_LEFT] = DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
518 drysend[BACK_RIGHT] = DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
519 drysend[SIDE_LEFT] = DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
520 drysend[SIDE_RIGHT] = DryMix * aluSqrt(( PanningLR)*( PanningFB));
521 drysend[FRONT_LEFT] = 0.0f;
522 drysend[FRONT_RIGHT] = 0.0f;
523 wetsend[BACK_LEFT] = WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
524 wetsend[BACK_RIGHT] = WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
525 wetsend[SIDE_LEFT] = WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
526 wetsend[SIDE_RIGHT] = WetMix * aluSqrt(( PanningLR)*( PanningFB));
527 wetsend[FRONT_LEFT] = 0.0f;
528 wetsend[FRONT_RIGHT] = 0.0f;
529 }
530 else
531 {
532 drysend[FRONT_LEFT] = DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
533 drysend[FRONT_RIGHT] = DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
534 drysend[SIDE_LEFT] = DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
535 drysend[SIDE_RIGHT] = DryMix * aluSqrt(( PanningLR)*( PanningFB));
536 drysend[BACK_LEFT] = 0.0f;
537 drysend[BACK_RIGHT] = 0.0f;
538 wetsend[FRONT_LEFT] = WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
539 wetsend[FRONT_RIGHT] = WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
540 wetsend[SIDE_LEFT] = WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
541 wetsend[SIDE_RIGHT] = WetMix * aluSqrt(( PanningLR)*( PanningFB));
542 wetsend[BACK_LEFT] = 0.0f;
543 wetsend[BACK_RIGHT] = 0.0f;
544 }
545 default:
546 break;
547 }
548
549 *drygainhf = DryGainHF;
550 *wetgainhf = WetGainHF;
551 }
552 else
553 {
554 //1. Multi-channel buffers always play "normal"
555 pitch[0] = ALSource->flPitch;
556
557 drysend[FRONT_LEFT] = SourceVolume * ListenerGain;
558 drysend[FRONT_RIGHT] = SourceVolume * ListenerGain;
559 drysend[SIDE_LEFT] = SourceVolume * ListenerGain;
560 drysend[SIDE_RIGHT] = SourceVolume * ListenerGain;
561 drysend[BACK_LEFT] = SourceVolume * ListenerGain;
562 drysend[BACK_RIGHT] = SourceVolume * ListenerGain;
563 drysend[CENTER] = SourceVolume * ListenerGain;
564 drysend[LFE] = SourceVolume * ListenerGain;
565 wetsend[FRONT_LEFT] = 0.0f;
566 wetsend[FRONT_RIGHT] = 0.0f;
567 wetsend[SIDE_LEFT] = 0.0f;
568 wetsend[SIDE_RIGHT] = 0.0f;
569 wetsend[BACK_LEFT] = 0.0f;
570 wetsend[BACK_RIGHT] = 0.0f;
571 wetsend[CENTER] = 0.0f;
572 wetsend[LFE] = 0.0f;
573 WetGainHF = 1.0f;
574
575 *drygainhf = DryGainHF;
576 *wetgainhf = WetGainHF;
577 }
578 }
579
580 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
581 {
582 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
583 static float WetBuffer[BUFFERSIZE][OUTPUTCHANNELS];
584 static float ReverbBuffer[BUFFERSIZE];
585 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
586 ALfloat WetSend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
587 ALfloat DryGainHF = 0.0f;
588 ALfloat WetGainHF = 0.0f;
589 ALuint BlockAlign,BufferSize;
590 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
591 ALuint Channels,Frequency,ulExtraSamples;
592 ALfloat DrySample, WetSample;
593 ALboolean doReverb;
594 ALfloat Pitch;
595 ALint Looping,increment,State;
596 ALuint Buffer,fraction;
597 ALuint SamplesToDo;
598 ALsource *ALSource;
599 ALbuffer *ALBuffer;
600 ALeffectslot *ALEffectSlot;
601 ALfloat value;
602 ALshort *Data;
603 ALuint i,j,k;
604 ALbufferlistitem *BufferListItem;
605 ALuint loop;
606 ALint64 DataSize64,DataPos64;
607
608 SuspendContext(ALContext);
609
610 //Figure output format variables
611 BlockAlign = aluChannelsFromFormat(format);
612 BlockAlign *= aluBytesFromFormat(format);
613
614 size /= BlockAlign;
615 while(size > 0)
616 {
617 //Setup variables
618 ALEffectSlot = (ALContext ? ALContext->AuxiliaryEffectSlot : NULL);
619 ALSource = (ALContext ? ALContext->Source : NULL);
620 SamplesToDo = min(size, BUFFERSIZE);
621
622 //Clear mixing buffer
623 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
624 memset(WetBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
625 memset(ReverbBuffer, 0, SamplesToDo*sizeof(ALfloat));
626
627 //Actual mixing loop
628 while(ALSource)
629 {
630 j = 0;
631 State = ALSource->state;
632
633 doReverb = ((ALSource->Send[0].Slot &&
634 ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB) ?
635 AL_TRUE : AL_FALSE);
636
637 while(State == AL_PLAYING && j < SamplesToDo)
638 {
639 DataSize = 0;
640 DataPosInt = 0;
641 DataPosFrac = 0;
642
643 //Get buffer info
644 if((Buffer = ALSource->ulBufferID))
645 {
646 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
647
648 Data = ALBuffer->data;
649 Channels = aluChannelsFromFormat(ALBuffer->format);
650 DataSize = ALBuffer->size;
651 Frequency = ALBuffer->frequency;
652
653 CalcSourceParams(ALContext, ALSource,
654 (Channels==1) ? AL_TRUE : AL_FALSE,
655 format, DrySend, WetSend, &Pitch,
656 &DryGainHF, &WetGainHF);
657
658
659 Pitch = (Pitch*Frequency) / ALContext->Frequency;
660 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
661
662 //Get source info
663 DataPosInt = ALSource->position;
664 DataPosFrac = ALSource->position_fraction;
665 DrySample = ALSource->LastDrySample;
666 WetSample = ALSource->LastWetSample;
667
668 //Compute 18.14 fixed point step
669 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
670 if(increment > (MAX_PITCH<<FRACTIONBITS))
671 increment = (MAX_PITCH<<FRACTIONBITS);
672
673 //Figure out how many samples we can mix.
674 //Pitch must be <= 4 (the number below !)
675 DataSize64 = DataSize+MAX_PITCH;
676 DataSize64 <<= FRACTIONBITS;
677 DataPos64 = DataPosInt;
678 DataPos64 <<= FRACTIONBITS;
679 DataPos64 += DataPosFrac;
680 BufferSize = (ALuint)((DataSize64-DataPos64) / increment);
681 BufferListItem = ALSource->queue;
682 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
683 {
684 if(BufferListItem)
685 BufferListItem = BufferListItem->next;
686 }
687 if (BufferListItem)
688 {
689 if (BufferListItem->next)
690 {
691 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
692 if(NextBuf && NextBuf->data)
693 {
694 ulExtraSamples = min(NextBuf->size, (ALint)(16*Channels));
695 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
696 }
697 }
698 else if (ALSource->bLooping)
699 {
700 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
701 if (NextBuf && NextBuf->data)
702 {
703 ulExtraSamples = min(NextBuf->size, (ALint)(16*Channels));
704 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
705 }
706 }
707 }
708 BufferSize = min(BufferSize, (SamplesToDo-j));
709
710 //Actual sample mixing loop
711 Data += DataPosInt*Channels;
712 while(BufferSize--)
713 {
714 k = DataPosFrac>>FRACTIONBITS;
715 fraction = DataPosFrac&FRACTIONMASK;
716 if(Channels==1)
717 {
718 //First order interpolator
719 ALfloat sample = (ALfloat)((ALshort)(((Data[k]*((1L<<FRACTIONBITS)-fraction))+(Data[k+1]*(fraction)))>>FRACTIONBITS));
720
721 //Direct path final mix buffer and panning
722 DrySample = aluComputeSample(DryGainHF, sample, DrySample);
723 DryBuffer[j][FRONT_LEFT] += DrySample*DrySend[FRONT_LEFT];
724 DryBuffer[j][FRONT_RIGHT] += DrySample*DrySend[FRONT_RIGHT];
725 DryBuffer[j][SIDE_LEFT] += DrySample*DrySend[SIDE_LEFT];
726 DryBuffer[j][SIDE_RIGHT] += DrySample*DrySend[SIDE_RIGHT];
727 DryBuffer[j][BACK_LEFT] += DrySample*DrySend[BACK_LEFT];
728 DryBuffer[j][BACK_RIGHT] += DrySample*DrySend[BACK_RIGHT];
729 //Room path final mix buffer and panning
730 WetSample = aluComputeSample(WetGainHF, sample, WetSample);
731 if(doReverb)
732 ReverbBuffer[j] += WetSample;
733 else
734 {
735 WetBuffer[j][FRONT_LEFT] += WetSample*WetSend[FRONT_LEFT];
736 WetBuffer[j][FRONT_RIGHT] += WetSample*WetSend[FRONT_RIGHT];
737 WetBuffer[j][SIDE_LEFT] += WetSample*WetSend[SIDE_LEFT];
738 WetBuffer[j][SIDE_RIGHT] += WetSample*WetSend[SIDE_RIGHT];
739 WetBuffer[j][BACK_LEFT] += WetSample*WetSend[BACK_LEFT];
740 WetBuffer[j][BACK_RIGHT] += WetSample*WetSend[BACK_RIGHT];
741 }
742 }
743 else
744 {
745 ALfloat samp1, samp2;
746 //First order interpolator (front left)
747 samp1 = (ALfloat)((ALshort)(((Data[k*Channels ]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels ]*(fraction)))>>FRACTIONBITS));
748 DryBuffer[j][FRONT_LEFT] += samp1*DrySend[FRONT_LEFT];
749 WetBuffer[j][FRONT_LEFT] += samp1*WetSend[FRONT_LEFT];
750 //First order interpolator (front right)
751 samp2 = (ALfloat)((ALshort)(((Data[k*Channels+1]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+1]*(fraction)))>>FRACTIONBITS));
752 DryBuffer[j][FRONT_RIGHT] += samp2*DrySend[FRONT_RIGHT];
753 WetBuffer[j][FRONT_RIGHT] += samp2*WetSend[FRONT_RIGHT];
754 if(Channels >= 4)
755 {
756 int i = 2;
757 if(Channels >= 6)
758 {
759 if(Channels != 7)
760 {
761 //First order interpolator (center)
762 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
763 DryBuffer[j][CENTER] += value*DrySend[CENTER];
764 WetBuffer[j][CENTER] += value*WetSend[CENTER];
765 i++;
766 }
767 //First order interpolator (lfe)
768 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
769 DryBuffer[j][LFE] += value*DrySend[LFE];
770 WetBuffer[j][LFE] += value*WetSend[LFE];
771 i++;
772 }
773 //First order interpolator (back left)
774 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
775 DryBuffer[j][BACK_LEFT] += value*DrySend[BACK_LEFT];
776 WetBuffer[j][BACK_LEFT] += value*WetSend[BACK_LEFT];
777 i++;
778 //First order interpolator (back right)
779 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
780 DryBuffer[j][BACK_RIGHT] += value*DrySend[BACK_RIGHT];
781 WetBuffer[j][BACK_RIGHT] += value*WetSend[BACK_RIGHT];
782 i++;
783 if(Channels >= 7)
784 {
785 //First order interpolator (side left)
786 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
787 DryBuffer[j][SIDE_LEFT] += value*DrySend[SIDE_LEFT];
788 WetBuffer[j][SIDE_LEFT] += value*WetSend[SIDE_LEFT];
789 i++;
790 //First order interpolator (side right)
791 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
792 DryBuffer[j][SIDE_RIGHT] += value*DrySend[SIDE_RIGHT];
793 WetBuffer[j][SIDE_RIGHT] += value*WetSend[SIDE_RIGHT];
794 i++;
795 }
796 }
797 else if(DuplicateStereo)
798 {
799 //Duplicate stereo channels on the back speakers
800 DryBuffer[j][BACK_LEFT] += samp1*DrySend[BACK_LEFT];
801 WetBuffer[j][BACK_LEFT] += samp1*WetSend[BACK_LEFT];
802 DryBuffer[j][BACK_RIGHT] += samp2*DrySend[BACK_RIGHT];
803 WetBuffer[j][BACK_RIGHT] += samp2*WetSend[BACK_RIGHT];
804 }
805 }
806 DataPosFrac += increment;
807 j++;
808 }
809 DataPosInt += (DataPosFrac>>FRACTIONBITS);
810 DataPosFrac = (DataPosFrac&FRACTIONMASK);
811
812 //Update source info
813 ALSource->position = DataPosInt;
814 ALSource->position_fraction = DataPosFrac;
815 ALSource->LastDrySample = DrySample;
816 ALSource->LastWetSample = WetSample;
817 }
818
819 //Handle looping sources
820 if(!Buffer || DataPosInt >= DataSize)
821 {
822 //queueing
823 if(ALSource->queue)
824 {
825 Looping = ALSource->bLooping;
826 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
827 {
828 BufferListItem = ALSource->queue;
829 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
830 {
831 if(BufferListItem)
832 {
833 if(!Looping)
834 BufferListItem->bufferstate = PROCESSED;
835 BufferListItem = BufferListItem->next;
836 }
837 }
838 if(!Looping)
839 ALSource->BuffersProcessed++;
840 if(BufferListItem)
841 ALSource->ulBufferID = BufferListItem->buffer;
842 ALSource->position = DataPosInt-DataSize;
843 ALSource->position_fraction = DataPosFrac;
844 ALSource->BuffersPlayed++;
845 }
846 else
847 {
848 if(!Looping)
849 {
850 /* alSourceStop */
851 ALSource->state = AL_STOPPED;
852 ALSource->inuse = AL_FALSE;
853 ALSource->BuffersPlayed = ALSource->BuffersProcessed = ALSource->BuffersInQueue;
854 BufferListItem = ALSource->queue;
855 while(BufferListItem != NULL)
856 {
857 BufferListItem->bufferstate = PROCESSED;
858 BufferListItem = BufferListItem->next;
859 }
860 }
861 else
862 {
863 /* alSourceRewind */
864 /* alSourcePlay */
865 ALSource->state = AL_PLAYING;
866 ALSource->inuse = AL_TRUE;
867 ALSource->play = AL_TRUE;
868 ALSource->BuffersPlayed = 0;
869 ALSource->BufferPosition = 0;
870 ALSource->lBytesPlayed = 0;
871 ALSource->BuffersProcessed = 0;
872 BufferListItem = ALSource->queue;
873 while(BufferListItem != NULL)
874 {
875 BufferListItem->bufferstate = PENDING;
876 BufferListItem = BufferListItem->next;
877 }
878 ALSource->ulBufferID = ALSource->queue->buffer;
879
880 ALSource->position = DataPosInt-DataSize;
881 ALSource->position_fraction = DataPosFrac;
882 }
883 }
884 }
885 }
886
887 //Get source state
888 State = ALSource->state;
889 }
890
891 ALSource = ALSource->next;
892 }
893
894 // effect slot processing
895 while(ALEffectSlot)
896 {
897 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
898 {
899 ALfloat *DelayBuffer = ALEffectSlot->ReverbBuffer;
900 ALuint Pos = ALEffectSlot->ReverbPos;
901 ALuint LatePos = ALEffectSlot->ReverbLatePos;
902 ALuint ReflectPos = ALEffectSlot->ReverbReflectPos;
903 ALuint Length = ALEffectSlot->ReverbLength;
904 ALfloat DecayGain = ALEffectSlot->ReverbDecayGain;
905 ALfloat DecayHFRatio = ALEffectSlot->effect.Reverb.DecayHFRatio;
906 ALfloat Gain = ALEffectSlot->effect.Reverb.Gain;
907 ALfloat ReflectGain = ALEffectSlot->effect.Reverb.ReflectionsGain;
908 ALfloat LateReverbGain = ALEffectSlot->effect.Reverb.LateReverbGain;
909 ALfloat LastDecaySample = ALEffectSlot->LastDecaySample;
910 ALfloat sample;
911
912 for(i = 0;i < SamplesToDo;i++)
913 {
914 DelayBuffer[Pos] = ReverbBuffer[i] * Gain;
915
916 sample = DelayBuffer[ReflectPos] * ReflectGain;
917
918 DelayBuffer[LatePos] *= LateReverbGain;
919
920 Pos = (Pos+1) % Length;
921 DelayBuffer[Pos] *= DecayHFRatio;
922 DelayBuffer[Pos] += LastDecaySample * (1.0f-DecayHFRatio);
923 LastDecaySample = DelayBuffer[Pos];
924 DelayBuffer[Pos] *= DecayGain;
925
926 DelayBuffer[LatePos] += DelayBuffer[Pos];
927
928 sample += DelayBuffer[LatePos];
929
930 WetBuffer[i][FRONT_LEFT] += sample;
931 WetBuffer[i][FRONT_RIGHT] += sample;
932 WetBuffer[i][SIDE_LEFT] += sample;
933 WetBuffer[i][SIDE_RIGHT] += sample;
934 WetBuffer[i][BACK_LEFT] += sample;
935 WetBuffer[i][BACK_RIGHT] += sample;
936
937 LatePos = (LatePos+1) % Length;
938 ReflectPos = (ReflectPos+1) % Length;
939 }
940
941 ALEffectSlot->ReverbPos = Pos;
942 ALEffectSlot->ReverbLatePos = LatePos;
943 ALEffectSlot->ReverbReflectPos = ReflectPos;
944 ALEffectSlot->LastDecaySample = LastDecaySample;
945 }
946
947 ALEffectSlot = ALEffectSlot->next;
948 }
949
950 //Post processing loop
951 switch(format)
952 {
953 case AL_FORMAT_MONO8:
954 for(i = 0;i < SamplesToDo;i++)
955 {
956 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]+
957 WetBuffer[i][FRONT_LEFT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
958 buffer = ((ALubyte*)buffer) + 1;
959 }
960 break;
961 case AL_FORMAT_STEREO8:
962 if(ALContext && ALContext->bs2b)
963 {
964 for(i = 0;i < SamplesToDo;i++)
965 {
966 float samples[2];
967 samples[0] = DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT];
968 samples[1] = DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT];
969 bs2b_cross_feed(ALContext->bs2b, samples);
970 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
971 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
972 buffer = ((ALubyte*)buffer) + 2;
973 }
974 }
975 else
976 {
977 for(i = 0;i < SamplesToDo;i++)
978 {
979 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
980 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
981 buffer = ((ALubyte*)buffer) + 2;
982 }
983 }
984 break;
985 case AL_FORMAT_QUAD8:
986 for(i = 0;i < SamplesToDo;i++)
987 {
988 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
989 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
990 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
991 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
992 buffer = ((ALubyte*)buffer) + 4;
993 }
994 break;
995 case AL_FORMAT_51CHN8:
996 for(i = 0;i < SamplesToDo;i++)
997 {
998 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
999 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
1000 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1001 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER])>>8)+128);
1002 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1003 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1004 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1005 #else
1006 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1007 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1008 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER])>>8)+128);
1009 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1010 #endif
1011 buffer = ((ALubyte*)buffer) + 6;
1012 }
1013 break;
1014 case AL_FORMAT_61CHN8:
1015 for(i = 0;i < SamplesToDo;i++)
1016 {
1017 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
1018 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
1019 #ifdef _WIN32
1020 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1021 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1022 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1023 #else
1024 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1025 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1026 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1027 #endif
1028 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT])>>8)+128);
1029 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT])>>8)+128);
1030 buffer = ((ALubyte*)buffer) + 7;
1031 }
1032 break;
1033 case AL_FORMAT_71CHN8:
1034 for(i = 0;i < SamplesToDo;i++)
1035 {
1036 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
1037 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
1038 #ifdef _WIN32
1039 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER])>>8)+128);
1040 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1041 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1042 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1043 #else
1044 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1045 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1046 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER])>>8)+128);
1047 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1048 #endif
1049 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT])>>8)+128);
1050 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT])>>8)+128);
1051 buffer = ((ALubyte*)buffer) + 8;
1052 }
1053 break;
1054
1055 case AL_FORMAT_MONO16:
1056 for(i = 0;i < SamplesToDo;i++)
1057 {
1058 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]+
1059 WetBuffer[i][FRONT_LEFT]+WetBuffer[i][FRONT_RIGHT]);
1060 buffer = ((ALshort*)buffer) + 1;
1061 }
1062 break;
1063 case AL_FORMAT_STEREO16:
1064 if(ALContext && ALContext->bs2b)
1065 {
1066 for(i = 0;i < SamplesToDo;i++)
1067 {
1068 float samples[2];
1069 samples[0] = DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT];
1070 samples[1] = DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT];
1071 bs2b_cross_feed(ALContext->bs2b, samples);
1072 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1073 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1074 buffer = ((ALshort*)buffer) + 2;
1075 }
1076 }
1077 else
1078 {
1079 for(i = 0;i < SamplesToDo;i++)
1080 {
1081 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1082 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1083 buffer = ((ALshort*)buffer) + 2;
1084 }
1085 }
1086 break;
1087 case AL_FORMAT_QUAD16:
1088 for(i = 0;i < SamplesToDo;i++)
1089 {
1090 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1091 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1092 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1093 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1094 buffer = ((ALshort*)buffer) + 4;
1095 }
1096 break;
1097 case AL_FORMAT_51CHN16:
1098 for(i = 0;i < SamplesToDo;i++)
1099 {
1100 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1101 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1102 #ifdef _WIN32
1103 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER]);
1104 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1105 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1106 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1107 #else
1108 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1109 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1110 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER]);
1111 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1112 #endif
1113 buffer = ((ALshort*)buffer) + 6;
1114 }
1115 break;
1116 case AL_FORMAT_61CHN16:
1117 for(i = 0;i < SamplesToDo;i++)
1118 {
1119 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1120 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1121 #ifdef _WIN32
1122 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1123 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1124 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1125 #else
1126 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1127 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1128 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1129 #endif
1130 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT]);
1131 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT]);
1132 buffer = ((ALshort*)buffer) + 7;
1133 }
1134 break;
1135 case AL_FORMAT_71CHN16:
1136 for(i = 0;i < SamplesToDo;i++)
1137 {
1138 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1139 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1140 #ifdef _WIN32
1141 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER]);
1142 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1143 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1144 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1145 #else
1146 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1147 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1148 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER]);
1149 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1150 #endif
1151 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT]);
1152 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT]);
1153 buffer = ((ALshort*)buffer) + 8;
1154 }
1155 break;
1156
1157 default:
1158 break;
1159 }
1160
1161 size -= SamplesToDo;
1162 }
1163
1164 ProcessContext(ALContext);
1165 }