Rename NfcFilterUpdate* to NfcFilterProcess* for consistency
[openal-soft.git] / Alc / mixvoice.c
blob8a382ffd6668ef3b4959f6689e0bf2a907150ee2
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 "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 "sample_cvt.h"
37 #include "alu.h"
38 #include "alconfig.h"
39 #include "ringbuffer.h"
41 #include "cpu_caps.h"
42 #include "mixer/defs.h"
45 static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
46 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
48 extern inline void InitiatePositionArrays(ALsizei frac, ALint increment, ALsizei *restrict frac_arr, ALint *restrict pos_arr, ALsizei size);
51 /* BSinc24 requires up to 23 extra samples before the current position, and 24 after. */
52 static_assert(MAX_RESAMPLE_PADDING >= 24, "MAX_RESAMPLE_PADDING must be at least 24!");
55 enum Resampler ResamplerDefault = LinearResampler;
57 MixerFunc MixSamples = Mix_C;
58 RowMixerFunc MixRowSamples = MixRow_C;
59 static HrtfMixerFunc MixHrtfSamples = MixHrtf_C;
60 static HrtfMixerBlendFunc MixHrtfBlendSamples = MixHrtfBlend_C;
62 static MixerFunc SelectMixer(void)
64 #ifdef HAVE_NEON
65 if((CPUCapFlags&CPU_CAP_NEON))
66 return Mix_Neon;
67 #endif
68 #ifdef HAVE_SSE
69 if((CPUCapFlags&CPU_CAP_SSE))
70 return Mix_SSE;
71 #endif
72 return Mix_C;
75 static RowMixerFunc SelectRowMixer(void)
77 #ifdef HAVE_NEON
78 if((CPUCapFlags&CPU_CAP_NEON))
79 return MixRow_Neon;
80 #endif
81 #ifdef HAVE_SSE
82 if((CPUCapFlags&CPU_CAP_SSE))
83 return MixRow_SSE;
84 #endif
85 return MixRow_C;
88 static inline HrtfMixerFunc SelectHrtfMixer(void)
90 #ifdef HAVE_NEON
91 if((CPUCapFlags&CPU_CAP_NEON))
92 return MixHrtf_Neon;
93 #endif
94 #ifdef HAVE_SSE
95 if((CPUCapFlags&CPU_CAP_SSE))
96 return MixHrtf_SSE;
97 #endif
98 return MixHrtf_C;
101 static inline HrtfMixerBlendFunc SelectHrtfBlendMixer(void)
103 #ifdef HAVE_NEON
104 if((CPUCapFlags&CPU_CAP_NEON))
105 return MixHrtfBlend_Neon;
106 #endif
107 #ifdef HAVE_SSE
108 if((CPUCapFlags&CPU_CAP_SSE))
109 return MixHrtfBlend_SSE;
110 #endif
111 return MixHrtfBlend_C;
114 ResamplerFunc SelectResampler(enum Resampler resampler)
116 switch(resampler)
118 case PointResampler:
119 return Resample_point_C;
120 case LinearResampler:
121 #ifdef HAVE_NEON
122 if((CPUCapFlags&CPU_CAP_NEON))
123 return Resample_lerp_Neon;
124 #endif
125 #ifdef HAVE_SSE4_1
126 if((CPUCapFlags&CPU_CAP_SSE4_1))
127 return Resample_lerp_SSE41;
128 #endif
129 #ifdef HAVE_SSE2
130 if((CPUCapFlags&CPU_CAP_SSE2))
131 return Resample_lerp_SSE2;
132 #endif
133 return Resample_lerp_C;
134 case FIR4Resampler:
135 return Resample_cubic_C;
136 case BSinc12Resampler:
137 case BSinc24Resampler:
138 #ifdef HAVE_NEON
139 if((CPUCapFlags&CPU_CAP_NEON))
140 return Resample_bsinc_Neon;
141 #endif
142 #ifdef HAVE_SSE
143 if((CPUCapFlags&CPU_CAP_SSE))
144 return Resample_bsinc_SSE;
145 #endif
146 return Resample_bsinc_C;
149 return Resample_point_C;
153 void aluInitMixer(void)
155 const char *str;
157 if(ConfigValueStr(NULL, NULL, "resampler", &str))
159 if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
160 ResamplerDefault = PointResampler;
161 else if(strcasecmp(str, "linear") == 0)
162 ResamplerDefault = LinearResampler;
163 else if(strcasecmp(str, "cubic") == 0)
164 ResamplerDefault = FIR4Resampler;
165 else if(strcasecmp(str, "bsinc12") == 0)
166 ResamplerDefault = BSinc12Resampler;
167 else if(strcasecmp(str, "bsinc24") == 0)
168 ResamplerDefault = BSinc24Resampler;
169 else if(strcasecmp(str, "bsinc") == 0)
171 WARN("Resampler option \"%s\" is deprecated, using bsinc12\n", str);
172 ResamplerDefault = BSinc12Resampler;
174 else if(strcasecmp(str, "sinc4") == 0 || strcasecmp(str, "sinc8") == 0)
176 WARN("Resampler option \"%s\" is deprecated, using cubic\n", str);
177 ResamplerDefault = FIR4Resampler;
179 else
181 char *end;
182 long n = strtol(str, &end, 0);
183 if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == FIR4Resampler))
184 ResamplerDefault = n;
185 else
186 WARN("Invalid resampler: %s\n", str);
190 MixHrtfBlendSamples = SelectHrtfBlendMixer();
191 MixHrtfSamples = SelectHrtfMixer();
192 MixSamples = SelectMixer();
193 MixRowSamples = SelectRowMixer();
197 static void SendAsyncEvent(ALCcontext *context, ALuint enumtype, ALenum type,
198 ALuint objid, ALuint param, const char *msg)
200 AsyncEvent evt;
201 evt.EnumType = enumtype;
202 evt.Type = type;
203 evt.ObjectId = objid;
204 evt.Param = param;
205 strcpy(evt.Message, msg);
206 if(ll_ringbuffer_write(context->AsyncEvents, (const char*)&evt, 1) == 1)
207 alsem_post(&context->EventSem);
211 static inline ALfloat Sample_ALubyte(ALubyte val)
212 { return (val-128) * (1.0f/128.0f); }
214 static inline ALfloat Sample_ALshort(ALshort val)
215 { return val * (1.0f/32768.0f); }
217 static inline ALfloat Sample_ALfloat(ALfloat val)
218 { return val; }
220 static inline ALfloat Sample_ALdouble(ALdouble val)
221 { return (ALfloat)val; }
223 typedef ALubyte ALmulaw;
224 static inline ALfloat Sample_ALmulaw(ALmulaw val)
225 { return muLawDecompressionTable[val] * (1.0f/32768.0f); }
227 typedef ALubyte ALalaw;
228 static inline ALfloat Sample_ALalaw(ALalaw val)
229 { return aLawDecompressionTable[val] * (1.0f/32768.0f); }
231 #define DECL_TEMPLATE(T) \
232 static inline void Load_##T(ALfloat *restrict dst, const T *restrict src, \
233 ALint srcstep, ALsizei samples) \
235 ALsizei i; \
236 for(i = 0;i < samples;i++) \
237 dst[i] += Sample_##T(src[i*srcstep]); \
240 DECL_TEMPLATE(ALubyte)
241 DECL_TEMPLATE(ALshort)
242 DECL_TEMPLATE(ALfloat)
243 DECL_TEMPLATE(ALdouble)
244 DECL_TEMPLATE(ALmulaw)
245 DECL_TEMPLATE(ALalaw)
247 #undef DECL_TEMPLATE
249 static void LoadSamples(ALfloat *restrict dst, const ALvoid *restrict src, ALint srcstep,
250 enum FmtType srctype, ALsizei samples)
252 #define HANDLE_FMT(ET, ST) case ET: Load_##ST(dst, src, srcstep, samples); break
253 switch(srctype)
255 HANDLE_FMT(FmtUByte, ALubyte);
256 HANDLE_FMT(FmtShort, ALshort);
257 HANDLE_FMT(FmtFloat, ALfloat);
258 HANDLE_FMT(FmtDouble, ALdouble);
259 HANDLE_FMT(FmtMulaw, ALmulaw);
260 HANDLE_FMT(FmtAlaw, ALalaw);
262 #undef HANDLE_FMT
266 static const ALfloat *DoFilters(BiquadState *lpfilter, BiquadState *hpfilter,
267 ALfloat *restrict dst, const ALfloat *restrict src,
268 ALsizei numsamples, enum ActiveFilters type)
270 ALsizei i;
271 switch(type)
273 case AF_None:
274 BiquadState_processPassthru(lpfilter, src, numsamples);
275 BiquadState_processPassthru(hpfilter, src, numsamples);
276 break;
278 case AF_LowPass:
279 BiquadState_process(lpfilter, dst, src, numsamples);
280 BiquadState_processPassthru(hpfilter, dst, numsamples);
281 return dst;
282 case AF_HighPass:
283 BiquadState_processPassthru(lpfilter, src, numsamples);
284 BiquadState_process(hpfilter, dst, src, numsamples);
285 return dst;
287 case AF_BandPass:
288 for(i = 0;i < numsamples;)
290 ALfloat temp[256];
291 ALsizei todo = mini(256, numsamples-i);
293 BiquadState_process(lpfilter, temp, src+i, todo);
294 BiquadState_process(hpfilter, dst+i, temp, todo);
295 i += todo;
297 return dst;
299 return src;
303 /* This function uses these device temp buffers. */
304 #define SOURCE_DATA_BUF 0
305 #define RESAMPLED_BUF 1
306 #define FILTERED_BUF 2
307 #define NFC_DATA_BUF 3
308 ALboolean MixSource(ALvoice *voice, ALuint SourceID, ALCcontext *Context, ALsizei SamplesToDo)
310 ALCdevice *Device = Context->Device;
311 ALbufferlistitem *BufferListItem;
312 ALbufferlistitem *BufferLoopItem;
313 ALsizei NumChannels, SampleSize;
314 ALbitfieldSOFT enabledevt;
315 ALsizei buffers_done = 0;
316 ResamplerFunc Resample;
317 ALsizei DataPosInt;
318 ALsizei DataPosFrac;
319 ALint64 DataSize64;
320 ALint increment;
321 ALsizei Counter;
322 ALsizei OutPos;
323 ALsizei IrSize;
324 bool isplaying;
325 bool firstpass;
326 bool isstatic;
327 ALsizei chan;
328 ALsizei send;
330 /* Get source info */
331 isplaying = true; /* Will only be called while playing. */
332 isstatic = !!(voice->Flags&VOICE_IS_STATIC);
333 DataPosInt = ATOMIC_LOAD(&voice->position, almemory_order_acquire);
334 DataPosFrac = ATOMIC_LOAD(&voice->position_fraction, almemory_order_relaxed);
335 BufferListItem = ATOMIC_LOAD(&voice->current_buffer, almemory_order_relaxed);
336 BufferLoopItem = ATOMIC_LOAD(&voice->loop_buffer, almemory_order_relaxed);
337 NumChannels = voice->NumChannels;
338 SampleSize = voice->SampleSize;
339 increment = voice->Step;
341 IrSize = (Device->HrtfHandle ? Device->HrtfHandle->irSize : 0);
343 Resample = ((increment == FRACTIONONE && DataPosFrac == 0) ?
344 Resample_copy_C : voice->Resampler);
346 Counter = (voice->Flags&VOICE_IS_FADING) ? SamplesToDo : 0;
347 firstpass = true;
348 OutPos = 0;
350 do {
351 ALsizei SrcBufferSize, DstBufferSize;
353 /* Figure out how many buffer samples will be needed */
354 DataSize64 = SamplesToDo-OutPos;
355 DataSize64 *= increment;
356 DataSize64 += DataPosFrac+FRACTIONMASK;
357 DataSize64 >>= FRACTIONBITS;
358 DataSize64 += MAX_RESAMPLE_PADDING*2;
359 SrcBufferSize = (ALsizei)mini64(DataSize64, BUFFERSIZE);
361 /* Figure out how many samples we can actually mix from this. */
362 DataSize64 = SrcBufferSize;
363 DataSize64 -= MAX_RESAMPLE_PADDING*2;
364 DataSize64 <<= FRACTIONBITS;
365 DataSize64 -= DataPosFrac;
366 DstBufferSize = (ALsizei)mini64((DataSize64+(increment-1)) / increment,
367 SamplesToDo - OutPos);
369 /* Some mixers like having a multiple of 4, so try to give that unless
370 * this is the last update. */
371 if(DstBufferSize < SamplesToDo-OutPos)
372 DstBufferSize &= ~3;
374 /* It's impossible to have a buffer list item with no entries. */
375 assert(BufferListItem->num_buffers > 0);
377 for(chan = 0;chan < NumChannels;chan++)
379 const ALfloat *ResampledData;
380 ALfloat *SrcData = Device->TempBuffer[SOURCE_DATA_BUF];
381 ALsizei FilledAmt;
383 /* Load the previous samples into the source data first, and clear the rest. */
384 memcpy(SrcData, voice->PrevSamples[chan], MAX_RESAMPLE_PADDING*sizeof(ALfloat));
385 memset(SrcData+MAX_RESAMPLE_PADDING, 0, (BUFFERSIZE-MAX_RESAMPLE_PADDING)*
386 sizeof(ALfloat));
387 FilledAmt = MAX_RESAMPLE_PADDING;
389 if(isstatic)
391 /* TODO: For static sources, loop points are taken from the
392 * first buffer (should be adjusted by any buffer offset, to
393 * possibly be added later).
395 const ALbuffer *Buffer0 = BufferListItem->buffers[0];
396 const ALsizei LoopStart = Buffer0->LoopStart;
397 const ALsizei LoopEnd = Buffer0->LoopEnd;
398 const ALsizei LoopSize = LoopEnd - LoopStart;
400 /* If current pos is beyond the loop range, do not loop */
401 if(!BufferLoopItem || DataPosInt >= LoopEnd)
403 ALsizei SizeToDo = SrcBufferSize - FilledAmt;
404 ALsizei CompLen = 0;
405 ALsizei i;
407 BufferLoopItem = NULL;
409 for(i = 0;i < BufferListItem->num_buffers;i++)
411 const ALbuffer *buffer = BufferListItem->buffers[i];
412 const ALubyte *Data = buffer->data;
413 ALsizei DataSize;
415 if(DataPosInt >= buffer->SampleLen)
416 continue;
418 /* Load what's left to play from the buffer */
419 DataSize = mini(SizeToDo, buffer->SampleLen - DataPosInt);
420 CompLen = maxi(CompLen, DataSize);
422 LoadSamples(&SrcData[FilledAmt],
423 &Data[(DataPosInt*NumChannels + chan)*SampleSize],
424 NumChannels, buffer->FmtType, DataSize
427 FilledAmt += CompLen;
429 else
431 ALsizei SizeToDo = mini(SrcBufferSize - FilledAmt, LoopEnd - DataPosInt);
432 ALsizei CompLen = 0;
433 ALsizei i;
435 for(i = 0;i < BufferListItem->num_buffers;i++)
437 const ALbuffer *buffer = BufferListItem->buffers[i];
438 const ALubyte *Data = buffer->data;
439 ALsizei DataSize;
441 if(DataPosInt >= buffer->SampleLen)
442 continue;
444 /* Load what's left of this loop iteration */
445 DataSize = mini(SizeToDo, buffer->SampleLen - DataPosInt);
446 CompLen = maxi(CompLen, DataSize);
448 LoadSamples(&SrcData[FilledAmt],
449 &Data[(DataPosInt*NumChannels + chan)*SampleSize],
450 NumChannels, buffer->FmtType, DataSize
453 FilledAmt += CompLen;
455 while(SrcBufferSize > FilledAmt)
457 const ALsizei SizeToDo = mini(SrcBufferSize - FilledAmt, LoopSize);
459 CompLen = 0;
460 for(i = 0;i < BufferListItem->num_buffers;i++)
462 const ALbuffer *buffer = BufferListItem->buffers[i];
463 const ALubyte *Data = buffer->data;
464 ALsizei DataSize;
466 if(LoopStart >= buffer->SampleLen)
467 continue;
469 DataSize = mini(SizeToDo, buffer->SampleLen - LoopStart);
470 CompLen = maxi(CompLen, DataSize);
472 LoadSamples(&SrcData[FilledAmt],
473 &Data[(LoopStart*NumChannels + chan)*SampleSize],
474 NumChannels, buffer->FmtType, DataSize
477 FilledAmt += CompLen;
481 else
483 /* Crawl the buffer queue to fill in the temp buffer */
484 ALbufferlistitem *tmpiter = BufferListItem;
485 ALsizei pos = DataPosInt;
487 while(tmpiter && SrcBufferSize > FilledAmt)
489 ALsizei SizeToDo = SrcBufferSize - FilledAmt;
490 ALsizei CompLen = 0;
491 ALsizei i;
493 for(i = 0;i < tmpiter->num_buffers;i++)
495 const ALbuffer *ALBuffer = tmpiter->buffers[i];
496 ALsizei DataSize = ALBuffer ? ALBuffer->SampleLen : 0;
497 CompLen = maxi(CompLen, DataSize);
499 if(DataSize > pos)
501 const ALubyte *Data = ALBuffer->data;
502 Data += (pos*NumChannels + chan)*SampleSize;
504 DataSize = minu(SizeToDo, DataSize - pos);
505 LoadSamples(&SrcData[FilledAmt], Data, NumChannels,
506 ALBuffer->FmtType, DataSize);
509 if(pos > CompLen)
510 pos -= CompLen;
511 else
513 FilledAmt += CompLen - pos;
514 pos = 0;
516 if(SrcBufferSize > FilledAmt)
518 tmpiter = ATOMIC_LOAD(&tmpiter->next, almemory_order_acquire);
519 if(!tmpiter) tmpiter = BufferLoopItem;
524 /* Store the last source samples used for next time. */
525 memcpy(voice->PrevSamples[chan],
526 &SrcData[(increment*DstBufferSize + DataPosFrac)>>FRACTIONBITS],
527 MAX_RESAMPLE_PADDING*sizeof(ALfloat)
530 /* Now resample, then filter and mix to the appropriate outputs. */
531 ResampledData = Resample(&voice->ResampleState,
532 &SrcData[MAX_RESAMPLE_PADDING], DataPosFrac, increment,
533 Device->TempBuffer[RESAMPLED_BUF], DstBufferSize
536 DirectParams *parms = &voice->Direct.Params[chan];
537 const ALfloat *samples;
539 samples = DoFilters(
540 &parms->LowPass, &parms->HighPass, Device->TempBuffer[FILTERED_BUF],
541 ResampledData, DstBufferSize, voice->Direct.FilterType
543 if(!(voice->Flags&VOICE_HAS_HRTF))
545 if(!Counter)
546 memcpy(parms->Gains.Current, parms->Gains.Target,
547 sizeof(parms->Gains.Current));
548 if(!(voice->Flags&VOICE_HAS_NFC))
549 MixSamples(samples, voice->Direct.Channels, voice->Direct.Buffer,
550 parms->Gains.Current, parms->Gains.Target, Counter, OutPos,
551 DstBufferSize
553 else
555 ALfloat *nfcsamples = Device->TempBuffer[NFC_DATA_BUF];
556 ALsizei chanoffset = 0;
558 MixSamples(samples,
559 voice->Direct.ChannelsPerOrder[0], voice->Direct.Buffer,
560 parms->Gains.Current, parms->Gains.Target, Counter, OutPos,
561 DstBufferSize
563 chanoffset += voice->Direct.ChannelsPerOrder[0];
564 #define APPLY_NFC_MIX(order) \
565 if(voice->Direct.ChannelsPerOrder[order] > 0) \
567 NfcFilterProcess##order(&parms->NFCtrlFilter, nfcsamples, samples, \
568 DstBufferSize); \
569 MixSamples(nfcsamples, voice->Direct.ChannelsPerOrder[order], \
570 voice->Direct.Buffer+chanoffset, parms->Gains.Current+chanoffset, \
571 parms->Gains.Target+chanoffset, Counter, OutPos, DstBufferSize \
572 ); \
573 chanoffset += voice->Direct.ChannelsPerOrder[order]; \
575 APPLY_NFC_MIX(1)
576 APPLY_NFC_MIX(2)
577 APPLY_NFC_MIX(3)
578 #undef APPLY_NFC_MIX
581 else
583 MixHrtfParams hrtfparams;
584 ALsizei fademix = 0;
585 int lidx, ridx;
587 lidx = GetChannelIdxByName(&Device->RealOut, FrontLeft);
588 ridx = GetChannelIdxByName(&Device->RealOut, FrontRight);
589 assert(lidx != -1 && ridx != -1);
591 if(!Counter)
593 /* No fading, just overwrite the old HRTF params. */
594 parms->Hrtf.Old = parms->Hrtf.Target;
596 else if(!(parms->Hrtf.Old.Gain > GAIN_SILENCE_THRESHOLD))
598 /* The old HRTF params are silent, so overwrite the old
599 * coefficients with the new, and reset the old gain to
600 * 0. The future mix will then fade from silence.
602 parms->Hrtf.Old = parms->Hrtf.Target;
603 parms->Hrtf.Old.Gain = 0.0f;
605 else if(firstpass)
607 ALfloat gain;
609 /* Fade between the coefficients over 128 samples. */
610 fademix = mini(DstBufferSize, 128);
612 /* The new coefficients need to fade in completely
613 * since they're replacing the old ones. To keep the
614 * gain fading consistent, interpolate between the old
615 * and new target gains given how much of the fade time
616 * this mix handles.
618 gain = lerp(parms->Hrtf.Old.Gain, parms->Hrtf.Target.Gain,
619 minf(1.0f, (ALfloat)fademix/Counter));
620 hrtfparams.Coeffs = parms->Hrtf.Target.Coeffs;
621 hrtfparams.Delay[0] = parms->Hrtf.Target.Delay[0];
622 hrtfparams.Delay[1] = parms->Hrtf.Target.Delay[1];
623 hrtfparams.Gain = 0.0f;
624 hrtfparams.GainStep = gain / (ALfloat)fademix;
626 MixHrtfBlendSamples(
627 voice->Direct.Buffer[lidx], voice->Direct.Buffer[ridx],
628 samples, voice->Offset, OutPos, IrSize, &parms->Hrtf.Old,
629 &hrtfparams, &parms->Hrtf.State, fademix
631 /* Update the old parameters with the result. */
632 parms->Hrtf.Old = parms->Hrtf.Target;
633 if(fademix < Counter)
634 parms->Hrtf.Old.Gain = hrtfparams.Gain;
637 if(fademix < DstBufferSize)
639 ALsizei todo = DstBufferSize - fademix;
640 ALfloat gain = parms->Hrtf.Target.Gain;
642 /* Interpolate the target gain if the gain fading lasts
643 * longer than this mix.
645 if(Counter > DstBufferSize)
646 gain = lerp(parms->Hrtf.Old.Gain, gain,
647 (ALfloat)todo/(Counter-fademix));
649 hrtfparams.Coeffs = parms->Hrtf.Target.Coeffs;
650 hrtfparams.Delay[0] = parms->Hrtf.Target.Delay[0];
651 hrtfparams.Delay[1] = parms->Hrtf.Target.Delay[1];
652 hrtfparams.Gain = parms->Hrtf.Old.Gain;
653 hrtfparams.GainStep = (gain - parms->Hrtf.Old.Gain) / (ALfloat)todo;
654 MixHrtfSamples(
655 voice->Direct.Buffer[lidx], voice->Direct.Buffer[ridx],
656 samples+fademix, voice->Offset+fademix, OutPos+fademix, IrSize,
657 &hrtfparams, &parms->Hrtf.State, todo
659 /* Store the interpolated gain or the final target gain
660 * depending if the fade is done.
662 if(DstBufferSize < Counter)
663 parms->Hrtf.Old.Gain = gain;
664 else
665 parms->Hrtf.Old.Gain = parms->Hrtf.Target.Gain;
670 for(send = 0;send < Device->NumAuxSends;send++)
672 SendParams *parms = &voice->Send[send].Params[chan];
673 const ALfloat *samples;
675 if(!voice->Send[send].Buffer)
676 continue;
678 samples = DoFilters(
679 &parms->LowPass, &parms->HighPass, Device->TempBuffer[FILTERED_BUF],
680 ResampledData, DstBufferSize, voice->Send[send].FilterType
683 if(!Counter)
684 memcpy(parms->Gains.Current, parms->Gains.Target,
685 sizeof(parms->Gains.Current));
686 MixSamples(samples, voice->Send[send].Channels, voice->Send[send].Buffer,
687 parms->Gains.Current, parms->Gains.Target, Counter, OutPos, DstBufferSize
691 /* Update positions */
692 DataPosFrac += increment*DstBufferSize;
693 DataPosInt += DataPosFrac>>FRACTIONBITS;
694 DataPosFrac &= FRACTIONMASK;
696 OutPos += DstBufferSize;
697 voice->Offset += DstBufferSize;
698 Counter = maxi(DstBufferSize, Counter) - DstBufferSize;
699 firstpass = false;
701 if(isstatic)
703 if(BufferLoopItem)
705 /* Handle looping static source */
706 const ALbuffer *Buffer = BufferListItem->buffers[0];
707 ALsizei LoopStart = Buffer->LoopStart;
708 ALsizei LoopEnd = Buffer->LoopEnd;
709 if(DataPosInt >= LoopEnd)
711 assert(LoopEnd > LoopStart);
712 DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
715 else
717 /* Handle non-looping static source */
718 ALsizei CompLen = 0;
719 ALsizei i;
721 for(i = 0;i < BufferListItem->num_buffers;i++)
723 const ALbuffer *buffer = BufferListItem->buffers[i];
724 if(buffer) CompLen = maxi(CompLen, buffer->SampleLen);
727 if(DataPosInt >= CompLen)
729 isplaying = false;
730 BufferListItem = NULL;
731 DataPosInt = 0;
732 DataPosFrac = 0;
733 break;
737 else while(1)
739 /* Handle streaming source */
740 ALsizei CompLen = 0;
741 ALsizei i;
743 for(i = 0;i < BufferListItem->num_buffers;i++)
745 const ALbuffer *buffer = BufferListItem->buffers[i];
746 if(buffer) CompLen = maxi(CompLen, buffer->SampleLen);
749 if(CompLen > DataPosInt)
750 break;
752 buffers_done += BufferListItem->num_buffers;
753 BufferListItem = ATOMIC_LOAD(&BufferListItem->next, almemory_order_acquire);
754 if(!BufferListItem && !(BufferListItem=BufferLoopItem))
756 isplaying = false;
757 DataPosInt = 0;
758 DataPosFrac = 0;
759 break;
762 DataPosInt -= CompLen;
764 } while(isplaying && OutPos < SamplesToDo);
766 voice->Flags |= VOICE_IS_FADING;
768 /* Update source info */
769 ATOMIC_STORE(&voice->position, DataPosInt, almemory_order_relaxed);
770 ATOMIC_STORE(&voice->position_fraction, DataPosFrac, almemory_order_relaxed);
771 ATOMIC_STORE(&voice->current_buffer, BufferListItem, almemory_order_release);
773 /* Send any events now, after the position/buffer info was updated. */
774 enabledevt = ATOMIC_LOAD(&Context->EnabledEvts, almemory_order_acquire);
775 if(buffers_done > 0 && (enabledevt&EventType_BufferCompleted))
776 SendAsyncEvent(Context, EventType_BufferCompleted,
777 AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT, SourceID, buffers_done, "Buffer completed"
780 return isplaying;