Fix copying the listener Up vector
[openal-soft.git] / Alc / ALu.c
blob43653048abc20f11dff4ecb6cde62bdfa3906c0c
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 "alSource.h"
31 #include "alBuffer.h"
32 #include "alListener.h"
33 #include "alAuxEffectSlot.h"
34 #include "alu.h"
35 #include "bs2b.h"
37 #include "mixer_defs.h"
40 struct ChanMap {
41 enum Channel channel;
42 ALfloat angle;
45 /* Cone scalar */
46 ALfloat ConeScale = 1.0f;
48 /* Localized Z scalar for mono sources */
49 ALfloat ZScale = 1.0f;
52 static ResamplerFunc SelectResampler(enum Resampler Resampler, ALuint increment)
54 if(increment == FRACTIONONE)
55 return Resample_copy32_C;
56 switch(Resampler)
58 case PointResampler:
59 return Resample_point32_C;
60 case LinearResampler:
61 return Resample_lerp32_C;
62 case CubicResampler:
63 return Resample_cubic32_C;
64 case ResamplerMax:
65 /* Shouldn't happen */
66 break;
69 return Resample_point32_C;
73 static DryMixerFunc SelectHrtfMixer(void)
75 #ifdef HAVE_SSE
76 if((CPUCapFlags&CPU_CAP_SSE))
77 return MixDirect_Hrtf_SSE;
78 #endif
79 #ifdef HAVE_NEON
80 if((CPUCapFlags&CPU_CAP_NEON))
81 return MixDirect_Hrtf_Neon;
82 #endif
84 return MixDirect_Hrtf_C;
87 static DryMixerFunc SelectDirectMixer(void)
89 #ifdef HAVE_SSE
90 if((CPUCapFlags&CPU_CAP_SSE))
91 return MixDirect_SSE;
92 #endif
94 return MixDirect_C;
97 static WetMixerFunc SelectSendMixer(void)
99 #ifdef HAVE_SSE
100 if((CPUCapFlags&CPU_CAP_SSE))
101 return MixSend_SSE;
102 #endif
104 return MixSend_C;
108 static __inline ALvoid aluMatrixVector(ALfloat *vector, ALfloat w, ALfloat (*RESTRICT matrix)[4])
110 ALfloat temp[4] = {
111 vector[0], vector[1], vector[2], w
114 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
115 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
116 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
120 static ALvoid CalcListenerParams(ALlistener *Listener)
122 ALfloat N[3], V[3], U[3], P[3];
124 /* AT then UP */
125 N[0] = Listener->Forward[0];
126 N[1] = Listener->Forward[1];
127 N[2] = Listener->Forward[2];
128 aluNormalize(N);
129 V[0] = Listener->Up[0];
130 V[1] = Listener->Up[1];
131 V[2] = Listener->Up[2];
132 aluNormalize(V);
133 /* Build and normalize right-vector */
134 aluCrossproduct(N, V, U);
135 aluNormalize(U);
137 Listener->Params.Matrix[0][0] = U[0];
138 Listener->Params.Matrix[0][1] = V[0];
139 Listener->Params.Matrix[0][2] = -N[0];
140 Listener->Params.Matrix[0][3] = 0.0f;
141 Listener->Params.Matrix[1][0] = U[1];
142 Listener->Params.Matrix[1][1] = V[1];
143 Listener->Params.Matrix[1][2] = -N[1];
144 Listener->Params.Matrix[1][3] = 0.0f;
145 Listener->Params.Matrix[2][0] = U[2];
146 Listener->Params.Matrix[2][1] = V[2];
147 Listener->Params.Matrix[2][2] = -N[2];
148 Listener->Params.Matrix[2][3] = 0.0f;
149 Listener->Params.Matrix[3][0] = 0.0f;
150 Listener->Params.Matrix[3][1] = 0.0f;
151 Listener->Params.Matrix[3][2] = 0.0f;
152 Listener->Params.Matrix[3][3] = 1.0f;
154 P[0] = Listener->Position[0];
155 P[1] = Listener->Position[1];
156 P[2] = Listener->Position[2];
157 aluMatrixVector(P, 1.0f, Listener->Params.Matrix);
158 Listener->Params.Matrix[3][0] = -P[0];
159 Listener->Params.Matrix[3][1] = -P[1];
160 Listener->Params.Matrix[3][2] = -P[2];
162 Listener->Params.Velocity[0] = Listener->Velocity[0];
163 Listener->Params.Velocity[1] = Listener->Velocity[1];
164 Listener->Params.Velocity[2] = Listener->Velocity[2];
165 aluMatrixVector(Listener->Params.Velocity, 0.0f, Listener->Params.Matrix);
168 ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
170 static const struct ChanMap MonoMap[1] = { { FrontCenter, 0.0f } };
171 static const struct ChanMap StereoMap[2] = {
172 { FrontLeft, -30.0f * F_PI/180.0f },
173 { FrontRight, 30.0f * F_PI/180.0f }
175 static const struct ChanMap StereoWideMap[2] = {
176 { FrontLeft, -90.0f * F_PI/180.0f },
177 { FrontRight, 90.0f * F_PI/180.0f }
179 static const struct ChanMap RearMap[2] = {
180 { BackLeft, -150.0f * F_PI/180.0f },
181 { BackRight, 150.0f * F_PI/180.0f }
183 static const struct ChanMap QuadMap[4] = {
184 { FrontLeft, -45.0f * F_PI/180.0f },
185 { FrontRight, 45.0f * F_PI/180.0f },
186 { BackLeft, -135.0f * F_PI/180.0f },
187 { BackRight, 135.0f * F_PI/180.0f }
189 static const struct ChanMap X51Map[6] = {
190 { FrontLeft, -30.0f * F_PI/180.0f },
191 { FrontRight, 30.0f * F_PI/180.0f },
192 { FrontCenter, 0.0f * F_PI/180.0f },
193 { LFE, 0.0f },
194 { BackLeft, -110.0f * F_PI/180.0f },
195 { BackRight, 110.0f * F_PI/180.0f }
197 static const struct ChanMap X61Map[7] = {
198 { FrontLeft, -30.0f * F_PI/180.0f },
199 { FrontRight, 30.0f * F_PI/180.0f },
200 { FrontCenter, 0.0f * F_PI/180.0f },
201 { LFE, 0.0f },
202 { BackCenter, 180.0f * F_PI/180.0f },
203 { SideLeft, -90.0f * F_PI/180.0f },
204 { SideRight, 90.0f * F_PI/180.0f }
206 static const struct ChanMap X71Map[8] = {
207 { FrontLeft, -30.0f * F_PI/180.0f },
208 { FrontRight, 30.0f * F_PI/180.0f },
209 { FrontCenter, 0.0f * F_PI/180.0f },
210 { LFE, 0.0f },
211 { BackLeft, -150.0f * F_PI/180.0f },
212 { BackRight, 150.0f * F_PI/180.0f },
213 { SideLeft, -90.0f * F_PI/180.0f },
214 { SideRight, 90.0f * F_PI/180.0f }
217 ALCdevice *Device = ALContext->Device;
218 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
219 ALbufferlistitem *BufferListItem;
220 enum FmtChannels Channels;
221 ALfloat (*SrcMatrix)[MaxChannels];
222 ALfloat DryGain, DryGainHF;
223 ALfloat WetGain[MAX_SENDS];
224 ALfloat WetGainHF[MAX_SENDS];
225 ALint NumSends, Frequency;
226 const struct ChanMap *chans = NULL;
227 enum Resampler Resampler;
228 ALint num_channels = 0;
229 ALboolean DirectChannels;
230 ALfloat hwidth = 0.0f;
231 ALfloat Pitch;
232 ALfloat cw;
233 ALint i, c;
235 /* Get device properties */
236 NumSends = Device->NumAuxSends;
237 Frequency = Device->Frequency;
239 /* Get listener properties */
240 ListenerGain = ALContext->Listener->Gain;
242 /* Get source properties */
243 SourceVolume = ALSource->Gain;
244 MinVolume = ALSource->MinGain;
245 MaxVolume = ALSource->MaxGain;
246 Pitch = ALSource->Pitch;
247 Resampler = ALSource->Resampler;
248 DirectChannels = ALSource->DirectChannels;
250 /* Calculate the stepping value */
251 Channels = FmtMono;
252 BufferListItem = ALSource->queue;
253 while(BufferListItem != NULL)
255 ALbuffer *ALBuffer;
256 if((ALBuffer=BufferListItem->buffer) != NULL)
258 ALsizei maxstep = BUFFERSIZE;
259 maxstep -= ResamplerPadding[Resampler] +
260 ResamplerPrePadding[Resampler] + 1;
261 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
263 Pitch = Pitch * ALBuffer->Frequency / Frequency;
264 if(Pitch > (ALfloat)maxstep)
265 ALSource->Params.Step = maxstep<<FRACTIONBITS;
266 else
268 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
269 if(ALSource->Params.Step == 0)
270 ALSource->Params.Step = 1;
272 ALSource->Params.Resample = SelectResampler(Resampler, ALSource->Params.Step);
274 Channels = ALBuffer->FmtChannels;
275 break;
277 BufferListItem = BufferListItem->next;
279 if(!DirectChannels && Device->Hrtf)
280 ALSource->Params.DryMix = SelectHrtfMixer();
281 else
282 ALSource->Params.DryMix = SelectDirectMixer();
283 ALSource->Params.WetMix = SelectSendMixer();
285 /* Calculate gains */
286 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
287 DryGain *= ALSource->DirectGain * ListenerGain;
288 DryGainHF = ALSource->DirectGainHF;
289 for(i = 0;i < NumSends;i++)
291 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
292 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
293 WetGainHF[i] = ALSource->Send[i].GainHF;
296 SrcMatrix = ALSource->Params.Direct.Gains;
297 for(i = 0;i < MaxChannels;i++)
299 for(c = 0;c < MaxChannels;c++)
300 SrcMatrix[i][c] = 0.0f;
302 switch(Channels)
304 case FmtMono:
305 chans = MonoMap;
306 num_channels = 1;
307 break;
309 case FmtStereo:
310 if(!(Device->Flags&DEVICE_WIDE_STEREO))
311 chans = StereoMap;
312 else
314 chans = StereoWideMap;
315 hwidth = 60.0f * F_PI/180.0f;
317 num_channels = 2;
318 break;
320 case FmtRear:
321 chans = RearMap;
322 num_channels = 2;
323 break;
325 case FmtQuad:
326 chans = QuadMap;
327 num_channels = 4;
328 break;
330 case FmtX51:
331 chans = X51Map;
332 num_channels = 6;
333 break;
335 case FmtX61:
336 chans = X61Map;
337 num_channels = 7;
338 break;
340 case FmtX71:
341 chans = X71Map;
342 num_channels = 8;
343 break;
346 if(DirectChannels != AL_FALSE)
348 for(c = 0;c < num_channels;c++)
350 for(i = 0;i < (ALint)Device->NumChan;i++)
352 enum Channel chan = Device->Speaker2Chan[i];
353 if(chan == chans[c].channel)
355 SrcMatrix[c][chan] += DryGain;
356 break;
361 else if(Device->Hrtf)
363 for(c = 0;c < num_channels;c++)
365 if(chans[c].channel == LFE)
367 /* Skip LFE */
368 ALSource->Params.Direct.Hrtf.Delay[c][0] = 0;
369 ALSource->Params.Direct.Hrtf.Delay[c][1] = 0;
370 for(i = 0;i < HRIR_LENGTH;i++)
372 ALSource->Params.Direct.Hrtf.Coeffs[c][i][0] = 0.0f;
373 ALSource->Params.Direct.Hrtf.Coeffs[c][i][1] = 0.0f;
376 else
378 /* Get the static HRIR coefficients and delays for this
379 * channel. */
380 GetLerpedHrtfCoeffs(Device->Hrtf,
381 0.0f, chans[c].angle, DryGain,
382 ALSource->Params.Direct.Hrtf.Coeffs[c],
383 ALSource->Params.Direct.Hrtf.Delay[c]);
386 ALSource->Hrtf.Counter = 0;
388 else
390 DryGain *= lerp(1.0f, 1.0f/sqrtf(Device->NumChan), hwidth/F_PI);
391 for(c = 0;c < num_channels;c++)
393 /* Special-case LFE */
394 if(chans[c].channel == LFE)
396 SrcMatrix[c][chans[c].channel] = DryGain;
397 continue;
399 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain,
400 SrcMatrix[c]);
403 for(i = 0;i < NumSends;i++)
405 ALeffectslot *Slot = ALSource->Send[i].Slot;
407 if(!Slot && i == 0)
408 Slot = Device->DefaultSlot;
409 if(Slot && Slot->effect.type == AL_EFFECT_NULL)
410 Slot = NULL;
411 ALSource->Params.Send[i].Slot = Slot;
412 ALSource->Params.Send[i].Gain = WetGain[i];
415 /* Update filter coefficients. Calculations based on the I3DL2
416 * spec. */
417 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / Frequency);
419 /* We use two chained one-pole filters, so we need to take the
420 * square root of the squared gain, which is the same as the base
421 * gain. */
422 ALSource->Params.Direct.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
423 for(i = 0;i < NumSends;i++)
425 ALfloat a = lpCoeffCalc(WetGainHF[i], cw);
426 ALSource->Params.Send[i].iirFilter.coeff = a;
430 ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
432 const ALCdevice *Device = ALContext->Device;
433 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
434 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
435 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
436 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
437 ALfloat DopplerFactor, SpeedOfSound;
438 ALfloat AirAbsorptionFactor;
439 ALfloat RoomAirAbsorption[MAX_SENDS];
440 ALbufferlistitem *BufferListItem;
441 ALfloat Attenuation;
442 ALfloat RoomAttenuation[MAX_SENDS];
443 ALfloat MetersPerUnit;
444 ALfloat RoomRolloffBase;
445 ALfloat RoomRolloff[MAX_SENDS];
446 ALfloat DecayDistance[MAX_SENDS];
447 ALfloat DryGain;
448 ALfloat DryGainHF;
449 ALboolean DryGainHFAuto;
450 ALfloat WetGain[MAX_SENDS];
451 ALfloat WetGainHF[MAX_SENDS];
452 ALboolean WetGainAuto;
453 ALboolean WetGainHFAuto;
454 enum Resampler Resampler;
455 ALfloat Pitch;
456 ALuint Frequency;
457 ALint NumSends;
458 ALfloat cw;
459 ALint i, j;
461 DryGainHF = 1.0f;
462 for(i = 0;i < MAX_SENDS;i++)
463 WetGainHF[i] = 1.0f;
465 /* Get context/device properties */
466 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
467 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
468 NumSends = Device->NumAuxSends;
469 Frequency = Device->Frequency;
471 /* Get listener properties */
472 ListenerGain = ALContext->Listener->Gain;
473 MetersPerUnit = ALContext->Listener->MetersPerUnit;
475 /* Get source properties */
476 SourceVolume = ALSource->Gain;
477 MinVolume = ALSource->MinGain;
478 MaxVolume = ALSource->MaxGain;
479 Pitch = ALSource->Pitch;
480 Resampler = ALSource->Resampler;
481 Position[0] = ALSource->Position[0];
482 Position[1] = ALSource->Position[1];
483 Position[2] = ALSource->Position[2];
484 Direction[0] = ALSource->Orientation[0];
485 Direction[1] = ALSource->Orientation[1];
486 Direction[2] = ALSource->Orientation[2];
487 Velocity[0] = ALSource->Velocity[0];
488 Velocity[1] = ALSource->Velocity[1];
489 Velocity[2] = ALSource->Velocity[2];
490 MinDist = ALSource->RefDistance;
491 MaxDist = ALSource->MaxDistance;
492 Rolloff = ALSource->RollOffFactor;
493 InnerAngle = ALSource->InnerAngle;
494 OuterAngle = ALSource->OuterAngle;
495 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
496 DryGainHFAuto = ALSource->DryGainHFAuto;
497 WetGainAuto = ALSource->WetGainAuto;
498 WetGainHFAuto = ALSource->WetGainHFAuto;
499 RoomRolloffBase = ALSource->RoomRolloffFactor;
500 for(i = 0;i < NumSends;i++)
502 ALeffectslot *Slot = ALSource->Send[i].Slot;
504 if(!Slot && i == 0)
505 Slot = Device->DefaultSlot;
506 if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
508 Slot = NULL;
509 RoomRolloff[i] = 0.0f;
510 DecayDistance[i] = 0.0f;
511 RoomAirAbsorption[i] = 1.0f;
513 else if(Slot->AuxSendAuto)
515 RoomRolloff[i] = RoomRolloffBase;
516 if(IsReverbEffect(Slot->effect.type))
518 RoomRolloff[i] += Slot->effect.Reverb.RoomRolloffFactor;
519 DecayDistance[i] = Slot->effect.Reverb.DecayTime *
520 SPEEDOFSOUNDMETRESPERSEC;
521 RoomAirAbsorption[i] = Slot->effect.Reverb.AirAbsorptionGainHF;
523 else
525 DecayDistance[i] = 0.0f;
526 RoomAirAbsorption[i] = 1.0f;
529 else
531 /* If the slot's auxiliary send auto is off, the data sent to the
532 * effect slot is the same as the dry path, sans filter effects */
533 RoomRolloff[i] = Rolloff;
534 DecayDistance[i] = 0.0f;
535 RoomAirAbsorption[i] = AIRABSORBGAINHF;
538 ALSource->Params.Send[i].Slot = Slot;
541 /* Transform source to listener space (convert to head relative) */
542 if(ALSource->HeadRelative == AL_FALSE)
544 ALfloat (*RESTRICT Matrix)[4] = ALContext->Listener->Params.Matrix;
545 /* Transform source vectors */
546 aluMatrixVector(Position, 1.0f, Matrix);
547 aluMatrixVector(Direction, 0.0f, Matrix);
548 aluMatrixVector(Velocity, 0.0f, Matrix);
550 else
552 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
553 /* Offset the source velocity to be relative of the listener velocity */
554 Velocity[0] += ListenerVel[0];
555 Velocity[1] += ListenerVel[1];
556 Velocity[2] += ListenerVel[2];
559 SourceToListener[0] = -Position[0];
560 SourceToListener[1] = -Position[1];
561 SourceToListener[2] = -Position[2];
562 aluNormalize(SourceToListener);
563 aluNormalize(Direction);
565 /* Calculate distance attenuation */
566 Distance = sqrtf(aluDotproduct(Position, Position));
567 ClampedDist = Distance;
569 Attenuation = 1.0f;
570 for(i = 0;i < NumSends;i++)
571 RoomAttenuation[i] = 1.0f;
572 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
573 ALContext->DistanceModel)
575 case InverseDistanceClamped:
576 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
577 if(MaxDist < MinDist)
578 break;
579 /*fall-through*/
580 case InverseDistance:
581 if(MinDist > 0.0f)
583 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
584 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
585 for(i = 0;i < NumSends;i++)
587 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
588 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
591 break;
593 case LinearDistanceClamped:
594 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
595 if(MaxDist < MinDist)
596 break;
597 /*fall-through*/
598 case LinearDistance:
599 if(MaxDist != MinDist)
601 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
602 Attenuation = maxf(Attenuation, 0.0f);
603 for(i = 0;i < NumSends;i++)
605 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
606 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
609 break;
611 case ExponentDistanceClamped:
612 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
613 if(MaxDist < MinDist)
614 break;
615 /*fall-through*/
616 case ExponentDistance:
617 if(ClampedDist > 0.0f && MinDist > 0.0f)
619 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
620 for(i = 0;i < NumSends;i++)
621 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
623 break;
625 case DisableDistance:
626 ClampedDist = MinDist;
627 break;
630 /* Source Gain + Attenuation */
631 DryGain = SourceVolume * Attenuation;
632 for(i = 0;i < NumSends;i++)
633 WetGain[i] = SourceVolume * RoomAttenuation[i];
635 /* Distance-based air absorption */
636 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
638 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
639 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
640 for(i = 0;i < NumSends;i++)
641 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
644 if(WetGainAuto)
646 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
648 /* Apply a decay-time transformation to the wet path, based on the
649 * attenuation of the dry path.
651 * Using the apparent distance, based on the distance attenuation, the
652 * initial decay of the reverb effect is calculated and applied to the
653 * wet path.
655 for(i = 0;i < NumSends;i++)
657 if(DecayDistance[i] > 0.0f)
658 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
662 /* Calculate directional soundcones */
663 Angle = acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale * (360.0f/F_PI);
664 if(Angle > InnerAngle && Angle <= OuterAngle)
666 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
667 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
668 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
670 else if(Angle > OuterAngle)
672 ConeVolume = ALSource->OuterGain;
673 ConeHF = ALSource->OuterGainHF;
675 else
677 ConeVolume = 1.0f;
678 ConeHF = 1.0f;
681 DryGain *= ConeVolume;
682 if(WetGainAuto)
684 for(i = 0;i < NumSends;i++)
685 WetGain[i] *= ConeVolume;
687 if(DryGainHFAuto)
688 DryGainHF *= ConeHF;
689 if(WetGainHFAuto)
691 for(i = 0;i < NumSends;i++)
692 WetGainHF[i] *= ConeHF;
695 /* Clamp to Min/Max Gain */
696 DryGain = clampf(DryGain, MinVolume, MaxVolume);
697 for(i = 0;i < NumSends;i++)
698 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
700 /* Apply gain and frequency filters */
701 DryGain *= ALSource->DirectGain * ListenerGain;
702 DryGainHF *= ALSource->DirectGainHF;
703 for(i = 0;i < NumSends;i++)
705 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
706 WetGainHF[i] *= ALSource->Send[i].GainHF;
709 /* Calculate velocity-based doppler effect */
710 if(DopplerFactor > 0.0f)
712 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
713 ALfloat VSS, VLS;
715 if(SpeedOfSound < 1.0f)
717 DopplerFactor *= 1.0f/SpeedOfSound;
718 SpeedOfSound = 1.0f;
721 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
722 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
724 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
725 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
728 BufferListItem = ALSource->queue;
729 while(BufferListItem != NULL)
731 ALbuffer *ALBuffer;
732 if((ALBuffer=BufferListItem->buffer) != NULL)
734 /* Calculate fixed-point stepping value, based on the pitch, buffer
735 * frequency, and output frequency. */
736 ALsizei maxstep = BUFFERSIZE;
737 maxstep -= ResamplerPadding[Resampler] +
738 ResamplerPrePadding[Resampler] + 1;
739 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
741 Pitch = Pitch * ALBuffer->Frequency / Frequency;
742 if(Pitch > (ALfloat)maxstep)
743 ALSource->Params.Step = maxstep<<FRACTIONBITS;
744 else
746 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
747 if(ALSource->Params.Step == 0)
748 ALSource->Params.Step = 1;
750 ALSource->Params.Resample = SelectResampler(Resampler, ALSource->Params.Step);
752 break;
754 BufferListItem = BufferListItem->next;
756 if(Device->Hrtf)
757 ALSource->Params.DryMix = SelectHrtfMixer();
758 else
759 ALSource->Params.DryMix = SelectDirectMixer();
760 ALSource->Params.WetMix = SelectSendMixer();
762 if(Device->Hrtf)
764 /* Use a binaural HRTF algorithm for stereo headphone playback */
765 ALfloat delta, ev = 0.0f, az = 0.0f;
767 if(Distance > FLT_EPSILON)
769 ALfloat invlen = 1.0f/Distance;
770 Position[0] *= invlen;
771 Position[1] *= invlen;
772 Position[2] *= invlen;
774 /* Calculate elevation and azimuth only when the source is not at
775 * the listener. This prevents +0 and -0 Z from producing
776 * inconsistent panning. Also, clamp Y in case FP precision errors
777 * cause it to land outside of -1..+1. */
778 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
779 az = atan2f(Position[0], -Position[2]*ZScale);
782 /* Check to see if the HRIR is already moving. */
783 if(ALSource->Hrtf.Moving)
785 /* Calculate the normalized HRTF transition factor (delta). */
786 delta = CalcHrtfDelta(ALSource->Params.Direct.Hrtf.Gain, DryGain,
787 ALSource->Params.Direct.Hrtf.Dir, Position);
788 /* If the delta is large enough, get the moving HRIR target
789 * coefficients, target delays, steppping values, and counter. */
790 if(delta > 0.001f)
792 ALSource->Hrtf.Counter = GetMovingHrtfCoeffs(Device->Hrtf,
793 ev, az, DryGain, delta,
794 ALSource->Hrtf.Counter,
795 ALSource->Params.Direct.Hrtf.Coeffs[0],
796 ALSource->Params.Direct.Hrtf.Delay[0],
797 ALSource->Params.Direct.Hrtf.CoeffStep,
798 ALSource->Params.Direct.Hrtf.DelayStep);
799 ALSource->Params.Direct.Hrtf.Gain = DryGain;
800 ALSource->Params.Direct.Hrtf.Dir[0] = Position[0];
801 ALSource->Params.Direct.Hrtf.Dir[1] = Position[1];
802 ALSource->Params.Direct.Hrtf.Dir[2] = Position[2];
805 else
807 /* Get the initial (static) HRIR coefficients and delays. */
808 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
809 ALSource->Params.Direct.Hrtf.Coeffs[0],
810 ALSource->Params.Direct.Hrtf.Delay[0]);
811 ALSource->Hrtf.Counter = 0;
812 ALSource->Hrtf.Moving = AL_TRUE;
813 ALSource->Params.Direct.Hrtf.Gain = DryGain;
814 ALSource->Params.Direct.Hrtf.Dir[0] = Position[0];
815 ALSource->Params.Direct.Hrtf.Dir[1] = Position[1];
816 ALSource->Params.Direct.Hrtf.Dir[2] = Position[2];
819 else
821 ALfloat (*Matrix)[MaxChannels] = ALSource->Params.Direct.Gains;
822 ALfloat DirGain = 0.0f;
823 ALfloat AmbientGain;
825 for(i = 0;i < MaxChannels;i++)
827 for(j = 0;j < MaxChannels;j++)
828 Matrix[i][j] = 0.0f;
831 /* Normalize the length, and compute panned gains. */
832 if(Distance > FLT_EPSILON)
834 ALfloat invlen = 1.0f/Distance;
835 Position[0] *= invlen;
836 Position[1] *= invlen;
837 Position[2] *= invlen;
839 DirGain = sqrtf(Position[0]*Position[0] + Position[2]*Position[2]);
840 ComputeAngleGains(Device, atan2f(Position[0], -Position[2]*ZScale), 0.0f,
841 DryGain*DirGain, Matrix[0]);
844 /* Adjustment for vertical offsets. Not the greatest, but simple
845 * enough. */
846 AmbientGain = DryGain * sqrtf(1.0f/Device->NumChan) * (1.0f-DirGain);
847 for(i = 0;i < (ALint)Device->NumChan;i++)
849 enum Channel chan = Device->Speaker2Chan[i];
850 Matrix[0][chan] = maxf(Matrix[0][chan], AmbientGain);
853 for(i = 0;i < NumSends;i++)
854 ALSource->Params.Send[i].Gain = WetGain[i];
856 /* Update filter coefficients. */
857 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / Frequency);
859 ALSource->Params.Direct.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
860 for(i = 0;i < NumSends;i++)
862 ALfloat a = lpCoeffCalc(WetGainHF[i], cw);
863 ALSource->Params.Send[i].iirFilter.coeff = a;
868 static __inline ALfloat aluF2F(ALfloat val)
869 { return val; }
870 static __inline ALint aluF2I(ALfloat val)
872 if(val > 1.0f) return 2147483647;
873 if(val < -1.0f) return -2147483647-1;
874 return fastf2i((ALfloat)(val*2147483647.0));
876 static __inline ALuint aluF2UI(ALfloat val)
877 { return aluF2I(val)+2147483648u; }
878 static __inline ALshort aluF2S(ALfloat val)
879 { return aluF2I(val)>>16; }
880 static __inline ALushort aluF2US(ALfloat val)
881 { return aluF2S(val)+32768; }
882 static __inline ALbyte aluF2B(ALfloat val)
883 { return aluF2I(val)>>24; }
884 static __inline ALubyte aluF2UB(ALfloat val)
885 { return aluF2B(val)+128; }
887 #define DECL_TEMPLATE(T, func) \
888 static int Write_##T(ALCdevice *device, T *RESTRICT buffer, \
889 ALuint SamplesToDo) \
891 ALfloat (*RESTRICT DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
892 ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
893 const enum Channel *ChanMap = device->DevChannels; \
894 ALuint i, j; \
896 for(j = 0;j < numchans;j++) \
898 T *RESTRICT out = buffer + j; \
899 enum Channel chan = ChanMap[j]; \
901 for(i = 0;i < SamplesToDo;i++) \
902 out[i*numchans] = func(DryBuffer[chan][i]); \
904 return SamplesToDo*numchans*sizeof(T); \
907 DECL_TEMPLATE(ALfloat, aluF2F)
908 DECL_TEMPLATE(ALuint, aluF2UI)
909 DECL_TEMPLATE(ALint, aluF2I)
910 DECL_TEMPLATE(ALushort, aluF2US)
911 DECL_TEMPLATE(ALshort, aluF2S)
912 DECL_TEMPLATE(ALubyte, aluF2UB)
913 DECL_TEMPLATE(ALbyte, aluF2B)
915 #undef DECL_TEMPLATE
918 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
920 ALuint SamplesToDo;
921 ALeffectslot **slot, **slot_end;
922 ALsource **src, **src_end;
923 ALCcontext *ctx;
924 FPUCtl oldMode;
925 ALuint i, c;
927 SetMixerFPUMode(&oldMode);
929 while(size > 0)
931 SamplesToDo = minu(size, BUFFERSIZE);
932 for(c = 0;c < MaxChannels;c++)
933 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
935 ALCdevice_Lock(device);
936 ctx = device->ContextList;
937 while(ctx)
939 ALenum DeferUpdates = ctx->DeferUpdates;
940 ALenum UpdateSources = AL_FALSE;
942 if(!DeferUpdates)
943 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
945 if(UpdateSources)
946 CalcListenerParams(ctx->Listener);
948 /* source processing */
949 src = ctx->ActiveSources;
950 src_end = src + ctx->ActiveSourceCount;
951 while(src != src_end)
953 if((*src)->state != AL_PLAYING)
955 --(ctx->ActiveSourceCount);
956 *src = *(--src_end);
957 continue;
960 if(!DeferUpdates && (ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) ||
961 UpdateSources))
962 ALsource_Update(*src, ctx);
964 MixSource(*src, device, SamplesToDo);
965 src++;
968 /* effect slot processing */
969 slot = ctx->ActiveEffectSlots;
970 slot_end = slot + ctx->ActiveEffectSlotCount;
971 while(slot != slot_end)
973 ALfloat offset = (*slot)->ClickRemoval[0];
974 if(offset < (1.0f/32768.0f))
975 offset = 0.0f;
976 else for(i = 0;i < SamplesToDo;i++)
978 (*slot)->WetBuffer[0][i] += offset;
979 offset -= offset * (1.0f/256.0f);
981 (*slot)->ClickRemoval[0] = offset + (*slot)->PendingClicks[0];
982 (*slot)->PendingClicks[0] = 0.0f;
984 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
985 ALeffectState_Update((*slot)->EffectState, device, *slot);
987 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
988 (*slot)->WetBuffer[0], device->DryBuffer);
990 for(i = 0;i < SamplesToDo;i++)
991 (*slot)->WetBuffer[0][i] = 0.0f;
993 slot++;
996 ctx = ctx->next;
999 slot = &device->DefaultSlot;
1000 if(*slot != NULL)
1002 ALfloat offset = (*slot)->ClickRemoval[0];
1003 if(offset < (1.0f/32768.0f))
1004 offset = 0.0f;
1005 else for(i = 0;i < SamplesToDo;i++)
1007 (*slot)->WetBuffer[0][i] += offset;
1008 offset -= offset * (1.0f/256.0f);
1010 (*slot)->ClickRemoval[0] = offset + (*slot)->PendingClicks[0];
1011 (*slot)->PendingClicks[0] = 0.0f;
1013 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1014 ALeffectState_Update((*slot)->EffectState, device, *slot);
1016 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
1017 (*slot)->WetBuffer[0], device->DryBuffer);
1019 for(i = 0;i < SamplesToDo;i++)
1020 (*slot)->WetBuffer[0][i] = 0.0f;
1022 ALCdevice_Unlock(device);
1024 /* Click-removal. Could do better; this only really handles immediate
1025 * changes between updates where a predictive sample could be
1026 * generated. Delays caused by effects and HRTF aren't caught. */
1027 if(device->FmtChans == DevFmtMono)
1029 ALfloat offset = device->ClickRemoval[FrontCenter];
1030 if(offset < (1.0f/32768.0f))
1031 offset = 0.0f;
1032 else for(i = 0;i < SamplesToDo;i++)
1034 device->DryBuffer[FrontCenter][i] += offset;
1035 offset -= offset * (1.0f/256.0f);
1037 device->ClickRemoval[FrontCenter] = offset + device->PendingClicks[FrontCenter];
1038 device->PendingClicks[FrontCenter] = 0.0f;
1040 else if(device->FmtChans == DevFmtStereo)
1042 /* Assumes the first two channels are FrontLeft and FrontRight */
1043 for(c = 0;c < 2;c++)
1045 ALfloat offset = device->ClickRemoval[c];
1046 if(offset < (1.0f/32768.0f))
1047 offset = 0.0f;
1048 else for(i = 0;i < SamplesToDo;i++)
1050 device->DryBuffer[c][i] += offset;
1051 offset -= offset * (1.0f/256.0f);
1053 device->ClickRemoval[c] = offset + device->PendingClicks[c];
1054 device->PendingClicks[c] = 0.0f;
1056 if(device->Bs2b)
1058 float samples[2];
1059 for(i = 0;i < SamplesToDo;i++)
1061 samples[0] = device->DryBuffer[FrontLeft][i];
1062 samples[1] = device->DryBuffer[FrontRight][i];
1063 bs2b_cross_feed(device->Bs2b, samples);
1064 device->DryBuffer[FrontLeft][i] = samples[0];
1065 device->DryBuffer[FrontRight][i] = samples[1];
1069 else
1071 for(c = 0;c < MaxChannels;c++)
1073 ALfloat offset = device->ClickRemoval[c];
1074 if(offset < (1.0f/32768.0f))
1075 offset = 0.0f;
1076 else for(i = 0;i < SamplesToDo;i++)
1078 device->DryBuffer[c][i] += offset;
1079 offset -= offset * (1.0f/256.0f);
1081 device->ClickRemoval[c] = offset + device->PendingClicks[c];
1082 device->PendingClicks[c] = 0.0f;
1086 if(buffer)
1088 int bytes = 0;
1089 switch(device->FmtType)
1091 case DevFmtByte:
1092 bytes = Write_ALbyte(device, buffer, SamplesToDo);
1093 break;
1094 case DevFmtUByte:
1095 bytes = Write_ALubyte(device, buffer, SamplesToDo);
1096 break;
1097 case DevFmtShort:
1098 bytes = Write_ALshort(device, buffer, SamplesToDo);
1099 break;
1100 case DevFmtUShort:
1101 bytes = Write_ALushort(device, buffer, SamplesToDo);
1102 break;
1103 case DevFmtInt:
1104 bytes = Write_ALint(device, buffer, SamplesToDo);
1105 break;
1106 case DevFmtUInt:
1107 bytes = Write_ALuint(device, buffer, SamplesToDo);
1108 break;
1109 case DevFmtFloat:
1110 bytes = Write_ALfloat(device, buffer, SamplesToDo);
1111 break;
1114 buffer = (ALubyte*)buffer + bytes;
1117 size -= SamplesToDo;
1120 RestoreFPUMode(&oldMode);
1124 ALvoid aluHandleDisconnect(ALCdevice *device)
1126 ALCcontext *Context;
1128 ALCdevice_Lock(device);
1129 device->Connected = ALC_FALSE;
1131 Context = device->ContextList;
1132 while(Context)
1134 ALsource **src, **src_end;
1136 src = Context->ActiveSources;
1137 src_end = src + Context->ActiveSourceCount;
1138 while(src != src_end)
1140 if((*src)->state == AL_PLAYING)
1142 (*src)->state = AL_STOPPED;
1143 (*src)->BuffersPlayed = (*src)->BuffersInQueue;
1144 (*src)->position = 0;
1145 (*src)->position_fraction = 0;
1147 src++;
1149 Context->ActiveSourceCount = 0;
1151 Context = Context->next;
1153 ALCdevice_Unlock(device);