Initialize panning after setting up HRTF
[openal-soft.git] / Alc / mixer.c
blob5927bb7b94a072c526fd025357b24ba9efa71811
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 HrtfMixerFunc SelectHrtfMixer(void)
46 #ifdef HAVE_SSE
47 if((CPUCapFlags&CPU_CAP_SSE))
48 return MixHrtf_SSE;
49 #endif
50 #ifdef HAVE_NEON
51 if((CPUCapFlags&CPU_CAP_NEON))
52 return MixHrtf_Neon;
53 #endif
55 return MixHrtf_C;
58 static inline MixerFunc SelectMixer(void)
60 #ifdef HAVE_SSE
61 if((CPUCapFlags&CPU_CAP_SSE))
62 return Mix_SSE;
63 #endif
64 #ifdef HAVE_NEON
65 if((CPUCapFlags&CPU_CAP_NEON))
66 return Mix_Neon;
67 #endif
69 return Mix_C;
72 static inline ResamplerFunc SelectResampler(enum Resampler resampler)
74 switch(resampler)
76 case PointResampler:
77 return Resample_point32_C;
78 case LinearResampler:
79 #ifdef HAVE_SSE4_1
80 if((CPUCapFlags&CPU_CAP_SSE4_1))
81 return Resample_lerp32_SSE41;
82 #endif
83 #ifdef HAVE_SSE2
84 if((CPUCapFlags&CPU_CAP_SSE2))
85 return Resample_lerp32_SSE2;
86 #endif
87 return Resample_lerp32_C;
88 case CubicResampler:
89 return Resample_cubic32_C;
90 case ResamplerMax:
91 /* Shouldn't happen */
92 break;
95 return Resample_point32_C;
99 static inline ALfloat Sample_ALbyte(ALbyte val)
100 { return val * (1.0f/127.0f); }
102 static inline ALfloat Sample_ALshort(ALshort val)
103 { return val * (1.0f/32767.0f); }
105 static inline ALfloat Sample_ALfloat(ALfloat val)
106 { return val; }
108 #define DECL_TEMPLATE(T) \
109 static void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
111 ALuint i; \
112 for(i = 0;i < samples;i++) \
113 dst[i] = Sample_##T(src[i*srcstep]); \
116 DECL_TEMPLATE(ALbyte)
117 DECL_TEMPLATE(ALshort)
118 DECL_TEMPLATE(ALfloat)
120 #undef DECL_TEMPLATE
122 static void LoadSamples(ALfloat *dst, const ALvoid *src, ALuint srcstep, enum FmtType srctype, ALuint samples)
124 switch(srctype)
126 case FmtByte:
127 Load_ALbyte(dst, src, srcstep, samples);
128 break;
129 case FmtShort:
130 Load_ALshort(dst, src, srcstep, samples);
131 break;
132 case FmtFloat:
133 Load_ALfloat(dst, src, srcstep, samples);
134 break;
138 static void SilenceSamples(ALfloat *dst, ALuint samples)
140 ALuint i;
141 for(i = 0;i < samples;i++)
142 dst[i] = 0.0f;
146 static const ALfloat *DoFilters(ALfilterState *lpfilter, ALfilterState *hpfilter,
147 ALfloat *restrict dst, const ALfloat *restrict src,
148 ALuint numsamples, enum ActiveFilters type)
150 ALuint i;
151 switch(type)
153 case AF_None:
154 break;
156 case AF_LowPass:
157 ALfilterState_process(lpfilter, dst, src, numsamples);
158 return dst;
159 case AF_HighPass:
160 ALfilterState_process(hpfilter, dst, src, numsamples);
161 return dst;
163 case AF_BandPass:
164 for(i = 0;i < numsamples;)
166 ALfloat temp[64];
167 ALuint todo = minu(64, numsamples-i);
169 ALfilterState_process(lpfilter, temp, src+i, todo);
170 ALfilterState_process(hpfilter, dst+i, temp, todo);
171 i += todo;
173 return dst;
175 return src;
179 ALvoid MixSource(ALvoice *voice, ALsource *Source, ALCdevice *Device, ALuint SamplesToDo)
181 MixerFunc Mix;
182 HrtfMixerFunc HrtfMix;
183 ResamplerFunc Resample;
184 ALbufferlistitem *BufferListItem;
185 ALuint DataPosInt, DataPosFrac;
186 ALboolean isbformat = AL_FALSE;
187 ALboolean Looping;
188 ALuint increment;
189 enum Resampler Resampler;
190 ALenum State;
191 ALuint OutPos;
192 ALuint NumChannels;
193 ALuint SampleSize;
194 ALint64 DataSize64;
195 ALuint chan, j;
197 /* Get source info */
198 State = Source->state;
199 BufferListItem = ATOMIC_LOAD(&Source->current_buffer);
200 DataPosInt = Source->position;
201 DataPosFrac = Source->position_fraction;
202 Looping = Source->Looping;
203 Resampler = Source->Resampler;
204 NumChannels = Source->NumChannels;
205 SampleSize = Source->SampleSize;
206 increment = voice->Step;
208 while(BufferListItem)
210 ALbuffer *buffer;
211 if((buffer=BufferListItem->buffer) != NULL)
213 isbformat = (buffer->FmtChannels == FmtBFormat2D ||
214 buffer->FmtChannels == FmtBFormat3D);
215 break;
217 BufferListItem = BufferListItem->next;
220 Mix = SelectMixer();
221 HrtfMix = SelectHrtfMixer();
222 Resample = ((increment == FRACTIONONE && DataPosFrac == 0) ?
223 Resample_copy32_C : SelectResampler(Resampler));
225 OutPos = 0;
226 do {
227 const ALuint BufferPrePadding = ResamplerPrePadding[Resampler];
228 const ALuint BufferPadding = ResamplerPadding[Resampler];
229 ALuint SrcBufferSize, DstBufferSize;
231 /* Figure out how many buffer samples will be needed */
232 DataSize64 = SamplesToDo-OutPos;
233 DataSize64 *= increment;
234 DataSize64 += DataPosFrac+FRACTIONMASK;
235 DataSize64 >>= FRACTIONBITS;
236 DataSize64 += BufferPadding+BufferPrePadding;
238 SrcBufferSize = (ALuint)mini64(DataSize64, BUFFERSIZE);
240 /* Figure out how many samples we can actually mix from this. */
241 DataSize64 = SrcBufferSize;
242 DataSize64 -= BufferPadding+BufferPrePadding;
243 DataSize64 <<= FRACTIONBITS;
244 DataSize64 -= DataPosFrac;
246 DstBufferSize = (ALuint)((DataSize64+(increment-1)) / increment);
247 DstBufferSize = minu(DstBufferSize, (SamplesToDo-OutPos));
249 /* Some mixers like having a multiple of 4, so try to give that unless
250 * this is the last update. */
251 if(OutPos+DstBufferSize < SamplesToDo)
252 DstBufferSize &= ~3;
254 for(chan = 0;chan < NumChannels;chan++)
256 const ALfloat *ResampledData;
257 ALfloat *SrcData = Device->SourceData;
258 ALuint SrcDataSize = 0;
260 if(Source->SourceType == AL_STATIC)
262 const ALbuffer *ALBuffer = BufferListItem->buffer;
263 const ALubyte *Data = ALBuffer->data;
264 ALuint DataSize;
265 ALuint pos;
267 /* If current pos is beyond the loop range, do not loop */
268 if(Looping == AL_FALSE || DataPosInt >= (ALuint)ALBuffer->LoopEnd)
270 Looping = AL_FALSE;
272 if(DataPosInt >= BufferPrePadding)
273 pos = DataPosInt - BufferPrePadding;
274 else
276 DataSize = BufferPrePadding - DataPosInt;
277 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
279 SilenceSamples(&SrcData[SrcDataSize], DataSize);
280 SrcDataSize += DataSize;
282 pos = 0;
285 /* Copy what's left to play in the source buffer, and clear the
286 * rest of the temp buffer */
287 DataSize = minu(SrcBufferSize - SrcDataSize, ALBuffer->SampleLen - pos);
289 LoadSamples(&SrcData[SrcDataSize], &Data[(pos*NumChannels + chan)*SampleSize],
290 NumChannels, ALBuffer->FmtType, DataSize);
291 SrcDataSize += DataSize;
293 SilenceSamples(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
294 SrcDataSize += SrcBufferSize - SrcDataSize;
296 else
298 ALuint LoopStart = ALBuffer->LoopStart;
299 ALuint LoopEnd = ALBuffer->LoopEnd;
301 if(DataPosInt >= LoopStart)
303 pos = DataPosInt-LoopStart;
304 while(pos < BufferPrePadding)
305 pos += LoopEnd-LoopStart;
306 pos -= BufferPrePadding;
307 pos += LoopStart;
309 else if(DataPosInt >= BufferPrePadding)
310 pos = DataPosInt - BufferPrePadding;
311 else
313 DataSize = BufferPrePadding - DataPosInt;
314 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
316 SilenceSamples(&SrcData[SrcDataSize], DataSize);
317 SrcDataSize += DataSize;
319 pos = 0;
322 /* Copy what's left of this loop iteration, then copy repeats
323 * of the loop section */
324 DataSize = LoopEnd - pos;
325 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
327 LoadSamples(&SrcData[SrcDataSize], &Data[(pos*NumChannels + chan)*SampleSize],
328 NumChannels, ALBuffer->FmtType, DataSize);
329 SrcDataSize += DataSize;
331 DataSize = LoopEnd-LoopStart;
332 while(SrcBufferSize > SrcDataSize)
334 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
336 LoadSamples(&SrcData[SrcDataSize], &Data[(LoopStart*NumChannels + chan)*SampleSize],
337 NumChannels, ALBuffer->FmtType, DataSize);
338 SrcDataSize += DataSize;
342 else
344 /* Crawl the buffer queue to fill in the temp buffer */
345 ALbufferlistitem *tmpiter = BufferListItem;
346 ALuint pos;
348 if(DataPosInt >= BufferPrePadding)
349 pos = DataPosInt - BufferPrePadding;
350 else
352 pos = BufferPrePadding - DataPosInt;
353 while(pos > 0)
355 ALbufferlistitem *prev;
356 if((prev=tmpiter->prev) != NULL)
357 tmpiter = prev;
358 else if(Looping)
360 while(tmpiter->next)
361 tmpiter = tmpiter->next;
363 else
365 ALuint DataSize = minu(SrcBufferSize - SrcDataSize, pos);
367 SilenceSamples(&SrcData[SrcDataSize], DataSize);
368 SrcDataSize += DataSize;
370 pos = 0;
371 break;
374 if(tmpiter->buffer)
376 if((ALuint)tmpiter->buffer->SampleLen > pos)
378 pos = tmpiter->buffer->SampleLen - pos;
379 break;
381 pos -= tmpiter->buffer->SampleLen;
386 while(tmpiter && SrcBufferSize > SrcDataSize)
388 const ALbuffer *ALBuffer;
389 if((ALBuffer=tmpiter->buffer) != NULL)
391 const ALubyte *Data = ALBuffer->data;
392 ALuint DataSize = ALBuffer->SampleLen;
394 /* Skip the data already played */
395 if(DataSize <= pos)
396 pos -= DataSize;
397 else
399 Data += (pos*NumChannels + chan)*SampleSize;
400 DataSize -= pos;
401 pos -= pos;
403 DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
404 LoadSamples(&SrcData[SrcDataSize], Data, NumChannels,
405 ALBuffer->FmtType, DataSize);
406 SrcDataSize += DataSize;
409 tmpiter = tmpiter->next;
410 if(!tmpiter && Looping)
411 tmpiter = ATOMIC_LOAD(&Source->queue);
412 else if(!tmpiter)
414 SilenceSamples(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
415 SrcDataSize += SrcBufferSize - SrcDataSize;
420 /* Now resample, then filter and mix to the appropriate outputs. */
421 ResampledData = Resample(
422 &SrcData[BufferPrePadding], DataPosFrac, increment,
423 Device->ResampledData, DstBufferSize
426 DirectParams *parms = &voice->Direct;
427 const ALfloat *samples;
429 samples = DoFilters(
430 &parms->Filters[chan].LowPass, &parms->Filters[chan].HighPass,
431 Device->FilteredData, ResampledData, DstBufferSize,
432 parms->Filters[chan].ActiveType
434 if(!voice->IsHrtf)
435 Mix(samples, Device->NumChannels, parms->OutBuffer, parms->Mix.Gains[chan],
436 parms->Counter, OutPos, DstBufferSize);
437 else
438 HrtfMix(parms->OutBuffer, samples, parms->Counter, voice->Offset,
439 OutPos, parms->Mix.Hrtf.IrSize, &parms->Mix.Hrtf.Params[chan],
440 &parms->Mix.Hrtf.State[chan], DstBufferSize);
443 /* Only the first channel for B-Format buffers (W channel) goes to
444 * the send paths. */
445 if(chan > 0 && isbformat)
446 continue;
447 for(j = 0;j < Device->NumAuxSends;j++)
449 SendParams *parms = &voice->Send[j];
450 const ALfloat *samples;
452 if(!parms->OutBuffer)
453 continue;
455 samples = DoFilters(
456 &parms->Filters[chan].LowPass, &parms->Filters[chan].HighPass,
457 Device->FilteredData, ResampledData, DstBufferSize,
458 parms->Filters[chan].ActiveType
460 Mix(samples, 1, parms->OutBuffer, &parms->Gain,
461 parms->Counter, OutPos, DstBufferSize);
464 /* Update positions */
465 DataPosFrac += increment*DstBufferSize;
466 DataPosInt += DataPosFrac>>FRACTIONBITS;
467 DataPosFrac &= FRACTIONMASK;
469 OutPos += DstBufferSize;
470 voice->Offset += DstBufferSize;
471 voice->Direct.Counter = maxu(voice->Direct.Counter, DstBufferSize) - DstBufferSize;
472 for(j = 0;j < Device->NumAuxSends;j++)
473 voice->Send[j].Counter = maxu(voice->Send[j].Counter, DstBufferSize) - DstBufferSize;
475 /* Handle looping sources */
476 while(1)
478 const ALbuffer *ALBuffer;
479 ALuint DataSize = 0;
480 ALuint LoopStart = 0;
481 ALuint LoopEnd = 0;
483 if((ALBuffer=BufferListItem->buffer) != NULL)
485 DataSize = ALBuffer->SampleLen;
486 LoopStart = ALBuffer->LoopStart;
487 LoopEnd = ALBuffer->LoopEnd;
488 if(LoopEnd > DataPosInt)
489 break;
492 if(Looping && Source->SourceType == AL_STATIC)
494 assert(LoopEnd > LoopStart);
495 DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
496 break;
499 if(DataSize > DataPosInt)
500 break;
502 if(!(BufferListItem=BufferListItem->next))
504 if(Looping)
505 BufferListItem = ATOMIC_LOAD(&Source->queue);
506 else
508 State = AL_STOPPED;
509 BufferListItem = NULL;
510 DataPosInt = 0;
511 DataPosFrac = 0;
512 break;
516 DataPosInt -= DataSize;
518 } while(State == AL_PLAYING && OutPos < SamplesToDo);
520 /* Update source info */
521 Source->state = State;
522 ATOMIC_STORE(&Source->current_buffer, BufferListItem);
523 Source->position = DataPosInt;
524 Source->position_fraction = DataPosFrac;