Add a helper to search for a channel index by name
[openal-soft.git] / Alc / ALu.c
blob49ebd0c2c96af037dda9f52e12bb3d07956a0d86
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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"
36 #include "hrtf.h"
37 #include "static_assert.h"
39 #include "midi/base.h"
42 static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
43 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
45 struct ChanMap {
46 enum Channel channel;
47 ALfloat angle;
48 ALfloat elevation;
51 /* Cone scalar */
52 ALfloat ConeScale = 1.0f;
54 /* Localized Z scalar for mono sources */
55 ALfloat ZScale = 1.0f;
57 extern inline ALfloat minf(ALfloat a, ALfloat b);
58 extern inline ALfloat maxf(ALfloat a, ALfloat b);
59 extern inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max);
61 extern inline ALdouble mind(ALdouble a, ALdouble b);
62 extern inline ALdouble maxd(ALdouble a, ALdouble b);
63 extern inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max);
65 extern inline ALuint minu(ALuint a, ALuint b);
66 extern inline ALuint maxu(ALuint a, ALuint b);
67 extern inline ALuint clampu(ALuint val, ALuint min, ALuint max);
69 extern inline ALint mini(ALint a, ALint b);
70 extern inline ALint maxi(ALint a, ALint b);
71 extern inline ALint clampi(ALint val, ALint min, ALint max);
73 extern inline ALint64 mini64(ALint64 a, ALint64 b);
74 extern inline ALint64 maxi64(ALint64 a, ALint64 b);
75 extern inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max);
77 extern inline ALuint64 minu64(ALuint64 a, ALuint64 b);
78 extern inline ALuint64 maxu64(ALuint64 a, ALuint64 b);
79 extern inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max);
81 extern inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu);
82 extern inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu);
85 static inline void aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
87 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
88 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
89 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
92 static inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
94 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
95 inVector1[2]*inVector2[2];
98 static inline void aluNormalize(ALfloat *inVector)
100 ALfloat lengthsqr = aluDotproduct(inVector, inVector);
101 if(lengthsqr > 0.0f)
103 ALfloat inv_length = 1.0f/sqrtf(lengthsqr);
104 inVector[0] *= inv_length;
105 inVector[1] *= inv_length;
106 inVector[2] *= inv_length;
110 static inline ALvoid aluMatrixVector(ALfloat *vector, ALfloat w, ALfloat (*restrict matrix)[4])
112 ALfloat temp[4] = {
113 vector[0], vector[1], vector[2], w
116 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
117 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
118 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
122 static void UpdateDryStepping(DirectParams *params, ALuint num_chans)
124 ALuint i, j;
126 if(!params->Moving)
128 for(i = 0;i < num_chans;i++)
130 MixGains *gains = params->Mix.Gains[i];
131 for(j = 0;j < MaxChannels;j++)
133 gains[j].Current = gains[j].Target;
134 gains[j].Step = 1.0f;
137 params->Moving = AL_TRUE;
138 params->Counter = 0;
139 return;
142 for(i = 0;i < num_chans;i++)
144 MixGains *gains = params->Mix.Gains[i];
145 for(j = 0;j < MaxChannels;j++)
147 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
148 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
149 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
150 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
151 else
152 gains[j].Step = 1.0f;
153 gains[j].Current = cur;
156 params->Counter = 64;
159 static void UpdateWetStepping(SendParams *params)
161 ALfloat cur, trg;
163 if(!params->Moving)
165 params->Gain.Current = params->Gain.Target;
166 params->Gain.Step = 1.0f;
168 params->Moving = AL_TRUE;
169 params->Counter = 0;
170 return;
173 cur = maxf(params->Gain.Current, FLT_EPSILON);
174 trg = maxf(params->Gain.Target, FLT_EPSILON);
175 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
176 params->Gain.Step = powf(trg/cur, 1.0f/64.0f);
177 else
178 params->Gain.Step = 1.0f;
179 params->Gain.Current = cur;
181 params->Counter = 64;
185 static ALvoid CalcListenerParams(ALlistener *Listener)
187 ALfloat N[3], V[3], U[3], P[3];
189 /* AT then UP */
190 N[0] = Listener->Forward[0];
191 N[1] = Listener->Forward[1];
192 N[2] = Listener->Forward[2];
193 aluNormalize(N);
194 V[0] = Listener->Up[0];
195 V[1] = Listener->Up[1];
196 V[2] = Listener->Up[2];
197 aluNormalize(V);
198 /* Build and normalize right-vector */
199 aluCrossproduct(N, V, U);
200 aluNormalize(U);
202 Listener->Params.Matrix[0][0] = U[0];
203 Listener->Params.Matrix[0][1] = V[0];
204 Listener->Params.Matrix[0][2] = -N[0];
205 Listener->Params.Matrix[0][3] = 0.0f;
206 Listener->Params.Matrix[1][0] = U[1];
207 Listener->Params.Matrix[1][1] = V[1];
208 Listener->Params.Matrix[1][2] = -N[1];
209 Listener->Params.Matrix[1][3] = 0.0f;
210 Listener->Params.Matrix[2][0] = U[2];
211 Listener->Params.Matrix[2][1] = V[2];
212 Listener->Params.Matrix[2][2] = -N[2];
213 Listener->Params.Matrix[2][3] = 0.0f;
214 Listener->Params.Matrix[3][0] = 0.0f;
215 Listener->Params.Matrix[3][1] = 0.0f;
216 Listener->Params.Matrix[3][2] = 0.0f;
217 Listener->Params.Matrix[3][3] = 1.0f;
219 P[0] = Listener->Position[0];
220 P[1] = Listener->Position[1];
221 P[2] = Listener->Position[2];
222 aluMatrixVector(P, 1.0f, Listener->Params.Matrix);
223 Listener->Params.Matrix[3][0] = -P[0];
224 Listener->Params.Matrix[3][1] = -P[1];
225 Listener->Params.Matrix[3][2] = -P[2];
227 Listener->Params.Velocity[0] = Listener->Velocity[0];
228 Listener->Params.Velocity[1] = Listener->Velocity[1];
229 Listener->Params.Velocity[2] = Listener->Velocity[2];
230 aluMatrixVector(Listener->Params.Velocity, 0.0f, Listener->Params.Matrix);
233 ALvoid CalcNonAttnSourceParams(ALvoice *voice, const ALsource *ALSource, const ALCcontext *ALContext)
235 static const struct ChanMap MonoMap[1] = { { FrontCenter, 0.0f, 0.0f } };
236 static const struct ChanMap StereoMap[2] = {
237 { FrontLeft, DEG2RAD(-30.0f), DEG2RAD(0.0f) },
238 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) }
240 static const struct ChanMap StereoWideMap[2] = {
241 { FrontLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f) },
242 { FrontRight, DEG2RAD( 90.0f), DEG2RAD(0.0f) }
244 static const struct ChanMap RearMap[2] = {
245 { BackLeft, DEG2RAD(-150.0f), DEG2RAD(0.0f) },
246 { BackRight, DEG2RAD( 150.0f), DEG2RAD(0.0f) }
248 static const struct ChanMap QuadMap[4] = {
249 { FrontLeft, DEG2RAD( -45.0f), DEG2RAD(0.0f) },
250 { FrontRight, DEG2RAD( 45.0f), DEG2RAD(0.0f) },
251 { BackLeft, DEG2RAD(-135.0f), DEG2RAD(0.0f) },
252 { BackRight, DEG2RAD( 135.0f), DEG2RAD(0.0f) }
254 static const struct ChanMap X51Map[6] = {
255 { FrontLeft, DEG2RAD( -30.0f), DEG2RAD(0.0f) },
256 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) },
257 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f) },
258 { LFE, 0.0f, 0.0f },
259 { BackLeft, DEG2RAD(-110.0f), DEG2RAD(0.0f) },
260 { BackRight, DEG2RAD( 110.0f), DEG2RAD(0.0f) }
262 static const struct ChanMap X61Map[7] = {
263 { FrontLeft, DEG2RAD(-30.0f), DEG2RAD(0.0f) },
264 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) },
265 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f) },
266 { LFE, 0.0f, 0.0f },
267 { BackCenter, DEG2RAD(180.0f), DEG2RAD(0.0f) },
268 { SideLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f) },
269 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f) }
271 static const struct ChanMap X71Map[8] = {
272 { FrontLeft, DEG2RAD( -30.0f), DEG2RAD(0.0f) },
273 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) },
274 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f) },
275 { LFE, 0.0f, 0.0f },
276 { BackLeft, DEG2RAD(-150.0f), DEG2RAD(0.0f) },
277 { BackRight, DEG2RAD( 150.0f), DEG2RAD(0.0f) },
278 { SideLeft, DEG2RAD( -90.0f), DEG2RAD(0.0f) },
279 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f) }
282 ALCdevice *Device = ALContext->Device;
283 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
284 ALbufferlistitem *BufferListItem;
285 enum FmtChannels Channels;
286 ALfloat DryGain, DryGainHF, DryGainLF;
287 ALfloat WetGain[MAX_SENDS];
288 ALfloat WetGainHF[MAX_SENDS];
289 ALfloat WetGainLF[MAX_SENDS];
290 ALint NumSends, Frequency;
291 const struct ChanMap *chans = NULL;
292 ALint num_channels = 0;
293 ALboolean DirectChannels;
294 ALfloat Pitch;
295 ALint i, j, c;
297 /* Get device properties */
298 NumSends = Device->NumAuxSends;
299 Frequency = Device->Frequency;
301 /* Get listener properties */
302 ListenerGain = ALContext->Listener->Gain;
304 /* Get source properties */
305 SourceVolume = ALSource->Gain;
306 MinVolume = ALSource->MinGain;
307 MaxVolume = ALSource->MaxGain;
308 Pitch = ALSource->Pitch;
309 DirectChannels = ALSource->DirectChannels;
311 voice->Direct.OutBuffer = Device->DryBuffer;
312 for(i = 0;i < NumSends;i++)
314 ALeffectslot *Slot = ALSource->Send[i].Slot;
315 if(!Slot && i == 0)
316 Slot = Device->DefaultSlot;
317 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
318 voice->Send[i].OutBuffer = NULL;
319 else
320 voice->Send[i].OutBuffer = Slot->WetBuffer;
323 /* Calculate the stepping value */
324 Channels = FmtMono;
325 BufferListItem = ATOMIC_LOAD(&ALSource->queue);
326 while(BufferListItem != NULL)
328 ALbuffer *ALBuffer;
329 if((ALBuffer=BufferListItem->buffer) != NULL)
331 Pitch = Pitch * ALBuffer->Frequency / Frequency;
332 if(Pitch > (ALfloat)MAX_PITCH)
333 voice->Step = MAX_PITCH<<FRACTIONBITS;
334 else
336 voice->Step = fastf2i(Pitch*FRACTIONONE);
337 if(voice->Step == 0)
338 voice->Step = 1;
341 Channels = ALBuffer->FmtChannels;
342 break;
344 BufferListItem = BufferListItem->next;
347 /* Calculate gains */
348 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
349 DryGain *= ALSource->Direct.Gain * ListenerGain;
350 DryGainHF = ALSource->Direct.GainHF;
351 DryGainLF = ALSource->Direct.GainLF;
352 for(i = 0;i < NumSends;i++)
354 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
355 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
356 WetGainHF[i] = ALSource->Send[i].GainHF;
357 WetGainLF[i] = ALSource->Send[i].GainLF;
360 switch(Channels)
362 case FmtMono:
363 chans = MonoMap;
364 num_channels = 1;
365 break;
367 case FmtStereo:
368 /* HACK: Place the stereo channels at +/-90 degrees when using non-
369 * HRTF stereo output. This helps reduce the "monoization" caused
370 * by them panning towards the center. */
371 if(Device->FmtChans == DevFmtStereo && !Device->Hrtf)
372 chans = StereoWideMap;
373 else
374 chans = StereoMap;
375 num_channels = 2;
376 break;
378 case FmtRear:
379 chans = RearMap;
380 num_channels = 2;
381 break;
383 case FmtQuad:
384 chans = QuadMap;
385 num_channels = 4;
386 break;
388 case FmtX51:
389 chans = X51Map;
390 num_channels = 6;
391 break;
393 case FmtX61:
394 chans = X61Map;
395 num_channels = 7;
396 break;
398 case FmtX71:
399 chans = X71Map;
400 num_channels = 8;
401 break;
404 if(DirectChannels != AL_FALSE)
406 for(c = 0;c < num_channels;c++)
408 MixGains *gains = voice->Direct.Mix.Gains[c];
410 for(j = 0;j < MaxChannels;j++)
411 gains[j].Target = 0.0f;
412 if(GetChannelIdxByName(Device, chans[c].channel) != -1)
413 gains[chans[c].channel].Target = DryGain;
415 UpdateDryStepping(&voice->Direct, num_channels);
417 voice->IsHrtf = AL_FALSE;
419 else if(Device->Hrtf)
421 for(c = 0;c < num_channels;c++)
423 if(chans[c].channel == LFE)
425 /* Skip LFE */
426 voice->Direct.Mix.Hrtf.Params[c].Delay[0] = 0;
427 voice->Direct.Mix.Hrtf.Params[c].Delay[1] = 0;
428 for(i = 0;i < HRIR_LENGTH;i++)
430 voice->Direct.Mix.Hrtf.Params[c].Coeffs[i][0] = 0.0f;
431 voice->Direct.Mix.Hrtf.Params[c].Coeffs[i][1] = 0.0f;
434 else
436 /* Get the static HRIR coefficients and delays for this
437 * channel. */
438 GetLerpedHrtfCoeffs(Device->Hrtf,
439 chans[c].elevation, chans[c].angle, 1.0f, DryGain,
440 voice->Direct.Mix.Hrtf.Params[c].Coeffs,
441 voice->Direct.Mix.Hrtf.Params[c].Delay);
444 voice->Direct.Counter = 0;
445 voice->Direct.Moving = AL_TRUE;
446 voice->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
448 voice->IsHrtf = AL_TRUE;
450 else
452 for(c = 0;c < num_channels;c++)
454 MixGains *gains = voice->Direct.Mix.Gains[c];
455 ALfloat Target[MaxChannels];
456 bool ok = false;
458 /* Special-case LFE */
459 if(chans[c].channel == LFE)
461 for(i = 0;i < MaxChannels;i++)
462 Target[i] = 0.0f;
463 if(GetChannelIdxByName(Device, chans[c].channel) != -1)
464 Target[chans[c].channel] = DryGain;
465 ok = true;
467 else for(i = 0;i < (ALint)Device->NumSpeakers;i++)
469 /* Attempt to match the input channel to an output based on its
470 * location. */
471 if(Device->Speaker[i].Angle == chans[c].angle &&
472 Device->Speaker[i].Elevation == chans[c].elevation)
474 for(j = 0;j < MaxChannels;j++)
475 Target[j] = 0.0f;
476 Target[Device->Speaker[i].ChanName] = DryGain;
477 ok = true;
478 break;
481 if(!ok)
483 /* All else fails, virtualize it. */
484 ComputeAngleGains(Device, chans[c].angle, chans[c].elevation, DryGain, Target);
487 for(i = 0;i < MaxChannels;i++)
488 gains[i].Target = Target[i];
490 UpdateDryStepping(&voice->Direct, num_channels);
492 voice->IsHrtf = AL_FALSE;
494 for(i = 0;i < NumSends;i++)
496 voice->Send[i].Gain.Target = WetGain[i];
497 UpdateWetStepping(&voice->Send[i]);
501 ALfloat gainhf = maxf(0.01f, DryGainHF);
502 ALfloat gainlf = maxf(0.01f, DryGainLF);
503 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
504 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
505 for(c = 0;c < num_channels;c++)
507 voice->Direct.Filters[c].ActiveType = AF_None;
508 if(gainhf != 1.0f) voice->Direct.Filters[c].ActiveType |= AF_LowPass;
509 if(gainlf != 1.0f) voice->Direct.Filters[c].ActiveType |= AF_HighPass;
510 ALfilterState_setParams(
511 &voice->Direct.Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
512 hfscale, 0.0f
514 ALfilterState_setParams(
515 &voice->Direct.Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
516 lfscale, 0.0f
520 for(i = 0;i < NumSends;i++)
522 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
523 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
524 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
525 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
526 for(c = 0;c < num_channels;c++)
528 voice->Send[i].Filters[c].ActiveType = AF_None;
529 if(gainhf != 1.0f) voice->Send[i].Filters[c].ActiveType |= AF_LowPass;
530 if(gainlf != 1.0f) voice->Send[i].Filters[c].ActiveType |= AF_HighPass;
531 ALfilterState_setParams(
532 &voice->Send[i].Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
533 hfscale, 0.0f
535 ALfilterState_setParams(
536 &voice->Send[i].Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
537 lfscale, 0.0f
543 ALvoid CalcSourceParams(ALvoice *voice, const ALsource *ALSource, const ALCcontext *ALContext)
545 ALCdevice *Device = ALContext->Device;
546 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
547 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
548 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
549 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
550 ALfloat DopplerFactor, SpeedOfSound;
551 ALfloat AirAbsorptionFactor;
552 ALfloat RoomAirAbsorption[MAX_SENDS];
553 ALbufferlistitem *BufferListItem;
554 ALfloat Attenuation;
555 ALfloat RoomAttenuation[MAX_SENDS];
556 ALfloat MetersPerUnit;
557 ALfloat RoomRolloffBase;
558 ALfloat RoomRolloff[MAX_SENDS];
559 ALfloat DecayDistance[MAX_SENDS];
560 ALfloat DryGain;
561 ALfloat DryGainHF;
562 ALfloat DryGainLF;
563 ALboolean DryGainHFAuto;
564 ALfloat WetGain[MAX_SENDS];
565 ALfloat WetGainHF[MAX_SENDS];
566 ALfloat WetGainLF[MAX_SENDS];
567 ALboolean WetGainAuto;
568 ALboolean WetGainHFAuto;
569 ALfloat Pitch;
570 ALuint Frequency;
571 ALint NumSends;
572 ALint i, j;
574 DryGainHF = 1.0f;
575 DryGainLF = 1.0f;
576 for(i = 0;i < MAX_SENDS;i++)
578 WetGainHF[i] = 1.0f;
579 WetGainLF[i] = 1.0f;
582 /* Get context/device properties */
583 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
584 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
585 NumSends = Device->NumAuxSends;
586 Frequency = Device->Frequency;
588 /* Get listener properties */
589 ListenerGain = ALContext->Listener->Gain;
590 MetersPerUnit = ALContext->Listener->MetersPerUnit;
592 /* Get source properties */
593 SourceVolume = ALSource->Gain;
594 MinVolume = ALSource->MinGain;
595 MaxVolume = ALSource->MaxGain;
596 Pitch = ALSource->Pitch;
597 Position[0] = ALSource->Position[0];
598 Position[1] = ALSource->Position[1];
599 Position[2] = ALSource->Position[2];
600 Direction[0] = ALSource->Orientation[0];
601 Direction[1] = ALSource->Orientation[1];
602 Direction[2] = ALSource->Orientation[2];
603 Velocity[0] = ALSource->Velocity[0];
604 Velocity[1] = ALSource->Velocity[1];
605 Velocity[2] = ALSource->Velocity[2];
606 MinDist = ALSource->RefDistance;
607 MaxDist = ALSource->MaxDistance;
608 Rolloff = ALSource->RollOffFactor;
609 InnerAngle = ALSource->InnerAngle;
610 OuterAngle = ALSource->OuterAngle;
611 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
612 DryGainHFAuto = ALSource->DryGainHFAuto;
613 WetGainAuto = ALSource->WetGainAuto;
614 WetGainHFAuto = ALSource->WetGainHFAuto;
615 RoomRolloffBase = ALSource->RoomRolloffFactor;
617 voice->Direct.OutBuffer = Device->DryBuffer;
618 for(i = 0;i < NumSends;i++)
620 ALeffectslot *Slot = ALSource->Send[i].Slot;
622 if(!Slot && i == 0)
623 Slot = Device->DefaultSlot;
624 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
626 Slot = NULL;
627 RoomRolloff[i] = 0.0f;
628 DecayDistance[i] = 0.0f;
629 RoomAirAbsorption[i] = 1.0f;
631 else if(Slot->AuxSendAuto)
633 RoomRolloff[i] = RoomRolloffBase;
634 if(IsReverbEffect(Slot->EffectType))
636 RoomRolloff[i] += Slot->EffectProps.Reverb.RoomRolloffFactor;
637 DecayDistance[i] = Slot->EffectProps.Reverb.DecayTime *
638 SPEEDOFSOUNDMETRESPERSEC;
639 RoomAirAbsorption[i] = Slot->EffectProps.Reverb.AirAbsorptionGainHF;
641 else
643 DecayDistance[i] = 0.0f;
644 RoomAirAbsorption[i] = 1.0f;
647 else
649 /* If the slot's auxiliary send auto is off, the data sent to the
650 * effect slot is the same as the dry path, sans filter effects */
651 RoomRolloff[i] = Rolloff;
652 DecayDistance[i] = 0.0f;
653 RoomAirAbsorption[i] = AIRABSORBGAINHF;
656 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
657 voice->Send[i].OutBuffer = NULL;
658 else
659 voice->Send[i].OutBuffer = Slot->WetBuffer;
662 /* Transform source to listener space (convert to head relative) */
663 if(ALSource->HeadRelative == AL_FALSE)
665 ALfloat (*restrict Matrix)[4] = ALContext->Listener->Params.Matrix;
666 /* Transform source vectors */
667 aluMatrixVector(Position, 1.0f, Matrix);
668 aluMatrixVector(Direction, 0.0f, Matrix);
669 aluMatrixVector(Velocity, 0.0f, Matrix);
671 else
673 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
674 /* Offset the source velocity to be relative of the listener velocity */
675 Velocity[0] += ListenerVel[0];
676 Velocity[1] += ListenerVel[1];
677 Velocity[2] += ListenerVel[2];
680 SourceToListener[0] = -Position[0];
681 SourceToListener[1] = -Position[1];
682 SourceToListener[2] = -Position[2];
683 aluNormalize(SourceToListener);
684 aluNormalize(Direction);
686 /* Calculate distance attenuation */
687 Distance = sqrtf(aluDotproduct(Position, Position));
688 ClampedDist = Distance;
690 Attenuation = 1.0f;
691 for(i = 0;i < NumSends;i++)
692 RoomAttenuation[i] = 1.0f;
693 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
694 ALContext->DistanceModel)
696 case InverseDistanceClamped:
697 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
698 if(MaxDist < MinDist)
699 break;
700 /*fall-through*/
701 case InverseDistance:
702 if(MinDist > 0.0f)
704 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
705 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
706 for(i = 0;i < NumSends;i++)
708 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
709 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
712 break;
714 case LinearDistanceClamped:
715 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
716 if(MaxDist < MinDist)
717 break;
718 /*fall-through*/
719 case LinearDistance:
720 if(MaxDist != MinDist)
722 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
723 Attenuation = maxf(Attenuation, 0.0f);
724 for(i = 0;i < NumSends;i++)
726 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
727 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
730 break;
732 case ExponentDistanceClamped:
733 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
734 if(MaxDist < MinDist)
735 break;
736 /*fall-through*/
737 case ExponentDistance:
738 if(ClampedDist > 0.0f && MinDist > 0.0f)
740 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
741 for(i = 0;i < NumSends;i++)
742 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
744 break;
746 case DisableDistance:
747 ClampedDist = MinDist;
748 break;
751 /* Source Gain + Attenuation */
752 DryGain = SourceVolume * Attenuation;
753 for(i = 0;i < NumSends;i++)
754 WetGain[i] = SourceVolume * RoomAttenuation[i];
756 /* Distance-based air absorption */
757 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
759 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
760 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
761 for(i = 0;i < NumSends;i++)
762 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
765 if(WetGainAuto)
767 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
769 /* Apply a decay-time transformation to the wet path, based on the
770 * attenuation of the dry path.
772 * Using the apparent distance, based on the distance attenuation, the
773 * initial decay of the reverb effect is calculated and applied to the
774 * wet path.
776 for(i = 0;i < NumSends;i++)
778 if(DecayDistance[i] > 0.0f)
779 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
783 /* Calculate directional soundcones */
784 Angle = RAD2DEG(acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale) * 2.0f;
785 if(Angle > InnerAngle && Angle <= OuterAngle)
787 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
788 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
789 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
791 else if(Angle > OuterAngle)
793 ConeVolume = ALSource->OuterGain;
794 ConeHF = ALSource->OuterGainHF;
796 else
798 ConeVolume = 1.0f;
799 ConeHF = 1.0f;
802 DryGain *= ConeVolume;
803 if(WetGainAuto)
805 for(i = 0;i < NumSends;i++)
806 WetGain[i] *= ConeVolume;
808 if(DryGainHFAuto)
809 DryGainHF *= ConeHF;
810 if(WetGainHFAuto)
812 for(i = 0;i < NumSends;i++)
813 WetGainHF[i] *= ConeHF;
816 /* Clamp to Min/Max Gain */
817 DryGain = clampf(DryGain, MinVolume, MaxVolume);
818 for(i = 0;i < NumSends;i++)
819 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
821 /* Apply gain and frequency filters */
822 DryGain *= ALSource->Direct.Gain * ListenerGain;
823 DryGainHF *= ALSource->Direct.GainHF;
824 DryGainLF *= ALSource->Direct.GainLF;
825 for(i = 0;i < NumSends;i++)
827 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
828 WetGainHF[i] *= ALSource->Send[i].GainHF;
829 WetGainLF[i] *= ALSource->Send[i].GainLF;
832 /* Calculate velocity-based doppler effect */
833 if(DopplerFactor > 0.0f)
835 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
836 ALfloat VSS, VLS;
838 if(SpeedOfSound < 1.0f)
840 DopplerFactor *= 1.0f/SpeedOfSound;
841 SpeedOfSound = 1.0f;
844 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
845 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
847 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
848 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
851 BufferListItem = ATOMIC_LOAD(&ALSource->queue);
852 while(BufferListItem != NULL)
854 ALbuffer *ALBuffer;
855 if((ALBuffer=BufferListItem->buffer) != NULL)
857 /* Calculate fixed-point stepping value, based on the pitch, buffer
858 * frequency, and output frequency. */
859 Pitch = Pitch * ALBuffer->Frequency / Frequency;
860 if(Pitch > (ALfloat)MAX_PITCH)
861 voice->Step = MAX_PITCH<<FRACTIONBITS;
862 else
864 voice->Step = fastf2i(Pitch*FRACTIONONE);
865 if(voice->Step == 0)
866 voice->Step = 1;
869 break;
871 BufferListItem = BufferListItem->next;
874 if(Device->Hrtf)
876 /* Use a binaural HRTF algorithm for stereo headphone playback */
877 ALfloat delta, ev = 0.0f, az = 0.0f;
878 ALfloat radius = ALSource->Radius;
879 ALfloat dirfact = 1.0f;
881 if(Distance > FLT_EPSILON)
883 ALfloat invlen = 1.0f/Distance;
884 Position[0] *= invlen;
885 Position[1] *= invlen;
886 Position[2] *= invlen;
888 /* Calculate elevation and azimuth only when the source is not at
889 * the listener. This prevents +0 and -0 Z from producing
890 * inconsistent panning. Also, clamp Y in case FP precision errors
891 * cause it to land outside of -1..+1. */
892 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
893 az = atan2f(Position[0], -Position[2]*ZScale);
895 if(radius > Distance)
896 dirfact *= Distance / radius;
898 /* Check to see if the HRIR is already moving. */
899 if(voice->Direct.Moving)
901 /* Calculate the normalized HRTF transition factor (delta). */
902 delta = CalcHrtfDelta(voice->Direct.Mix.Hrtf.Gain, DryGain,
903 voice->Direct.Mix.Hrtf.Dir, Position);
904 /* If the delta is large enough, get the moving HRIR target
905 * coefficients, target delays, steppping values, and counter. */
906 if(delta > 0.001f)
908 ALuint counter = GetMovingHrtfCoeffs(Device->Hrtf,
909 ev, az, dirfact, DryGain, delta, voice->Direct.Counter,
910 voice->Direct.Mix.Hrtf.Params[0].Coeffs, voice->Direct.Mix.Hrtf.Params[0].Delay,
911 voice->Direct.Mix.Hrtf.Params[0].CoeffStep, voice->Direct.Mix.Hrtf.Params[0].DelayStep
913 voice->Direct.Counter = counter;
914 voice->Direct.Mix.Hrtf.Gain = DryGain;
915 voice->Direct.Mix.Hrtf.Dir[0] = Position[0];
916 voice->Direct.Mix.Hrtf.Dir[1] = Position[1];
917 voice->Direct.Mix.Hrtf.Dir[2] = Position[2];
920 else
922 /* Get the initial (static) HRIR coefficients and delays. */
923 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, dirfact, DryGain,
924 voice->Direct.Mix.Hrtf.Params[0].Coeffs,
925 voice->Direct.Mix.Hrtf.Params[0].Delay);
926 voice->Direct.Counter = 0;
927 voice->Direct.Moving = AL_TRUE;
928 voice->Direct.Mix.Hrtf.Gain = DryGain;
929 voice->Direct.Mix.Hrtf.Dir[0] = Position[0];
930 voice->Direct.Mix.Hrtf.Dir[1] = Position[1];
931 voice->Direct.Mix.Hrtf.Dir[2] = Position[2];
933 voice->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
935 voice->IsHrtf = AL_TRUE;
937 else
939 MixGains *gains = voice->Direct.Mix.Gains[0];
941 /* Normalize the length, and compute panned gains. */
942 if(!(Distance > FLT_EPSILON))
944 ALfloat gain = 1.0f / sqrtf((float)Device->NumSpeakers);
945 for(j = 0;j < MaxChannels;j++)
946 gains[j].Target = 0.0f;
947 for(i = 0;i < (ALint)Device->NumSpeakers;i++)
949 enum Channel chan = Device->Speaker[i].ChanName;
950 gains[chan].Target = gain;
953 else
955 ALfloat radius = ALSource->Radius;
956 ALfloat Target[MaxChannels];
957 ALfloat invlen = 1.0f/maxf(Distance, radius);
958 Position[0] *= invlen;
959 Position[1] *= invlen;
960 Position[2] *= invlen;
962 ComputeDirectionalGains(Device, Position, DryGain, Target);
963 for(j = 0;j < MaxChannels;j++)
964 gains[j].Target = Target[j];
966 UpdateDryStepping(&voice->Direct, 1);
968 voice->IsHrtf = AL_FALSE;
970 for(i = 0;i < NumSends;i++)
972 voice->Send[i].Gain.Target = WetGain[i];
973 UpdateWetStepping(&voice->Send[i]);
977 ALfloat gainhf = maxf(0.01f, DryGainHF);
978 ALfloat gainlf = maxf(0.01f, DryGainLF);
979 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
980 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
981 voice->Direct.Filters[0].ActiveType = AF_None;
982 if(gainhf != 1.0f) voice->Direct.Filters[0].ActiveType |= AF_LowPass;
983 if(gainlf != 1.0f) voice->Direct.Filters[0].ActiveType |= AF_HighPass;
984 ALfilterState_setParams(
985 &voice->Direct.Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
986 hfscale, 0.0f
988 ALfilterState_setParams(
989 &voice->Direct.Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
990 lfscale, 0.0f
993 for(i = 0;i < NumSends;i++)
995 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
996 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
997 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
998 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
999 voice->Send[i].Filters[0].ActiveType = AF_None;
1000 if(gainhf != 1.0f) voice->Send[i].Filters[0].ActiveType |= AF_LowPass;
1001 if(gainlf != 1.0f) voice->Send[i].Filters[0].ActiveType |= AF_HighPass;
1002 ALfilterState_setParams(
1003 &voice->Send[i].Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1004 hfscale, 0.0f
1006 ALfilterState_setParams(
1007 &voice->Send[i].Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1008 lfscale, 0.0f
1014 static inline ALint aluF2I25(ALfloat val)
1016 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1017 if(fabsf(val) > 1.0f)
1018 val = (ALfloat)((0.0f < val) - (val < 0.0f));
1019 /* Convert to a signed integer, between -16777215 and +16777215. */
1020 return fastf2i(val*16777215.0f);
1023 static inline ALfloat aluF2F(ALfloat val)
1024 { return val; }
1025 static inline ALint aluF2I(ALfloat val)
1026 { return aluF2I25(val)<<7; }
1027 static inline ALuint aluF2UI(ALfloat val)
1028 { return aluF2I(val)+2147483648u; }
1029 static inline ALshort aluF2S(ALfloat val)
1030 { return aluF2I25(val)>>9; }
1031 static inline ALushort aluF2US(ALfloat val)
1032 { return aluF2S(val)+32768; }
1033 static inline ALbyte aluF2B(ALfloat val)
1034 { return aluF2I25(val)>>17; }
1035 static inline ALubyte aluF2UB(ALfloat val)
1036 { return aluF2B(val)+128; }
1038 #define DECL_TEMPLATE(T, func) \
1039 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1041 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1042 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1043 const enum Channel *chans = device->ChannelName; \
1044 ALuint i, j; \
1046 for(j = 0;j < MaxChannels;j++) \
1048 const enum Channel c = chans[j]; \
1049 const ALfloat *in; \
1050 T *restrict out; \
1052 if(c == InvalidChannel) \
1053 continue; \
1055 in = DryBuffer[c]; \
1056 out = (T*)(*buffer) + j; \
1057 for(i = 0;i < SamplesToDo;i++) \
1058 out[i*numchans] = func(in[i]); \
1060 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1063 DECL_TEMPLATE(ALfloat, aluF2F)
1064 DECL_TEMPLATE(ALuint, aluF2UI)
1065 DECL_TEMPLATE(ALint, aluF2I)
1066 DECL_TEMPLATE(ALushort, aluF2US)
1067 DECL_TEMPLATE(ALshort, aluF2S)
1068 DECL_TEMPLATE(ALubyte, aluF2UB)
1069 DECL_TEMPLATE(ALbyte, aluF2B)
1071 #undef DECL_TEMPLATE
1074 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1076 ALuint SamplesToDo;
1077 ALeffectslot **slot, **slot_end;
1078 ALvoice *voice, *voice_end;
1079 ALCcontext *ctx;
1080 FPUCtl oldMode;
1081 ALuint i, c;
1083 SetMixerFPUMode(&oldMode);
1085 while(size > 0)
1087 IncrementRef(&device->MixCount);
1089 SamplesToDo = minu(size, BUFFERSIZE);
1090 for(c = 0;c < MaxChannels;c++)
1091 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
1093 ALCdevice_Lock(device);
1094 V(device->Synth,process)(SamplesToDo, device->DryBuffer);
1096 ctx = ATOMIC_LOAD(&device->ContextList);
1097 while(ctx)
1099 ALenum DeferUpdates = ctx->DeferUpdates;
1100 ALenum UpdateSources = AL_FALSE;
1102 if(!DeferUpdates)
1103 UpdateSources = ATOMIC_EXCHANGE(ALenum, &ctx->UpdateSources, AL_FALSE);
1105 if(UpdateSources)
1106 CalcListenerParams(ctx->Listener);
1108 /* source processing */
1109 voice = ctx->Voices;
1110 voice_end = voice + ctx->VoiceCount;
1111 while(voice != voice_end)
1113 ALsource *source = voice->Source;
1114 if(!source) goto next;
1116 if(source->state != AL_PLAYING && source->state != AL_PAUSED)
1118 voice->Source = NULL;
1119 goto next;
1122 if(!DeferUpdates && (ATOMIC_EXCHANGE(ALenum, &source->NeedsUpdate, AL_FALSE) ||
1123 UpdateSources))
1124 voice->Update(voice, source, ctx);
1126 if(source->state != AL_PAUSED)
1127 MixSource(voice, source, device, SamplesToDo);
1128 next:
1129 voice++;
1132 /* effect slot processing */
1133 slot = VECTOR_ITER_BEGIN(ctx->ActiveAuxSlots);
1134 slot_end = VECTOR_ITER_END(ctx->ActiveAuxSlots);
1135 while(slot != slot_end)
1137 if(!DeferUpdates && ATOMIC_EXCHANGE(ALenum, &(*slot)->NeedsUpdate, AL_FALSE))
1138 V((*slot)->EffectState,update)(device, *slot);
1140 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1141 device->DryBuffer);
1143 for(i = 0;i < SamplesToDo;i++)
1144 (*slot)->WetBuffer[0][i] = 0.0f;
1146 slot++;
1149 ctx = ctx->next;
1152 slot = &device->DefaultSlot;
1153 if(*slot != NULL)
1155 if(ATOMIC_EXCHANGE(ALenum, &(*slot)->NeedsUpdate, AL_FALSE))
1156 V((*slot)->EffectState,update)(device, *slot);
1158 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1159 device->DryBuffer);
1161 for(i = 0;i < SamplesToDo;i++)
1162 (*slot)->WetBuffer[0][i] = 0.0f;
1165 /* Increment the clock time. Every second's worth of samples is
1166 * converted and added to clock base so that large sample counts don't
1167 * overflow during conversion. This also guarantees an exact, stable
1168 * conversion. */
1169 device->SamplesDone += SamplesToDo;
1170 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1171 device->SamplesDone %= device->Frequency;
1172 ALCdevice_Unlock(device);
1174 if(device->Bs2b)
1176 /* Apply binaural/crossfeed filter */
1177 for(i = 0;i < SamplesToDo;i++)
1179 float samples[2];
1180 samples[0] = device->DryBuffer[FrontLeft][i];
1181 samples[1] = device->DryBuffer[FrontRight][i];
1182 bs2b_cross_feed(device->Bs2b, samples);
1183 device->DryBuffer[FrontLeft][i] = samples[0];
1184 device->DryBuffer[FrontRight][i] = samples[1];
1188 if(buffer)
1190 switch(device->FmtType)
1192 case DevFmtByte:
1193 Write_ALbyte(device, &buffer, SamplesToDo);
1194 break;
1195 case DevFmtUByte:
1196 Write_ALubyte(device, &buffer, SamplesToDo);
1197 break;
1198 case DevFmtShort:
1199 Write_ALshort(device, &buffer, SamplesToDo);
1200 break;
1201 case DevFmtUShort:
1202 Write_ALushort(device, &buffer, SamplesToDo);
1203 break;
1204 case DevFmtInt:
1205 Write_ALint(device, &buffer, SamplesToDo);
1206 break;
1207 case DevFmtUInt:
1208 Write_ALuint(device, &buffer, SamplesToDo);
1209 break;
1210 case DevFmtFloat:
1211 Write_ALfloat(device, &buffer, SamplesToDo);
1212 break;
1216 size -= SamplesToDo;
1217 IncrementRef(&device->MixCount);
1220 RestoreFPUMode(&oldMode);
1224 ALvoid aluHandleDisconnect(ALCdevice *device)
1226 ALCcontext *Context;
1228 device->Connected = ALC_FALSE;
1230 Context = ATOMIC_LOAD(&device->ContextList);
1231 while(Context)
1233 ALvoice *voice, *voice_end;
1235 voice = Context->Voices;
1236 voice_end = voice + Context->VoiceCount;
1237 while(voice != voice_end)
1239 ALsource *source = voice->Source;
1240 voice->Source = NULL;
1242 if(source && source->state == AL_PLAYING)
1244 source->state = AL_STOPPED;
1245 ATOMIC_STORE(&source->current_buffer, NULL);
1246 source->position = 0;
1247 source->position_fraction = 0;
1250 voice++;
1252 Context->VoiceCount = 0;
1254 Context = Context->next;