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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
34 #include "alListener.h"
35 #include "alAuxEffectSlot.h"
39 #include "mixer_defs.h"
42 DryMixerFunc
SelectDirectMixer(void)
45 if((CPUCapFlags
&CPU_CAP_SSE
))
49 if((CPUCapFlags
&CPU_CAP_NEON
))
50 return MixDirect_Neon
;
56 DryMixerFunc
SelectHrtfMixer(void)
59 if((CPUCapFlags
&CPU_CAP_SSE
))
60 return MixDirect_Hrtf_SSE
;
63 if((CPUCapFlags
&CPU_CAP_NEON
))
64 return MixDirect_Hrtf_Neon
;
67 return MixDirect_Hrtf_C
;
70 WetMixerFunc
SelectSendMixer(void)
73 if((CPUCapFlags
&CPU_CAP_SSE
))
77 if((CPUCapFlags
&CPU_CAP_NEON
))
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 samples) \
98 for(i = 0;i < samples;i++) \
99 dst[i] = Sample_##T(src[i]); \
102 DECL_TEMPLATE(ALbyte
)
103 DECL_TEMPLATE(ALshort
)
104 DECL_TEMPLATE(ALfloat
)
108 static void LoadStack(ALfloat
*dst
, const ALvoid
*src
, enum FmtType srctype
, ALuint samples
)
113 Load_ALbyte(dst
, src
, samples
);
116 Load_ALshort(dst
, src
, samples
);
119 Load_ALfloat(dst
, src
, samples
);
124 static void SilenceStack(ALfloat
*dst
, ALuint samples
)
127 for(i
= 0;i
< samples
;i
++)
132 static __inline ALfloat
point32(const ALfloat
*vals
, ALint step
, ALint frac
)
133 { return vals
[0]; (void)step
; (void)frac
; }
134 static __inline ALfloat
lerp32(const ALfloat
*vals
, ALint step
, ALint frac
)
135 { return lerp(vals
[0], vals
[step
], frac
* (1.0f
/FRACTIONONE
)); }
136 static __inline ALfloat
cubic32(const ALfloat
*vals
, ALint step
, ALint frac
)
137 { return cubic(vals
[-step
], vals
[0], vals
[step
], vals
[step
+step
],
138 frac
* (1.0f
/FRACTIONONE
)); }
140 #define DECL_TEMPLATE(Sampler) \
141 static void Resample_##Sampler(const ALfloat *data, ALuint frac, \
142 ALuint increment, ALuint NumChannels, ALfloat *RESTRICT OutBuffer, \
149 for(i = 0;i < BufferSize+1;i++) \
151 value = Sampler(data + pos*NumChannels, NumChannels, frac); \
152 OutBuffer[i] = value; \
155 pos += frac>>FRACTIONBITS; \
156 frac &= FRACTIONMASK; \
160 DECL_TEMPLATE(point32
)
161 DECL_TEMPLATE(lerp32
)
162 DECL_TEMPLATE(cubic32
)
166 static void Resample(enum Resampler Resampler
, const ALfloat
*data
, ALuint frac
,
167 ALuint increment
, ALuint NumChannels
,
168 ALfloat
*RESTRICT OutBuffer
, ALuint BufferSize
)
170 if(increment
== FRACTIONONE
)
176 Resample_point32(data
, frac
, increment
, NumChannels
,
177 OutBuffer
, BufferSize
);
179 case LinearResampler
:
180 Resample_lerp32(data
, frac
, increment
, NumChannels
,
181 OutBuffer
, BufferSize
);
184 Resample_cubic32(data
, frac
, increment
, NumChannels
,
185 OutBuffer
, BufferSize
);
188 /* Shouldn't happen */
194 static void Filter2P(FILTER
*filter
, ALuint chan
, ALfloat
*RESTRICT dst
,
195 const ALfloat
*RESTRICT src
, ALuint numsamples
)
198 for(i
= 0;i
< numsamples
;i
++)
199 dst
[i
] = lpFilter2P(filter
, chan
, src
[i
]);
200 dst
[i
] = lpFilter2PC(filter
, chan
, src
[i
]);
204 ALvoid
MixSource(ALsource
*Source
, ALCdevice
*Device
, ALuint SamplesToDo
)
206 ALbufferlistitem
*BufferListItem
;
207 ALuint DataPosInt
, DataPosFrac
;
208 ALuint BuffersPlayed
;
211 enum Resampler Resampler
;
219 /* Get source info */
220 State
= Source
->state
;
221 BuffersPlayed
= Source
->BuffersPlayed
;
222 DataPosInt
= Source
->position
;
223 DataPosFrac
= Source
->position_fraction
;
224 Looping
= Source
->Looping
;
225 increment
= Source
->Params
.Step
;
226 Resampler
= Source
->Resampler
;
227 NumChannels
= Source
->NumChannels
;
228 FrameSize
= NumChannels
* Source
->SampleSize
;
230 /* Get current buffer queue item */
231 BufferListItem
= Source
->queue
;
232 for(i
= 0;i
< BuffersPlayed
;i
++)
233 BufferListItem
= BufferListItem
->next
;
237 const ALuint BufferPrePadding
= ResamplerPrePadding
[Resampler
];
238 const ALuint BufferPadding
= ResamplerPadding
[Resampler
];
239 ALfloat StackData
[BUFFERSIZE
];
240 ALfloat
*SrcData
= StackData
;
241 ALuint SrcDataSize
= 0;
244 /* Figure out how many buffer bytes will be needed */
245 DataSize64
= SamplesToDo
-OutPos
+1;
246 DataSize64
*= increment
;
247 DataSize64
+= DataPosFrac
+FRACTIONMASK
;
248 DataSize64
>>= FRACTIONBITS
;
249 DataSize64
+= BufferPadding
+BufferPrePadding
;
250 DataSize64
*= NumChannels
;
252 BufferSize
= (ALuint
)mini64(DataSize64
, BUFFERSIZE
);
253 BufferSize
/= NumChannels
;
255 if(Source
->SourceType
== AL_STATIC
)
257 const ALbuffer
*ALBuffer
= Source
->queue
->buffer
;
258 const ALubyte
*Data
= ALBuffer
->data
;
262 /* If current pos is beyond the loop range, do not loop */
263 if(Looping
== AL_FALSE
|| DataPosInt
>= (ALuint
)ALBuffer
->LoopEnd
)
267 if(DataPosInt
>= BufferPrePadding
)
268 pos
= DataPosInt
- BufferPrePadding
;
271 DataSize
= BufferPrePadding
- DataPosInt
;
272 DataSize
= minu(BufferSize
, DataSize
);
274 SilenceStack(&SrcData
[SrcDataSize
*NumChannels
],
275 DataSize
*NumChannels
);
276 SrcDataSize
+= DataSize
;
277 BufferSize
-= DataSize
;
282 /* Copy what's left to play in the source buffer, and clear the
283 * rest of the temp buffer */
284 DataSize
= ALBuffer
->SampleLen
- pos
;
285 DataSize
= minu(BufferSize
, DataSize
);
287 LoadStack(&SrcData
[SrcDataSize
*NumChannels
], &Data
[pos
*FrameSize
],
288 ALBuffer
->FmtType
, DataSize
*NumChannels
);
289 SrcDataSize
+= DataSize
;
290 BufferSize
-= DataSize
;
292 SilenceStack(&SrcData
[SrcDataSize
*NumChannels
],
293 BufferSize
*NumChannels
);
294 SrcDataSize
+= BufferSize
;
295 BufferSize
-= BufferSize
;
299 ALuint LoopStart
= ALBuffer
->LoopStart
;
300 ALuint LoopEnd
= ALBuffer
->LoopEnd
;
302 if(DataPosInt
>= LoopStart
)
304 pos
= DataPosInt
-LoopStart
;
305 while(pos
< BufferPrePadding
)
306 pos
+= LoopEnd
-LoopStart
;
307 pos
-= BufferPrePadding
;
310 else if(DataPosInt
>= BufferPrePadding
)
311 pos
= DataPosInt
- BufferPrePadding
;
314 DataSize
= BufferPrePadding
- DataPosInt
;
315 DataSize
= minu(BufferSize
, DataSize
);
317 SilenceStack(&SrcData
[SrcDataSize
*NumChannels
], DataSize
*NumChannels
);
318 SrcDataSize
+= DataSize
;
319 BufferSize
-= DataSize
;
324 /* Copy what's left of this loop iteration, then copy repeats
325 * of the loop section */
326 DataSize
= LoopEnd
- pos
;
327 DataSize
= minu(BufferSize
, DataSize
);
329 LoadStack(&SrcData
[SrcDataSize
*NumChannels
], &Data
[pos
*FrameSize
],
330 ALBuffer
->FmtType
, DataSize
*NumChannels
);
331 SrcDataSize
+= DataSize
;
332 BufferSize
-= DataSize
;
334 DataSize
= LoopEnd
-LoopStart
;
335 while(BufferSize
> 0)
337 DataSize
= minu(BufferSize
, DataSize
);
339 LoadStack(&SrcData
[SrcDataSize
*NumChannels
], &Data
[LoopStart
*FrameSize
],
340 ALBuffer
->FmtType
, DataSize
*NumChannels
);
341 SrcDataSize
+= DataSize
;
342 BufferSize
-= DataSize
;
348 /* Crawl the buffer queue to fill in the temp buffer */
349 ALbufferlistitem
*tmpiter
= BufferListItem
;
352 if(DataPosInt
>= BufferPrePadding
)
353 pos
= DataPosInt
- BufferPrePadding
;
356 pos
= BufferPrePadding
- DataPosInt
;
359 if(!tmpiter
->prev
&& !Looping
)
361 ALuint DataSize
= minu(BufferSize
, pos
);
363 SilenceStack(&SrcData
[SrcDataSize
*NumChannels
], DataSize
*NumChannels
);
364 SrcDataSize
+= DataSize
;
365 BufferSize
-= DataSize
;
372 tmpiter
= tmpiter
->prev
;
376 tmpiter
= tmpiter
->next
;
381 if((ALuint
)tmpiter
->buffer
->SampleLen
> pos
)
383 pos
= tmpiter
->buffer
->SampleLen
- pos
;
386 pos
-= tmpiter
->buffer
->SampleLen
;
391 while(tmpiter
&& BufferSize
> 0)
393 const ALbuffer
*ALBuffer
;
394 if((ALBuffer
=tmpiter
->buffer
) != NULL
)
396 const ALubyte
*Data
= ALBuffer
->data
;
397 ALuint DataSize
= ALBuffer
->SampleLen
;
399 /* Skip the data already played */
404 Data
+= pos
*FrameSize
;
408 DataSize
= minu(BufferSize
, DataSize
);
409 LoadStack(&SrcData
[SrcDataSize
*NumChannels
], Data
,
410 ALBuffer
->FmtType
, DataSize
*NumChannels
);
411 SrcDataSize
+= DataSize
;
412 BufferSize
-= DataSize
;
415 tmpiter
= tmpiter
->next
;
416 if(!tmpiter
&& Looping
)
417 tmpiter
= Source
->queue
;
420 SilenceStack(&SrcData
[SrcDataSize
*NumChannels
], BufferSize
*NumChannels
);
421 SrcDataSize
+= BufferSize
;
422 BufferSize
-= BufferSize
;
427 /* Figure out how many samples we can mix. */
428 DataSize64
= SrcDataSize
;
429 DataSize64
-= BufferPadding
+BufferPrePadding
;
430 DataSize64
<<= FRACTIONBITS
;
431 DataSize64
-= increment
;
432 DataSize64
-= DataPosFrac
;
434 BufferSize
= (ALuint
)((DataSize64
+(increment
-1)) / increment
);
435 BufferSize
= minu(BufferSize
, (SamplesToDo
-OutPos
));
437 /* Some mixers like having a multiple of 4, so try to give that unless
438 * this is the last update. */
439 if(OutPos
+BufferSize
< SamplesToDo
)
442 SrcData
+= BufferPrePadding
*NumChannels
;
443 for(i
= 0;i
< NumChannels
;i
++)
445 DirectParams
*directparms
= &Source
->Params
.Direct
;
446 ALIGN(16) ALfloat FilteredData
[BUFFERSIZE
];
447 ALfloat ResampledData
[BUFFERSIZE
];
449 Resample(Resampler
, SrcData
+i
, DataPosFrac
, increment
,
450 NumChannels
, ResampledData
, BufferSize
);
452 Filter2P(&directparms
->iirFilter
, i
, FilteredData
, ResampledData
,
454 Source
->Params
.DryMix(Source
, Device
, directparms
,
455 FilteredData
, i
, OutPos
, SamplesToDo
,
458 for(j
= 0;j
< Device
->NumAuxSends
;j
++)
460 SendParams
*sendparms
= &Source
->Params
.Send
[j
];
464 Filter2P(&sendparms
->iirFilter
, i
, FilteredData
, ResampledData
,
466 Source
->Params
.WetMix(sendparms
, FilteredData
, OutPos
,
467 SamplesToDo
, BufferSize
);
470 for(i
= 0;i
< BufferSize
;i
++)
472 DataPosFrac
+= increment
;
473 DataPosInt
+= DataPosFrac
>>FRACTIONBITS
;
474 DataPosFrac
&= FRACTIONMASK
;
478 /* Handle looping sources */
481 const ALbuffer
*ALBuffer
;
483 ALuint LoopStart
= 0;
486 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
488 DataSize
= ALBuffer
->SampleLen
;
489 LoopStart
= ALBuffer
->LoopStart
;
490 LoopEnd
= ALBuffer
->LoopEnd
;
491 if(LoopEnd
> DataPosInt
)
495 if(Looping
&& Source
->SourceType
== AL_STATIC
)
497 DataPosInt
= ((DataPosInt
-LoopStart
)%(LoopEnd
-LoopStart
)) + LoopStart
;
501 if(DataSize
> DataPosInt
)
504 if(BufferListItem
->next
)
506 BufferListItem
= BufferListItem
->next
;
511 BufferListItem
= Source
->queue
;
517 BufferListItem
= Source
->queue
;
518 BuffersPlayed
= Source
->BuffersInQueue
;
524 DataPosInt
-= DataSize
;
526 } while(State
== AL_PLAYING
&& OutPos
< SamplesToDo
);
528 /* Update source info */
529 Source
->state
= State
;
530 Source
->BuffersPlayed
= BuffersPlayed
;
531 Source
->position
= DataPosInt
;
532 Source
->position_fraction
= DataPosFrac
;
533 Source
->Hrtf
.Offset
+= OutPos
;
534 if(State
== AL_PLAYING
)
536 Source
->Hrtf
.Counter
= maxu(Source
->Hrtf
.Counter
, OutPos
) - OutPos
;
537 Source
->Hrtf
.Moving
= AL_TRUE
;
541 Source
->Hrtf
.Counter
= 0;
542 Source
->Hrtf
.Moving
= AL_FALSE
;