Avoid building redundant mixers
[openal-soft/openal-hmr.git] / Alc / ALu.c
bloba7f0839a8e5b13fc1d0416b43297b3faf4b20ef4
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 "alListener.h"
35 #include "alAuxEffectSlot.h"
36 #include "alu.h"
37 #include "bs2b.h"
39 #include "mixer_defs.h"
42 struct ChanMap {
43 enum Channel channel;
44 ALfloat angle;
47 /* Cone scalar */
48 ALfloat ConeScale = 1.0f;
50 /* Localized Z scalar for mono sources */
51 ALfloat ZScale = 1.0f;
54 static ResamplerFunc SelectResampler(enum Resampler Resampler, ALuint increment)
56 if(increment == FRACTIONONE)
57 return Resample_point32_C;
58 switch(Resampler)
60 case PointResampler:
61 return Resample_point32_C;
62 case LinearResampler:
63 #ifdef HAVE_SSE
64 if((CPUCapFlags&CPU_CAP_SSE))
65 return Resample_lerp32_SSE;
66 #endif
67 return Resample_lerp32_C;
68 case CubicResampler:
69 #ifdef HAVE_SSE
70 if((CPUCapFlags&CPU_CAP_SSE))
71 return Resample_cubic32_SSE;
72 #endif
73 return Resample_cubic32_C;
74 case ResamplerMax:
75 /* Shouldn't happen */
76 break;
79 return Resample_point32_C;
83 static DryMixerFunc SelectHrtfMixer(void)
85 #ifdef HAVE_SSE
86 if((CPUCapFlags&CPU_CAP_SSE))
87 return MixDirect_Hrtf_SSE;
88 #endif
89 #ifdef HAVE_NEON
90 if((CPUCapFlags&CPU_CAP_NEON))
91 return MixDirect_Hrtf_Neon;
92 #endif
94 return MixDirect_Hrtf_C;
97 static DryMixerFunc SelectDirectMixer(void)
99 #ifdef HAVE_SSE
100 if((CPUCapFlags&CPU_CAP_SSE))
101 return MixDirect_SSE;
102 #endif
104 return MixDirect_C;
107 static WetMixerFunc SelectSendMixer(void)
109 #ifdef HAVE_SSE
110 if((CPUCapFlags&CPU_CAP_SSE))
111 return MixSend_SSE;
112 #endif
114 return MixSend_C;
118 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
120 ALfloat temp[4] = {
121 vector[0], vector[1], vector[2], w
124 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
125 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
126 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
130 ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
132 static const struct ChanMap MonoMap[1] = { { FrontCenter, 0.0f } };
133 static const struct ChanMap StereoMap[2] = {
134 { FrontLeft, -30.0f * F_PI/180.0f },
135 { FrontRight, 30.0f * F_PI/180.0f }
137 static const struct ChanMap StereoWideMap[2] = {
138 { FrontLeft, -90.0f * F_PI/180.0f },
139 { FrontRight, 90.0f * F_PI/180.0f }
141 static const struct ChanMap RearMap[2] = {
142 { BackLeft, -150.0f * F_PI/180.0f },
143 { BackRight, 150.0f * F_PI/180.0f }
145 static const struct ChanMap QuadMap[4] = {
146 { FrontLeft, -45.0f * F_PI/180.0f },
147 { FrontRight, 45.0f * F_PI/180.0f },
148 { BackLeft, -135.0f * F_PI/180.0f },
149 { BackRight, 135.0f * F_PI/180.0f }
151 static const struct ChanMap X51Map[6] = {
152 { FrontLeft, -30.0f * F_PI/180.0f },
153 { FrontRight, 30.0f * F_PI/180.0f },
154 { FrontCenter, 0.0f * F_PI/180.0f },
155 { LFE, 0.0f },
156 { BackLeft, -110.0f * F_PI/180.0f },
157 { BackRight, 110.0f * F_PI/180.0f }
159 static const struct ChanMap X61Map[7] = {
160 { FrontLeft, -30.0f * F_PI/180.0f },
161 { FrontRight, 30.0f * F_PI/180.0f },
162 { FrontCenter, 0.0f * F_PI/180.0f },
163 { LFE, 0.0f },
164 { BackCenter, 180.0f * F_PI/180.0f },
165 { SideLeft, -90.0f * F_PI/180.0f },
166 { SideRight, 90.0f * F_PI/180.0f }
168 static const struct ChanMap X71Map[8] = {
169 { FrontLeft, -30.0f * F_PI/180.0f },
170 { FrontRight, 30.0f * F_PI/180.0f },
171 { FrontCenter, 0.0f * F_PI/180.0f },
172 { LFE, 0.0f },
173 { BackLeft, -150.0f * F_PI/180.0f },
174 { BackRight, 150.0f * F_PI/180.0f },
175 { SideLeft, -90.0f * F_PI/180.0f },
176 { SideRight, 90.0f * F_PI/180.0f }
179 ALCdevice *Device = ALContext->Device;
180 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
181 ALbufferlistitem *BufferListItem;
182 enum FmtChannels Channels;
183 ALfloat (*SrcMatrix)[MaxChannels];
184 ALfloat DryGain, DryGainHF;
185 ALfloat WetGain[MAX_SENDS];
186 ALfloat WetGainHF[MAX_SENDS];
187 ALint NumSends, Frequency;
188 const struct ChanMap *chans = NULL;
189 enum Resampler Resampler;
190 ALint num_channels = 0;
191 ALboolean DirectChannels;
192 ALfloat hwidth = 0.0f;
193 ALfloat Pitch;
194 ALfloat cw;
195 ALint i, c;
197 /* Get device properties */
198 NumSends = Device->NumAuxSends;
199 Frequency = Device->Frequency;
201 /* Get listener properties */
202 ListenerGain = ALContext->Listener.Gain;
204 /* Get source properties */
205 SourceVolume = ALSource->Gain;
206 MinVolume = ALSource->MinGain;
207 MaxVolume = ALSource->MaxGain;
208 Pitch = ALSource->Pitch;
209 Resampler = ALSource->Resampler;
210 DirectChannels = ALSource->DirectChannels;
212 /* Calculate the stepping value */
213 Channels = FmtMono;
214 BufferListItem = ALSource->queue;
215 while(BufferListItem != NULL)
217 ALbuffer *ALBuffer;
218 if((ALBuffer=BufferListItem->buffer) != NULL)
220 ALsizei maxstep = BUFFERSIZE / ALSource->NumChannels;
221 maxstep -= ResamplerPadding[Resampler] +
222 ResamplerPrePadding[Resampler] + 1;
223 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
225 Pitch = Pitch * ALBuffer->Frequency / Frequency;
226 if(Pitch > (ALfloat)maxstep)
227 ALSource->Params.Step = maxstep<<FRACTIONBITS;
228 else
230 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
231 if(ALSource->Params.Step == 0)
232 ALSource->Params.Step = 1;
234 ALSource->Params.Resample = SelectResampler(Resampler, ALSource->Params.Step);
236 Channels = ALBuffer->FmtChannels;
237 break;
239 BufferListItem = BufferListItem->next;
241 if(!DirectChannels && Device->Hrtf)
242 ALSource->Params.DryMix = SelectHrtfMixer();
243 else
244 ALSource->Params.DryMix = SelectDirectMixer();
245 ALSource->Params.WetMix = SelectSendMixer();
247 /* Calculate gains */
248 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
249 DryGain *= ALSource->DirectGain * ListenerGain;
250 DryGainHF = ALSource->DirectGainHF;
251 for(i = 0;i < NumSends;i++)
253 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
254 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
255 WetGainHF[i] = ALSource->Send[i].GainHF;
258 SrcMatrix = ALSource->Params.Direct.Gains;
259 for(i = 0;i < MaxChannels;i++)
261 for(c = 0;c < MaxChannels;c++)
262 SrcMatrix[i][c] = 0.0f;
264 switch(Channels)
266 case FmtMono:
267 chans = MonoMap;
268 num_channels = 1;
269 break;
271 case FmtStereo:
272 if(!(Device->Flags&DEVICE_WIDE_STEREO))
273 chans = StereoMap;
274 else
276 chans = StereoWideMap;
277 hwidth = 60.0f * F_PI/180.0f;
279 num_channels = 2;
280 break;
282 case FmtRear:
283 chans = RearMap;
284 num_channels = 2;
285 break;
287 case FmtQuad:
288 chans = QuadMap;
289 num_channels = 4;
290 break;
292 case FmtX51:
293 chans = X51Map;
294 num_channels = 6;
295 break;
297 case FmtX61:
298 chans = X61Map;
299 num_channels = 7;
300 break;
302 case FmtX71:
303 chans = X71Map;
304 num_channels = 8;
305 break;
308 if(DirectChannels != AL_FALSE)
310 for(c = 0;c < num_channels;c++)
312 for(i = 0;i < (ALint)Device->NumChan;i++)
314 enum Channel chan = Device->Speaker2Chan[i];
315 if(chan == chans[c].channel)
317 SrcMatrix[c][chan] += DryGain;
318 break;
323 else if(Device->Hrtf)
325 for(c = 0;c < num_channels;c++)
327 if(chans[c].channel == LFE)
329 /* Skip LFE */
330 ALSource->Params.Direct.Hrtf.Delay[c][0] = 0;
331 ALSource->Params.Direct.Hrtf.Delay[c][1] = 0;
332 for(i = 0;i < HRIR_LENGTH;i++)
334 ALSource->Params.Direct.Hrtf.Coeffs[c][i][0] = 0.0f;
335 ALSource->Params.Direct.Hrtf.Coeffs[c][i][1] = 0.0f;
338 else
340 /* Get the static HRIR coefficients and delays for this
341 * channel. */
342 GetLerpedHrtfCoeffs(Device->Hrtf,
343 0.0f, chans[c].angle, DryGain,
344 ALSource->Params.Direct.Hrtf.Coeffs[c],
345 ALSource->Params.Direct.Hrtf.Delay[c]);
348 ALSource->Hrtf.Counter = 0;
350 else
352 DryGain *= lerp(1.0f, 1.0f/sqrtf(Device->NumChan), hwidth/(F_PI*2.0f));
353 for(c = 0;c < num_channels;c++)
355 /* Special-case LFE */
356 if(chans[c].channel == LFE)
358 SrcMatrix[c][chans[c].channel] = DryGain;
359 continue;
361 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain,
362 SrcMatrix[c]);
365 for(i = 0;i < NumSends;i++)
367 ALeffectslot *Slot = ALSource->Send[i].Slot;
369 if(!Slot && i == 0)
370 Slot = Device->DefaultSlot;
371 if(Slot && Slot->effect.type == AL_EFFECT_NULL)
372 Slot = NULL;
373 ALSource->Params.Send[i].Slot = Slot;
374 ALSource->Params.Send[i].Gain = WetGain[i];
377 /* Update filter coefficients. Calculations based on the I3DL2
378 * spec. */
379 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / Frequency);
381 /* We use two chained one-pole filters, so we need to take the
382 * square root of the squared gain, which is the same as the base
383 * gain. */
384 ALSource->Params.Direct.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
385 for(i = 0;i < NumSends;i++)
387 ALfloat a = lpCoeffCalc(WetGainHF[i], cw);
388 ALSource->Params.Send[i].iirFilter.coeff = a;
392 ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
394 const ALCdevice *Device = ALContext->Device;
395 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
396 ALfloat Direction[3],Position[3],SourceToListener[3];
397 ALfloat Velocity[3],ListenerVel[3];
398 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
399 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
400 ALfloat DopplerFactor, SpeedOfSound;
401 ALfloat AirAbsorptionFactor;
402 ALfloat RoomAirAbsorption[MAX_SENDS];
403 ALbufferlistitem *BufferListItem;
404 ALfloat Attenuation;
405 ALfloat RoomAttenuation[MAX_SENDS];
406 ALfloat MetersPerUnit;
407 ALfloat RoomRolloffBase;
408 ALfloat RoomRolloff[MAX_SENDS];
409 ALfloat DecayDistance[MAX_SENDS];
410 ALfloat DryGain;
411 ALfloat DryGainHF;
412 ALboolean DryGainHFAuto;
413 ALfloat WetGain[MAX_SENDS];
414 ALfloat WetGainHF[MAX_SENDS];
415 ALboolean WetGainAuto;
416 ALboolean WetGainHFAuto;
417 enum Resampler Resampler;
418 ALfloat Matrix[4][4];
419 ALfloat Pitch;
420 ALuint Frequency;
421 ALint NumSends;
422 ALfloat cw;
423 ALint i, j;
425 DryGainHF = 1.0f;
426 for(i = 0;i < MAX_SENDS;i++)
427 WetGainHF[i] = 1.0f;
429 /* Get context/device properties */
430 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
431 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
432 NumSends = Device->NumAuxSends;
433 Frequency = Device->Frequency;
435 /* Get listener properties */
436 ListenerGain = ALContext->Listener.Gain;
437 MetersPerUnit = ALContext->Listener.MetersPerUnit;
438 ListenerVel[0] = ALContext->Listener.Velocity[0];
439 ListenerVel[1] = ALContext->Listener.Velocity[1];
440 ListenerVel[2] = ALContext->Listener.Velocity[2];
441 for(i = 0;i < 4;i++)
443 for(j = 0;j < 4;j++)
444 Matrix[i][j] = ALContext->Listener.Matrix[i][j];
447 /* Get source properties */
448 SourceVolume = ALSource->Gain;
449 MinVolume = ALSource->MinGain;
450 MaxVolume = ALSource->MaxGain;
451 Pitch = ALSource->Pitch;
452 Resampler = ALSource->Resampler;
453 Position[0] = ALSource->Position[0];
454 Position[1] = ALSource->Position[1];
455 Position[2] = ALSource->Position[2];
456 Direction[0] = ALSource->Orientation[0];
457 Direction[1] = ALSource->Orientation[1];
458 Direction[2] = ALSource->Orientation[2];
459 Velocity[0] = ALSource->Velocity[0];
460 Velocity[1] = ALSource->Velocity[1];
461 Velocity[2] = ALSource->Velocity[2];
462 MinDist = ALSource->RefDistance;
463 MaxDist = ALSource->MaxDistance;
464 Rolloff = ALSource->RollOffFactor;
465 InnerAngle = ALSource->InnerAngle;
466 OuterAngle = ALSource->OuterAngle;
467 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
468 DryGainHFAuto = ALSource->DryGainHFAuto;
469 WetGainAuto = ALSource->WetGainAuto;
470 WetGainHFAuto = ALSource->WetGainHFAuto;
471 RoomRolloffBase = ALSource->RoomRolloffFactor;
472 for(i = 0;i < NumSends;i++)
474 ALeffectslot *Slot = ALSource->Send[i].Slot;
476 if(!Slot && i == 0)
477 Slot = Device->DefaultSlot;
478 if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
480 Slot = NULL;
481 RoomRolloff[i] = 0.0f;
482 DecayDistance[i] = 0.0f;
483 RoomAirAbsorption[i] = 1.0f;
485 else if(Slot->AuxSendAuto)
487 RoomRolloff[i] = RoomRolloffBase;
488 if(IsReverbEffect(Slot->effect.type))
490 RoomRolloff[i] += Slot->effect.Reverb.RoomRolloffFactor;
491 DecayDistance[i] = Slot->effect.Reverb.DecayTime *
492 SPEEDOFSOUNDMETRESPERSEC;
493 RoomAirAbsorption[i] = Slot->effect.Reverb.AirAbsorptionGainHF;
495 else
497 DecayDistance[i] = 0.0f;
498 RoomAirAbsorption[i] = 1.0f;
501 else
503 /* If the slot's auxiliary send auto is off, the data sent to the
504 * effect slot is the same as the dry path, sans filter effects */
505 RoomRolloff[i] = Rolloff;
506 DecayDistance[i] = 0.0f;
507 RoomAirAbsorption[i] = AIRABSORBGAINHF;
510 ALSource->Params.Send[i].Slot = Slot;
513 /* Transform source to listener space (convert to head relative) */
514 if(ALSource->HeadRelative == AL_FALSE)
516 /* Translate position */
517 Position[0] -= ALContext->Listener.Position[0];
518 Position[1] -= ALContext->Listener.Position[1];
519 Position[2] -= ALContext->Listener.Position[2];
521 /* Transform source vectors */
522 aluMatrixVector(Position, 1.0f, Matrix);
523 aluMatrixVector(Direction, 0.0f, Matrix);
524 aluMatrixVector(Velocity, 0.0f, Matrix);
525 /* Transform listener velocity */
526 aluMatrixVector(ListenerVel, 0.0f, Matrix);
528 else
530 /* Transform listener velocity from world space to listener space */
531 aluMatrixVector(ListenerVel, 0.0f, Matrix);
532 /* Offset the source velocity to be relative of the listener velocity */
533 Velocity[0] += ListenerVel[0];
534 Velocity[1] += ListenerVel[1];
535 Velocity[2] += ListenerVel[2];
538 SourceToListener[0] = -Position[0];
539 SourceToListener[1] = -Position[1];
540 SourceToListener[2] = -Position[2];
541 aluNormalize(SourceToListener);
542 aluNormalize(Direction);
544 /* Calculate distance attenuation */
545 Distance = sqrtf(aluDotproduct(Position, Position));
546 ClampedDist = Distance;
548 Attenuation = 1.0f;
549 for(i = 0;i < NumSends;i++)
550 RoomAttenuation[i] = 1.0f;
551 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
552 ALContext->DistanceModel)
554 case InverseDistanceClamped:
555 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
556 if(MaxDist < MinDist)
557 break;
558 /*fall-through*/
559 case InverseDistance:
560 if(MinDist > 0.0f)
562 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
563 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
564 for(i = 0;i < NumSends;i++)
566 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
567 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
570 break;
572 case LinearDistanceClamped:
573 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
574 if(MaxDist < MinDist)
575 break;
576 /*fall-through*/
577 case LinearDistance:
578 if(MaxDist != MinDist)
580 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
581 Attenuation = maxf(Attenuation, 0.0f);
582 for(i = 0;i < NumSends;i++)
584 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
585 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
588 break;
590 case ExponentDistanceClamped:
591 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
592 if(MaxDist < MinDist)
593 break;
594 /*fall-through*/
595 case ExponentDistance:
596 if(ClampedDist > 0.0f && MinDist > 0.0f)
598 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
599 for(i = 0;i < NumSends;i++)
600 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
602 break;
604 case DisableDistance:
605 ClampedDist = MinDist;
606 break;
609 /* Source Gain + Attenuation */
610 DryGain = SourceVolume * Attenuation;
611 for(i = 0;i < NumSends;i++)
612 WetGain[i] = SourceVolume * RoomAttenuation[i];
614 /* Distance-based air absorption */
615 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
617 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
618 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
619 for(i = 0;i < NumSends;i++)
620 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
623 if(WetGainAuto)
625 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
627 /* Apply a decay-time transformation to the wet path, based on the
628 * attenuation of the dry path.
630 * Using the apparent distance, based on the distance attenuation, the
631 * initial decay of the reverb effect is calculated and applied to the
632 * wet path.
634 for(i = 0;i < NumSends;i++)
636 if(DecayDistance[i] > 0.0f)
637 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
641 /* Calculate directional soundcones */
642 Angle = acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale * (360.0f/F_PI);
643 if(Angle > InnerAngle && Angle <= OuterAngle)
645 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
646 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
647 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
649 else if(Angle > OuterAngle)
651 ConeVolume = ALSource->OuterGain;
652 ConeHF = ALSource->OuterGainHF;
654 else
656 ConeVolume = 1.0f;
657 ConeHF = 1.0f;
660 DryGain *= ConeVolume;
661 if(WetGainAuto)
663 for(i = 0;i < NumSends;i++)
664 WetGain[i] *= ConeVolume;
666 if(DryGainHFAuto)
667 DryGainHF *= ConeHF;
668 if(WetGainHFAuto)
670 for(i = 0;i < NumSends;i++)
671 WetGainHF[i] *= ConeHF;
674 /* Clamp to Min/Max Gain */
675 DryGain = clampf(DryGain, MinVolume, MaxVolume);
676 for(i = 0;i < NumSends;i++)
677 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
679 /* Apply gain and frequency filters */
680 DryGain *= ALSource->DirectGain * ListenerGain;
681 DryGainHF *= ALSource->DirectGainHF;
682 for(i = 0;i < NumSends;i++)
684 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
685 WetGainHF[i] *= ALSource->Send[i].GainHF;
688 /* Calculate velocity-based doppler effect */
689 if(DopplerFactor > 0.0f)
691 ALfloat VSS, VLS;
693 if(SpeedOfSound < 1.0f)
695 DopplerFactor *= 1.0f/SpeedOfSound;
696 SpeedOfSound = 1.0f;
699 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
700 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
702 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
703 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
706 BufferListItem = ALSource->queue;
707 while(BufferListItem != NULL)
709 ALbuffer *ALBuffer;
710 if((ALBuffer=BufferListItem->buffer) != NULL)
712 /* Calculate fixed-point stepping value, based on the pitch, buffer
713 * frequency, and output frequency. */
714 ALsizei maxstep = BUFFERSIZE / ALSource->NumChannels;
715 maxstep -= ResamplerPadding[Resampler] +
716 ResamplerPrePadding[Resampler] + 1;
717 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
719 Pitch = Pitch * ALBuffer->Frequency / Frequency;
720 if(Pitch > (ALfloat)maxstep)
721 ALSource->Params.Step = maxstep<<FRACTIONBITS;
722 else
724 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
725 if(ALSource->Params.Step == 0)
726 ALSource->Params.Step = 1;
728 ALSource->Params.Resample = SelectResampler(Resampler, ALSource->Params.Step);
730 break;
732 BufferListItem = BufferListItem->next;
734 if(Device->Hrtf)
735 ALSource->Params.DryMix = SelectHrtfMixer();
736 else
737 ALSource->Params.DryMix = SelectDirectMixer();
738 ALSource->Params.WetMix = SelectSendMixer();
740 if(Device->Hrtf)
742 /* Use a binaural HRTF algorithm for stereo headphone playback */
743 ALfloat delta, ev = 0.0f, az = 0.0f;
745 if(Distance > 0.0f)
747 ALfloat invlen = 1.0f/Distance;
748 Position[0] *= invlen;
749 Position[1] *= invlen;
750 Position[2] *= invlen;
752 /* Calculate elevation and azimuth only when the source is not at
753 * the listener. This prevents +0 and -0 Z from producing
754 * inconsistent panning. Also, clamp Y in case FP precision errors
755 * cause it to land outside of -1..+1. */
756 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
757 az = atan2f(Position[0], -Position[2]*ZScale);
760 /* Check to see if the HRIR is already moving. */
761 if(ALSource->Hrtf.Moving)
763 /* Calculate the normalized HRTF transition factor (delta). */
764 delta = CalcHrtfDelta(ALSource->Params.Direct.Hrtf.Gain, DryGain,
765 ALSource->Params.Direct.Hrtf.Dir, Position);
766 /* If the delta is large enough, get the moving HRIR target
767 * coefficients, target delays, steppping values, and counter. */
768 if(delta > 0.001f)
770 ALSource->Hrtf.Counter = GetMovingHrtfCoeffs(Device->Hrtf,
771 ev, az, DryGain, delta,
772 ALSource->Hrtf.Counter,
773 ALSource->Params.Direct.Hrtf.Coeffs[0],
774 ALSource->Params.Direct.Hrtf.Delay[0],
775 ALSource->Params.Direct.Hrtf.CoeffStep,
776 ALSource->Params.Direct.Hrtf.DelayStep);
777 ALSource->Params.Direct.Hrtf.Gain = DryGain;
778 ALSource->Params.Direct.Hrtf.Dir[0] = Position[0];
779 ALSource->Params.Direct.Hrtf.Dir[1] = Position[1];
780 ALSource->Params.Direct.Hrtf.Dir[2] = Position[2];
783 else
785 /* Get the initial (static) HRIR coefficients and delays. */
786 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
787 ALSource->Params.Direct.Hrtf.Coeffs[0],
788 ALSource->Params.Direct.Hrtf.Delay[0]);
789 ALSource->Hrtf.Counter = 0;
790 ALSource->Params.Direct.Hrtf.Gain = DryGain;
791 ALSource->Params.Direct.Hrtf.Dir[0] = Position[0];
792 ALSource->Params.Direct.Hrtf.Dir[1] = Position[1];
793 ALSource->Params.Direct.Hrtf.Dir[2] = Position[2];
796 else
798 ALfloat (*Matrix)[MaxChannels] = ALSource->Params.Direct.Gains;
799 ALfloat DirGain = 0.0f;
800 ALfloat AmbientGain;
802 for(i = 0;i < MaxChannels;i++)
804 for(j = 0;j < MaxChannels;j++)
805 Matrix[i][j] = 0.0f;
808 /* Normalize the length, and compute panned gains. */
809 if(Distance > 0.0f)
811 ALfloat invlen = 1.0f/Distance;
812 Position[0] *= invlen;
813 Position[1] *= invlen;
814 Position[2] *= invlen;
816 DirGain = sqrtf(Position[0]*Position[0] + Position[2]*Position[2]);
817 ComputeAngleGains(Device, atan2f(Position[0], -Position[2]*ZScale), 0.0f,
818 DryGain*DirGain, Matrix[0]);
821 /* Adjustment for vertical offsets. Not the greatest, but simple
822 * enough. */
823 AmbientGain = DryGain * sqrtf(1.0f/Device->NumChan) * (1.0f-DirGain);
824 for(i = 0;i < (ALint)Device->NumChan;i++)
826 enum Channel chan = Device->Speaker2Chan[i];
827 Matrix[0][chan] = maxf(Matrix[0][chan], AmbientGain);
830 for(i = 0;i < NumSends;i++)
831 ALSource->Params.Send[i].Gain = WetGain[i];
833 /* Update filter coefficients. */
834 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / Frequency);
836 ALSource->Params.Direct.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
837 for(i = 0;i < NumSends;i++)
839 ALfloat a = lpCoeffCalc(WetGainHF[i], cw);
840 ALSource->Params.Send[i].iirFilter.coeff = a;
845 static __inline ALfloat aluF2F(ALfloat val)
846 { return val; }
847 static __inline ALint aluF2I(ALfloat val)
849 if(val > 1.0f) return 2147483647;
850 if(val < -1.0f) return -2147483647-1;
851 return fastf2i((ALfloat)(val*2147483647.0));
853 static __inline ALuint aluF2UI(ALfloat val)
854 { return aluF2I(val)+2147483648u; }
855 static __inline ALshort aluF2S(ALfloat val)
856 { return aluF2I(val)>>16; }
857 static __inline ALushort aluF2US(ALfloat val)
858 { return aluF2S(val)+32768; }
859 static __inline ALbyte aluF2B(ALfloat val)
860 { return aluF2I(val)>>24; }
861 static __inline ALubyte aluF2UB(ALfloat val)
862 { return aluF2B(val)+128; }
864 #define DECL_TEMPLATE(T, func) \
865 static void Write_##T(ALCdevice *device, T *RESTRICT buffer, \
866 ALuint SamplesToDo) \
868 ALfloat (*RESTRICT DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
869 ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
870 const enum Channel *ChanMap = device->DevChannels; \
871 ALuint i, j; \
873 for(j = 0;j < numchans;j++) \
875 T *RESTRICT out = buffer + j; \
876 enum Channel chan = ChanMap[j]; \
878 for(i = 0;i < SamplesToDo;i++) \
879 out[i*numchans] = func(DryBuffer[chan][i]); \
883 DECL_TEMPLATE(ALfloat, aluF2F)
884 DECL_TEMPLATE(ALuint, aluF2UI)
885 DECL_TEMPLATE(ALint, aluF2I)
886 DECL_TEMPLATE(ALushort, aluF2US)
887 DECL_TEMPLATE(ALshort, aluF2S)
888 DECL_TEMPLATE(ALubyte, aluF2UB)
889 DECL_TEMPLATE(ALbyte, aluF2B)
891 #undef DECL_TEMPLATE
894 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
896 ALuint SamplesToDo;
897 ALeffectslot **slot, **slot_end;
898 ALsource **src, **src_end;
899 ALCcontext *ctx;
900 FPUCtl oldMode;
901 ALuint i, c;
903 SetMixerFPUMode(&oldMode);
905 while(size > 0)
907 SamplesToDo = minu(size, BUFFERSIZE);
908 for(c = 0;c < MaxChannels;c++)
909 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
911 ALCdevice_Lock(device);
912 ctx = device->ContextList;
913 while(ctx)
915 ALenum DeferUpdates = ctx->DeferUpdates;
916 ALenum UpdateSources = AL_FALSE;
918 if(!DeferUpdates)
919 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
921 /* source processing */
922 src = ctx->ActiveSources;
923 src_end = src + ctx->ActiveSourceCount;
924 while(src != src_end)
926 if((*src)->state != AL_PLAYING)
928 --(ctx->ActiveSourceCount);
929 *src = *(--src_end);
930 continue;
933 if(!DeferUpdates && (ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) ||
934 UpdateSources))
935 ALsource_Update(*src, ctx);
937 MixSource(*src, device, SamplesToDo);
938 src++;
941 /* effect slot processing */
942 slot = ctx->ActiveEffectSlots;
943 slot_end = slot + ctx->ActiveEffectSlotCount;
944 while(slot != slot_end)
946 for(c = 0;c < SamplesToDo;c++)
948 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
949 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
951 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
952 (*slot)->PendingClicks[0] = 0.0f;
954 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
955 ALeffectState_Update((*slot)->EffectState, device, *slot);
957 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
958 (*slot)->WetBuffer, device->DryBuffer);
960 for(i = 0;i < SamplesToDo;i++)
961 (*slot)->WetBuffer[i] = 0.0f;
963 slot++;
966 ctx = ctx->next;
969 slot = &device->DefaultSlot;
970 if(*slot != NULL)
972 for(c = 0;c < SamplesToDo;c++)
974 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
975 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
977 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
978 (*slot)->PendingClicks[0] = 0.0f;
980 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
981 ALeffectState_Update((*slot)->EffectState, device, *slot);
983 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
984 (*slot)->WetBuffer, device->DryBuffer);
986 for(i = 0;i < SamplesToDo;i++)
987 (*slot)->WetBuffer[i] = 0.0f;
989 ALCdevice_Unlock(device);
991 /* Click-removal. Could do better; this only really handles immediate
992 * changes between updates where a predictive sample could be
993 * generated. Delays caused by effects and HRTF aren't caught. */
994 if(device->FmtChans == DevFmtMono)
996 for(i = 0;i < SamplesToDo;i++)
998 device->DryBuffer[FrontCenter][i] += device->ClickRemoval[FrontCenter];
999 device->ClickRemoval[FrontCenter] -= device->ClickRemoval[FrontCenter] * (1.0f/256.0f);
1001 device->ClickRemoval[FrontCenter] += device->PendingClicks[FrontCenter];
1002 device->PendingClicks[FrontCenter] = 0.0f;
1004 else if(device->FmtChans == DevFmtStereo)
1006 /* Assumes the first two channels are FrontLeft and FrontRight */
1007 for(c = 0;c < 2;c++)
1009 ALfloat offset = device->ClickRemoval[c];
1010 for(i = 0;i < SamplesToDo;i++)
1012 device->DryBuffer[c][i] += offset;
1013 offset -= offset * (1.0f/256.0f);
1015 device->ClickRemoval[c] = offset + device->PendingClicks[c];
1016 device->PendingClicks[c] = 0.0f;
1018 if(device->Bs2b)
1020 float samples[2];
1021 for(i = 0;i < SamplesToDo;i++)
1023 samples[0] = device->DryBuffer[FrontLeft][i];
1024 samples[1] = device->DryBuffer[FrontRight][i];
1025 bs2b_cross_feed(device->Bs2b, samples);
1026 device->DryBuffer[FrontLeft][i] = samples[0];
1027 device->DryBuffer[FrontRight][i] = samples[1];
1031 else
1033 for(c = 0;c < MaxChannels;c++)
1035 ALfloat offset = device->ClickRemoval[c];
1036 for(i = 0;i < SamplesToDo;i++)
1038 device->DryBuffer[c][i] += offset;
1039 offset -= offset * (1.0f/256.0f);
1041 device->ClickRemoval[c] = offset + device->PendingClicks[c];
1042 device->PendingClicks[c] = 0.0f;
1046 if(buffer)
1048 switch(device->FmtType)
1050 case DevFmtByte:
1051 Write_ALbyte(device, buffer, SamplesToDo);
1052 break;
1053 case DevFmtUByte:
1054 Write_ALubyte(device, buffer, SamplesToDo);
1055 break;
1056 case DevFmtShort:
1057 Write_ALshort(device, buffer, SamplesToDo);
1058 break;
1059 case DevFmtUShort:
1060 Write_ALushort(device, buffer, SamplesToDo);
1061 break;
1062 case DevFmtInt:
1063 Write_ALint(device, buffer, SamplesToDo);
1064 break;
1065 case DevFmtUInt:
1066 Write_ALuint(device, buffer, SamplesToDo);
1067 break;
1068 case DevFmtFloat:
1069 Write_ALfloat(device, buffer, SamplesToDo);
1070 break;
1074 size -= SamplesToDo;
1077 RestoreFPUMode(&oldMode);
1081 ALvoid aluHandleDisconnect(ALCdevice *device)
1083 ALCcontext *Context;
1085 ALCdevice_Lock(device);
1086 device->Connected = ALC_FALSE;
1088 Context = device->ContextList;
1089 while(Context)
1091 ALsource **src, **src_end;
1093 src = Context->ActiveSources;
1094 src_end = src + Context->ActiveSourceCount;
1095 while(src != src_end)
1097 if((*src)->state == AL_PLAYING)
1099 (*src)->state = AL_STOPPED;
1100 (*src)->BuffersPlayed = (*src)->BuffersInQueue;
1101 (*src)->position = 0;
1102 (*src)->position_fraction = 0;
1104 src++;
1106 Context->ActiveSourceCount = 0;
1108 Context = Context->next;
1110 ALCdevice_Unlock(device);