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
);
43 alignas(16) ALfloat CubicLUT
[FRACTIONONE
][4];
46 void aluInitResamplers(void)
49 for(i
= 0;i
< FRACTIONONE
;i
++)
51 ALfloat mu
= (ALfloat
)i
/ FRACTIONONE
;
52 ALfloat mu2
= mu
*mu
, mu3
= mu
*mu
*mu
;
53 CubicLUT
[i
][0] = -0.5f
*mu3
+ mu2
+ -0.5f
*mu
;
54 CubicLUT
[i
][1] = 1.5f
*mu3
+ -2.5f
*mu2
+ 1.0f
;
55 CubicLUT
[i
][2] = -1.5f
*mu3
+ 2.0f
*mu2
+ 0.5f
*mu
;
56 CubicLUT
[i
][3] = 0.5f
*mu3
+ -0.5f
*mu2
;
61 static inline HrtfMixerFunc
SelectHrtfMixer(void)
64 if((CPUCapFlags
&CPU_CAP_SSE
))
68 if((CPUCapFlags
&CPU_CAP_NEON
))
75 static inline MixerFunc
SelectMixer(void)
78 if((CPUCapFlags
&CPU_CAP_SSE
))
82 if((CPUCapFlags
&CPU_CAP_NEON
))
89 static inline ResamplerFunc
SelectResampler(enum Resampler resampler
)
94 return Resample_point32_C
;
97 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
98 return Resample_lerp32_SSE41
;
101 if((CPUCapFlags
&CPU_CAP_SSE2
))
102 return Resample_lerp32_SSE2
;
104 return Resample_lerp32_C
;
107 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
108 return Resample_cubic32_SSE41
;
111 if((CPUCapFlags
&CPU_CAP_SSE2
))
112 return Resample_cubic32_SSE2
;
114 return Resample_cubic32_C
;
116 /* Shouldn't happen */
120 return Resample_point32_C
;
124 static inline ALfloat
Sample_ALbyte(ALbyte val
)
125 { return val
* (1.0f
/127.0f
); }
127 static inline ALfloat
Sample_ALshort(ALshort val
)
128 { return val
* (1.0f
/32767.0f
); }
130 static inline ALfloat
Sample_ALfloat(ALfloat val
)
133 #define DECL_TEMPLATE(T) \
134 static void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
137 for(i = 0;i < samples;i++) \
138 dst[i] = Sample_##T(src[i*srcstep]); \
141 DECL_TEMPLATE(ALbyte
)
142 DECL_TEMPLATE(ALshort
)
143 DECL_TEMPLATE(ALfloat
)
147 static void LoadSamples(ALfloat
*dst
, const ALvoid
*src
, ALuint srcstep
, enum FmtType srctype
, ALuint samples
)
152 Load_ALbyte(dst
, src
, srcstep
, samples
);
155 Load_ALshort(dst
, src
, srcstep
, samples
);
158 Load_ALfloat(dst
, src
, srcstep
, samples
);
163 static void SilenceSamples(ALfloat
*dst
, ALuint samples
)
166 for(i
= 0;i
< samples
;i
++)
171 static const ALfloat
*DoFilters(ALfilterState
*lpfilter
, ALfilterState
*hpfilter
,
172 ALfloat
*restrict dst
, const ALfloat
*restrict src
,
173 ALuint numsamples
, enum ActiveFilters type
)
182 ALfilterState_process(lpfilter
, dst
, src
, numsamples
);
185 ALfilterState_process(hpfilter
, dst
, src
, numsamples
);
189 for(i
= 0;i
< numsamples
;)
192 ALuint todo
= minu(64, numsamples
-i
);
194 ALfilterState_process(lpfilter
, temp
, src
+i
, todo
);
195 ALfilterState_process(hpfilter
, dst
+i
, temp
, todo
);
204 ALvoid
MixSource(ALvoice
*voice
, ALsource
*Source
, ALCdevice
*Device
, ALuint SamplesToDo
)
207 HrtfMixerFunc HrtfMix
;
208 ResamplerFunc Resample
;
209 ALbufferlistitem
*BufferListItem
;
210 ALuint DataPosInt
, DataPosFrac
;
211 ALboolean isbformat
= AL_FALSE
;
214 enum Resampler Resampler
;
223 /* Get source info */
224 State
= Source
->state
;
225 BufferListItem
= ATOMIC_LOAD(&Source
->current_buffer
);
226 DataPosInt
= Source
->position
;
227 DataPosFrac
= Source
->position_fraction
;
228 Looping
= Source
->Looping
;
229 Resampler
= Source
->Resampler
;
230 NumChannels
= Source
->NumChannels
;
231 SampleSize
= Source
->SampleSize
;
232 increment
= voice
->Step
;
234 while(BufferListItem
)
237 if((buffer
=BufferListItem
->buffer
) != NULL
)
239 isbformat
= (buffer
->FmtChannels
== FmtBFormat2D
||
240 buffer
->FmtChannels
== FmtBFormat3D
);
243 BufferListItem
= BufferListItem
->next
;
245 assert(BufferListItem
!= NULL
);
247 IrSize
= (Device
->Hrtf
? GetHrtfIrSize(Device
->Hrtf
) : 0);
250 HrtfMix
= SelectHrtfMixer();
251 Resample
= ((increment
== FRACTIONONE
&& DataPosFrac
== 0) ?
252 Resample_copy32_C
: SelectResampler(Resampler
));
256 const ALuint BufferPrePadding
= ResamplerPrePadding
[Resampler
];
257 const ALuint BufferPadding
= ResamplerPadding
[Resampler
];
258 ALuint SrcBufferSize
, DstBufferSize
;
260 /* Figure out how many buffer samples will be needed */
261 DataSize64
= SamplesToDo
-OutPos
;
262 DataSize64
*= increment
;
263 DataSize64
+= DataPosFrac
+FRACTIONMASK
;
264 DataSize64
>>= FRACTIONBITS
;
265 DataSize64
+= BufferPadding
+BufferPrePadding
;
267 SrcBufferSize
= (ALuint
)mini64(DataSize64
, BUFFERSIZE
);
269 /* Figure out how many samples we can actually mix from this. */
270 DataSize64
= SrcBufferSize
;
271 DataSize64
-= BufferPadding
+BufferPrePadding
;
272 DataSize64
<<= FRACTIONBITS
;
273 DataSize64
-= DataPosFrac
;
275 DstBufferSize
= (ALuint
)((DataSize64
+(increment
-1)) / increment
);
276 DstBufferSize
= minu(DstBufferSize
, (SamplesToDo
-OutPos
));
278 /* Some mixers like having a multiple of 4, so try to give that unless
279 * this is the last update. */
280 if(OutPos
+DstBufferSize
< SamplesToDo
)
283 for(chan
= 0;chan
< NumChannels
;chan
++)
285 const ALfloat
*ResampledData
;
286 ALfloat
*SrcData
= Device
->SourceData
;
287 ALuint SrcDataSize
= 0;
289 if(Source
->SourceType
== AL_STATIC
)
291 const ALbuffer
*ALBuffer
= BufferListItem
->buffer
;
292 const ALubyte
*Data
= ALBuffer
->data
;
296 /* If current pos is beyond the loop range, do not loop */
297 if(Looping
== AL_FALSE
|| DataPosInt
>= (ALuint
)ALBuffer
->LoopEnd
)
301 if(DataPosInt
>= BufferPrePadding
)
302 pos
= DataPosInt
- BufferPrePadding
;
305 DataSize
= BufferPrePadding
- DataPosInt
;
306 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
308 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
309 SrcDataSize
+= DataSize
;
314 /* Copy what's left to play in the source buffer, and clear the
315 * rest of the temp buffer */
316 DataSize
= minu(SrcBufferSize
- SrcDataSize
, ALBuffer
->SampleLen
- pos
);
318 LoadSamples(&SrcData
[SrcDataSize
], &Data
[(pos
*NumChannels
+ chan
)*SampleSize
],
319 NumChannels
, ALBuffer
->FmtType
, DataSize
);
320 SrcDataSize
+= DataSize
;
322 SilenceSamples(&SrcData
[SrcDataSize
], SrcBufferSize
- SrcDataSize
);
323 SrcDataSize
+= SrcBufferSize
- SrcDataSize
;
327 ALuint LoopStart
= ALBuffer
->LoopStart
;
328 ALuint LoopEnd
= ALBuffer
->LoopEnd
;
330 if(DataPosInt
>= LoopStart
)
332 pos
= DataPosInt
-LoopStart
;
333 while(pos
< BufferPrePadding
)
334 pos
+= LoopEnd
-LoopStart
;
335 pos
-= BufferPrePadding
;
338 else if(DataPosInt
>= BufferPrePadding
)
339 pos
= DataPosInt
- BufferPrePadding
;
342 DataSize
= BufferPrePadding
- DataPosInt
;
343 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
345 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
346 SrcDataSize
+= DataSize
;
351 /* Copy what's left of this loop iteration, then copy repeats
352 * of the loop section */
353 DataSize
= LoopEnd
- pos
;
354 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
356 LoadSamples(&SrcData
[SrcDataSize
], &Data
[(pos
*NumChannels
+ chan
)*SampleSize
],
357 NumChannels
, ALBuffer
->FmtType
, DataSize
);
358 SrcDataSize
+= DataSize
;
360 DataSize
= LoopEnd
-LoopStart
;
361 while(SrcBufferSize
> SrcDataSize
)
363 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
365 LoadSamples(&SrcData
[SrcDataSize
], &Data
[(LoopStart
*NumChannels
+ chan
)*SampleSize
],
366 NumChannels
, ALBuffer
->FmtType
, DataSize
);
367 SrcDataSize
+= DataSize
;
373 /* Crawl the buffer queue to fill in the temp buffer */
374 ALbufferlistitem
*tmpiter
= BufferListItem
;
377 if(DataPosInt
>= BufferPrePadding
)
378 pos
= DataPosInt
- BufferPrePadding
;
381 pos
= BufferPrePadding
- DataPosInt
;
384 ALbufferlistitem
*prev
;
385 if((prev
=tmpiter
->prev
) != NULL
)
390 tmpiter
= tmpiter
->next
;
394 ALuint DataSize
= minu(SrcBufferSize
- SrcDataSize
, pos
);
396 SilenceSamples(&SrcData
[SrcDataSize
], DataSize
);
397 SrcDataSize
+= DataSize
;
405 if((ALuint
)tmpiter
->buffer
->SampleLen
> pos
)
407 pos
= tmpiter
->buffer
->SampleLen
- pos
;
410 pos
-= tmpiter
->buffer
->SampleLen
;
415 while(tmpiter
&& SrcBufferSize
> SrcDataSize
)
417 const ALbuffer
*ALBuffer
;
418 if((ALBuffer
=tmpiter
->buffer
) != NULL
)
420 const ALubyte
*Data
= ALBuffer
->data
;
421 ALuint DataSize
= ALBuffer
->SampleLen
;
423 /* Skip the data already played */
428 Data
+= (pos
*NumChannels
+ chan
)*SampleSize
;
432 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
433 LoadSamples(&SrcData
[SrcDataSize
], Data
, NumChannels
,
434 ALBuffer
->FmtType
, DataSize
);
435 SrcDataSize
+= DataSize
;
438 tmpiter
= tmpiter
->next
;
439 if(!tmpiter
&& Looping
)
440 tmpiter
= ATOMIC_LOAD(&Source
->queue
);
443 SilenceSamples(&SrcData
[SrcDataSize
], SrcBufferSize
- SrcDataSize
);
444 SrcDataSize
+= SrcBufferSize
- SrcDataSize
;
449 /* Now resample, then filter and mix to the appropriate outputs. */
450 ResampledData
= Resample(
451 &SrcData
[BufferPrePadding
], DataPosFrac
, increment
,
452 Device
->ResampledData
, DstBufferSize
455 DirectParams
*parms
= &voice
->Direct
;
456 const ALfloat
*samples
;
459 &parms
->Filters
[chan
].LowPass
, &parms
->Filters
[chan
].HighPass
,
460 Device
->FilteredData
, ResampledData
, DstBufferSize
,
461 parms
->Filters
[chan
].ActiveType
464 Mix(samples
, parms
->OutChannels
, parms
->OutBuffer
, parms
->Gains
[chan
],
465 parms
->Counter
, OutPos
, DstBufferSize
);
467 HrtfMix(parms
->OutBuffer
, samples
, parms
->Counter
, voice
->Offset
,
468 OutPos
, IrSize
, &parms
->Hrtf
.Params
[chan
],
469 &parms
->Hrtf
.State
[chan
], DstBufferSize
);
472 /* Only the first channel for B-Format buffers (W channel) goes to
474 if(chan
> 0 && isbformat
)
476 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
478 SendParams
*parms
= &voice
->Send
[j
];
479 const ALfloat
*samples
;
481 if(!parms
->OutBuffer
)
485 &parms
->Filters
[chan
].LowPass
, &parms
->Filters
[chan
].HighPass
,
486 Device
->FilteredData
, ResampledData
, DstBufferSize
,
487 parms
->Filters
[chan
].ActiveType
489 Mix(samples
, 1, parms
->OutBuffer
, &parms
->Gain
,
490 parms
->Counter
, OutPos
, DstBufferSize
);
493 /* Update positions */
494 DataPosFrac
+= increment
*DstBufferSize
;
495 DataPosInt
+= DataPosFrac
>>FRACTIONBITS
;
496 DataPosFrac
&= FRACTIONMASK
;
498 OutPos
+= DstBufferSize
;
499 voice
->Offset
+= DstBufferSize
;
500 voice
->Direct
.Counter
= maxu(voice
->Direct
.Counter
, DstBufferSize
) - DstBufferSize
;
501 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
502 voice
->Send
[j
].Counter
= maxu(voice
->Send
[j
].Counter
, DstBufferSize
) - DstBufferSize
;
504 /* Handle looping sources */
507 const ALbuffer
*ALBuffer
;
509 ALuint LoopStart
= 0;
512 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
514 DataSize
= ALBuffer
->SampleLen
;
515 LoopStart
= ALBuffer
->LoopStart
;
516 LoopEnd
= ALBuffer
->LoopEnd
;
517 if(LoopEnd
> DataPosInt
)
521 if(Looping
&& Source
->SourceType
== AL_STATIC
)
523 assert(LoopEnd
> LoopStart
);
524 DataPosInt
= ((DataPosInt
-LoopStart
)%(LoopEnd
-LoopStart
)) + LoopStart
;
528 if(DataSize
> DataPosInt
)
531 if(!(BufferListItem
=BufferListItem
->next
))
534 BufferListItem
= ATOMIC_LOAD(&Source
->queue
);
538 BufferListItem
= NULL
;
545 DataPosInt
-= DataSize
;
547 } while(State
== AL_PLAYING
&& OutPos
< SamplesToDo
);
549 /* Update source info */
550 Source
->state
= State
;
551 ATOMIC_STORE(&Source
->current_buffer
, BufferListItem
);
552 Source
->position
= DataPosInt
;
553 Source
->position_fraction
= DataPosFrac
;