Fix typo
[openal-soft/openal-hmr.git] / Alc / mixer.c
blobed2f49a59dd65313bb4c1cb528234926486bfb5d
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 static __inline ALfloat point32(const ALfloat *vals, ALint step, ALint frac)
41 { return vals[0]; (void)step; (void)frac; }
42 static __inline ALfloat lerp32(const ALfloat *vals, ALint step, ALint frac)
43 { return lerp(vals[0], vals[step], frac * (1.0f/FRACTIONONE)); }
44 static __inline ALfloat cubic32(const ALfloat *vals, ALint step, ALint frac)
45 { return cubic(vals[-step], vals[0], vals[step], vals[step+step],
46 frac * (1.0f/FRACTIONONE)); }
48 #ifdef __GNUC__
49 #define LIKELY(x) __builtin_expect(!!(x), 1)
50 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
51 #else
52 #define LIKELY(x) (x)
53 #define UNLIKELY(x) (x)
54 #endif
56 #if defined(__ARM_NEON__) && defined(HAVE_ARM_NEON_H)
57 #include <arm_neon.h>
59 static __inline void ApplyCoeffs(ALuint Offset, ALfloat (*RESTRICT Values)[2],
60 ALfloat (*RESTRICT Coeffs)[2],
61 ALfloat left, ALfloat right)
63 ALuint c;
64 float32x4_t leftright4;
66 float32x2_t leftright2 = vdup_n_f32(0.0);
67 leftright2 = vset_lane_f32(left, leftright2, 0);
68 leftright2 = vset_lane_f32(right, leftright2, 1);
69 leftright4 = vcombine_f32(leftright2, leftright2);
71 for(c = 0;c < HRIR_LENGTH;c += 2)
73 const ALuint o0 = (Offset+c)&HRIR_MASK;
74 const ALuint o1 = (o0+1)&HRIR_MASK;
75 float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[o0][0]),
76 vld1_f32((float32_t*)&Values[o1][0]));
77 float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
79 vals = vmlaq_f32(vals, coefs, leftright4);
81 vst1_f32((float32_t*)&Values[o0][0], vget_low_f32(vals));
82 vst1_f32((float32_t*)&Values[o1][0], vget_high_f32(vals));
86 #else
88 static __inline void ApplyCoeffs(ALuint Offset, ALfloat (*RESTRICT Values)[2],
89 ALfloat (*RESTRICT Coeffs)[2],
90 ALfloat left, ALfloat right)
92 ALuint c;
93 for(c = 0;c < HRIR_LENGTH;c++)
95 const ALuint off = (Offset+c)&HRIR_MASK;
96 Values[off][0] += Coeffs[c][0] * left;
97 Values[off][1] += Coeffs[c][1] * right;
101 #endif
103 #define DECL_TEMPLATE(T, sampler) \
104 static void Mix_Hrtf_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
105 const ALvoid *srcdata, ALuint *DataPosInt, ALuint *DataPosFrac, \
106 ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize) \
108 const ALuint NumChannels = Source->NumChannels; \
109 const T *RESTRICT data = srcdata; \
110 const ALint *RESTRICT DelayStep = Source->Params.HrtfDelayStep; \
111 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS]; \
112 ALfloat *RESTRICT ClickRemoval, *RESTRICT PendingClicks; \
113 ALfloat (*RESTRICT CoeffStep)[2] = Source->Params.HrtfCoeffStep; \
114 ALuint pos, frac; \
115 FILTER *DryFilter; \
116 ALuint BufferIdx; \
117 ALuint increment; \
118 ALuint i, out, c; \
119 ALfloat value; \
121 increment = Source->Params.Step; \
123 DryBuffer = Device->DryBuffer; \
124 ClickRemoval = Device->ClickRemoval; \
125 PendingClicks = Device->PendingClicks; \
126 DryFilter = &Source->Params.iirFilter; \
128 pos = 0; \
129 frac = *DataPosFrac; \
131 for(i = 0;i < NumChannels;i++) \
133 ALfloat (*RESTRICT TargetCoeffs)[2] = Source->Params.HrtfCoeffs[i]; \
134 ALuint *RESTRICT TargetDelay = Source->Params.HrtfDelay[i]; \
135 ALfloat *RESTRICT History = Source->HrtfHistory[i]; \
136 ALfloat (*RESTRICT Values)[2] = Source->HrtfValues[i]; \
137 ALint Counter = maxu(Source->HrtfCounter, OutPos) - OutPos; \
138 ALuint Offset = Source->HrtfOffset + OutPos; \
139 ALfloat Coeffs[HRIR_LENGTH][2]; \
140 ALuint Delay[2]; \
141 ALfloat left, right; \
143 pos = 0; \
144 frac = *DataPosFrac; \
146 for(c = 0;c < HRIR_LENGTH;c++) \
148 Coeffs[c][0] = TargetCoeffs[c][0] - (CoeffStep[c][0]*Counter); \
149 Coeffs[c][1] = TargetCoeffs[c][1] - (CoeffStep[c][1]*Counter); \
152 Delay[0] = TargetDelay[0] - (DelayStep[0]*Counter) + 32768; \
153 Delay[1] = TargetDelay[1] - (DelayStep[1]*Counter) + 32768; \
155 if(LIKELY(OutPos == 0)) \
157 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
158 value = lpFilter2PC(DryFilter, i, value); \
160 History[Offset&SRC_HISTORY_MASK] = value; \
161 left = History[(Offset-(Delay[0]>>16))&SRC_HISTORY_MASK]; \
162 right = History[(Offset-(Delay[1]>>16))&SRC_HISTORY_MASK]; \
164 ClickRemoval[FRONT_LEFT] -= Values[(Offset+1)&HRIR_MASK][0] + \
165 Coeffs[0][0] * left; \
166 ClickRemoval[FRONT_RIGHT] -= Values[(Offset+1)&HRIR_MASK][1] + \
167 Coeffs[0][1] * right; \
169 for(BufferIdx = 0;BufferIdx < BufferSize && Counter > 0;BufferIdx++) \
171 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
172 value = lpFilter2P(DryFilter, i, value); \
174 History[Offset&SRC_HISTORY_MASK] = value; \
175 left = History[(Offset-(Delay[0]>>16))&SRC_HISTORY_MASK]; \
176 right = History[(Offset-(Delay[1]>>16))&SRC_HISTORY_MASK]; \
178 Delay[0] += DelayStep[0]; \
179 Delay[1] += DelayStep[1]; \
181 Values[Offset&HRIR_MASK][0] = 0.0f; \
182 Values[Offset&HRIR_MASK][1] = 0.0f; \
183 Offset++; \
185 for(c = 0;c < HRIR_LENGTH;c++) \
187 const ALuint off = (Offset+c)&HRIR_MASK; \
188 Values[off][0] += Coeffs[c][0] * left; \
189 Values[off][1] += Coeffs[c][1] * right; \
190 Coeffs[c][0] += CoeffStep[c][0]; \
191 Coeffs[c][1] += CoeffStep[c][1]; \
194 DryBuffer[OutPos][FRONT_LEFT] += Values[Offset&HRIR_MASK][0]; \
195 DryBuffer[OutPos][FRONT_RIGHT] += Values[Offset&HRIR_MASK][1]; \
197 frac += increment; \
198 pos += frac>>FRACTIONBITS; \
199 frac &= FRACTIONMASK; \
200 OutPos++; \
201 Counter--; \
204 Delay[0] >>= 16; \
205 Delay[1] >>= 16; \
206 for(;BufferIdx < BufferSize;BufferIdx++) \
208 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
209 value = lpFilter2P(DryFilter, i, value); \
211 History[Offset&SRC_HISTORY_MASK] = value; \
212 left = History[(Offset-Delay[0])&SRC_HISTORY_MASK]; \
213 right = History[(Offset-Delay[1])&SRC_HISTORY_MASK]; \
215 Values[Offset&HRIR_MASK][0] = 0.0f; \
216 Values[Offset&HRIR_MASK][1] = 0.0f; \
217 Offset++; \
219 ApplyCoeffs(Offset, Values, Coeffs, left, right); \
220 DryBuffer[OutPos][FRONT_LEFT] += Values[Offset&HRIR_MASK][0]; \
221 DryBuffer[OutPos][FRONT_RIGHT] += Values[Offset&HRIR_MASK][1]; \
223 frac += increment; \
224 pos += frac>>FRACTIONBITS; \
225 frac &= FRACTIONMASK; \
226 OutPos++; \
228 if(LIKELY(OutPos == SamplesToDo)) \
230 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
231 value = lpFilter2PC(DryFilter, i, value); \
233 History[Offset&SRC_HISTORY_MASK] = value; \
234 left = History[(Offset-Delay[0])&SRC_HISTORY_MASK]; \
235 right = History[(Offset-Delay[1])&SRC_HISTORY_MASK]; \
237 PendingClicks[FRONT_LEFT] += Values[(Offset+1)&HRIR_MASK][0] + \
238 Coeffs[0][0] * left; \
239 PendingClicks[FRONT_RIGHT] += Values[(Offset+1)&HRIR_MASK][1] + \
240 Coeffs[0][1] * right; \
242 OutPos -= BufferSize; \
245 for(out = 0;out < Device->NumAuxSends;out++) \
247 ALeffectslot *Slot = Source->Params.Send[out].Slot; \
248 ALfloat WetSend; \
249 ALfloat *RESTRICT WetBuffer; \
250 ALfloat *RESTRICT WetClickRemoval; \
251 ALfloat *RESTRICT WetPendingClicks; \
252 FILTER *WetFilter; \
254 if(Slot == NULL) \
255 continue; \
257 WetBuffer = Slot->WetBuffer; \
258 WetClickRemoval = Slot->ClickRemoval; \
259 WetPendingClicks = Slot->PendingClicks; \
260 WetFilter = &Source->Params.Send[out].iirFilter; \
261 WetSend = Source->Params.Send[out].WetGain; \
263 for(i = 0;i < NumChannels;i++) \
265 pos = 0; \
266 frac = *DataPosFrac; \
268 if(LIKELY(OutPos == 0)) \
270 value = sampler(data + pos*NumChannels + i, NumChannels,frac);\
271 value = lpFilter1PC(WetFilter, i, value); \
273 WetClickRemoval[0] -= value * WetSend; \
275 for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \
277 value = sampler(data + pos*NumChannels + i, NumChannels,frac);\
278 value = lpFilter1P(WetFilter, i, value); \
280 WetBuffer[OutPos] += value * WetSend; \
282 frac += increment; \
283 pos += frac>>FRACTIONBITS; \
284 frac &= FRACTIONMASK; \
285 OutPos++; \
287 if(LIKELY(OutPos == SamplesToDo)) \
289 value = sampler(data + pos*NumChannels + i, NumChannels,frac);\
290 value = lpFilter1PC(WetFilter, i, value); \
292 WetPendingClicks[0] += value * WetSend; \
294 OutPos -= BufferSize; \
297 *DataPosInt += pos; \
298 *DataPosFrac = frac; \
301 DECL_TEMPLATE(ALfloat, point32)
302 DECL_TEMPLATE(ALfloat, lerp32)
303 DECL_TEMPLATE(ALfloat, cubic32)
305 #undef DECL_TEMPLATE
308 #define DECL_TEMPLATE(T, sampler) \
309 static void Mix_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
310 const ALvoid *srcdata, ALuint *DataPosInt, ALuint *DataPosFrac, \
311 ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize) \
313 const ALuint NumChannels = Source->NumChannels; \
314 const T *RESTRICT data = srcdata; \
315 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS]; \
316 ALfloat *RESTRICT ClickRemoval, *RESTRICT PendingClicks; \
317 ALfloat DrySend[MAXCHANNELS]; \
318 FILTER *DryFilter; \
319 ALuint pos, frac; \
320 ALuint BufferIdx; \
321 ALuint increment; \
322 ALuint i, out, c; \
323 ALfloat value; \
325 increment = Source->Params.Step; \
327 DryBuffer = Device->DryBuffer; \
328 ClickRemoval = Device->ClickRemoval; \
329 PendingClicks = Device->PendingClicks; \
330 DryFilter = &Source->Params.iirFilter; \
332 pos = 0; \
333 frac = *DataPosFrac; \
335 for(i = 0;i < NumChannels;i++) \
337 for(c = 0;c < MAXCHANNELS;c++) \
338 DrySend[c] = Source->Params.DryGains[i][c]; \
340 pos = 0; \
341 frac = *DataPosFrac; \
343 if(OutPos == 0) \
345 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
347 value = lpFilter2PC(DryFilter, i, value); \
348 for(c = 0;c < MAXCHANNELS;c++) \
349 ClickRemoval[c] -= value*DrySend[c]; \
351 for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \
353 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
355 value = lpFilter2P(DryFilter, i, value); \
356 for(c = 0;c < MAXCHANNELS;c++) \
357 DryBuffer[OutPos][c] += value*DrySend[c]; \
359 frac += increment; \
360 pos += frac>>FRACTIONBITS; \
361 frac &= FRACTIONMASK; \
362 OutPos++; \
364 if(OutPos == SamplesToDo) \
366 value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
368 value = lpFilter2PC(DryFilter, i, value); \
369 for(c = 0;c < MAXCHANNELS;c++) \
370 PendingClicks[c] += value*DrySend[c]; \
372 OutPos -= BufferSize; \
375 for(out = 0;out < Device->NumAuxSends;out++) \
377 ALeffectslot *Slot = Source->Params.Send[out].Slot; \
378 ALfloat WetSend; \
379 ALfloat *WetBuffer; \
380 ALfloat *WetClickRemoval; \
381 ALfloat *WetPendingClicks; \
382 FILTER *WetFilter; \
384 if(Slot == NULL) \
385 continue; \
387 WetBuffer = Slot->WetBuffer; \
388 WetClickRemoval = Slot->ClickRemoval; \
389 WetPendingClicks = Slot->PendingClicks; \
390 WetFilter = &Source->Params.Send[out].iirFilter; \
391 WetSend = Source->Params.Send[out].WetGain; \
393 for(i = 0;i < NumChannels;i++) \
395 pos = 0; \
396 frac = *DataPosFrac; \
398 if(OutPos == 0) \
400 value = sampler(data + pos*NumChannels + i, NumChannels,frac);\
402 value = lpFilter1PC(WetFilter, i, value); \
403 WetClickRemoval[0] -= value * WetSend; \
405 for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \
407 value = sampler(data + pos*NumChannels + i, NumChannels,frac);\
409 value = lpFilter1P(WetFilter, i, value); \
410 WetBuffer[OutPos] += value * WetSend; \
412 frac += increment; \
413 pos += frac>>FRACTIONBITS; \
414 frac &= FRACTIONMASK; \
415 OutPos++; \
417 if(OutPos == SamplesToDo) \
419 value = sampler(data + pos*NumChannels + i, NumChannels,frac);\
421 value = lpFilter1PC(WetFilter, i, value); \
422 WetPendingClicks[0] += value * WetSend; \
424 OutPos -= BufferSize; \
427 *DataPosInt += pos; \
428 *DataPosFrac = frac; \
431 DECL_TEMPLATE(ALfloat, point32)
432 DECL_TEMPLATE(ALfloat, lerp32)
433 DECL_TEMPLATE(ALfloat, cubic32)
435 #undef DECL_TEMPLATE
438 MixerFunc SelectMixer(enum Resampler Resampler)
440 switch(Resampler)
442 case PointResampler:
443 return Mix_ALfloat_point32;
444 case LinearResampler:
445 return Mix_ALfloat_lerp32;
446 case CubicResampler:
447 return Mix_ALfloat_cubic32;
448 case ResamplerMax:
449 break;
451 return NULL;
454 MixerFunc SelectHrtfMixer(enum Resampler Resampler)
456 switch(Resampler)
458 case PointResampler:
459 return Mix_Hrtf_ALfloat_point32;
460 case LinearResampler:
461 return Mix_Hrtf_ALfloat_lerp32;
462 case CubicResampler:
463 return Mix_Hrtf_ALfloat_cubic32;
464 case ResamplerMax:
465 break;
467 return NULL;
471 static __inline ALfloat Sample_ALbyte(ALbyte val)
472 { return val * (1.0f/127.0f); }
474 static __inline ALfloat Sample_ALshort(ALshort val)
475 { return val * (1.0f/32767.0f); }
477 static __inline ALfloat Sample_ALfloat(ALfloat val)
478 { return val; }
480 #define DECL_TEMPLATE(T) \
481 static void Load_##T(ALfloat *dst, const T *src, ALuint samples) \
483 ALuint i; \
484 for(i = 0;i < samples;i++) \
485 dst[i] = Sample_##T(src[i]); \
488 DECL_TEMPLATE(ALbyte)
489 DECL_TEMPLATE(ALshort)
490 DECL_TEMPLATE(ALfloat)
492 #undef DECL_TEMPLATE
494 static void LoadStack(ALfloat *dst, const ALvoid *src, enum FmtType srctype, ALuint samples)
496 switch(srctype)
498 case FmtByte:
499 Load_ALbyte(dst, src, samples);
500 break;
501 case FmtShort:
502 Load_ALshort(dst, src, samples);
503 break;
504 case FmtFloat:
505 Load_ALfloat(dst, src, samples);
506 break;
510 static void SilenceStack(ALfloat *dst, ALuint samples)
512 ALuint i;
513 for(i = 0;i < samples;i++)
514 dst[i] = 0.0f;
518 ALvoid MixSource(ALsource *Source, ALCdevice *Device, ALuint SamplesToDo)
520 ALbufferlistitem *BufferListItem;
521 ALuint DataPosInt, DataPosFrac;
522 ALuint BuffersPlayed;
523 ALboolean Looping;
524 ALuint increment;
525 enum Resampler Resampler;
526 ALenum State;
527 ALuint OutPos;
528 ALuint NumChannels;
529 ALuint FrameSize;
530 ALint64 DataSize64;
531 ALuint i;
533 /* Get source info */
534 State = Source->state;
535 BuffersPlayed = Source->BuffersPlayed;
536 DataPosInt = Source->position;
537 DataPosFrac = Source->position_fraction;
538 Looping = Source->bLooping;
539 increment = Source->Params.Step;
540 Resampler = Source->Resampler;
541 NumChannels = Source->NumChannels;
542 FrameSize = NumChannels * Source->SampleSize;
544 /* Get current buffer queue item */
545 BufferListItem = Source->queue;
546 for(i = 0;i < BuffersPlayed;i++)
547 BufferListItem = BufferListItem->next;
549 OutPos = 0;
550 do {
551 const ALuint BufferPrePadding = ResamplerPrePadding[Resampler];
552 const ALuint BufferPadding = ResamplerPadding[Resampler];
553 ALfloat StackData[STACK_DATA_SIZE/sizeof(ALfloat)];
554 ALfloat *SrcData = StackData;
555 ALuint SrcDataSize = 0;
556 ALuint BufferSize;
558 /* Figure out how many buffer bytes will be needed */
559 DataSize64 = SamplesToDo-OutPos+1;
560 DataSize64 *= increment;
561 DataSize64 += DataPosFrac+FRACTIONMASK;
562 DataSize64 >>= FRACTIONBITS;
563 DataSize64 += BufferPadding+BufferPrePadding;
564 DataSize64 *= NumChannels;
566 BufferSize = (ALuint)mini64(DataSize64, STACK_DATA_SIZE/sizeof(ALfloat));
567 BufferSize /= NumChannels;
569 if(Source->lSourceType == AL_STATIC)
571 const ALbuffer *ALBuffer = Source->queue->buffer;
572 const ALubyte *Data = ALBuffer->data;
573 ALuint DataSize;
574 ALuint pos;
576 /* If current pos is beyond the loop range, do not loop */
577 if(Looping == AL_FALSE || DataPosInt >= (ALuint)ALBuffer->LoopEnd)
579 Looping = AL_FALSE;
581 if(DataPosInt >= BufferPrePadding)
582 pos = DataPosInt - BufferPrePadding;
583 else
585 DataSize = BufferPrePadding - DataPosInt;
586 DataSize = minu(BufferSize, DataSize);
588 SilenceStack(&SrcData[SrcDataSize*NumChannels],
589 DataSize*NumChannels);
590 SrcDataSize += DataSize;
591 BufferSize -= DataSize;
593 pos = 0;
596 /* Copy what's left to play in the source buffer, and clear the
597 * rest of the temp buffer */
598 DataSize = ALBuffer->SampleLen - pos;
599 DataSize = minu(BufferSize, DataSize);
601 LoadStack(&SrcData[SrcDataSize*NumChannels], &Data[pos*FrameSize],
602 ALBuffer->FmtType, DataSize*NumChannels);
603 SrcDataSize += DataSize;
604 BufferSize -= DataSize;
606 SilenceStack(&SrcData[SrcDataSize*NumChannels],
607 BufferSize*NumChannels);
608 SrcDataSize += BufferSize;
609 BufferSize -= BufferSize;
611 else
613 ALuint LoopStart = ALBuffer->LoopStart;
614 ALuint LoopEnd = ALBuffer->LoopEnd;
616 if(DataPosInt >= LoopStart)
618 pos = DataPosInt-LoopStart;
619 while(pos < BufferPrePadding)
620 pos += LoopEnd-LoopStart;
621 pos -= BufferPrePadding;
622 pos += LoopStart;
624 else if(DataPosInt >= BufferPrePadding)
625 pos = DataPosInt - BufferPrePadding;
626 else
628 DataSize = BufferPrePadding - DataPosInt;
629 DataSize = minu(BufferSize, DataSize);
631 SilenceStack(&SrcData[SrcDataSize*NumChannels], DataSize*NumChannels);
632 SrcDataSize += DataSize;
633 BufferSize -= DataSize;
635 pos = 0;
638 /* Copy what's left of this loop iteration, then copy repeats
639 * of the loop section */
640 DataSize = LoopEnd - pos;
641 DataSize = minu(BufferSize, DataSize);
643 LoadStack(&SrcData[SrcDataSize*NumChannels], &Data[pos*FrameSize],
644 ALBuffer->FmtType, DataSize*NumChannels);
645 SrcDataSize += DataSize;
646 BufferSize -= DataSize;
648 DataSize = LoopEnd-LoopStart;
649 while(BufferSize > 0)
651 DataSize = minu(BufferSize, DataSize);
653 LoadStack(&SrcData[SrcDataSize*NumChannels], &Data[LoopStart*FrameSize],
654 ALBuffer->FmtType, DataSize*NumChannels);
655 SrcDataSize += DataSize;
656 BufferSize -= DataSize;
660 else
662 /* Crawl the buffer queue to fill in the temp buffer */
663 ALbufferlistitem *tmpiter = BufferListItem;
664 ALuint pos;
666 if(DataPosInt >= BufferPrePadding)
667 pos = DataPosInt - BufferPrePadding;
668 else
670 pos = BufferPrePadding - DataPosInt;
671 while(pos > 0)
673 if(!tmpiter->prev && !Looping)
675 ALuint DataSize = minu(BufferSize, pos);
677 SilenceStack(&SrcData[SrcDataSize*NumChannels], DataSize*NumChannels);
678 SrcDataSize += DataSize;
679 BufferSize -= DataSize;
681 pos = 0;
682 break;
685 if(tmpiter->prev)
686 tmpiter = tmpiter->prev;
687 else
689 while(tmpiter->next)
690 tmpiter = tmpiter->next;
693 if(tmpiter->buffer)
695 if((ALuint)tmpiter->buffer->SampleLen > pos)
697 pos = tmpiter->buffer->SampleLen - pos;
698 break;
700 pos -= tmpiter->buffer->SampleLen;
705 while(tmpiter && BufferSize > 0)
707 const ALbuffer *ALBuffer;
708 if((ALBuffer=tmpiter->buffer) != NULL)
710 const ALubyte *Data = ALBuffer->data;
711 ALuint DataSize = ALBuffer->SampleLen;
713 /* Skip the data already played */
714 if(DataSize <= pos)
715 pos -= DataSize;
716 else
718 Data += pos*FrameSize;
719 DataSize -= pos;
720 pos -= pos;
722 DataSize = minu(BufferSize, DataSize);
723 LoadStack(&SrcData[SrcDataSize*NumChannels], Data,
724 ALBuffer->FmtType, DataSize*NumChannels);
725 SrcDataSize += DataSize;
726 BufferSize -= DataSize;
729 tmpiter = tmpiter->next;
730 if(!tmpiter && Looping)
731 tmpiter = Source->queue;
732 else if(!tmpiter)
734 SilenceStack(&SrcData[SrcDataSize*NumChannels], BufferSize*NumChannels);
735 SrcDataSize += BufferSize;
736 BufferSize -= BufferSize;
741 /* Figure out how many samples we can mix. */
742 DataSize64 = SrcDataSize;
743 DataSize64 -= BufferPadding+BufferPrePadding;
744 DataSize64 <<= FRACTIONBITS;
745 DataSize64 -= increment;
746 DataSize64 -= DataPosFrac;
748 BufferSize = (ALuint)((DataSize64+(increment-1)) / increment);
749 BufferSize = minu(BufferSize, (SamplesToDo-OutPos));
751 SrcData += BufferPrePadding*NumChannels;
752 Source->Params.DoMix(Source, Device, SrcData, &DataPosInt, &DataPosFrac,
753 OutPos, SamplesToDo, BufferSize);
754 OutPos += BufferSize;
756 /* Handle looping sources */
757 while(1)
759 const ALbuffer *ALBuffer;
760 ALuint DataSize = 0;
761 ALuint LoopStart = 0;
762 ALuint LoopEnd = 0;
764 if((ALBuffer=BufferListItem->buffer) != NULL)
766 DataSize = ALBuffer->SampleLen;
767 LoopStart = ALBuffer->LoopStart;
768 LoopEnd = ALBuffer->LoopEnd;
769 if(LoopEnd > DataPosInt)
770 break;
773 if(Looping && Source->lSourceType == AL_STATIC)
775 DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
776 break;
779 if(DataSize > DataPosInt)
780 break;
782 if(BufferListItem->next)
784 BufferListItem = BufferListItem->next;
785 BuffersPlayed++;
787 else if(Looping)
789 BufferListItem = Source->queue;
790 BuffersPlayed = 0;
792 else
794 State = AL_STOPPED;
795 BufferListItem = Source->queue;
796 BuffersPlayed = Source->BuffersInQueue;
797 DataPosInt = 0;
798 DataPosFrac = 0;
799 break;
802 DataPosInt -= DataSize;
804 } while(State == AL_PLAYING && OutPos < SamplesToDo);
806 /* Update source info */
807 Source->state = State;
808 Source->BuffersPlayed = BuffersPlayed;
809 Source->position = DataPosInt;
810 Source->position_fraction = DataPosFrac;
811 Source->HrtfOffset += OutPos;
812 if(State == AL_PLAYING)
814 Source->HrtfCounter = maxu(Source->HrtfCounter, OutPos) - OutPos;
815 Source->HrtfMoving = AL_TRUE;
817 else
819 Source->HrtfCounter = 0;
820 Source->HrtfMoving = AL_FALSE;