Rework HRTF decision logic
[openal-soft.git] / Alc / mixer.c
blob4a98ee8f26eda5a3c37ebefea89594b723d6b030
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 "alu.h"
38 #include "mixer_defs.h"
41 extern inline void InitiatePositionArrays(ALuint frac, ALuint increment, ALuint *frac_arr, ALuint *pos_arr, ALuint size);
44 static inline MixerFunc SelectMixer(void)
46 #ifdef HAVE_SSE
47 if((CPUCapFlags&CPU_CAP_SSE))
48 return Mix_SSE;
49 #endif
50 #ifdef HAVE_NEON
51 if((CPUCapFlags&CPU_CAP_NEON))
52 return Mix_Neon;
53 #endif
55 return Mix_C;
58 static inline ResamplerFunc SelectResampler(enum Resampler resampler)
60 switch(resampler)
62 case PointResampler:
63 return Resample_point32_C;
64 case LinearResampler:
65 #ifdef HAVE_SSE4_1
66 if((CPUCapFlags&CPU_CAP_SSE4_1))
67 return Resample_lerp32_SSE41;
68 #endif
69 #ifdef HAVE_SSE2
70 if((CPUCapFlags&CPU_CAP_SSE2))
71 return Resample_lerp32_SSE2;
72 #endif
73 return Resample_lerp32_C;
74 case CubicResampler:
75 return Resample_cubic32_C;
76 case ResamplerMax:
77 /* Shouldn't happen */
78 break;
81 return Resample_point32_C;
85 static inline ALfloat Sample_ALbyte(ALbyte val)
86 { return val * (1.0f/127.0f); }
88 static inline ALfloat Sample_ALshort(ALshort val)
89 { return val * (1.0f/32767.0f); }
91 static inline ALfloat Sample_ALfloat(ALfloat val)
92 { return val; }
94 #define DECL_TEMPLATE(T) \
95 static void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
96 { \
97 ALuint i; \
98 for(i = 0;i < samples;i++) \
99 dst[i] = Sample_##T(src[i*srcstep]); \
102 DECL_TEMPLATE(ALbyte)
103 DECL_TEMPLATE(ALshort)
104 DECL_TEMPLATE(ALfloat)
106 #undef DECL_TEMPLATE
108 static void LoadSamples(ALfloat *dst, const ALvoid *src, ALuint srcstep, enum FmtType srctype, ALuint samples)
110 switch(srctype)
112 case FmtByte:
113 Load_ALbyte(dst, src, srcstep, samples);
114 break;
115 case FmtShort:
116 Load_ALshort(dst, src, srcstep, samples);
117 break;
118 case FmtFloat:
119 Load_ALfloat(dst, src, srcstep, samples);
120 break;
124 static void SilenceSamples(ALfloat *dst, ALuint samples)
126 ALuint i;
127 for(i = 0;i < samples;i++)
128 dst[i] = 0.0f;
132 static const ALfloat *DoFilters(ALfilterState *lpfilter, ALfilterState *hpfilter,
133 ALfloat *restrict dst, const ALfloat *restrict src,
134 ALuint numsamples, enum ActiveFilters type)
136 ALuint i;
137 switch(type)
139 case AF_None:
140 break;
142 case AF_LowPass:
143 ALfilterState_process(lpfilter, dst, src, numsamples);
144 return dst;
145 case AF_HighPass:
146 ALfilterState_process(hpfilter, dst, src, numsamples);
147 return dst;
149 case AF_BandPass:
150 for(i = 0;i < numsamples;)
152 ALfloat temp[64];
153 ALuint todo = minu(64, numsamples-i);
155 ALfilterState_process(lpfilter, temp, src+i, todo);
156 ALfilterState_process(hpfilter, dst+i, temp, todo);
157 i += todo;
159 return dst;
161 return src;
165 ALvoid MixSource(ALvoice *voice, ALsource *Source, ALCdevice *Device, ALuint SamplesToDo)
167 MixerFunc Mix;
168 ResamplerFunc Resample;
169 ALbufferlistitem *BufferListItem;
170 ALuint DataPosInt, DataPosFrac;
171 ALboolean isbformat = AL_FALSE;
172 ALboolean Looping;
173 ALuint increment;
174 enum Resampler Resampler;
175 ALenum State;
176 ALuint OutPos;
177 ALuint NumChannels;
178 ALuint SampleSize;
179 ALint64 DataSize64;
180 ALuint chan, j;
182 /* Get source info */
183 State = Source->state;
184 BufferListItem = ATOMIC_LOAD(&Source->current_buffer);
185 DataPosInt = Source->position;
186 DataPosFrac = Source->position_fraction;
187 Looping = Source->Looping;
188 Resampler = Source->Resampler;
189 NumChannels = Source->NumChannels;
190 SampleSize = Source->SampleSize;
191 increment = voice->Step;
193 while(BufferListItem)
195 ALbuffer *buffer;
196 if((buffer=BufferListItem->buffer) != NULL)
198 isbformat = (buffer->FmtChannels == FmtBFormat2D ||
199 buffer->FmtChannels == FmtBFormat3D);
200 break;
202 BufferListItem = BufferListItem->next;
205 Mix = SelectMixer();
206 Resample = ((increment == FRACTIONONE && DataPosFrac == 0) ?
207 Resample_copy32_C : SelectResampler(Resampler));
209 OutPos = 0;
210 do {
211 const ALuint BufferPrePadding = ResamplerPrePadding[Resampler];
212 const ALuint BufferPadding = ResamplerPadding[Resampler];
213 ALuint SrcBufferSize, DstBufferSize;
215 /* Figure out how many buffer samples will be needed */
216 DataSize64 = SamplesToDo-OutPos;
217 DataSize64 *= increment;
218 DataSize64 += DataPosFrac+FRACTIONMASK;
219 DataSize64 >>= FRACTIONBITS;
220 DataSize64 += BufferPadding+BufferPrePadding;
222 SrcBufferSize = (ALuint)mini64(DataSize64, BUFFERSIZE);
224 /* Figure out how many samples we can actually mix from this. */
225 DataSize64 = SrcBufferSize;
226 DataSize64 -= BufferPadding+BufferPrePadding;
227 DataSize64 <<= FRACTIONBITS;
228 DataSize64 -= DataPosFrac;
230 DstBufferSize = (ALuint)((DataSize64+(increment-1)) / increment);
231 DstBufferSize = minu(DstBufferSize, (SamplesToDo-OutPos));
233 /* Some mixers like having a multiple of 4, so try to give that unless
234 * this is the last update. */
235 if(OutPos+DstBufferSize < SamplesToDo)
236 DstBufferSize &= ~3;
238 for(chan = 0;chan < NumChannels;chan++)
240 const ALfloat *ResampledData;
241 ALfloat *SrcData = Device->SourceData;
242 ALuint SrcDataSize = 0;
244 if(Source->SourceType == AL_STATIC)
246 const ALbuffer *ALBuffer = BufferListItem->buffer;
247 const ALubyte *Data = ALBuffer->data;
248 ALuint DataSize;
249 ALuint pos;
251 /* If current pos is beyond the loop range, do not loop */
252 if(Looping == AL_FALSE || DataPosInt >= (ALuint)ALBuffer->LoopEnd)
254 Looping = AL_FALSE;
256 if(DataPosInt >= BufferPrePadding)
257 pos = DataPosInt - BufferPrePadding;
258 else
260 DataSize = BufferPrePadding - DataPosInt;
261 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
263 SilenceSamples(&SrcData[SrcDataSize], DataSize);
264 SrcDataSize += DataSize;
266 pos = 0;
269 /* Copy what's left to play in the source buffer, and clear the
270 * rest of the temp buffer */
271 DataSize = minu(SrcBufferSize - SrcDataSize, ALBuffer->SampleLen - pos);
273 LoadSamples(&SrcData[SrcDataSize], &Data[(pos*NumChannels + chan)*SampleSize],
274 NumChannels, ALBuffer->FmtType, DataSize);
275 SrcDataSize += DataSize;
277 SilenceSamples(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
278 SrcDataSize += SrcBufferSize - SrcDataSize;
280 else
282 ALuint LoopStart = ALBuffer->LoopStart;
283 ALuint LoopEnd = ALBuffer->LoopEnd;
285 if(DataPosInt >= LoopStart)
287 pos = DataPosInt-LoopStart;
288 while(pos < BufferPrePadding)
289 pos += LoopEnd-LoopStart;
290 pos -= BufferPrePadding;
291 pos += LoopStart;
293 else if(DataPosInt >= BufferPrePadding)
294 pos = DataPosInt - BufferPrePadding;
295 else
297 DataSize = BufferPrePadding - DataPosInt;
298 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
300 SilenceSamples(&SrcData[SrcDataSize], DataSize);
301 SrcDataSize += DataSize;
303 pos = 0;
306 /* Copy what's left of this loop iteration, then copy repeats
307 * of the loop section */
308 DataSize = LoopEnd - pos;
309 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
311 LoadSamples(&SrcData[SrcDataSize], &Data[(pos*NumChannels + chan)*SampleSize],
312 NumChannels, ALBuffer->FmtType, DataSize);
313 SrcDataSize += DataSize;
315 DataSize = LoopEnd-LoopStart;
316 while(SrcBufferSize > SrcDataSize)
318 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
320 LoadSamples(&SrcData[SrcDataSize], &Data[(LoopStart*NumChannels + chan)*SampleSize],
321 NumChannels, ALBuffer->FmtType, DataSize);
322 SrcDataSize += DataSize;
326 else
328 /* Crawl the buffer queue to fill in the temp buffer */
329 ALbufferlistitem *tmpiter = BufferListItem;
330 ALuint pos;
332 if(DataPosInt >= BufferPrePadding)
333 pos = DataPosInt - BufferPrePadding;
334 else
336 pos = BufferPrePadding - DataPosInt;
337 while(pos > 0)
339 ALbufferlistitem *prev;
340 if((prev=tmpiter->prev) != NULL)
341 tmpiter = prev;
342 else if(Looping)
344 while(tmpiter->next)
345 tmpiter = tmpiter->next;
347 else
349 ALuint DataSize = minu(SrcBufferSize - SrcDataSize, pos);
351 SilenceSamples(&SrcData[SrcDataSize], DataSize);
352 SrcDataSize += DataSize;
354 pos = 0;
355 break;
358 if(tmpiter->buffer)
360 if((ALuint)tmpiter->buffer->SampleLen > pos)
362 pos = tmpiter->buffer->SampleLen - pos;
363 break;
365 pos -= tmpiter->buffer->SampleLen;
370 while(tmpiter && SrcBufferSize > SrcDataSize)
372 const ALbuffer *ALBuffer;
373 if((ALBuffer=tmpiter->buffer) != NULL)
375 const ALubyte *Data = ALBuffer->data;
376 ALuint DataSize = ALBuffer->SampleLen;
378 /* Skip the data already played */
379 if(DataSize <= pos)
380 pos -= DataSize;
381 else
383 Data += (pos*NumChannels + chan)*SampleSize;
384 DataSize -= pos;
385 pos -= pos;
387 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
388 LoadSamples(&SrcData[SrcDataSize], Data, NumChannels,
389 ALBuffer->FmtType, DataSize);
390 SrcDataSize += DataSize;
393 tmpiter = tmpiter->next;
394 if(!tmpiter && Looping)
395 tmpiter = ATOMIC_LOAD(&Source->queue);
396 else if(!tmpiter)
398 SilenceSamples(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
399 SrcDataSize += SrcBufferSize - SrcDataSize;
404 /* Now resample, then filter and mix to the appropriate outputs. */
405 ResampledData = Resample(
406 &SrcData[BufferPrePadding], DataPosFrac, increment,
407 Device->ResampledData, DstBufferSize
410 DirectParams *parms = &voice->Direct;
411 const ALfloat *samples;
413 samples = DoFilters(
414 &parms->Filters[chan].LowPass, &parms->Filters[chan].HighPass,
415 Device->FilteredData, ResampledData, DstBufferSize,
416 parms->Filters[chan].ActiveType
418 Mix(samples, parms->OutChannels, parms->OutBuffer, parms->Gains[chan],
419 parms->Counter, OutPos, DstBufferSize);
422 /* Only the first channel for B-Format buffers (W channel) goes to
423 * the send paths. */
424 if(chan > 0 && isbformat)
425 continue;
426 for(j = 0;j < Device->NumAuxSends;j++)
428 SendParams *parms = &voice->Send[j];
429 const ALfloat *samples;
431 if(!parms->OutBuffer)
432 continue;
434 samples = DoFilters(
435 &parms->Filters[chan].LowPass, &parms->Filters[chan].HighPass,
436 Device->FilteredData, ResampledData, DstBufferSize,
437 parms->Filters[chan].ActiveType
439 Mix(samples, 1, parms->OutBuffer, &parms->Gain,
440 parms->Counter, OutPos, DstBufferSize);
443 /* Update positions */
444 DataPosFrac += increment*DstBufferSize;
445 DataPosInt += DataPosFrac>>FRACTIONBITS;
446 DataPosFrac &= FRACTIONMASK;
448 OutPos += DstBufferSize;
449 voice->Offset += DstBufferSize;
450 voice->Direct.Counter = maxu(voice->Direct.Counter, DstBufferSize) - DstBufferSize;
451 for(j = 0;j < Device->NumAuxSends;j++)
452 voice->Send[j].Counter = maxu(voice->Send[j].Counter, DstBufferSize) - DstBufferSize;
454 /* Handle looping sources */
455 while(1)
457 const ALbuffer *ALBuffer;
458 ALuint DataSize = 0;
459 ALuint LoopStart = 0;
460 ALuint LoopEnd = 0;
462 if((ALBuffer=BufferListItem->buffer) != NULL)
464 DataSize = ALBuffer->SampleLen;
465 LoopStart = ALBuffer->LoopStart;
466 LoopEnd = ALBuffer->LoopEnd;
467 if(LoopEnd > DataPosInt)
468 break;
471 if(Looping && Source->SourceType == AL_STATIC)
473 assert(LoopEnd > LoopStart);
474 DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
475 break;
478 if(DataSize > DataPosInt)
479 break;
481 if(!(BufferListItem=BufferListItem->next))
483 if(Looping)
484 BufferListItem = ATOMIC_LOAD(&Source->queue);
485 else
487 State = AL_STOPPED;
488 BufferListItem = NULL;
489 DataPosInt = 0;
490 DataPosFrac = 0;
491 break;
495 DataPosInt -= DataSize;
497 } while(State == AL_PLAYING && OutPos < SamplesToDo);
499 /* Update source info */
500 Source->state = State;
501 ATOMIC_STORE(&Source->current_buffer, BufferListItem);
502 Source->position = DataPosInt;
503 Source->position_fraction = DataPosFrac;