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
34 #include "alListener.h"
35 #include "alAuxEffectSlot.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)
47 if((CPUCapFlags
&CPU_CAP_SSE
))
51 if((CPUCapFlags
&CPU_CAP_NEON
))
58 static inline MixerFunc
SelectMixer(void)
61 if((CPUCapFlags
&CPU_CAP_SSE
))
65 if((CPUCapFlags
&CPU_CAP_NEON
))
72 static inline ResamplerFunc
SelectResampler(enum Resampler resampler
)
77 return Resample_point32_C
;
80 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
81 return Resample_lerp32_SSE41
;
84 if((CPUCapFlags
&CPU_CAP_SSE2
))
85 return Resample_lerp32_SSE2
;
87 return Resample_lerp32_C
;
89 return Resample_cubic32_C
;
91 /* Shouldn't happen */
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
)
108 #define DECL_TEMPLATE(T) \
109 static void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
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
)
122 static void LoadSamples(ALfloat
*dst
, const ALvoid
*src
, ALuint srcstep
, enum FmtType srctype
, ALuint samples
)
127 Load_ALbyte(dst
, src
, srcstep
, samples
);
130 Load_ALshort(dst
, src
, srcstep
, samples
);
133 Load_ALfloat(dst
, src
, srcstep
, samples
);
138 static void SilenceSamples(ALfloat
*dst
, ALuint samples
)
141 for(i
= 0;i
< samples
;i
++)
146 static const ALfloat
*DoFilters(ALfilterState
*lpfilter
, ALfilterState
*hpfilter
,
147 ALfloat
*restrict dst
, const ALfloat
*restrict src
,
148 ALuint numsamples
, enum ActiveFilters type
)
157 ALfilterState_process(lpfilter
, dst
, src
, numsamples
);
160 ALfilterState_process(hpfilter
, dst
, src
, numsamples
);
164 for(i
= 0;i
< numsamples
;)
167 ALuint todo
= minu(64, numsamples
-i
);
169 ALfilterState_process(lpfilter
, temp
, src
+i
, todo
);
170 ALfilterState_process(hpfilter
, dst
+i
, temp
, todo
);
179 ALvoid
MixSource(ALvoice
*voice
, ALsource
*Source
, ALCdevice
*Device
, ALuint SamplesToDo
)
182 HrtfMixerFunc HrtfMix
;
183 ResamplerFunc Resample
;
184 ALbufferlistitem
*BufferListItem
;
185 ALuint DataPosInt
, DataPosFrac
;
186 ALboolean isbformat
= AL_FALSE
;
189 enum Resampler Resampler
;
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
)
211 if((buffer
=BufferListItem
->buffer
) != NULL
)
213 isbformat
= (buffer
->FmtChannels
== FmtBFormat2D
||
214 buffer
->FmtChannels
== FmtBFormat3D
);
217 BufferListItem
= BufferListItem
->next
;
221 HrtfMix
= SelectHrtfMixer();
222 Resample
= ((increment
== FRACTIONONE
&& DataPosFrac
== 0) ?
223 Resample_copy32_C
: SelectResampler(Resampler
));
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
)
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
;
267 /* If current pos is beyond the loop range, do not loop */
268 if(Looping
== AL_FALSE
|| DataPosInt
>= (ALuint
)ALBuffer
->LoopEnd
)
272 if(DataPosInt
>= BufferPrePadding
)
273 pos
= DataPosInt
- BufferPrePadding
;
276 DataSize
= BufferPrePadding
- DataPosInt
;
277 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
279 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
280 SrcDataSize
+= DataSize
;
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
;
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
;
309 else if(DataPosInt
>= BufferPrePadding
)
310 pos
= DataPosInt
- BufferPrePadding
;
313 DataSize
= BufferPrePadding
- DataPosInt
;
314 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
316 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
317 SrcDataSize
+= DataSize
;
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
;
344 /* Crawl the buffer queue to fill in the temp buffer */
345 ALbufferlistitem
*tmpiter
= BufferListItem
;
348 if(DataPosInt
>= BufferPrePadding
)
349 pos
= DataPosInt
- BufferPrePadding
;
352 pos
= BufferPrePadding
- DataPosInt
;
355 ALbufferlistitem
*prev
;
356 if((prev
=tmpiter
->prev
) != NULL
)
361 tmpiter
= tmpiter
->next
;
365 ALuint DataSize
= minu(SrcBufferSize
- SrcDataSize
, pos
);
367 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
368 SrcDataSize
+= DataSize
;
376 if((ALuint
)tmpiter
->buffer
->SampleLen
> pos
)
378 pos
= tmpiter
->buffer
->SampleLen
- pos
;
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 */
399 Data
+= (pos
*NumChannels
+ chan
)*SampleSize
;
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
);
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
;
430 &parms
->Filters
[chan
].LowPass
, &parms
->Filters
[chan
].HighPass
,
431 Device
->FilteredData
, ResampledData
, DstBufferSize
,
432 parms
->Filters
[chan
].ActiveType
435 Mix(samples
, parms
->OutChannels
, parms
->OutBuffer
, parms
->Gains
[chan
],
436 parms
->Counter
, OutPos
, DstBufferSize
);
438 HrtfMix(parms
->OutBuffer
, samples
, parms
->Counter
, voice
->Offset
,
439 OutPos
, parms
->Hrtf
.IrSize
, &parms
->Hrtf
.Params
[chan
],
440 &parms
->Hrtf
.State
[chan
], DstBufferSize
);
443 /* Only the first channel for B-Format buffers (W channel) goes to
445 if(chan
> 0 && isbformat
)
447 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
449 SendParams
*parms
= &voice
->Send
[j
];
450 const ALfloat
*samples
;
452 if(!parms
->OutBuffer
)
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 */
478 const ALbuffer
*ALBuffer
;
480 ALuint LoopStart
= 0;
483 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
485 DataSize
= ALBuffer
->SampleLen
;
486 LoopStart
= ALBuffer
->LoopStart
;
487 LoopEnd
= ALBuffer
->LoopEnd
;
488 if(LoopEnd
> DataPosInt
)
492 if(Looping
&& Source
->SourceType
== AL_STATIC
)
494 assert(LoopEnd
> LoopStart
);
495 DataPosInt
= ((DataPosInt
-LoopStart
)%(LoopEnd
-LoopStart
)) + LoopStart
;
499 if(DataSize
> DataPosInt
)
502 if(!(BufferListItem
=BufferListItem
->next
))
505 BufferListItem
= ATOMIC_LOAD(&Source
->queue
);
509 BufferListItem
= NULL
;
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
;