Use more proper enum names for the resampler
[openal-soft.git] / Alc / ALu.c
blob21d484c3053ed97b9ed8e9e37b8d5436f759e699
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"
40 struct ChanMap {
41 enum Channel channel;
42 ALfloat angle;
45 /* Cone scalar */
46 ALfloat ConeScale = 0.5f;
48 /* Localized Z scalar for mono sources */
49 ALfloat ZScale = 1.0f;
52 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
54 ALfloat temp[4] = {
55 vector[0], vector[1], vector[2], w
58 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
59 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
60 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
64 ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
66 static const struct ChanMap MonoMap[1] = { { FRONT_CENTER, 0.0f } };
67 static const struct ChanMap StereoMap[2] = {
68 { FRONT_LEFT, -30.0f * F_PI/180.0f },
69 { FRONT_RIGHT, 30.0f * F_PI/180.0f }
71 static const struct ChanMap RearMap[2] = {
72 { BACK_LEFT, -150.0f * F_PI/180.0f },
73 { BACK_RIGHT, 150.0f * F_PI/180.0f }
75 static const struct ChanMap QuadMap[4] = {
76 { FRONT_LEFT, -45.0f * F_PI/180.0f },
77 { FRONT_RIGHT, 45.0f * F_PI/180.0f },
78 { BACK_LEFT, -135.0f * F_PI/180.0f },
79 { BACK_RIGHT, 135.0f * F_PI/180.0f }
81 static const struct ChanMap X51Map[6] = {
82 { FRONT_LEFT, -30.0f * F_PI/180.0f },
83 { FRONT_RIGHT, 30.0f * F_PI/180.0f },
84 { FRONT_CENTER, 0.0f * F_PI/180.0f },
85 { LFE, 0.0f },
86 { BACK_LEFT, -110.0f * F_PI/180.0f },
87 { BACK_RIGHT, 110.0f * F_PI/180.0f }
89 static const struct ChanMap X61Map[7] = {
90 { FRONT_LEFT, -30.0f * F_PI/180.0f },
91 { FRONT_RIGHT, 30.0f * F_PI/180.0f },
92 { FRONT_CENTER, 0.0f * F_PI/180.0f },
93 { LFE, 0.0f },
94 { BACK_CENTER, 180.0f * F_PI/180.0f },
95 { SIDE_LEFT, -90.0f * F_PI/180.0f },
96 { SIDE_RIGHT, 90.0f * F_PI/180.0f }
98 static const struct ChanMap X71Map[8] = {
99 { FRONT_LEFT, -30.0f * F_PI/180.0f },
100 { FRONT_RIGHT, 30.0f * F_PI/180.0f },
101 { FRONT_CENTER, 0.0f * F_PI/180.0f },
102 { LFE, 0.0f },
103 { BACK_LEFT, -150.0f * F_PI/180.0f },
104 { BACK_RIGHT, 150.0f * F_PI/180.0f },
105 { SIDE_LEFT, -90.0f * F_PI/180.0f },
106 { SIDE_RIGHT, 90.0f * F_PI/180.0f }
109 ALCdevice *Device = ALContext->Device;
110 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
111 ALbufferlistitem *BufferListItem;
112 enum DevFmtChannels DevChans;
113 enum FmtChannels Channels;
114 ALfloat (*SrcMatrix)[MAXCHANNELS];
115 ALfloat DryGain, DryGainHF;
116 ALfloat WetGain[MAX_SENDS];
117 ALfloat WetGainHF[MAX_SENDS];
118 ALint NumSends, Frequency;
119 const ALfloat *ChannelGain;
120 const struct ChanMap *chans = NULL;
121 enum Resampler Resampler;
122 ALint num_channels = 0;
123 ALboolean DirectChannels;
124 ALfloat Pitch;
125 ALfloat cw;
126 ALuint pos;
127 ALint i, c;
129 /* Get device properties */
130 DevChans = Device->FmtChans;
131 NumSends = Device->NumAuxSends;
132 Frequency = Device->Frequency;
134 /* Get listener properties */
135 ListenerGain = ALContext->Listener.Gain;
137 /* Get source properties */
138 SourceVolume = ALSource->flGain;
139 MinVolume = ALSource->flMinGain;
140 MaxVolume = ALSource->flMaxGain;
141 Pitch = ALSource->flPitch;
142 Resampler = ALSource->Resampler;
143 DirectChannels = ALSource->DirectChannels;
145 /* Calculate the stepping value */
146 Channels = FmtMono;
147 BufferListItem = ALSource->queue;
148 while(BufferListItem != NULL)
150 ALbuffer *ALBuffer;
151 if((ALBuffer=BufferListItem->buffer) != NULL)
153 ALsizei maxstep = STACK_DATA_SIZE/sizeof(ALfloat) /
154 ALSource->NumChannels;
155 maxstep -= ResamplerPadding[Resampler] +
156 ResamplerPrePadding[Resampler] + 1;
157 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
159 Pitch = Pitch * ALBuffer->Frequency / Frequency;
160 if(Pitch > (ALfloat)maxstep)
161 ALSource->Params.Step = maxstep<<FRACTIONBITS;
162 else
164 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
165 if(ALSource->Params.Step == 0)
166 ALSource->Params.Step = 1;
169 Channels = ALBuffer->FmtChannels;
170 break;
172 BufferListItem = BufferListItem->next;
174 if(!DirectChannels && Device->Hrtf)
175 ALSource->Params.DoMix = SelectHrtfMixer((ALSource->Params.Step==FRACTIONONE) ?
176 PointResampler : Resampler);
177 else
178 ALSource->Params.DoMix = SelectMixer((ALSource->Params.Step==FRACTIONONE) ?
179 PointResampler : Resampler);
181 /* Calculate gains */
182 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
183 DryGain *= ALSource->DirectGain;
184 DryGainHF = ALSource->DirectGainHF;
185 for(i = 0;i < NumSends;i++)
187 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
188 WetGain[i] *= ALSource->Send[i].WetGain;
189 WetGainHF[i] = ALSource->Send[i].WetGainHF;
192 SrcMatrix = ALSource->Params.DryGains;
193 for(i = 0;i < MAXCHANNELS;i++)
195 for(c = 0;c < MAXCHANNELS;c++)
196 SrcMatrix[i][c] = 0.0f;
198 switch(Channels)
200 case FmtMono:
201 chans = MonoMap;
202 num_channels = 1;
203 break;
204 case FmtStereo:
205 if(!DirectChannels && (Device->Flags&DEVICE_DUPLICATE_STEREO))
207 DryGain *= aluSqrt(2.0f/4.0f);
208 for(c = 0;c < 2;c++)
210 pos = aluCart2LUTpos(aluCos(RearMap[c].angle),
211 aluSin(RearMap[c].angle));
212 ChannelGain = Device->PanningLUT[pos];
214 for(i = 0;i < (ALint)Device->NumChan;i++)
216 enum Channel chan = Device->Speaker2Chan[i];
217 SrcMatrix[c][chan] += DryGain * ListenerGain *
218 ChannelGain[chan];
222 chans = StereoMap;
223 num_channels = 2;
224 break;
226 case FmtRear:
227 chans = RearMap;
228 num_channels = 2;
229 break;
231 case FmtQuad:
232 chans = QuadMap;
233 num_channels = 4;
234 break;
236 case FmtX51:
237 chans = X51Map;
238 num_channels = 6;
239 break;
241 case FmtX61:
242 chans = X61Map;
243 num_channels = 7;
244 break;
246 case FmtX71:
247 chans = X71Map;
248 num_channels = 8;
249 break;
252 if(DirectChannels != AL_FALSE)
254 for(c = 0;c < num_channels;c++)
255 SrcMatrix[c][chans[c].channel] += DryGain * ListenerGain;
257 else if(Device->Hrtf)
259 for(c = 0;c < num_channels;c++)
261 if(chans[c].channel == LFE)
263 /* Skip LFE */
264 ALSource->Params.HrtfDelay[c][0] = 0;
265 ALSource->Params.HrtfDelay[c][1] = 0;
266 for(i = 0;i < HRIR_LENGTH;i++)
268 ALSource->Params.HrtfCoeffs[c][i][0] = 0.0f;
269 ALSource->Params.HrtfCoeffs[c][i][1] = 0.0f;
272 else
274 /* Get the static HRIR coefficients and delays for this
275 * channel. */
276 GetLerpedHrtfCoeffs(Device->Hrtf,
277 0.0f, F_PI/180.0f * chans[c].angle,
278 DryGain*ListenerGain,
279 ALSource->Params.HrtfCoeffs[c],
280 ALSource->Params.HrtfDelay[c]);
282 ALSource->HrtfCounter = 0;
285 else
287 for(c = 0;c < num_channels;c++)
289 if(chans[c].channel == LFE) /* Special-case LFE */
291 SrcMatrix[c][LFE] += DryGain * ListenerGain;
292 continue;
294 pos = aluCart2LUTpos(aluCos(chans[c].angle), aluSin(chans[c].angle));
295 ChannelGain = Device->PanningLUT[pos];
297 for(i = 0;i < (ALint)Device->NumChan;i++)
299 enum Channel chan = Device->Speaker2Chan[i];
300 SrcMatrix[c][chan] += DryGain * ListenerGain *
301 ChannelGain[chan];
305 for(i = 0;i < NumSends;i++)
307 ALeffectslot *Slot = ALSource->Send[i].Slot;
309 if(!Slot && i == 0)
310 Slot = Device->DefaultSlot;
311 if(Slot && Slot->effect.type == AL_EFFECT_NULL)
312 Slot = NULL;
313 ALSource->Params.Send[i].Slot = Slot;
314 ALSource->Params.Send[i].WetGain = WetGain[i] * ListenerGain;
317 /* Update filter coefficients. Calculations based on the I3DL2
318 * spec. */
319 cw = aluCos(F_PI*2.0f * LOWPASSFREQREF / Frequency);
321 /* We use two chained one-pole filters, so we need to take the
322 * square root of the squared gain, which is the same as the base
323 * gain. */
324 ALSource->Params.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
325 for(i = 0;i < NumSends;i++)
327 /* We use a one-pole filter, so we need to take the squared gain */
328 ALfloat a = lpCoeffCalc(WetGainHF[i]*WetGainHF[i], cw);
329 ALSource->Params.Send[i].iirFilter.coeff = a;
333 ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
335 const ALCdevice *Device = ALContext->Device;
336 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
337 ALfloat Direction[3],Position[3],SourceToListener[3];
338 ALfloat Velocity[3],ListenerVel[3];
339 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
340 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
341 ALfloat DopplerFactor, DopplerVelocity, SpeedOfSound;
342 ALfloat AirAbsorptionFactor;
343 ALfloat RoomAirAbsorption[MAX_SENDS];
344 ALbufferlistitem *BufferListItem;
345 ALfloat Attenuation, EffectiveDist;
346 ALfloat RoomAttenuation[MAX_SENDS];
347 ALfloat MetersPerUnit;
348 ALfloat RoomRolloffBase;
349 ALfloat RoomRolloff[MAX_SENDS];
350 ALfloat DecayDistance[MAX_SENDS];
351 ALfloat DryGain;
352 ALfloat DryGainHF;
353 ALboolean DryGainHFAuto;
354 ALfloat WetGain[MAX_SENDS];
355 ALfloat WetGainHF[MAX_SENDS];
356 ALboolean WetGainAuto;
357 ALboolean WetGainHFAuto;
358 enum Resampler Resampler;
359 ALfloat Pitch;
360 ALuint Frequency;
361 ALint NumSends;
362 ALfloat cw;
363 ALint i;
365 DryGainHF = 1.0f;
366 for(i = 0;i < MAX_SENDS;i++)
367 WetGainHF[i] = 1.0f;
369 //Get context properties
370 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
371 DopplerVelocity = ALContext->DopplerVelocity;
372 SpeedOfSound = ALContext->flSpeedOfSound;
373 NumSends = Device->NumAuxSends;
374 Frequency = Device->Frequency;
376 //Get listener properties
377 ListenerGain = ALContext->Listener.Gain;
378 MetersPerUnit = ALContext->Listener.MetersPerUnit;
379 ListenerVel[0] = ALContext->Listener.Velocity[0];
380 ListenerVel[1] = ALContext->Listener.Velocity[1];
381 ListenerVel[2] = ALContext->Listener.Velocity[2];
383 //Get source properties
384 SourceVolume = ALSource->flGain;
385 MinVolume = ALSource->flMinGain;
386 MaxVolume = ALSource->flMaxGain;
387 Pitch = ALSource->flPitch;
388 Resampler = ALSource->Resampler;
389 Position[0] = ALSource->vPosition[0];
390 Position[1] = ALSource->vPosition[1];
391 Position[2] = ALSource->vPosition[2];
392 Direction[0] = ALSource->vOrientation[0];
393 Direction[1] = ALSource->vOrientation[1];
394 Direction[2] = ALSource->vOrientation[2];
395 Velocity[0] = ALSource->vVelocity[0];
396 Velocity[1] = ALSource->vVelocity[1];
397 Velocity[2] = ALSource->vVelocity[2];
398 MinDist = ALSource->flRefDistance;
399 MaxDist = ALSource->flMaxDistance;
400 Rolloff = ALSource->flRollOffFactor;
401 InnerAngle = ALSource->flInnerAngle * ConeScale;
402 OuterAngle = ALSource->flOuterAngle * ConeScale;
403 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
404 DryGainHFAuto = ALSource->DryGainHFAuto;
405 WetGainAuto = ALSource->WetGainAuto;
406 WetGainHFAuto = ALSource->WetGainHFAuto;
407 RoomRolloffBase = ALSource->RoomRolloffFactor;
408 for(i = 0;i < NumSends;i++)
410 ALeffectslot *Slot = ALSource->Send[i].Slot;
412 if(!Slot && i == 0)
413 Slot = Device->DefaultSlot;
414 if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
416 Slot = NULL;
417 RoomRolloff[i] = 0.0f;
418 DecayDistance[i] = 0.0f;
419 RoomAirAbsorption[i] = 1.0f;
421 else if(Slot->AuxSendAuto)
423 RoomRolloff[i] = RoomRolloffBase;
424 if(IsReverbEffect(Slot->effect.type))
426 RoomRolloff[i] += Slot->effect.Reverb.RoomRolloffFactor;
427 DecayDistance[i] = Slot->effect.Reverb.DecayTime *
428 SPEEDOFSOUNDMETRESPERSEC;
429 RoomAirAbsorption[i] = Slot->effect.Reverb.AirAbsorptionGainHF;
431 else
433 DecayDistance[i] = 0.0f;
434 RoomAirAbsorption[i] = 1.0f;
437 else
439 /* If the slot's auxiliary send auto is off, the data sent to the
440 * effect slot is the same as the dry path, sans filter effects */
441 RoomRolloff[i] = Rolloff;
442 DecayDistance[i] = 0.0f;
443 RoomAirAbsorption[i] = AIRABSORBGAINHF;
446 ALSource->Params.Send[i].Slot = Slot;
449 //1. Translate Listener to origin (convert to head relative)
450 if(ALSource->bHeadRelative == AL_FALSE)
452 ALfloat Matrix[4][4];
453 for(i = 0;i < 4;i++)
455 ALint i2;
456 for(i2 = 0;i2 < 4;i2++)
457 Matrix[i][i2] = ALContext->Listener.Matrix[i][i2];
460 /* Translate position */
461 Position[0] -= ALContext->Listener.Position[0];
462 Position[1] -= ALContext->Listener.Position[1];
463 Position[2] -= ALContext->Listener.Position[2];
465 /* Transform source vectors into listener space */
466 aluMatrixVector(Position, 1.0f, Matrix);
467 aluMatrixVector(Direction, 0.0f, Matrix);
468 aluMatrixVector(Velocity, 0.0f, Matrix);
470 else
472 ListenerVel[0] = 0.0f;
473 ListenerVel[1] = 0.0f;
474 ListenerVel[2] = 0.0f;
477 SourceToListener[0] = -Position[0];
478 SourceToListener[1] = -Position[1];
479 SourceToListener[2] = -Position[2];
480 aluNormalize(SourceToListener);
481 aluNormalize(Direction);
483 //2. Calculate distance attenuation
484 Distance = aluSqrt(aluDotproduct(Position, Position));
485 ClampedDist = Distance;
487 Attenuation = 1.0f;
488 for(i = 0;i < NumSends;i++)
489 RoomAttenuation[i] = 1.0f;
490 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
491 ALContext->DistanceModel)
493 case InverseDistanceClamped:
494 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
495 if(MaxDist < MinDist)
496 break;
497 //fall-through
498 case InverseDistance:
499 if(MinDist > 0.0f)
501 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
502 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
503 for(i = 0;i < NumSends;i++)
505 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
506 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
509 break;
511 case LinearDistanceClamped:
512 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
513 if(MaxDist < MinDist)
514 break;
515 //fall-through
516 case LinearDistance:
517 if(MaxDist != MinDist)
519 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
520 Attenuation = maxf(Attenuation, 0.0f);
521 for(i = 0;i < NumSends;i++)
523 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
524 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
527 break;
529 case ExponentDistanceClamped:
530 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
531 if(MaxDist < MinDist)
532 break;
533 //fall-through
534 case ExponentDistance:
535 if(ClampedDist > 0.0f && MinDist > 0.0f)
537 Attenuation = aluPow(ClampedDist/MinDist, -Rolloff);
538 for(i = 0;i < NumSends;i++)
539 RoomAttenuation[i] = aluPow(ClampedDist/MinDist, -RoomRolloff[i]);
541 break;
543 case DisableDistance:
544 break;
547 // Source Gain + Attenuation
548 DryGain = SourceVolume * Attenuation;
549 for(i = 0;i < NumSends;i++)
550 WetGain[i] = SourceVolume * RoomAttenuation[i];
552 // Distance-based air absorption
553 EffectiveDist = 0.0f;
554 if(MinDist > 0.0f && Attenuation < 1.0f)
555 EffectiveDist = (MinDist/Attenuation - MinDist)*MetersPerUnit;
556 if(AirAbsorptionFactor > 0.0f && EffectiveDist > 0.0f)
558 DryGainHF *= aluPow(AIRABSORBGAINHF, AirAbsorptionFactor*EffectiveDist);
559 for(i = 0;i < NumSends;i++)
560 WetGainHF[i] *= aluPow(RoomAirAbsorption[i],
561 AirAbsorptionFactor*EffectiveDist);
564 if(WetGainAuto)
566 /* Apply a decay-time transformation to the wet path, based on the
567 * attenuation of the dry path.
569 * Using the approximate (effective) source to listener distance, the
570 * initial decay of the reverb effect is calculated and applied to the
571 * wet path.
573 for(i = 0;i < NumSends;i++)
575 if(DecayDistance[i] > 0.0f)
576 WetGain[i] *= aluPow(0.001f /* -60dB */,
577 EffectiveDist / DecayDistance[i]);
581 /* Calculate directional soundcones */
582 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * (180.0f/F_PI);
583 if(Angle >= InnerAngle && Angle <= OuterAngle)
585 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
586 ConeVolume = lerp(1.0f, ALSource->flOuterGain, scale);
587 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
589 else if(Angle > OuterAngle)
591 ConeVolume = ALSource->flOuterGain;
592 ConeHF = ALSource->OuterGainHF;
594 else
596 ConeVolume = 1.0f;
597 ConeHF = 1.0f;
600 DryGain *= ConeVolume;
601 if(WetGainAuto)
603 for(i = 0;i < NumSends;i++)
604 WetGain[i] *= ConeVolume;
606 if(DryGainHFAuto)
607 DryGainHF *= ConeHF;
608 if(WetGainHFAuto)
610 for(i = 0;i < NumSends;i++)
611 WetGainHF[i] *= ConeHF;
614 // Clamp to Min/Max Gain
615 DryGain = clampf(DryGain, MinVolume, MaxVolume);
616 for(i = 0;i < NumSends;i++)
617 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
619 // Apply filter gains and filters
620 DryGain *= ALSource->DirectGain * ListenerGain;
621 DryGainHF *= ALSource->DirectGainHF;
622 for(i = 0;i < NumSends;i++)
624 WetGain[i] *= ALSource->Send[i].WetGain * ListenerGain;
625 WetGainHF[i] *= ALSource->Send[i].WetGainHF;
628 // Calculate Velocity
629 if(DopplerFactor != 0.0f)
631 ALfloat VSS, VLS;
632 ALfloat MaxVelocity = (SpeedOfSound*DopplerVelocity) /
633 DopplerFactor;
635 VSS = aluDotproduct(Velocity, SourceToListener);
636 if(VSS >= MaxVelocity)
637 VSS = (MaxVelocity - 1.0f);
638 else if(VSS <= -MaxVelocity)
639 VSS = -MaxVelocity + 1.0f;
641 VLS = aluDotproduct(ListenerVel, SourceToListener);
642 if(VLS >= MaxVelocity)
643 VLS = (MaxVelocity - 1.0f);
644 else if(VLS <= -MaxVelocity)
645 VLS = -MaxVelocity + 1.0f;
647 Pitch *= ((SpeedOfSound*DopplerVelocity) - (DopplerFactor*VLS)) /
648 ((SpeedOfSound*DopplerVelocity) - (DopplerFactor*VSS));
651 BufferListItem = ALSource->queue;
652 while(BufferListItem != NULL)
654 ALbuffer *ALBuffer;
655 if((ALBuffer=BufferListItem->buffer) != NULL)
657 ALsizei maxstep = STACK_DATA_SIZE/sizeof(ALfloat) /
658 ALSource->NumChannels;
659 maxstep -= ResamplerPadding[Resampler] +
660 ResamplerPrePadding[Resampler] + 1;
661 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
663 Pitch = Pitch * ALBuffer->Frequency / Frequency;
664 if(Pitch > (ALfloat)maxstep)
665 ALSource->Params.Step = maxstep<<FRACTIONBITS;
666 else
668 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
669 if(ALSource->Params.Step == 0)
670 ALSource->Params.Step = 1;
673 break;
675 BufferListItem = BufferListItem->next;
677 if(Device->Hrtf)
678 ALSource->Params.DoMix = SelectHrtfMixer((ALSource->Params.Step==FRACTIONONE) ?
679 PointResampler : Resampler);
680 else
681 ALSource->Params.DoMix = SelectMixer((ALSource->Params.Step==FRACTIONONE) ?
682 PointResampler : Resampler);
684 if(Device->Hrtf)
686 // Use a binaural HRTF algorithm for stereo headphone playback
687 ALfloat delta, ev = 0.0f, az = 0.0f;
689 if(Distance > 0.0f)
691 ALfloat invlen = 1.0f/Distance;
692 Position[0] *= invlen;
693 Position[1] *= invlen;
694 Position[2] *= invlen;
696 // Calculate elevation and azimuth only when the source is not at
697 // the listener. This prevents +0 and -0 Z from producing
698 // inconsistent panning.
699 ev = aluAsin(Position[1]);
700 az = aluAtan2(Position[0], -Position[2]*ZScale);
703 // Check to see if the HRIR is already moving.
704 if(ALSource->HrtfMoving)
706 // Calculate the normalized HRTF transition factor (delta).
707 delta = CalcHrtfDelta(ALSource->Params.HrtfGain, DryGain,
708 ALSource->Params.HrtfDir, Position);
709 // If the delta is large enough, get the moving HRIR target
710 // coefficients, target delays, steppping values, and counter.
711 if(delta > 0.001f)
713 ALSource->HrtfCounter = GetMovingHrtfCoeffs(Device->Hrtf,
714 ev, az, DryGain, delta,
715 ALSource->HrtfCounter,
716 ALSource->Params.HrtfCoeffs[0],
717 ALSource->Params.HrtfDelay[0],
718 ALSource->Params.HrtfCoeffStep,
719 ALSource->Params.HrtfDelayStep);
720 ALSource->Params.HrtfGain = DryGain;
721 ALSource->Params.HrtfDir[0] = Position[0];
722 ALSource->Params.HrtfDir[1] = Position[1];
723 ALSource->Params.HrtfDir[2] = Position[2];
726 else
728 // Get the initial (static) HRIR coefficients and delays.
729 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
730 ALSource->Params.HrtfCoeffs[0],
731 ALSource->Params.HrtfDelay[0]);
732 ALSource->HrtfCounter = 0;
733 ALSource->Params.HrtfGain = DryGain;
734 ALSource->Params.HrtfDir[0] = Position[0];
735 ALSource->Params.HrtfDir[1] = Position[1];
736 ALSource->Params.HrtfDir[2] = Position[2];
739 else
741 // Use energy-preserving panning algorithm for multi-speaker playback
742 ALfloat DirGain, AmbientGain;
743 const ALfloat *ChannelGain;
744 ALfloat length;
745 ALint pos;
747 length = maxf(Distance, MinDist);
748 if(length > 0.0f)
750 ALfloat invlen = 1.0f/length;
751 Position[0] *= invlen;
752 Position[1] *= invlen;
753 Position[2] *= invlen;
756 pos = aluCart2LUTpos(-Position[2]*ZScale, Position[0]);
757 ChannelGain = Device->PanningLUT[pos];
759 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
760 // elevation adjustment for directional gain. this sucks, but
761 // has low complexity
762 AmbientGain = aluSqrt(1.0f/Device->NumChan);
763 for(i = 0;i < MAXCHANNELS;i++)
765 ALuint i2;
766 for(i2 = 0;i2 < MAXCHANNELS;i2++)
767 ALSource->Params.DryGains[i][i2] = 0.0f;
769 for(i = 0;i < (ALint)Device->NumChan;i++)
771 enum Channel chan = Device->Speaker2Chan[i];
772 ALfloat gain = lerp(AmbientGain, ChannelGain[chan], DirGain);
773 ALSource->Params.DryGains[0][chan] = DryGain * gain;
776 for(i = 0;i < NumSends;i++)
777 ALSource->Params.Send[i].WetGain = WetGain[i];
779 /* Update filter coefficients. */
780 cw = aluCos(F_PI*2.0f * LOWPASSFREQREF / Frequency);
782 ALSource->Params.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
783 for(i = 0;i < NumSends;i++)
785 ALfloat a = lpCoeffCalc(WetGainHF[i]*WetGainHF[i], cw);
786 ALSource->Params.Send[i].iirFilter.coeff = a;
791 static __inline ALfloat aluF2F(ALfloat val)
792 { return val; }
793 static __inline ALshort aluF2S(ALfloat val)
795 if(val > 1.0f) return 32767;
796 if(val < -1.0f) return -32768;
797 return fastf2i(val*32767.0f);
799 static __inline ALushort aluF2US(ALfloat val)
800 { return aluF2S(val)+32768; }
801 static __inline ALbyte aluF2B(ALfloat val)
802 { return aluF2S(val)>>8; }
803 static __inline ALubyte aluF2UB(ALfloat val)
804 { return aluF2US(val)>>8; }
806 #define DECL_TEMPLATE(T, N, func) \
807 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
808 ALuint SamplesToDo) \
810 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
811 const enum Channel *ChanMap = device->DevChannels; \
812 ALuint i, j; \
814 for(i = 0;i < SamplesToDo;i++) \
816 for(j = 0;j < N;j++) \
817 *(buffer++) = func(DryBuffer[i][ChanMap[j]]); \
821 DECL_TEMPLATE(ALfloat, 1, aluF2F)
822 DECL_TEMPLATE(ALfloat, 4, aluF2F)
823 DECL_TEMPLATE(ALfloat, 6, aluF2F)
824 DECL_TEMPLATE(ALfloat, 7, aluF2F)
825 DECL_TEMPLATE(ALfloat, 8, aluF2F)
827 DECL_TEMPLATE(ALushort, 1, aluF2US)
828 DECL_TEMPLATE(ALushort, 4, aluF2US)
829 DECL_TEMPLATE(ALushort, 6, aluF2US)
830 DECL_TEMPLATE(ALushort, 7, aluF2US)
831 DECL_TEMPLATE(ALushort, 8, aluF2US)
833 DECL_TEMPLATE(ALshort, 1, aluF2S)
834 DECL_TEMPLATE(ALshort, 4, aluF2S)
835 DECL_TEMPLATE(ALshort, 6, aluF2S)
836 DECL_TEMPLATE(ALshort, 7, aluF2S)
837 DECL_TEMPLATE(ALshort, 8, aluF2S)
839 DECL_TEMPLATE(ALubyte, 1, aluF2UB)
840 DECL_TEMPLATE(ALubyte, 4, aluF2UB)
841 DECL_TEMPLATE(ALubyte, 6, aluF2UB)
842 DECL_TEMPLATE(ALubyte, 7, aluF2UB)
843 DECL_TEMPLATE(ALubyte, 8, aluF2UB)
845 DECL_TEMPLATE(ALbyte, 1, aluF2B)
846 DECL_TEMPLATE(ALbyte, 4, aluF2B)
847 DECL_TEMPLATE(ALbyte, 6, aluF2B)
848 DECL_TEMPLATE(ALbyte, 7, aluF2B)
849 DECL_TEMPLATE(ALbyte, 8, aluF2B)
851 #undef DECL_TEMPLATE
853 #define DECL_TEMPLATE(T, N, func) \
854 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
855 ALuint SamplesToDo) \
857 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
858 const enum Channel *ChanMap = device->DevChannels; \
859 ALuint i, j; \
861 if(device->Bs2b) \
863 for(i = 0;i < SamplesToDo;i++) \
865 float samples[2]; \
866 samples[0] = DryBuffer[i][ChanMap[0]]; \
867 samples[1] = DryBuffer[i][ChanMap[1]]; \
868 bs2b_cross_feed(device->Bs2b, samples); \
869 *(buffer++) = func(samples[0]); \
870 *(buffer++) = func(samples[1]); \
873 else \
875 for(i = 0;i < SamplesToDo;i++) \
877 for(j = 0;j < N;j++) \
878 *(buffer++) = func(DryBuffer[i][ChanMap[j]]); \
883 DECL_TEMPLATE(ALfloat, 2, aluF2F)
884 DECL_TEMPLATE(ALushort, 2, aluF2US)
885 DECL_TEMPLATE(ALshort, 2, aluF2S)
886 DECL_TEMPLATE(ALubyte, 2, aluF2UB)
887 DECL_TEMPLATE(ALbyte, 2, aluF2B)
889 #undef DECL_TEMPLATE
891 #define DECL_TEMPLATE(T) \
892 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
894 switch(device->FmtChans) \
896 case DevFmtMono: \
897 Write_##T##_1(device, buffer, SamplesToDo); \
898 break; \
899 case DevFmtStereo: \
900 Write_##T##_2(device, buffer, SamplesToDo); \
901 break; \
902 case DevFmtQuad: \
903 Write_##T##_4(device, buffer, SamplesToDo); \
904 break; \
905 case DevFmtX51: \
906 case DevFmtX51Side: \
907 Write_##T##_6(device, buffer, SamplesToDo); \
908 break; \
909 case DevFmtX61: \
910 Write_##T##_7(device, buffer, SamplesToDo); \
911 break; \
912 case DevFmtX71: \
913 Write_##T##_8(device, buffer, SamplesToDo); \
914 break; \
918 DECL_TEMPLATE(ALfloat)
919 DECL_TEMPLATE(ALushort)
920 DECL_TEMPLATE(ALshort)
921 DECL_TEMPLATE(ALubyte)
922 DECL_TEMPLATE(ALbyte)
924 #undef DECL_TEMPLATE
926 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
928 ALuint SamplesToDo;
929 ALeffectslot **slot, **slot_end;
930 ALsource **src, **src_end;
931 ALCcontext *ctx;
932 int fpuState;
933 ALuint i, c;
935 fpuState = SetMixerFPUMode();
937 while(size > 0)
939 /* Setup variables */
940 SamplesToDo = minu(size, BUFFERSIZE);
942 /* Clear mixing buffer */
943 memset(device->DryBuffer, 0, SamplesToDo*MAXCHANNELS*sizeof(ALfloat));
945 LockDevice(device);
946 ctx = device->ContextList;
947 while(ctx)
949 ALenum DeferUpdates = ctx->DeferUpdates;
950 ALenum UpdateSources = AL_FALSE;
952 if(!DeferUpdates)
953 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
955 src = ctx->ActiveSources;
956 src_end = src + ctx->ActiveSourceCount;
957 while(src != src_end)
959 if((*src)->state != AL_PLAYING)
961 --(ctx->ActiveSourceCount);
962 *src = *(--src_end);
963 continue;
966 if(!DeferUpdates && (ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) ||
967 UpdateSources))
968 ALsource_Update(*src, ctx);
970 MixSource(*src, device, SamplesToDo);
971 src++;
974 /* effect slot processing */
975 slot = ctx->ActiveEffectSlots;
976 slot_end = slot + ctx->ActiveEffectSlotCount;
977 while(slot != slot_end)
979 for(c = 0;c < SamplesToDo;c++)
981 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
982 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
984 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
985 (*slot)->PendingClicks[0] = 0.0f;
987 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
988 ALeffectState_Update((*slot)->EffectState, ctx, *slot);
990 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
991 (*slot)->WetBuffer, device->DryBuffer);
993 for(i = 0;i < SamplesToDo;i++)
994 (*slot)->WetBuffer[i] = 0.0f;
996 slot++;
999 ctx = ctx->next;
1002 slot = &device->DefaultSlot;
1003 if(*slot != NULL)
1005 for(c = 0;c < SamplesToDo;c++)
1007 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
1008 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
1010 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
1011 (*slot)->PendingClicks[0] = 0.0f;
1013 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1014 ALeffectState_Update((*slot)->EffectState, ctx, *slot);
1016 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
1017 (*slot)->WetBuffer, device->DryBuffer);
1019 for(i = 0;i < SamplesToDo;i++)
1020 (*slot)->WetBuffer[i] = 0.0f;
1022 UnlockDevice(device);
1024 //Post processing loop
1025 if(device->FmtChans == DevFmtMono)
1027 for(i = 0;i < SamplesToDo;i++)
1029 device->DryBuffer[i][FRONT_CENTER] += device->ClickRemoval[FRONT_CENTER];
1030 device->ClickRemoval[FRONT_CENTER] -= device->ClickRemoval[FRONT_CENTER] * (1.0f/256.0f);
1032 device->ClickRemoval[FRONT_CENTER] += device->PendingClicks[FRONT_CENTER];
1033 device->PendingClicks[FRONT_CENTER] = 0.0f;
1035 else if(device->FmtChans == DevFmtStereo)
1037 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1038 for(i = 0;i < SamplesToDo;i++)
1040 for(c = 0;c < 2;c++)
1042 device->DryBuffer[i][c] += device->ClickRemoval[c];
1043 device->ClickRemoval[c] -= device->ClickRemoval[c] * (1.0f/256.0f);
1046 for(c = 0;c < 2;c++)
1048 device->ClickRemoval[c] += device->PendingClicks[c];
1049 device->PendingClicks[c] = 0.0f;
1052 else
1054 for(i = 0;i < SamplesToDo;i++)
1056 for(c = 0;c < MAXCHANNELS;c++)
1058 device->DryBuffer[i][c] += device->ClickRemoval[c];
1059 device->ClickRemoval[c] -= device->ClickRemoval[c] * (1.0f/256.0f);
1062 for(c = 0;c < MAXCHANNELS;c++)
1064 device->ClickRemoval[c] += device->PendingClicks[c];
1065 device->PendingClicks[c] = 0.0f;
1069 if(buffer)
1071 switch(device->FmtType)
1073 case DevFmtByte:
1074 Write_ALbyte(device, buffer, SamplesToDo);
1075 break;
1076 case DevFmtUByte:
1077 Write_ALubyte(device, buffer, SamplesToDo);
1078 break;
1079 case DevFmtShort:
1080 Write_ALshort(device, buffer, SamplesToDo);
1081 break;
1082 case DevFmtUShort:
1083 Write_ALushort(device, buffer, SamplesToDo);
1084 break;
1085 case DevFmtFloat:
1086 Write_ALfloat(device, buffer, SamplesToDo);
1087 break;
1091 size -= SamplesToDo;
1094 RestoreFPUMode(fpuState);
1098 ALvoid aluHandleDisconnect(ALCdevice *device)
1100 ALCcontext *Context;
1102 LockDevice(device);
1103 device->Connected = ALC_FALSE;
1105 Context = device->ContextList;
1106 while(Context)
1108 ALsource **src, **src_end;
1110 src = Context->ActiveSources;
1111 src_end = src + Context->ActiveSourceCount;
1112 while(src != src_end)
1114 if((*src)->state == AL_PLAYING)
1116 (*src)->state = AL_STOPPED;
1117 (*src)->BuffersPlayed = (*src)->BuffersInQueue;
1118 (*src)->position = 0;
1119 (*src)->position_fraction = 0;
1121 src++;
1123 Context->ActiveSourceCount = 0;
1125 Context = Context->next;
1127 UnlockDevice(device);