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 MixerFunc
SelectMixer(void)
47 if((CPUCapFlags
&CPU_CAP_SSE
))
51 if((CPUCapFlags
&CPU_CAP_NEON
))
58 static inline ResamplerFunc
SelectResampler(enum Resampler resampler
)
63 return Resample_point32_C
;
66 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
67 return Resample_lerp32_SSE41
;
70 if((CPUCapFlags
&CPU_CAP_SSE2
))
71 return Resample_lerp32_SSE2
;
73 return Resample_lerp32_C
;
75 return Resample_cubic32_C
;
77 /* Shouldn't happen */
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
)
94 #define DECL_TEMPLATE(T) \
95 static void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
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
)
108 static void LoadSamples(ALfloat
*dst
, const ALvoid
*src
, ALuint srcstep
, enum FmtType srctype
, ALuint samples
)
113 Load_ALbyte(dst
, src
, srcstep
, samples
);
116 Load_ALshort(dst
, src
, srcstep
, samples
);
119 Load_ALfloat(dst
, src
, srcstep
, samples
);
124 static void SilenceSamples(ALfloat
*dst
, ALuint samples
)
127 for(i
= 0;i
< samples
;i
++)
132 static const ALfloat
*DoFilters(ALfilterState
*lpfilter
, ALfilterState
*hpfilter
,
133 ALfloat
*restrict dst
, const ALfloat
*restrict src
,
134 ALuint numsamples
, enum ActiveFilters type
)
143 ALfilterState_process(lpfilter
, dst
, src
, numsamples
);
146 ALfilterState_process(hpfilter
, dst
, src
, numsamples
);
150 for(i
= 0;i
< numsamples
;)
153 ALuint todo
= minu(64, numsamples
-i
);
155 ALfilterState_process(lpfilter
, temp
, src
+i
, todo
);
156 ALfilterState_process(hpfilter
, dst
+i
, temp
, todo
);
165 ALvoid
MixSource(ALvoice
*voice
, ALsource
*Source
, ALCdevice
*Device
, ALuint SamplesToDo
)
168 ResamplerFunc Resample
;
169 ALbufferlistitem
*BufferListItem
;
170 ALuint DataPosInt
, DataPosFrac
;
171 ALboolean isbformat
= AL_FALSE
;
174 enum Resampler Resampler
;
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
)
196 if((buffer
=BufferListItem
->buffer
) != NULL
)
198 isbformat
= (buffer
->FmtChannels
== FmtBFormat2D
||
199 buffer
->FmtChannels
== FmtBFormat3D
);
202 BufferListItem
= BufferListItem
->next
;
206 Resample
= ((increment
== FRACTIONONE
&& DataPosFrac
== 0) ?
207 Resample_copy32_C
: SelectResampler(Resampler
));
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
)
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
;
251 /* If current pos is beyond the loop range, do not loop */
252 if(Looping
== AL_FALSE
|| DataPosInt
>= (ALuint
)ALBuffer
->LoopEnd
)
256 if(DataPosInt
>= BufferPrePadding
)
257 pos
= DataPosInt
- BufferPrePadding
;
260 DataSize
= BufferPrePadding
- DataPosInt
;
261 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
263 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
264 SrcDataSize
+= DataSize
;
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
;
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
;
293 else if(DataPosInt
>= BufferPrePadding
)
294 pos
= DataPosInt
- BufferPrePadding
;
297 DataSize
= BufferPrePadding
- DataPosInt
;
298 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
300 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
301 SrcDataSize
+= DataSize
;
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
;
328 /* Crawl the buffer queue to fill in the temp buffer */
329 ALbufferlistitem
*tmpiter
= BufferListItem
;
332 if(DataPosInt
>= BufferPrePadding
)
333 pos
= DataPosInt
- BufferPrePadding
;
336 pos
= BufferPrePadding
- DataPosInt
;
339 ALbufferlistitem
*prev
;
340 if((prev
=tmpiter
->prev
) != NULL
)
345 tmpiter
= tmpiter
->next
;
349 ALuint DataSize
= minu(SrcBufferSize
- SrcDataSize
, pos
);
351 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
352 SrcDataSize
+= DataSize
;
360 if((ALuint
)tmpiter
->buffer
->SampleLen
> pos
)
362 pos
= tmpiter
->buffer
->SampleLen
- pos
;
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 */
383 Data
+= (pos
*NumChannels
+ chan
)*SampleSize
;
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
);
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
;
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
424 if(chan
> 0 && isbformat
)
426 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
428 SendParams
*parms
= &voice
->Send
[j
];
429 const ALfloat
*samples
;
431 if(!parms
->OutBuffer
)
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 */
457 const ALbuffer
*ALBuffer
;
459 ALuint LoopStart
= 0;
462 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
464 DataSize
= ALBuffer
->SampleLen
;
465 LoopStart
= ALBuffer
->LoopStart
;
466 LoopEnd
= ALBuffer
->LoopEnd
;
467 if(LoopEnd
> DataPosInt
)
471 if(Looping
&& Source
->SourceType
== AL_STATIC
)
473 assert(LoopEnd
> LoopStart
);
474 DataPosInt
= ((DataPosInt
-LoopStart
)%(LoopEnd
-LoopStart
)) + LoopStart
;
478 if(DataSize
> DataPosInt
)
481 if(!(BufferListItem
=BufferListItem
->next
))
484 BufferListItem
= ATOMIC_LOAD(&Source
->queue
);
488 BufferListItem
= NULL
;
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
;