Use a non-interleaved DryBuffer
[openal-soft.git] / Alc / ALu.c
blobb83e7df16f791c272c8cd72d6c69b4ccaeef9ff9
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 = 1.0f;
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] = { { FrontCenter, 0.0f } };
67 static const struct ChanMap StereoMap[2] = {
68 { FrontLeft, -30.0f * F_PI/180.0f },
69 { FrontRight, 30.0f * F_PI/180.0f }
71 static const struct ChanMap StereoWideMap[2] = {
72 { FrontLeft, -90.0f * F_PI/180.0f },
73 { FrontRight, 90.0f * F_PI/180.0f }
75 static const struct ChanMap RearMap[2] = {
76 { BackLeft, -150.0f * F_PI/180.0f },
77 { BackRight, 150.0f * F_PI/180.0f }
79 static const struct ChanMap QuadMap[4] = {
80 { FrontLeft, -45.0f * F_PI/180.0f },
81 { FrontRight, 45.0f * F_PI/180.0f },
82 { BackLeft, -135.0f * F_PI/180.0f },
83 { BackRight, 135.0f * F_PI/180.0f }
85 static const struct ChanMap X51Map[6] = {
86 { FrontLeft, -30.0f * F_PI/180.0f },
87 { FrontRight, 30.0f * F_PI/180.0f },
88 { FrontCenter, 0.0f * F_PI/180.0f },
89 { LFE, 0.0f },
90 { BackLeft, -110.0f * F_PI/180.0f },
91 { BackRight, 110.0f * F_PI/180.0f }
93 static const struct ChanMap X61Map[7] = {
94 { FrontLeft, -30.0f * F_PI/180.0f },
95 { FrontRight, 30.0f * F_PI/180.0f },
96 { FrontCenter, 0.0f * F_PI/180.0f },
97 { LFE, 0.0f },
98 { BackCenter, 180.0f * F_PI/180.0f },
99 { SideLeft, -90.0f * F_PI/180.0f },
100 { SideRight, 90.0f * F_PI/180.0f }
102 static const struct ChanMap X71Map[8] = {
103 { FrontLeft, -30.0f * F_PI/180.0f },
104 { FrontRight, 30.0f * F_PI/180.0f },
105 { FrontCenter, 0.0f * F_PI/180.0f },
106 { LFE, 0.0f },
107 { BackLeft, -150.0f * F_PI/180.0f },
108 { BackRight, 150.0f * F_PI/180.0f },
109 { SideLeft, -90.0f * F_PI/180.0f },
110 { SideRight, 90.0f * F_PI/180.0f }
113 ALCdevice *Device = ALContext->Device;
114 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
115 ALbufferlistitem *BufferListItem;
116 enum FmtChannels Channels;
117 ALfloat (*SrcMatrix)[MaxChannels];
118 ALfloat DryGain, DryGainHF;
119 ALfloat WetGain[MAX_SENDS];
120 ALfloat WetGainHF[MAX_SENDS];
121 ALint NumSends, Frequency;
122 const struct ChanMap *chans = NULL;
123 enum Resampler Resampler;
124 ALint num_channels = 0;
125 ALboolean DirectChannels;
126 ALfloat hwidth = 0.0f;
127 ALfloat Pitch;
128 ALfloat cw;
129 ALint i, c;
131 /* Get device properties */
132 NumSends = Device->NumAuxSends;
133 Frequency = Device->Frequency;
135 /* Get listener properties */
136 ListenerGain = ALContext->Listener.Gain;
138 /* Get source properties */
139 SourceVolume = ALSource->Gain;
140 MinVolume = ALSource->MinGain;
141 MaxVolume = ALSource->MaxGain;
142 Pitch = ALSource->Pitch;
143 Resampler = ALSource->Resampler;
144 DirectChannels = ALSource->DirectChannels;
146 /* Calculate the stepping value */
147 Channels = FmtMono;
148 BufferListItem = ALSource->queue;
149 while(BufferListItem != NULL)
151 ALbuffer *ALBuffer;
152 if((ALBuffer=BufferListItem->buffer) != NULL)
154 ALsizei maxstep = BUFFERSIZE / 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.DryMix = SelectHrtfMixer();
176 else
177 ALSource->Params.DryMix = SelectDirectMixer();
178 ALSource->Params.WetMix = SelectSendMixer();
180 /* Calculate gains */
181 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
182 DryGain *= ALSource->DirectGain * ListenerGain;
183 DryGainHF = ALSource->DirectGainHF;
184 for(i = 0;i < NumSends;i++)
186 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
187 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
188 WetGainHF[i] = ALSource->Send[i].GainHF;
191 SrcMatrix = ALSource->Params.Direct.Gains;
192 for(i = 0;i < MaxChannels;i++)
194 for(c = 0;c < MaxChannels;c++)
195 SrcMatrix[i][c] = 0.0f;
197 switch(Channels)
199 case FmtMono:
200 chans = MonoMap;
201 num_channels = 1;
202 break;
204 case FmtStereo:
205 if(!(Device->Flags&DEVICE_WIDE_STEREO))
206 chans = StereoMap;
207 else
209 chans = StereoWideMap;
210 hwidth = 60.0f * F_PI/180.0f;
212 num_channels = 2;
213 break;
215 case FmtRear:
216 chans = RearMap;
217 num_channels = 2;
218 break;
220 case FmtQuad:
221 chans = QuadMap;
222 num_channels = 4;
223 break;
225 case FmtX51:
226 chans = X51Map;
227 num_channels = 6;
228 break;
230 case FmtX61:
231 chans = X61Map;
232 num_channels = 7;
233 break;
235 case FmtX71:
236 chans = X71Map;
237 num_channels = 8;
238 break;
241 if(DirectChannels != AL_FALSE)
243 for(c = 0;c < num_channels;c++)
245 for(i = 0;i < (ALint)Device->NumChan;i++)
247 enum Channel chan = Device->Speaker2Chan[i];
248 if(chan == chans[c].channel)
250 SrcMatrix[c][chan] += DryGain;
251 break;
256 else if(Device->Hrtf)
258 for(c = 0;c < num_channels;c++)
260 if(chans[c].channel == LFE)
262 /* Skip LFE */
263 ALSource->Params.Direct.Hrtf.Delay[c][0] = 0;
264 ALSource->Params.Direct.Hrtf.Delay[c][1] = 0;
265 for(i = 0;i < HRIR_LENGTH;i++)
267 ALSource->Params.Direct.Hrtf.Coeffs[c][i][0] = 0.0f;
268 ALSource->Params.Direct.Hrtf.Coeffs[c][i][1] = 0.0f;
271 else
273 /* Get the static HRIR coefficients and delays for this
274 * channel. */
275 GetLerpedHrtfCoeffs(Device->Hrtf,
276 0.0f, chans[c].angle, DryGain,
277 ALSource->Params.Direct.Hrtf.Coeffs[c],
278 ALSource->Params.Direct.Hrtf.Delay[c]);
281 ALSource->Hrtf.Counter = 0;
283 else
285 DryGain *= lerp(1.0f, 1.0f/sqrtf(Device->NumChan), hwidth/(F_PI*2.0f));
286 for(c = 0;c < num_channels;c++)
288 /* Special-case LFE */
289 if(chans[c].channel == LFE)
291 SrcMatrix[c][chans[c].channel] = DryGain;
292 continue;
294 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain,
295 SrcMatrix[c]);
298 for(i = 0;i < NumSends;i++)
300 ALeffectslot *Slot = ALSource->Send[i].Slot;
302 if(!Slot && i == 0)
303 Slot = Device->DefaultSlot;
304 if(Slot && Slot->effect.type == AL_EFFECT_NULL)
305 Slot = NULL;
306 ALSource->Params.Send[i].Slot = Slot;
307 ALSource->Params.Send[i].Gain = WetGain[i];
310 /* Update filter coefficients. Calculations based on the I3DL2
311 * spec. */
312 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / Frequency);
314 /* We use two chained one-pole filters, so we need to take the
315 * square root of the squared gain, which is the same as the base
316 * gain. */
317 ALSource->Params.Direct.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
318 for(i = 0;i < NumSends;i++)
320 ALfloat a = lpCoeffCalc(WetGainHF[i], cw);
321 ALSource->Params.Send[i].iirFilter.coeff = a;
325 ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
327 const ALCdevice *Device = ALContext->Device;
328 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
329 ALfloat Direction[3],Position[3],SourceToListener[3];
330 ALfloat Velocity[3],ListenerVel[3];
331 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
332 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
333 ALfloat DopplerFactor, SpeedOfSound;
334 ALfloat AirAbsorptionFactor;
335 ALfloat RoomAirAbsorption[MAX_SENDS];
336 ALbufferlistitem *BufferListItem;
337 ALfloat Attenuation;
338 ALfloat RoomAttenuation[MAX_SENDS];
339 ALfloat MetersPerUnit;
340 ALfloat RoomRolloffBase;
341 ALfloat RoomRolloff[MAX_SENDS];
342 ALfloat DecayDistance[MAX_SENDS];
343 ALfloat DryGain;
344 ALfloat DryGainHF;
345 ALboolean DryGainHFAuto;
346 ALfloat WetGain[MAX_SENDS];
347 ALfloat WetGainHF[MAX_SENDS];
348 ALboolean WetGainAuto;
349 ALboolean WetGainHFAuto;
350 enum Resampler Resampler;
351 ALfloat Matrix[4][4];
352 ALfloat Pitch;
353 ALuint Frequency;
354 ALint NumSends;
355 ALfloat cw;
356 ALint i, j;
358 DryGainHF = 1.0f;
359 for(i = 0;i < MAX_SENDS;i++)
360 WetGainHF[i] = 1.0f;
362 /* Get context/device properties */
363 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
364 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
365 NumSends = Device->NumAuxSends;
366 Frequency = Device->Frequency;
368 /* Get listener properties */
369 ListenerGain = ALContext->Listener.Gain;
370 MetersPerUnit = ALContext->Listener.MetersPerUnit;
371 ListenerVel[0] = ALContext->Listener.Velocity[0];
372 ListenerVel[1] = ALContext->Listener.Velocity[1];
373 ListenerVel[2] = ALContext->Listener.Velocity[2];
374 for(i = 0;i < 4;i++)
376 for(j = 0;j < 4;j++)
377 Matrix[i][j] = ALContext->Listener.Matrix[i][j];
380 /* Get source properties */
381 SourceVolume = ALSource->Gain;
382 MinVolume = ALSource->MinGain;
383 MaxVolume = ALSource->MaxGain;
384 Pitch = ALSource->Pitch;
385 Resampler = ALSource->Resampler;
386 Position[0] = ALSource->Position[0];
387 Position[1] = ALSource->Position[1];
388 Position[2] = ALSource->Position[2];
389 Direction[0] = ALSource->Orientation[0];
390 Direction[1] = ALSource->Orientation[1];
391 Direction[2] = ALSource->Orientation[2];
392 Velocity[0] = ALSource->Velocity[0];
393 Velocity[1] = ALSource->Velocity[1];
394 Velocity[2] = ALSource->Velocity[2];
395 MinDist = ALSource->RefDistance;
396 MaxDist = ALSource->MaxDistance;
397 Rolloff = ALSource->RollOffFactor;
398 InnerAngle = ALSource->InnerAngle;
399 OuterAngle = ALSource->OuterAngle;
400 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
401 DryGainHFAuto = ALSource->DryGainHFAuto;
402 WetGainAuto = ALSource->WetGainAuto;
403 WetGainHFAuto = ALSource->WetGainHFAuto;
404 RoomRolloffBase = ALSource->RoomRolloffFactor;
405 for(i = 0;i < NumSends;i++)
407 ALeffectslot *Slot = ALSource->Send[i].Slot;
409 if(!Slot && i == 0)
410 Slot = Device->DefaultSlot;
411 if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
413 Slot = NULL;
414 RoomRolloff[i] = 0.0f;
415 DecayDistance[i] = 0.0f;
416 RoomAirAbsorption[i] = 1.0f;
418 else if(Slot->AuxSendAuto)
420 RoomRolloff[i] = RoomRolloffBase;
421 if(IsReverbEffect(Slot->effect.type))
423 RoomRolloff[i] += Slot->effect.Reverb.RoomRolloffFactor;
424 DecayDistance[i] = Slot->effect.Reverb.DecayTime *
425 SPEEDOFSOUNDMETRESPERSEC;
426 RoomAirAbsorption[i] = Slot->effect.Reverb.AirAbsorptionGainHF;
428 else
430 DecayDistance[i] = 0.0f;
431 RoomAirAbsorption[i] = 1.0f;
434 else
436 /* If the slot's auxiliary send auto is off, the data sent to the
437 * effect slot is the same as the dry path, sans filter effects */
438 RoomRolloff[i] = Rolloff;
439 DecayDistance[i] = 0.0f;
440 RoomAirAbsorption[i] = AIRABSORBGAINHF;
443 ALSource->Params.Send[i].Slot = Slot;
446 /* Transform source to listener space (convert to head relative) */
447 if(ALSource->HeadRelative == AL_FALSE)
449 /* Translate position */
450 Position[0] -= ALContext->Listener.Position[0];
451 Position[1] -= ALContext->Listener.Position[1];
452 Position[2] -= ALContext->Listener.Position[2];
454 /* Transform source vectors */
455 aluMatrixVector(Position, 1.0f, Matrix);
456 aluMatrixVector(Direction, 0.0f, Matrix);
457 aluMatrixVector(Velocity, 0.0f, Matrix);
458 /* Transform listener velocity */
459 aluMatrixVector(ListenerVel, 0.0f, Matrix);
461 else
463 /* Transform listener velocity from world space to listener space */
464 aluMatrixVector(ListenerVel, 0.0f, Matrix);
465 /* Offset the source velocity to be relative of the listener velocity */
466 Velocity[0] += ListenerVel[0];
467 Velocity[1] += ListenerVel[1];
468 Velocity[2] += ListenerVel[2];
471 SourceToListener[0] = -Position[0];
472 SourceToListener[1] = -Position[1];
473 SourceToListener[2] = -Position[2];
474 aluNormalize(SourceToListener);
475 aluNormalize(Direction);
477 /* Calculate distance attenuation */
478 Distance = sqrtf(aluDotproduct(Position, Position));
479 ClampedDist = Distance;
481 Attenuation = 1.0f;
482 for(i = 0;i < NumSends;i++)
483 RoomAttenuation[i] = 1.0f;
484 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
485 ALContext->DistanceModel)
487 case InverseDistanceClamped:
488 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
489 if(MaxDist < MinDist)
490 break;
491 /*fall-through*/
492 case InverseDistance:
493 if(MinDist > 0.0f)
495 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
496 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
497 for(i = 0;i < NumSends;i++)
499 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
500 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
503 break;
505 case LinearDistanceClamped:
506 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
507 if(MaxDist < MinDist)
508 break;
509 /*fall-through*/
510 case LinearDistance:
511 if(MaxDist != MinDist)
513 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
514 Attenuation = maxf(Attenuation, 0.0f);
515 for(i = 0;i < NumSends;i++)
517 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
518 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
521 break;
523 case ExponentDistanceClamped:
524 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
525 if(MaxDist < MinDist)
526 break;
527 /*fall-through*/
528 case ExponentDistance:
529 if(ClampedDist > 0.0f && MinDist > 0.0f)
531 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
532 for(i = 0;i < NumSends;i++)
533 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
535 break;
537 case DisableDistance:
538 ClampedDist = MinDist;
539 break;
542 /* Source Gain + Attenuation */
543 DryGain = SourceVolume * Attenuation;
544 for(i = 0;i < NumSends;i++)
545 WetGain[i] = SourceVolume * RoomAttenuation[i];
547 /* Distance-based air absorption */
548 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
550 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
551 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
552 for(i = 0;i < NumSends;i++)
553 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
556 if(WetGainAuto)
558 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
560 /* Apply a decay-time transformation to the wet path, based on the
561 * attenuation of the dry path.
563 * Using the apparent distance, based on the distance attenuation, the
564 * initial decay of the reverb effect is calculated and applied to the
565 * wet path.
567 for(i = 0;i < NumSends;i++)
569 if(DecayDistance[i] > 0.0f)
570 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
574 /* Calculate directional soundcones */
575 Angle = acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale * (360.0f/F_PI);
576 if(Angle > InnerAngle && Angle <= OuterAngle)
578 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
579 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
580 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
582 else if(Angle > OuterAngle)
584 ConeVolume = ALSource->OuterGain;
585 ConeHF = ALSource->OuterGainHF;
587 else
589 ConeVolume = 1.0f;
590 ConeHF = 1.0f;
593 DryGain *= ConeVolume;
594 if(WetGainAuto)
596 for(i = 0;i < NumSends;i++)
597 WetGain[i] *= ConeVolume;
599 if(DryGainHFAuto)
600 DryGainHF *= ConeHF;
601 if(WetGainHFAuto)
603 for(i = 0;i < NumSends;i++)
604 WetGainHF[i] *= ConeHF;
607 /* Clamp to Min/Max Gain */
608 DryGain = clampf(DryGain, MinVolume, MaxVolume);
609 for(i = 0;i < NumSends;i++)
610 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
612 /* Apply gain and frequency filters */
613 DryGain *= ALSource->DirectGain * ListenerGain;
614 DryGainHF *= ALSource->DirectGainHF;
615 for(i = 0;i < NumSends;i++)
617 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
618 WetGainHF[i] *= ALSource->Send[i].GainHF;
621 /* Calculate velocity-based doppler effect */
622 if(DopplerFactor > 0.0f)
624 ALfloat VSS, VLS;
626 if(SpeedOfSound < 1.0f)
628 DopplerFactor *= 1.0f/SpeedOfSound;
629 SpeedOfSound = 1.0f;
632 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
633 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
635 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
636 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
639 BufferListItem = ALSource->queue;
640 while(BufferListItem != NULL)
642 ALbuffer *ALBuffer;
643 if((ALBuffer=BufferListItem->buffer) != NULL)
645 /* Calculate fixed-point stepping value, based on the pitch, buffer
646 * frequency, and output frequency. */
647 ALsizei maxstep = BUFFERSIZE / ALSource->NumChannels;
648 maxstep -= ResamplerPadding[Resampler] +
649 ResamplerPrePadding[Resampler] + 1;
650 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
652 Pitch = Pitch * ALBuffer->Frequency / Frequency;
653 if(Pitch > (ALfloat)maxstep)
654 ALSource->Params.Step = maxstep<<FRACTIONBITS;
655 else
657 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
658 if(ALSource->Params.Step == 0)
659 ALSource->Params.Step = 1;
662 break;
664 BufferListItem = BufferListItem->next;
666 if(Device->Hrtf)
667 ALSource->Params.DryMix = SelectHrtfMixer();
668 else
669 ALSource->Params.DryMix = SelectDirectMixer();
670 ALSource->Params.WetMix = SelectSendMixer();
672 if(Device->Hrtf)
674 /* Use a binaural HRTF algorithm for stereo headphone playback */
675 ALfloat delta, ev = 0.0f, az = 0.0f;
677 if(Distance > 0.0f)
679 ALfloat invlen = 1.0f/Distance;
680 Position[0] *= invlen;
681 Position[1] *= invlen;
682 Position[2] *= invlen;
684 /* Calculate elevation and azimuth only when the source is not at
685 * the listener. This prevents +0 and -0 Z from producing
686 * inconsistent panning. Also, clamp Y in case FP precision errors
687 * cause it to land outside of -1..+1. */
688 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
689 az = atan2f(Position[0], -Position[2]*ZScale);
692 /* Check to see if the HRIR is already moving. */
693 if(ALSource->Hrtf.Moving)
695 /* Calculate the normalized HRTF transition factor (delta). */
696 delta = CalcHrtfDelta(ALSource->Params.Direct.Hrtf.Gain, DryGain,
697 ALSource->Params.Direct.Hrtf.Dir, Position);
698 /* If the delta is large enough, get the moving HRIR target
699 * coefficients, target delays, steppping values, and counter. */
700 if(delta > 0.001f)
702 ALSource->Hrtf.Counter = GetMovingHrtfCoeffs(Device->Hrtf,
703 ev, az, DryGain, delta,
704 ALSource->Hrtf.Counter,
705 ALSource->Params.Direct.Hrtf.Coeffs[0],
706 ALSource->Params.Direct.Hrtf.Delay[0],
707 ALSource->Params.Direct.Hrtf.CoeffStep,
708 ALSource->Params.Direct.Hrtf.DelayStep);
709 ALSource->Params.Direct.Hrtf.Gain = DryGain;
710 ALSource->Params.Direct.Hrtf.Dir[0] = Position[0];
711 ALSource->Params.Direct.Hrtf.Dir[1] = Position[1];
712 ALSource->Params.Direct.Hrtf.Dir[2] = Position[2];
715 else
717 /* Get the initial (static) HRIR coefficients and delays. */
718 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
719 ALSource->Params.Direct.Hrtf.Coeffs[0],
720 ALSource->Params.Direct.Hrtf.Delay[0]);
721 ALSource->Hrtf.Counter = 0;
722 ALSource->Params.Direct.Hrtf.Gain = DryGain;
723 ALSource->Params.Direct.Hrtf.Dir[0] = Position[0];
724 ALSource->Params.Direct.Hrtf.Dir[1] = Position[1];
725 ALSource->Params.Direct.Hrtf.Dir[2] = Position[2];
728 else
730 ALfloat (*Matrix)[MaxChannels] = ALSource->Params.Direct.Gains;
731 ALfloat DirGain = 0.0f;
732 ALfloat AmbientGain;
734 for(i = 0;i < MaxChannels;i++)
736 for(j = 0;j < MaxChannels;j++)
737 Matrix[i][j] = 0.0f;
740 /* Normalize the length, and compute panned gains. */
741 if(Distance > 0.0f)
743 ALfloat invlen = 1.0f/Distance;
744 Position[0] *= invlen;
745 Position[1] *= invlen;
746 Position[2] *= invlen;
748 DirGain = sqrtf(Position[0]*Position[0] + Position[2]*Position[2]);
749 ComputeAngleGains(Device, atan2f(Position[0], -Position[2]*ZScale), 0.0f,
750 DryGain*DirGain, Matrix[0]);
753 /* Adjustment for vertical offsets. Not the greatest, but simple
754 * enough. */
755 AmbientGain = DryGain * sqrtf(1.0f/Device->NumChan) * (1.0f-DirGain);
756 for(i = 0;i < (ALint)Device->NumChan;i++)
758 enum Channel chan = Device->Speaker2Chan[i];
759 Matrix[0][chan] = maxf(Matrix[0][chan], AmbientGain);
762 for(i = 0;i < NumSends;i++)
763 ALSource->Params.Send[i].Gain = WetGain[i];
765 /* Update filter coefficients. */
766 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / Frequency);
768 ALSource->Params.Direct.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
769 for(i = 0;i < NumSends;i++)
771 ALfloat a = lpCoeffCalc(WetGainHF[i], cw);
772 ALSource->Params.Send[i].iirFilter.coeff = a;
777 static __inline ALfloat aluF2F(ALfloat val)
778 { return val; }
779 static __inline ALint aluF2I(ALfloat val)
781 if(val > 1.0f) return 2147483647;
782 if(val < -1.0f) return -2147483647-1;
783 return fastf2i((ALfloat)(val*2147483647.0));
785 static __inline ALuint aluF2UI(ALfloat val)
786 { return aluF2I(val)+2147483648u; }
787 static __inline ALshort aluF2S(ALfloat val)
788 { return aluF2I(val)>>16; }
789 static __inline ALushort aluF2US(ALfloat val)
790 { return aluF2S(val)+32768; }
791 static __inline ALbyte aluF2B(ALfloat val)
792 { return aluF2I(val)>>24; }
793 static __inline ALubyte aluF2UB(ALfloat val)
794 { return aluF2B(val)+128; }
796 #define DECL_TEMPLATE(T, N, func) \
797 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
798 ALuint SamplesToDo) \
800 ALfloat (*RESTRICT DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
801 const enum Channel *ChanMap = device->DevChannels; \
802 ALuint i, j; \
804 for(j = 0;j < N;j++) \
806 T *RESTRICT out = buffer + j; \
807 enum Channel chan = ChanMap[j]; \
809 for(i = 0;i < SamplesToDo;i++) \
810 out[i*N] = func(DryBuffer[chan][i]); \
814 DECL_TEMPLATE(ALfloat, 1, aluF2F)
815 DECL_TEMPLATE(ALfloat, 2, aluF2F)
816 DECL_TEMPLATE(ALfloat, 4, aluF2F)
817 DECL_TEMPLATE(ALfloat, 6, aluF2F)
818 DECL_TEMPLATE(ALfloat, 7, aluF2F)
819 DECL_TEMPLATE(ALfloat, 8, aluF2F)
821 DECL_TEMPLATE(ALuint, 1, aluF2UI)
822 DECL_TEMPLATE(ALuint, 2, aluF2UI)
823 DECL_TEMPLATE(ALuint, 4, aluF2UI)
824 DECL_TEMPLATE(ALuint, 6, aluF2UI)
825 DECL_TEMPLATE(ALuint, 7, aluF2UI)
826 DECL_TEMPLATE(ALuint, 8, aluF2UI)
828 DECL_TEMPLATE(ALint, 1, aluF2I)
829 DECL_TEMPLATE(ALint, 2, aluF2I)
830 DECL_TEMPLATE(ALint, 4, aluF2I)
831 DECL_TEMPLATE(ALint, 6, aluF2I)
832 DECL_TEMPLATE(ALint, 7, aluF2I)
833 DECL_TEMPLATE(ALint, 8, aluF2I)
835 DECL_TEMPLATE(ALushort, 1, aluF2US)
836 DECL_TEMPLATE(ALushort, 2, aluF2US)
837 DECL_TEMPLATE(ALushort, 4, aluF2US)
838 DECL_TEMPLATE(ALushort, 6, aluF2US)
839 DECL_TEMPLATE(ALushort, 7, aluF2US)
840 DECL_TEMPLATE(ALushort, 8, aluF2US)
842 DECL_TEMPLATE(ALshort, 1, aluF2S)
843 DECL_TEMPLATE(ALshort, 2, aluF2S)
844 DECL_TEMPLATE(ALshort, 4, aluF2S)
845 DECL_TEMPLATE(ALshort, 6, aluF2S)
846 DECL_TEMPLATE(ALshort, 7, aluF2S)
847 DECL_TEMPLATE(ALshort, 8, aluF2S)
849 DECL_TEMPLATE(ALubyte, 1, aluF2UB)
850 DECL_TEMPLATE(ALubyte, 2, aluF2UB)
851 DECL_TEMPLATE(ALubyte, 4, aluF2UB)
852 DECL_TEMPLATE(ALubyte, 6, aluF2UB)
853 DECL_TEMPLATE(ALubyte, 7, aluF2UB)
854 DECL_TEMPLATE(ALubyte, 8, aluF2UB)
856 DECL_TEMPLATE(ALbyte, 1, aluF2B)
857 DECL_TEMPLATE(ALbyte, 2, aluF2B)
858 DECL_TEMPLATE(ALbyte, 4, aluF2B)
859 DECL_TEMPLATE(ALbyte, 6, aluF2B)
860 DECL_TEMPLATE(ALbyte, 7, aluF2B)
861 DECL_TEMPLATE(ALbyte, 8, aluF2B)
863 #undef DECL_TEMPLATE
865 #define DECL_TEMPLATE(T) \
866 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
868 switch(device->FmtChans) \
870 case DevFmtMono: \
871 Write_##T##_1(device, buffer, SamplesToDo); \
872 break; \
873 case DevFmtStereo: \
874 Write_##T##_2(device, buffer, SamplesToDo); \
875 break; \
876 case DevFmtQuad: \
877 Write_##T##_4(device, buffer, SamplesToDo); \
878 break; \
879 case DevFmtX51: \
880 case DevFmtX51Side: \
881 Write_##T##_6(device, buffer, SamplesToDo); \
882 break; \
883 case DevFmtX61: \
884 Write_##T##_7(device, buffer, SamplesToDo); \
885 break; \
886 case DevFmtX71: \
887 Write_##T##_8(device, buffer, SamplesToDo); \
888 break; \
892 DECL_TEMPLATE(ALfloat)
893 DECL_TEMPLATE(ALuint)
894 DECL_TEMPLATE(ALint)
895 DECL_TEMPLATE(ALushort)
896 DECL_TEMPLATE(ALshort)
897 DECL_TEMPLATE(ALubyte)
898 DECL_TEMPLATE(ALbyte)
900 #undef DECL_TEMPLATE
902 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
904 ALuint SamplesToDo;
905 ALeffectslot **slot, **slot_end;
906 ALsource **src, **src_end;
907 ALCcontext *ctx;
908 int fpuState;
909 ALuint i, c;
911 fpuState = SetMixerFPUMode();
913 while(size > 0)
915 SamplesToDo = minu(size, BUFFERSIZE);
916 for(c = 0;c < MaxChannels;c++)
917 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
919 ALCdevice_Lock(device);
920 ctx = device->ContextList;
921 while(ctx)
923 ALenum DeferUpdates = ctx->DeferUpdates;
924 ALenum UpdateSources = AL_FALSE;
926 if(!DeferUpdates)
927 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
929 /* source processing */
930 src = ctx->ActiveSources;
931 src_end = src + ctx->ActiveSourceCount;
932 while(src != src_end)
934 if((*src)->state != AL_PLAYING)
936 --(ctx->ActiveSourceCount);
937 *src = *(--src_end);
938 continue;
941 if(!DeferUpdates && (ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) ||
942 UpdateSources))
943 ALsource_Update(*src, ctx);
945 MixSource(*src, device, SamplesToDo);
946 src++;
949 /* effect slot processing */
950 slot = ctx->ActiveEffectSlots;
951 slot_end = slot + ctx->ActiveEffectSlotCount;
952 while(slot != slot_end)
954 for(c = 0;c < SamplesToDo;c++)
956 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
957 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
959 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
960 (*slot)->PendingClicks[0] = 0.0f;
962 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
963 ALeffectState_Update((*slot)->EffectState, device, *slot);
965 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
966 (*slot)->WetBuffer, device->DryBuffer);
968 for(i = 0;i < SamplesToDo;i++)
969 (*slot)->WetBuffer[i] = 0.0f;
971 slot++;
974 ctx = ctx->next;
977 slot = &device->DefaultSlot;
978 if(*slot != NULL)
980 for(c = 0;c < SamplesToDo;c++)
982 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
983 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
985 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
986 (*slot)->PendingClicks[0] = 0.0f;
988 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
989 ALeffectState_Update((*slot)->EffectState, device, *slot);
991 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
992 (*slot)->WetBuffer, device->DryBuffer);
994 for(i = 0;i < SamplesToDo;i++)
995 (*slot)->WetBuffer[i] = 0.0f;
997 ALCdevice_Unlock(device);
999 /* Click-removal. Could do better; this only really handles immediate
1000 * changes between updates where a predictive sample could be
1001 * generated. Delays caused by effects and HRTF aren't caught. */
1002 if(device->FmtChans == DevFmtMono)
1004 for(i = 0;i < SamplesToDo;i++)
1006 device->DryBuffer[FrontCenter][i] += device->ClickRemoval[FrontCenter];
1007 device->ClickRemoval[FrontCenter] -= device->ClickRemoval[FrontCenter] * (1.0f/256.0f);
1009 device->ClickRemoval[FrontCenter] += device->PendingClicks[FrontCenter];
1010 device->PendingClicks[FrontCenter] = 0.0f;
1012 else if(device->FmtChans == DevFmtStereo)
1014 /* Assumes the first two channels are FrontLeft and FrontRight */
1015 for(c = 0;c < 2;c++)
1017 ALfloat offset = device->ClickRemoval[c];
1018 for(i = 0;i < SamplesToDo;i++)
1020 device->DryBuffer[c][i] += offset;
1021 offset -= offset * (1.0f/256.0f);
1023 device->ClickRemoval[c] = offset + device->PendingClicks[c];
1024 device->PendingClicks[c] = 0.0f;
1026 if(device->Bs2b)
1028 float samples[2];
1029 for(i = 0;i < SamplesToDo;i++)
1031 samples[0] = device->DryBuffer[FrontLeft][i];
1032 samples[1] = device->DryBuffer[FrontRight][i];
1033 bs2b_cross_feed(device->Bs2b, samples);
1034 device->DryBuffer[FrontLeft][i] = samples[0];
1035 device->DryBuffer[FrontRight][i] = samples[1];
1039 else
1041 for(c = 0;c < MaxChannels;c++)
1043 ALfloat offset = device->ClickRemoval[c];
1044 for(i = 0;i < SamplesToDo;i++)
1046 device->DryBuffer[c][i] += offset;
1047 offset -= offset * (1.0f/256.0f);
1049 device->ClickRemoval[c] = offset + device->PendingClicks[c];
1050 device->PendingClicks[c] = 0.0f;
1054 if(buffer)
1056 switch(device->FmtType)
1058 case DevFmtByte:
1059 Write_ALbyte(device, buffer, SamplesToDo);
1060 break;
1061 case DevFmtUByte:
1062 Write_ALubyte(device, buffer, SamplesToDo);
1063 break;
1064 case DevFmtShort:
1065 Write_ALshort(device, buffer, SamplesToDo);
1066 break;
1067 case DevFmtUShort:
1068 Write_ALushort(device, buffer, SamplesToDo);
1069 break;
1070 case DevFmtInt:
1071 Write_ALint(device, buffer, SamplesToDo);
1072 break;
1073 case DevFmtUInt:
1074 Write_ALuint(device, buffer, SamplesToDo);
1075 break;
1076 case DevFmtFloat:
1077 Write_ALfloat(device, buffer, SamplesToDo);
1078 break;
1082 size -= SamplesToDo;
1085 RestoreFPUMode(fpuState);
1089 ALvoid aluHandleDisconnect(ALCdevice *device)
1091 ALCcontext *Context;
1093 ALCdevice_Lock(device);
1094 device->Connected = ALC_FALSE;
1096 Context = device->ContextList;
1097 while(Context)
1099 ALsource **src, **src_end;
1101 src = Context->ActiveSources;
1102 src_end = src + Context->ActiveSourceCount;
1103 while(src != src_end)
1105 if((*src)->state == AL_PLAYING)
1107 (*src)->state = AL_STOPPED;
1108 (*src)->BuffersPlayed = (*src)->BuffersInQueue;
1109 (*src)->position = 0;
1110 (*src)->position_fraction = 0;
1112 src++;
1114 Context->ActiveSourceCount = 0;
1116 Context = Context->next;
1118 ALCdevice_Unlock(device);