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 static_assert((INT_MAX
>>FRACTIONBITS
)/MAX_PITCH
> BUFFERSIZE
,
42 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
44 extern inline void InitiatePositionArrays(ALuint frac
, ALuint increment
, ALuint
*frac_arr
, ALuint
*pos_arr
, ALuint size
);
46 alignas(16) union ResamplerCoeffs ResampleCoeffs
;
56 ResamplerDefault
= LinearResampler
59 /* FIR8 requires 3 extra samples before the current position, and 4 after. */
60 static_assert(MAX_PRE_SAMPLES
>= 3, "MAX_PRE_SAMPLES must be at least 3!");
61 static_assert(MAX_POST_SAMPLES
>= 4, "MAX_POST_SAMPLES must be at least 4!");
64 static HrtfMixerFunc MixHrtfSamples
= MixHrtf_C
;
65 static MixerFunc MixSamples
= Mix_C
;
66 static ResamplerFunc ResampleSamples
= Resample_point32_C
;
68 static inline HrtfMixerFunc
SelectHrtfMixer(void)
71 if((CPUCapFlags
&CPU_CAP_SSE
))
75 if((CPUCapFlags
&CPU_CAP_NEON
))
82 static inline MixerFunc
SelectMixer(void)
85 if((CPUCapFlags
&CPU_CAP_SSE
))
89 if((CPUCapFlags
&CPU_CAP_NEON
))
96 static inline ResamplerFunc
SelectResampler(enum Resampler resampler
)
101 return Resample_point32_C
;
102 case LinearResampler
:
104 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
105 return Resample_lerp32_SSE41
;
108 if((CPUCapFlags
&CPU_CAP_SSE2
))
109 return Resample_lerp32_SSE2
;
111 return Resample_lerp32_C
;
114 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
115 return Resample_fir4_32_SSE41
;
118 if((CPUCapFlags
&CPU_CAP_SSE3
))
119 return Resample_fir4_32_SSE3
;
121 return Resample_fir4_32_C
;
124 if((CPUCapFlags
&CPU_CAP_SSE4_1
))
125 return Resample_fir8_32_SSE41
;
128 if((CPUCapFlags
&CPU_CAP_SSE3
))
129 return Resample_fir8_32_SSE3
;
131 return Resample_fir8_32_C
;
134 if((CPUCapFlags
&CPU_CAP_SSE
))
135 return Resample_bsinc32_SSE
;
137 return Resample_bsinc32_C
;
140 return Resample_point32_C
;
144 /* The sinc resampler makes use of a Kaiser window to limit the needed sample
145 * points to 4 and 8, respectively.
149 #define M_PI (3.14159265358979323846)
151 static inline double Sinc(double x
)
153 if(x
== 0.0) return 1.0;
154 return sin(x
*M_PI
) / (x
*M_PI
);
157 /* The zero-order modified Bessel function of the first kind, used for the
160 * I_0(x) = sum_{k=0}^inf (1 / k!)^2 (x / 2)^(2 k)
161 * = sum_{k=0}^inf ((x / 2)^k / k!)^2
163 static double BesselI_0(double x
)
165 double term
, sum
, x2
, y
, last_sum
;
168 /* Start at k=1 since k=0 is trivial. */
174 /* Let the integration converge until the term of the sum is no longer
183 } while(sum
!= last_sum
);
187 /* Calculate a Kaiser window from the given beta value and a normalized k
190 * w(k) = { I_0(B sqrt(1 - k^2)) / I_0(B), -1 <= k <= 1
193 * Where k can be calculated as:
195 * k = i / l, where -l <= i <= l.
199 * k = 2 i / M - 1, where 0 <= i <= M.
201 static inline double Kaiser(double b
, double k
)
203 if(k
<= -1.0 || k
>= 1.0) return 0.0;
204 return BesselI_0(b
* sqrt(1.0 - (k
*k
))) / BesselI_0(b
);
207 static inline double CalcKaiserBeta(double rejection
)
210 return 0.1102 * (rejection
- 8.7);
211 if(rejection
>= 21.0)
212 return (0.5842 * pow(rejection
- 21.0, 0.4)) +
213 (0.07886 * (rejection
- 21.0));
217 static float SincKaiser(double r
, double x
)
219 /* Limit rippling to -60dB. */
220 return (float)(Kaiser(CalcKaiserBeta(60.0), x
/ r
) * Sinc(x
));
224 void aluInitMixer(void)
226 enum Resampler resampler
= ResamplerDefault
;
230 if(ConfigValueStr(NULL
, NULL
, "resampler", &str
))
232 if(strcasecmp(str
, "point") == 0 || strcasecmp(str
, "none") == 0)
233 resampler
= PointResampler
;
234 else if(strcasecmp(str
, "linear") == 0)
235 resampler
= LinearResampler
;
236 else if(strcasecmp(str
, "sinc4") == 0)
237 resampler
= FIR4Resampler
;
238 else if(strcasecmp(str
, "sinc8") == 0)
239 resampler
= FIR8Resampler
;
240 else if(strcasecmp(str
, "bsinc") == 0)
241 resampler
= BSincResampler
;
242 else if(strcasecmp(str
, "cubic") == 0)
244 WARN("Resampler option \"cubic\" is deprecated, using sinc4\n");
245 resampler
= FIR4Resampler
;
250 long n
= strtol(str
, &end
, 0);
251 if(*end
== '\0' && (n
== PointResampler
|| n
== LinearResampler
|| n
== FIR4Resampler
))
254 WARN("Invalid resampler: %s\n", str
);
258 if(resampler
== FIR8Resampler
)
259 for(i
= 0;i
< FRACTIONONE
;i
++)
261 ALdouble mu
= (ALdouble
)i
/ FRACTIONONE
;
262 ResampleCoeffs
.FIR8
[i
][0] = SincKaiser(4.0, mu
- -3.0);
263 ResampleCoeffs
.FIR8
[i
][1] = SincKaiser(4.0, mu
- -2.0);
264 ResampleCoeffs
.FIR8
[i
][2] = SincKaiser(4.0, mu
- -1.0);
265 ResampleCoeffs
.FIR8
[i
][3] = SincKaiser(4.0, mu
- 0.0);
266 ResampleCoeffs
.FIR8
[i
][4] = SincKaiser(4.0, mu
- 1.0);
267 ResampleCoeffs
.FIR8
[i
][5] = SincKaiser(4.0, mu
- 2.0);
268 ResampleCoeffs
.FIR8
[i
][6] = SincKaiser(4.0, mu
- 3.0);
269 ResampleCoeffs
.FIR8
[i
][7] = SincKaiser(4.0, mu
- 4.0);
271 else if(resampler
== FIR4Resampler
)
272 for(i
= 0;i
< FRACTIONONE
;i
++)
274 ALdouble mu
= (ALdouble
)i
/ FRACTIONONE
;
275 ResampleCoeffs
.FIR4
[i
][0] = SincKaiser(2.0, mu
- -1.0);
276 ResampleCoeffs
.FIR4
[i
][1] = SincKaiser(2.0, mu
- 0.0);
277 ResampleCoeffs
.FIR4
[i
][2] = SincKaiser(2.0, mu
- 1.0);
278 ResampleCoeffs
.FIR4
[i
][3] = SincKaiser(2.0, mu
- 2.0);
281 MixHrtfSamples
= SelectHrtfMixer();
282 MixSamples
= SelectMixer();
283 ResampleSamples
= SelectResampler(resampler
);
287 static inline ALfloat
Sample_ALbyte(ALbyte val
)
288 { return val
* (1.0f
/127.0f
); }
290 static inline ALfloat
Sample_ALshort(ALshort val
)
291 { return val
* (1.0f
/32767.0f
); }
293 static inline ALfloat
Sample_ALfloat(ALfloat val
)
296 #define DECL_TEMPLATE(T) \
297 static inline void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
300 for(i = 0;i < samples;i++) \
301 dst[i] = Sample_##T(src[i*srcstep]); \
304 DECL_TEMPLATE(ALbyte
)
305 DECL_TEMPLATE(ALshort
)
306 DECL_TEMPLATE(ALfloat
)
310 static void LoadSamples(ALfloat
*dst
, const ALvoid
*src
, ALuint srcstep
, enum FmtType srctype
, ALuint samples
)
315 Load_ALbyte(dst
, src
, srcstep
, samples
);
318 Load_ALshort(dst
, src
, srcstep
, samples
);
321 Load_ALfloat(dst
, src
, srcstep
, samples
);
326 static inline void SilenceSamples(ALfloat
*dst
, ALuint samples
)
329 for(i
= 0;i
< samples
;i
++)
334 static const ALfloat
*DoFilters(ALfilterState
*lpfilter
, ALfilterState
*hpfilter
,
335 ALfloat
*restrict dst
, const ALfloat
*restrict src
,
336 ALuint numsamples
, enum ActiveFilters type
)
342 ALfilterState_processPassthru(lpfilter
, src
, numsamples
);
343 ALfilterState_processPassthru(hpfilter
, src
, numsamples
);
347 ALfilterState_process(lpfilter
, dst
, src
, numsamples
);
348 ALfilterState_processPassthru(hpfilter
, dst
, numsamples
);
351 ALfilterState_processPassthru(lpfilter
, src
, numsamples
);
352 ALfilterState_process(hpfilter
, dst
, src
, numsamples
);
356 for(i
= 0;i
< numsamples
;)
359 ALuint todo
= minu(256, numsamples
-i
);
361 ALfilterState_process(lpfilter
, temp
, src
+i
, todo
);
362 ALfilterState_process(hpfilter
, dst
+i
, temp
, todo
);
371 ALvoid
MixSource(ALvoice
*voice
, ALsource
*Source
, ALCdevice
*Device
, ALuint SamplesToDo
)
373 ResamplerFunc Resample
;
374 ALbufferlistitem
*BufferListItem
;
375 ALuint DataPosInt
, DataPosFrac
;
386 /* Get source info */
387 State
= Source
->state
;
388 BufferListItem
= ATOMIC_LOAD(&Source
->current_buffer
);
389 DataPosInt
= Source
->position
;
390 DataPosFrac
= Source
->position_fraction
;
391 Looping
= Source
->Looping
;
392 NumChannels
= Source
->NumChannels
;
393 SampleSize
= Source
->SampleSize
;
394 increment
= voice
->Step
;
396 IrSize
= (Device
->Hrtf
? GetHrtfIrSize(Device
->Hrtf
) : 0);
398 Resample
= ((increment
== FRACTIONONE
&& DataPosFrac
== 0) ?
399 Resample_copy32_C
: ResampleSamples
);
403 ALuint SrcBufferSize
, DstBufferSize
;
405 /* Figure out how many buffer samples will be needed */
406 DataSize64
= SamplesToDo
-OutPos
;
407 DataSize64
*= increment
;
408 DataSize64
+= DataPosFrac
+FRACTIONMASK
;
409 DataSize64
>>= FRACTIONBITS
;
410 DataSize64
+= MAX_POST_SAMPLES
+MAX_PRE_SAMPLES
;
412 SrcBufferSize
= (ALuint
)mini64(DataSize64
, BUFFERSIZE
);
414 /* Figure out how many samples we can actually mix from this. */
415 DataSize64
= SrcBufferSize
;
416 DataSize64
-= MAX_POST_SAMPLES
+MAX_PRE_SAMPLES
;
417 DataSize64
<<= FRACTIONBITS
;
418 DataSize64
-= DataPosFrac
;
420 DstBufferSize
= (ALuint
)((DataSize64
+(increment
-1)) / increment
);
421 DstBufferSize
= minu(DstBufferSize
, (SamplesToDo
-OutPos
));
423 /* Some mixers like having a multiple of 4, so try to give that unless
424 * this is the last update. */
425 if(OutPos
+DstBufferSize
< SamplesToDo
)
428 for(chan
= 0;chan
< NumChannels
;chan
++)
430 const ALfloat
*ResampledData
;
431 ALfloat
*SrcData
= Device
->SourceData
;
434 /* Load the previous samples into the source data first. */
435 memcpy(SrcData
, voice
->PrevSamples
[chan
], MAX_PRE_SAMPLES
*sizeof(ALfloat
));
436 SrcDataSize
= MAX_PRE_SAMPLES
;
438 if(Source
->SourceType
== AL_STATIC
)
440 const ALbuffer
*ALBuffer
= BufferListItem
->buffer
;
441 const ALubyte
*Data
= ALBuffer
->data
;
445 /* Offset buffer data to current channel */
446 Data
+= chan
*SampleSize
;
448 /* If current pos is beyond the loop range, do not loop */
449 if(Looping
== AL_FALSE
|| DataPosInt
>= (ALuint
)ALBuffer
->LoopEnd
)
453 /* Load what's left to play from the source buffer, and
454 * clear the rest of the temp buffer */
456 DataSize
= minu(SrcBufferSize
- SrcDataSize
, ALBuffer
->SampleLen
- pos
);
458 LoadSamples(&SrcData
[SrcDataSize
], &Data
[pos
* NumChannels
*SampleSize
],
459 NumChannels
, ALBuffer
->FmtType
, DataSize
);
460 SrcDataSize
+= DataSize
;
462 SilenceSamples(&SrcData
[SrcDataSize
], SrcBufferSize
- SrcDataSize
);
463 SrcDataSize
+= SrcBufferSize
- SrcDataSize
;
467 ALuint LoopStart
= ALBuffer
->LoopStart
;
468 ALuint LoopEnd
= ALBuffer
->LoopEnd
;
470 /* Load what's left of this loop iteration, then load
471 * repeats of the loop section */
473 DataSize
= LoopEnd
- pos
;
474 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
476 LoadSamples(&SrcData
[SrcDataSize
], &Data
[pos
* NumChannels
*SampleSize
],
477 NumChannels
, ALBuffer
->FmtType
, DataSize
);
478 SrcDataSize
+= DataSize
;
480 DataSize
= LoopEnd
-LoopStart
;
481 while(SrcBufferSize
> SrcDataSize
)
483 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
485 LoadSamples(&SrcData
[SrcDataSize
], &Data
[LoopStart
* NumChannels
*SampleSize
],
486 NumChannels
, ALBuffer
->FmtType
, DataSize
);
487 SrcDataSize
+= DataSize
;
493 /* Crawl the buffer queue to fill in the temp buffer */
494 ALbufferlistitem
*tmpiter
= BufferListItem
;
495 ALuint pos
= DataPosInt
;
497 while(tmpiter
&& SrcBufferSize
> SrcDataSize
)
499 const ALbuffer
*ALBuffer
;
500 if((ALBuffer
=tmpiter
->buffer
) != NULL
)
502 const ALubyte
*Data
= ALBuffer
->data
;
503 ALuint DataSize
= ALBuffer
->SampleLen
;
505 /* Skip the data already played */
510 Data
+= (pos
*NumChannels
+ chan
)*SampleSize
;
514 DataSize
= minu(SrcBufferSize
- SrcDataSize
, DataSize
);
515 LoadSamples(&SrcData
[SrcDataSize
], Data
, NumChannels
,
516 ALBuffer
->FmtType
, DataSize
);
517 SrcDataSize
+= DataSize
;
520 tmpiter
= tmpiter
->next
;
521 if(!tmpiter
&& Looping
)
522 tmpiter
= ATOMIC_LOAD(&Source
->queue
);
525 SilenceSamples(&SrcData
[SrcDataSize
], SrcBufferSize
- SrcDataSize
);
526 SrcDataSize
+= SrcBufferSize
- SrcDataSize
;
531 /* Store the last source samples used for next time. */
532 memcpy(voice
->PrevSamples
[chan
],
533 &SrcData
[(increment
*DstBufferSize
+ DataPosFrac
)>>FRACTIONBITS
],
534 MAX_PRE_SAMPLES
*sizeof(ALfloat
)
537 /* Now resample, then filter and mix to the appropriate outputs. */
538 ResampledData
= Resample(&voice
->SincState
,
539 &SrcData
[MAX_PRE_SAMPLES
], DataPosFrac
, increment
,
540 Device
->ResampledData
, DstBufferSize
543 DirectParams
*parms
= &voice
->Direct
;
544 const ALfloat
*samples
;
547 &parms
->Filters
[chan
].LowPass
, &parms
->Filters
[chan
].HighPass
,
548 Device
->FilteredData
, ResampledData
, DstBufferSize
,
549 parms
->Filters
[chan
].ActiveType
552 MixSamples(samples
, parms
->OutChannels
, parms
->OutBuffer
, parms
->Gains
[chan
],
553 parms
->Counter
, OutPos
, DstBufferSize
);
555 MixHrtfSamples(parms
->OutBuffer
, samples
, parms
->Counter
, voice
->Offset
,
556 OutPos
, IrSize
, &parms
->Hrtf
[chan
].Params
,
557 &parms
->Hrtf
[chan
].State
, DstBufferSize
);
560 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
562 SendParams
*parms
= &voice
->Send
[j
];
563 const ALfloat
*samples
;
565 if(!parms
->OutBuffer
)
569 &parms
->Filters
[chan
].LowPass
, &parms
->Filters
[chan
].HighPass
,
570 Device
->FilteredData
, ResampledData
, DstBufferSize
,
571 parms
->Filters
[chan
].ActiveType
573 MixSamples(samples
, parms
->OutChannels
, parms
->OutBuffer
, parms
->Gains
[chan
],
574 parms
->Counter
, OutPos
, DstBufferSize
);
577 /* Update positions */
578 DataPosFrac
+= increment
*DstBufferSize
;
579 DataPosInt
+= DataPosFrac
>>FRACTIONBITS
;
580 DataPosFrac
&= FRACTIONMASK
;
582 OutPos
+= DstBufferSize
;
583 voice
->Offset
+= DstBufferSize
;
584 voice
->Direct
.Counter
= maxu(voice
->Direct
.Counter
, DstBufferSize
) - DstBufferSize
;
585 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
586 voice
->Send
[j
].Counter
= maxu(voice
->Send
[j
].Counter
, DstBufferSize
) - DstBufferSize
;
588 /* Handle looping sources */
591 const ALbuffer
*ALBuffer
;
593 ALuint LoopStart
= 0;
596 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
598 DataSize
= ALBuffer
->SampleLen
;
599 LoopStart
= ALBuffer
->LoopStart
;
600 LoopEnd
= ALBuffer
->LoopEnd
;
601 if(LoopEnd
> DataPosInt
)
605 if(Looping
&& Source
->SourceType
== AL_STATIC
)
607 assert(LoopEnd
> LoopStart
);
608 DataPosInt
= ((DataPosInt
-LoopStart
)%(LoopEnd
-LoopStart
)) + LoopStart
;
612 if(DataSize
> DataPosInt
)
615 if(!(BufferListItem
=BufferListItem
->next
))
618 BufferListItem
= ATOMIC_LOAD(&Source
->queue
);
622 BufferListItem
= NULL
;
629 DataPosInt
-= DataSize
;
631 } while(State
== AL_PLAYING
&& OutPos
< SamplesToDo
);
633 /* Update source info */
634 Source
->state
= State
;
635 ATOMIC_STORE(&Source
->current_buffer
, BufferListItem
);
636 Source
->position
= DataPosInt
;
637 Source
->position_fraction
= DataPosFrac
;