With DirectChannels enabled, don't try to mix channels that have no matching output
[openal-soft.git] / Alc / ALu.c
blob365aa55df64af5b26c0bb00d1d53e63ab32a452f
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 FmtChannels Channels;
113 ALfloat (*SrcMatrix)[MAXCHANNELS];
114 ALfloat DryGain, DryGainHF;
115 ALfloat WetGain[MAX_SENDS];
116 ALfloat WetGainHF[MAX_SENDS];
117 ALint NumSends, Frequency;
118 const ALfloat *ChannelGain;
119 const struct ChanMap *chans = NULL;
120 enum Resampler Resampler;
121 ALint num_channels = 0;
122 ALboolean DirectChannels;
123 ALfloat Pitch;
124 ALfloat cw;
125 ALuint pos;
126 ALint i, c;
128 /* Get device properties */
129 NumSends = Device->NumAuxSends;
130 Frequency = Device->Frequency;
132 /* Get listener properties */
133 ListenerGain = ALContext->Listener.Gain;
135 /* Get source properties */
136 SourceVolume = ALSource->flGain;
137 MinVolume = ALSource->flMinGain;
138 MaxVolume = ALSource->flMaxGain;
139 Pitch = ALSource->flPitch;
140 Resampler = ALSource->Resampler;
141 DirectChannels = ALSource->DirectChannels;
143 /* Calculate the stepping value */
144 Channels = FmtMono;
145 BufferListItem = ALSource->queue;
146 while(BufferListItem != NULL)
148 ALbuffer *ALBuffer;
149 if((ALBuffer=BufferListItem->buffer) != NULL)
151 ALsizei maxstep = STACK_DATA_SIZE/sizeof(ALfloat) /
152 ALSource->NumChannels;
153 maxstep -= ResamplerPadding[Resampler] +
154 ResamplerPrePadding[Resampler] + 1;
155 maxstep = mini(maxstep, INT_MAX>>FRACTIONBITS);
157 Pitch = Pitch * ALBuffer->Frequency / Frequency;
158 if(Pitch > (ALfloat)maxstep)
159 ALSource->Params.Step = maxstep<<FRACTIONBITS;
160 else
162 ALSource->Params.Step = fastf2i(Pitch*FRACTIONONE);
163 if(ALSource->Params.Step == 0)
164 ALSource->Params.Step = 1;
166 if(ALSource->Params.Step == FRACTIONONE)
167 Resampler = PointResampler;
169 Channels = ALBuffer->FmtChannels;
170 break;
172 BufferListItem = BufferListItem->next;
174 if(!DirectChannels && Device->Hrtf)
175 ALSource->Params.DoMix = SelectHrtfMixer(Resampler);
176 else
177 ALSource->Params.DoMix = SelectMixer(Resampler);
179 /* Calculate gains */
180 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
181 DryGain *= ALSource->DirectGain;
182 DryGainHF = ALSource->DirectGainHF;
183 for(i = 0;i < NumSends;i++)
185 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
186 WetGain[i] *= ALSource->Send[i].WetGain;
187 WetGainHF[i] = ALSource->Send[i].WetGainHF;
190 SrcMatrix = ALSource->Params.DryGains;
191 for(i = 0;i < MAXCHANNELS;i++)
193 for(c = 0;c < MAXCHANNELS;c++)
194 SrcMatrix[i][c] = 0.0f;
196 switch(Channels)
198 case FmtMono:
199 chans = MonoMap;
200 num_channels = 1;
201 break;
202 case FmtStereo:
203 if(!DirectChannels && (Device->Flags&DEVICE_DUPLICATE_STEREO))
205 DryGain *= aluSqrt(2.0f/4.0f);
206 for(c = 0;c < 2;c++)
208 pos = aluCart2LUTpos(aluCos(RearMap[c].angle),
209 aluSin(RearMap[c].angle));
210 ChannelGain = Device->PanningLUT[pos];
212 for(i = 0;i < (ALint)Device->NumChan;i++)
214 enum Channel chan = Device->Speaker2Chan[i];
215 SrcMatrix[c][chan] += DryGain * ListenerGain *
216 ChannelGain[chan];
220 chans = StereoMap;
221 num_channels = 2;
222 break;
224 case FmtRear:
225 chans = RearMap;
226 num_channels = 2;
227 break;
229 case FmtQuad:
230 chans = QuadMap;
231 num_channels = 4;
232 break;
234 case FmtX51:
235 chans = X51Map;
236 num_channels = 6;
237 break;
239 case FmtX61:
240 chans = X61Map;
241 num_channels = 7;
242 break;
244 case FmtX71:
245 chans = X71Map;
246 num_channels = 8;
247 break;
250 if(DirectChannels != AL_FALSE)
252 for(c = 0;c < num_channels;c++)
254 for(i = 0;i < (ALint)Device->NumChan;i++)
256 enum Channel chan = Device->Speaker2Chan[i];
257 if(chan == chans[c].channel)
259 SrcMatrix[c][chan] += DryGain * ListenerGain;
260 break;
265 else if(Device->Hrtf)
267 for(c = 0;c < num_channels;c++)
269 if(chans[c].channel == LFE)
271 /* Skip LFE */
272 ALSource->Params.HrtfDelay[c][0] = 0;
273 ALSource->Params.HrtfDelay[c][1] = 0;
274 for(i = 0;i < HRIR_LENGTH;i++)
276 ALSource->Params.HrtfCoeffs[c][i][0] = 0.0f;
277 ALSource->Params.HrtfCoeffs[c][i][1] = 0.0f;
280 else
282 /* Get the static HRIR coefficients and delays for this
283 * channel. */
284 GetLerpedHrtfCoeffs(Device->Hrtf,
285 0.0f, F_PI/180.0f * chans[c].angle,
286 DryGain*ListenerGain,
287 ALSource->Params.HrtfCoeffs[c],
288 ALSource->Params.HrtfDelay[c]);
290 ALSource->HrtfCounter = 0;
293 else
295 for(c = 0;c < num_channels;c++)
297 if(chans[c].channel == LFE) /* Special-case LFE */
299 SrcMatrix[c][LFE] += DryGain * ListenerGain;
300 continue;
302 pos = aluCart2LUTpos(aluCos(chans[c].angle), aluSin(chans[c].angle));
303 ChannelGain = Device->PanningLUT[pos];
305 for(i = 0;i < (ALint)Device->NumChan;i++)
307 enum Channel chan = Device->Speaker2Chan[i];
308 SrcMatrix[c][chan] += DryGain * ListenerGain *
309 ChannelGain[chan];
313 for(i = 0;i < NumSends;i++)
315 ALeffectslot *Slot = ALSource->Send[i].Slot;
317 if(!Slot && i == 0)
318 Slot = Device->DefaultSlot;
319 if(Slot && Slot->effect.type == AL_EFFECT_NULL)
320 Slot = NULL;
321 ALSource->Params.Send[i].Slot = Slot;
322 ALSource->Params.Send[i].WetGain = WetGain[i] * ListenerGain;
325 /* Update filter coefficients. Calculations based on the I3DL2
326 * spec. */
327 cw = aluCos(F_PI*2.0f * LOWPASSFREQREF / Frequency);
329 /* We use two chained one-pole filters, so we need to take the
330 * square root of the squared gain, which is the same as the base
331 * gain. */
332 ALSource->Params.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
333 for(i = 0;i < NumSends;i++)
335 /* We use a one-pole filter, so we need to take the squared gain */
336 ALfloat a = lpCoeffCalc(WetGainHF[i]*WetGainHF[i], cw);
337 ALSource->Params.Send[i].iirFilter.coeff = a;
341 ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
343 const ALCdevice *Device = ALContext->Device;
344 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
345 ALfloat Direction[3],Position[3],SourceToListener[3];
346 ALfloat Velocity[3],ListenerVel[3];
347 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
348 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
349 ALfloat DopplerFactor, SpeedOfSound;
350 ALfloat AirAbsorptionFactor;
351 ALfloat RoomAirAbsorption[MAX_SENDS];
352 ALbufferlistitem *BufferListItem;
353 ALfloat Attenuation, EffectiveDist;
354 ALfloat RoomAttenuation[MAX_SENDS];
355 ALfloat MetersPerUnit;
356 ALfloat RoomRolloffBase;
357 ALfloat RoomRolloff[MAX_SENDS];
358 ALfloat DecayDistance[MAX_SENDS];
359 ALfloat DryGain;
360 ALfloat DryGainHF;
361 ALboolean DryGainHFAuto;
362 ALfloat WetGain[MAX_SENDS];
363 ALfloat WetGainHF[MAX_SENDS];
364 ALboolean WetGainAuto;
365 ALboolean WetGainHFAuto;
366 enum Resampler Resampler;
367 ALfloat Matrix[4][4];
368 ALfloat Pitch;
369 ALuint Frequency;
370 ALint NumSends;
371 ALfloat cw;
372 ALint i, j;
374 DryGainHF = 1.0f;
375 for(i = 0;i < MAX_SENDS;i++)
376 WetGainHF[i] = 1.0f;
378 //Get context properties
379 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
380 SpeedOfSound = ALContext->flSpeedOfSound * ALContext->DopplerVelocity;
381 NumSends = Device->NumAuxSends;
382 Frequency = Device->Frequency;
384 //Get listener properties
385 ListenerGain = ALContext->Listener.Gain;
386 MetersPerUnit = ALContext->Listener.MetersPerUnit;
387 ListenerVel[0] = ALContext->Listener.Velocity[0];
388 ListenerVel[1] = ALContext->Listener.Velocity[1];
389 ListenerVel[2] = ALContext->Listener.Velocity[2];
391 //Get source properties
392 SourceVolume = ALSource->flGain;
393 MinVolume = ALSource->flMinGain;
394 MaxVolume = ALSource->flMaxGain;
395 Pitch = ALSource->flPitch;
396 Resampler = ALSource->Resampler;
397 Position[0] = ALSource->vPosition[0];
398 Position[1] = ALSource->vPosition[1];
399 Position[2] = ALSource->vPosition[2];
400 Direction[0] = ALSource->vOrientation[0];
401 Direction[1] = ALSource->vOrientation[1];
402 Direction[2] = ALSource->vOrientation[2];
403 Velocity[0] = ALSource->vVelocity[0];
404 Velocity[1] = ALSource->vVelocity[1];
405 Velocity[2] = ALSource->vVelocity[2];
406 MinDist = ALSource->flRefDistance;
407 MaxDist = ALSource->flMaxDistance;
408 Rolloff = ALSource->flRollOffFactor;
409 InnerAngle = ALSource->flInnerAngle * ConeScale;
410 OuterAngle = ALSource->flOuterAngle * ConeScale;
411 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
412 DryGainHFAuto = ALSource->DryGainHFAuto;
413 WetGainAuto = ALSource->WetGainAuto;
414 WetGainHFAuto = ALSource->WetGainHFAuto;
415 RoomRolloffBase = ALSource->RoomRolloffFactor;
416 for(i = 0;i < NumSends;i++)
418 ALeffectslot *Slot = ALSource->Send[i].Slot;
420 if(!Slot && i == 0)
421 Slot = Device->DefaultSlot;
422 if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
424 Slot = NULL;
425 RoomRolloff[i] = 0.0f;
426 DecayDistance[i] = 0.0f;
427 RoomAirAbsorption[i] = 1.0f;
429 else if(Slot->AuxSendAuto)
431 RoomRolloff[i] = RoomRolloffBase;
432 if(IsReverbEffect(Slot->effect.type))
434 RoomRolloff[i] += Slot->effect.Reverb.RoomRolloffFactor;
435 DecayDistance[i] = Slot->effect.Reverb.DecayTime *
436 SPEEDOFSOUNDMETRESPERSEC;
437 RoomAirAbsorption[i] = Slot->effect.Reverb.AirAbsorptionGainHF;
439 else
441 DecayDistance[i] = 0.0f;
442 RoomAirAbsorption[i] = 1.0f;
445 else
447 /* If the slot's auxiliary send auto is off, the data sent to the
448 * effect slot is the same as the dry path, sans filter effects */
449 RoomRolloff[i] = Rolloff;
450 DecayDistance[i] = 0.0f;
451 RoomAirAbsorption[i] = AIRABSORBGAINHF;
454 ALSource->Params.Send[i].Slot = Slot;
457 for(i = 0;i < 4;i++)
459 for(j = 0;j < 4;j++)
460 Matrix[i][j] = ALContext->Listener.Matrix[i][j];
463 //1. Translate Listener to origin (convert to head relative)
464 if(ALSource->bHeadRelative == AL_FALSE)
466 /* Translate position */
467 Position[0] -= ALContext->Listener.Position[0];
468 Position[1] -= ALContext->Listener.Position[1];
469 Position[2] -= ALContext->Listener.Position[2];
471 /* Transform source vectors into listener space */
472 aluMatrixVector(Position, 1.0f, Matrix);
473 aluMatrixVector(Direction, 0.0f, Matrix);
474 aluMatrixVector(Velocity, 0.0f, Matrix);
475 /* Transform listener velocity into listener space */
476 aluMatrixVector(ListenerVel, 0.0f, Matrix);
478 else
480 /* Transform listener velocity into listener space */
481 aluMatrixVector(ListenerVel, 0.0f, Matrix);
482 /* Offset the source velocity to be relative of the listener velocity */
483 Velocity[0] += ListenerVel[0];
484 Velocity[1] += ListenerVel[1];
485 Velocity[2] += ListenerVel[2];
488 SourceToListener[0] = -Position[0];
489 SourceToListener[1] = -Position[1];
490 SourceToListener[2] = -Position[2];
491 aluNormalize(SourceToListener);
492 aluNormalize(Direction);
494 //2. Calculate distance attenuation
495 Distance = aluSqrt(aluDotproduct(Position, Position));
496 ClampedDist = Distance;
498 Attenuation = 1.0f;
499 for(i = 0;i < NumSends;i++)
500 RoomAttenuation[i] = 1.0f;
501 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
502 ALContext->DistanceModel)
504 case InverseDistanceClamped:
505 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
506 if(MaxDist < MinDist)
507 break;
508 //fall-through
509 case InverseDistance:
510 if(MinDist > 0.0f)
512 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
513 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
514 for(i = 0;i < NumSends;i++)
516 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
517 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
520 break;
522 case LinearDistanceClamped:
523 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
524 if(MaxDist < MinDist)
525 break;
526 //fall-through
527 case LinearDistance:
528 if(MaxDist != MinDist)
530 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
531 Attenuation = maxf(Attenuation, 0.0f);
532 for(i = 0;i < NumSends;i++)
534 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
535 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
538 break;
540 case ExponentDistanceClamped:
541 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
542 if(MaxDist < MinDist)
543 break;
544 //fall-through
545 case ExponentDistance:
546 if(ClampedDist > 0.0f && MinDist > 0.0f)
548 Attenuation = aluPow(ClampedDist/MinDist, -Rolloff);
549 for(i = 0;i < NumSends;i++)
550 RoomAttenuation[i] = aluPow(ClampedDist/MinDist, -RoomRolloff[i]);
552 break;
554 case DisableDistance:
555 break;
558 // Source Gain + Attenuation
559 DryGain = SourceVolume * Attenuation;
560 for(i = 0;i < NumSends;i++)
561 WetGain[i] = SourceVolume * RoomAttenuation[i];
563 // Distance-based air absorption
564 EffectiveDist = 0.0f;
565 if(MinDist > 0.0f && Attenuation < 1.0f)
566 EffectiveDist = (MinDist/Attenuation - MinDist)*MetersPerUnit;
567 if(AirAbsorptionFactor > 0.0f && EffectiveDist > 0.0f)
569 DryGainHF *= aluPow(AIRABSORBGAINHF, AirAbsorptionFactor*EffectiveDist);
570 for(i = 0;i < NumSends;i++)
571 WetGainHF[i] *= aluPow(RoomAirAbsorption[i],
572 AirAbsorptionFactor*EffectiveDist);
575 if(WetGainAuto)
577 /* Apply a decay-time transformation to the wet path, based on the
578 * attenuation of the dry path.
580 * Using the approximate (effective) source to listener distance, the
581 * initial decay of the reverb effect is calculated and applied to the
582 * wet path.
584 for(i = 0;i < NumSends;i++)
586 if(DecayDistance[i] > 0.0f)
587 WetGain[i] *= aluPow(0.001f /* -60dB */,
588 EffectiveDist / DecayDistance[i]);
592 /* Calculate directional soundcones */
593 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * (180.0f/F_PI);
594 if(Angle >= InnerAngle && Angle <= OuterAngle)
596 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
597 ConeVolume = lerp(1.0f, ALSource->flOuterGain, scale);
598 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
600 else if(Angle > OuterAngle)
602 ConeVolume = ALSource->flOuterGain;
603 ConeHF = ALSource->OuterGainHF;
605 else
607 ConeVolume = 1.0f;
608 ConeHF = 1.0f;
611 DryGain *= ConeVolume;
612 if(WetGainAuto)
614 for(i = 0;i < NumSends;i++)
615 WetGain[i] *= ConeVolume;
617 if(DryGainHFAuto)
618 DryGainHF *= ConeHF;
619 if(WetGainHFAuto)
621 for(i = 0;i < NumSends;i++)
622 WetGainHF[i] *= ConeHF;
625 // Clamp to Min/Max Gain
626 DryGain = clampf(DryGain, MinVolume, MaxVolume);
627 for(i = 0;i < NumSends;i++)
628 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
630 // Apply filter gains and filters
631 DryGain *= ALSource->DirectGain * ListenerGain;
632 DryGainHF *= ALSource->DirectGainHF;
633 for(i = 0;i < NumSends;i++)
635 WetGain[i] *= ALSource->Send[i].WetGain * ListenerGain;
636 WetGainHF[i] *= ALSource->Send[i].WetGainHF;
639 // Calculate Velocity
640 if(DopplerFactor > 0.0f && SpeedOfSound > 0.5f)
642 ALfloat VSS, VLS;
644 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
645 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
647 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
648 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
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;
672 if(ALSource->Params.Step == FRACTIONONE)
673 Resampler = PointResampler;
675 break;
677 BufferListItem = BufferListItem->next;
679 if(Device->Hrtf)
680 ALSource->Params.DoMix = SelectHrtfMixer(Resampler);
681 else
682 ALSource->Params.DoMix = SelectMixer(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 ALint aluF2I(ALfloat val)
795 if(val > 1.0f) return 2147483647;
796 if(val < -1.0f) return -2147483647-1;
797 return fastf2i((ALfloat)(val*2147483647.0));
799 static __inline ALuint aluF2UI(ALfloat val)
800 { return aluF2I(val)+2147483648u; }
801 static __inline ALshort aluF2S(ALfloat val)
802 { return aluF2I(val)>>16; }
803 static __inline ALushort aluF2US(ALfloat val)
804 { return aluF2S(val)+32768; }
805 static __inline ALbyte aluF2B(ALfloat val)
806 { return aluF2I(val)>>24; }
807 static __inline ALubyte aluF2UB(ALfloat val)
808 { return aluF2B(val)+128; }
810 #define DECL_TEMPLATE(T, N, func) \
811 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
812 ALuint SamplesToDo) \
814 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
815 const enum Channel *ChanMap = device->DevChannels; \
816 ALuint i, j; \
818 for(j = 0;j < N;j++) \
820 T *RESTRICT out = buffer + j; \
821 enum Channel chan = ChanMap[j]; \
823 for(i = 0;i < SamplesToDo;i++) \
824 out[i*N] = func(DryBuffer[i][chan]); \
828 DECL_TEMPLATE(ALfloat, 1, aluF2F)
829 DECL_TEMPLATE(ALfloat, 2, aluF2F)
830 DECL_TEMPLATE(ALfloat, 4, aluF2F)
831 DECL_TEMPLATE(ALfloat, 6, aluF2F)
832 DECL_TEMPLATE(ALfloat, 7, aluF2F)
833 DECL_TEMPLATE(ALfloat, 8, aluF2F)
835 DECL_TEMPLATE(ALuint, 1, aluF2UI)
836 DECL_TEMPLATE(ALuint, 2, aluF2UI)
837 DECL_TEMPLATE(ALuint, 4, aluF2UI)
838 DECL_TEMPLATE(ALuint, 6, aluF2UI)
839 DECL_TEMPLATE(ALuint, 7, aluF2UI)
840 DECL_TEMPLATE(ALuint, 8, aluF2UI)
842 DECL_TEMPLATE(ALint, 1, aluF2I)
843 DECL_TEMPLATE(ALint, 2, aluF2I)
844 DECL_TEMPLATE(ALint, 4, aluF2I)
845 DECL_TEMPLATE(ALint, 6, aluF2I)
846 DECL_TEMPLATE(ALint, 7, aluF2I)
847 DECL_TEMPLATE(ALint, 8, aluF2I)
849 DECL_TEMPLATE(ALushort, 1, aluF2US)
850 DECL_TEMPLATE(ALushort, 2, aluF2US)
851 DECL_TEMPLATE(ALushort, 4, aluF2US)
852 DECL_TEMPLATE(ALushort, 6, aluF2US)
853 DECL_TEMPLATE(ALushort, 7, aluF2US)
854 DECL_TEMPLATE(ALushort, 8, aluF2US)
856 DECL_TEMPLATE(ALshort, 1, aluF2S)
857 DECL_TEMPLATE(ALshort, 2, aluF2S)
858 DECL_TEMPLATE(ALshort, 4, aluF2S)
859 DECL_TEMPLATE(ALshort, 6, aluF2S)
860 DECL_TEMPLATE(ALshort, 7, aluF2S)
861 DECL_TEMPLATE(ALshort, 8, aluF2S)
863 DECL_TEMPLATE(ALubyte, 1, aluF2UB)
864 DECL_TEMPLATE(ALubyte, 2, aluF2UB)
865 DECL_TEMPLATE(ALubyte, 4, aluF2UB)
866 DECL_TEMPLATE(ALubyte, 6, aluF2UB)
867 DECL_TEMPLATE(ALubyte, 7, aluF2UB)
868 DECL_TEMPLATE(ALubyte, 8, aluF2UB)
870 DECL_TEMPLATE(ALbyte, 1, aluF2B)
871 DECL_TEMPLATE(ALbyte, 2, aluF2B)
872 DECL_TEMPLATE(ALbyte, 4, aluF2B)
873 DECL_TEMPLATE(ALbyte, 6, aluF2B)
874 DECL_TEMPLATE(ALbyte, 7, aluF2B)
875 DECL_TEMPLATE(ALbyte, 8, aluF2B)
877 #undef DECL_TEMPLATE
879 #define DECL_TEMPLATE(T) \
880 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
882 switch(device->FmtChans) \
884 case DevFmtMono: \
885 Write_##T##_1(device, buffer, SamplesToDo); \
886 break; \
887 case DevFmtStereo: \
888 Write_##T##_2(device, buffer, SamplesToDo); \
889 break; \
890 case DevFmtQuad: \
891 Write_##T##_4(device, buffer, SamplesToDo); \
892 break; \
893 case DevFmtX51: \
894 case DevFmtX51Side: \
895 Write_##T##_6(device, buffer, SamplesToDo); \
896 break; \
897 case DevFmtX61: \
898 Write_##T##_7(device, buffer, SamplesToDo); \
899 break; \
900 case DevFmtX71: \
901 Write_##T##_8(device, buffer, SamplesToDo); \
902 break; \
906 DECL_TEMPLATE(ALfloat)
907 DECL_TEMPLATE(ALuint)
908 DECL_TEMPLATE(ALint)
909 DECL_TEMPLATE(ALushort)
910 DECL_TEMPLATE(ALshort)
911 DECL_TEMPLATE(ALubyte)
912 DECL_TEMPLATE(ALbyte)
914 #undef DECL_TEMPLATE
916 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
918 ALuint SamplesToDo;
919 ALeffectslot **slot, **slot_end;
920 ALsource **src, **src_end;
921 ALCcontext *ctx;
922 int fpuState;
923 ALuint i, c;
925 fpuState = SetMixerFPUMode();
927 while(size > 0)
929 /* Setup variables */
930 SamplesToDo = minu(size, BUFFERSIZE);
932 /* Clear mixing buffer */
933 memset(device->DryBuffer, 0, SamplesToDo*MAXCHANNELS*sizeof(ALfloat));
935 LockDevice(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 src = ctx->ActiveSources;
946 src_end = src + ctx->ActiveSourceCount;
947 while(src != src_end)
949 if((*src)->state != AL_PLAYING)
951 --(ctx->ActiveSourceCount);
952 *src = *(--src_end);
953 continue;
956 if(!DeferUpdates && (ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) ||
957 UpdateSources))
958 ALsource_Update(*src, ctx);
960 MixSource(*src, device, SamplesToDo);
961 src++;
964 /* effect slot processing */
965 slot = ctx->ActiveEffectSlots;
966 slot_end = slot + ctx->ActiveEffectSlotCount;
967 while(slot != slot_end)
969 for(c = 0;c < SamplesToDo;c++)
971 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
972 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
974 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
975 (*slot)->PendingClicks[0] = 0.0f;
977 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
978 ALeffectState_Update((*slot)->EffectState, ctx, *slot);
980 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
981 (*slot)->WetBuffer, device->DryBuffer);
983 for(i = 0;i < SamplesToDo;i++)
984 (*slot)->WetBuffer[i] = 0.0f;
986 slot++;
989 ctx = ctx->next;
992 slot = &device->DefaultSlot;
993 if(*slot != NULL)
995 for(c = 0;c < SamplesToDo;c++)
997 (*slot)->WetBuffer[c] += (*slot)->ClickRemoval[0];
998 (*slot)->ClickRemoval[0] -= (*slot)->ClickRemoval[0] * (1.0f/256.0f);
1000 (*slot)->ClickRemoval[0] += (*slot)->PendingClicks[0];
1001 (*slot)->PendingClicks[0] = 0.0f;
1003 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1004 ALeffectState_Update((*slot)->EffectState, ctx, *slot);
1006 ALeffectState_Process((*slot)->EffectState, SamplesToDo,
1007 (*slot)->WetBuffer, device->DryBuffer);
1009 for(i = 0;i < SamplesToDo;i++)
1010 (*slot)->WetBuffer[i] = 0.0f;
1012 UnlockDevice(device);
1014 //Post processing loop
1015 if(device->FmtChans == DevFmtMono)
1017 for(i = 0;i < SamplesToDo;i++)
1019 device->DryBuffer[i][FRONT_CENTER] += device->ClickRemoval[FRONT_CENTER];
1020 device->ClickRemoval[FRONT_CENTER] -= device->ClickRemoval[FRONT_CENTER] * (1.0f/256.0f);
1022 device->ClickRemoval[FRONT_CENTER] += device->PendingClicks[FRONT_CENTER];
1023 device->PendingClicks[FRONT_CENTER] = 0.0f;
1025 else if(device->FmtChans == DevFmtStereo)
1027 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1028 for(i = 0;i < SamplesToDo;i++)
1030 for(c = 0;c < 2;c++)
1032 device->DryBuffer[i][c] += device->ClickRemoval[c];
1033 device->ClickRemoval[c] -= device->ClickRemoval[c] * (1.0f/256.0f);
1036 for(c = 0;c < 2;c++)
1038 device->ClickRemoval[c] += device->PendingClicks[c];
1039 device->PendingClicks[c] = 0.0f;
1041 if(device->Bs2b)
1043 for(i = 0;i < SamplesToDo;i++)
1044 bs2b_cross_feed(device->Bs2b, &device->DryBuffer[i][0]);
1047 else
1049 for(i = 0;i < SamplesToDo;i++)
1051 for(c = 0;c < MAXCHANNELS;c++)
1053 device->DryBuffer[i][c] += device->ClickRemoval[c];
1054 device->ClickRemoval[c] -= device->ClickRemoval[c] * (1.0f/256.0f);
1057 for(c = 0;c < MAXCHANNELS;c++)
1059 device->ClickRemoval[c] += device->PendingClicks[c];
1060 device->PendingClicks[c] = 0.0f;
1064 if(buffer)
1066 switch(device->FmtType)
1068 case DevFmtByte:
1069 Write_ALbyte(device, buffer, SamplesToDo);
1070 break;
1071 case DevFmtUByte:
1072 Write_ALubyte(device, buffer, SamplesToDo);
1073 break;
1074 case DevFmtShort:
1075 Write_ALshort(device, buffer, SamplesToDo);
1076 break;
1077 case DevFmtUShort:
1078 Write_ALushort(device, buffer, SamplesToDo);
1079 break;
1080 case DevFmtInt:
1081 Write_ALint(device, buffer, SamplesToDo);
1082 break;
1083 case DevFmtUInt:
1084 Write_ALuint(device, buffer, SamplesToDo);
1085 break;
1086 case DevFmtFloat:
1087 Write_ALfloat(device, buffer, SamplesToDo);
1088 break;
1092 size -= SamplesToDo;
1095 RestoreFPUMode(fpuState);
1099 ALvoid aluHandleDisconnect(ALCdevice *device)
1101 ALCcontext *Context;
1103 LockDevice(device);
1104 device->Connected = ALC_FALSE;
1106 Context = device->ContextList;
1107 while(Context)
1109 ALsource **src, **src_end;
1111 src = Context->ActiveSources;
1112 src_end = src + Context->ActiveSourceCount;
1113 while(src != src_end)
1115 if((*src)->state == AL_PLAYING)
1117 (*src)->state = AL_STOPPED;
1118 (*src)->BuffersPlayed = (*src)->BuffersInQueue;
1119 (*src)->position = 0;
1120 (*src)->position_fraction = 0;
1122 src++;
1124 Context->ActiveSourceCount = 0;
1126 Context = Context->next;
1128 UnlockDevice(device);