Fix the reverb panning behavior to better fit the spec
[openal-soft.git] / Alc / converter.c
blob157073f2d2cac7cdf73388b6b682e83ea7838ed7
2 #include "config.h"
4 #include "converter.h"
6 #include "fpu_modes.h"
7 #include "mixer_defs.h"
10 SampleConverter *CreateSampleConverter(enum DevFmtType srcType, enum DevFmtType dstType, ALsizei numchans, ALsizei srcRate, ALsizei dstRate)
12 SampleConverter *converter;
13 ALsizei step;
15 if(numchans <= 0 || srcRate <= 0 || dstRate <= 0)
16 return NULL;
18 converter = al_calloc(16, FAM_SIZE(SampleConverter, Chan, numchans));
19 converter->mSrcType = srcType;
20 converter->mDstType = dstType;
21 converter->mNumChannels = numchans;
22 converter->mSrcTypeSize = BytesFromDevFmt(srcType);
23 converter->mDstTypeSize = BytesFromDevFmt(dstType);
25 converter->mSrcPrepCount = 0;
26 converter->mFracOffset = 0;
28 /* Have to set the mixer FPU mode since that's what the resampler code expects. */
29 START_MIXER_MODE();
30 step = fastf2i((ALfloat)mind((ALdouble)srcRate / dstRate, MAX_PITCH)*FRACTIONONE + 0.5f);
31 converter->mIncrement = maxi(step, 1);
32 if(converter->mIncrement == FRACTIONONE)
33 converter->mResample = Resample_copy_C;
34 else
36 /* TODO: Allow other resamplers. */
37 BsincPrepare(converter->mIncrement, &converter->mState.bsinc, &bsinc12);
38 converter->mResample = SelectResampler(BSinc12Resampler);
40 END_MIXER_MODE();
42 return converter;
45 void DestroySampleConverter(SampleConverter **converter)
47 if(converter)
49 al_free(*converter);
50 *converter = NULL;
55 static inline ALfloat Sample_ALbyte(ALbyte val)
56 { return val * (1.0f/128.0f); }
57 static inline ALfloat Sample_ALubyte(ALubyte val)
58 { return Sample_ALbyte((ALint)val - 128); }
60 static inline ALfloat Sample_ALshort(ALshort val)
61 { return val * (1.0f/32768.0f); }
62 static inline ALfloat Sample_ALushort(ALushort val)
63 { return Sample_ALshort((ALint)val - 32768); }
65 static inline ALfloat Sample_ALint(ALint val)
66 { return (val>>7) * (1.0f/16777216.0f); }
67 static inline ALfloat Sample_ALuint(ALuint val)
68 { return Sample_ALint(val - INT_MAX - 1); }
70 static inline ALfloat Sample_ALfloat(ALfloat val)
71 { return val; }
73 #define DECL_TEMPLATE(T) \
74 static inline void Load_##T(ALfloat *restrict dst, const T *restrict src, \
75 ALint srcstep, ALsizei samples) \
76 { \
77 ALsizei i; \
78 for(i = 0;i < samples;i++) \
79 dst[i] = Sample_##T(src[i*srcstep]); \
82 DECL_TEMPLATE(ALbyte)
83 DECL_TEMPLATE(ALubyte)
84 DECL_TEMPLATE(ALshort)
85 DECL_TEMPLATE(ALushort)
86 DECL_TEMPLATE(ALint)
87 DECL_TEMPLATE(ALuint)
88 DECL_TEMPLATE(ALfloat)
90 #undef DECL_TEMPLATE
92 static void LoadSamples(ALfloat *dst, const ALvoid *src, ALint srcstep, enum DevFmtType srctype, ALsizei samples)
94 switch(srctype)
96 case DevFmtByte:
97 Load_ALbyte(dst, src, srcstep, samples);
98 break;
99 case DevFmtUByte:
100 Load_ALubyte(dst, src, srcstep, samples);
101 break;
102 case DevFmtShort:
103 Load_ALshort(dst, src, srcstep, samples);
104 break;
105 case DevFmtUShort:
106 Load_ALushort(dst, src, srcstep, samples);
107 break;
108 case DevFmtInt:
109 Load_ALint(dst, src, srcstep, samples);
110 break;
111 case DevFmtUInt:
112 Load_ALuint(dst, src, srcstep, samples);
113 break;
114 case DevFmtFloat:
115 Load_ALfloat(dst, src, srcstep, samples);
116 break;
121 static inline ALbyte ALbyte_Sample(ALfloat val)
122 { return fastf2i(clampf(val*128.0f, -128.0f, 127.0f)); }
123 static inline ALubyte ALubyte_Sample(ALfloat val)
124 { return ALbyte_Sample(val)+128; }
126 static inline ALshort ALshort_Sample(ALfloat val)
127 { return fastf2i(clampf(val*32768.0f, -32768.0f, 32767.0f)); }
128 static inline ALushort ALushort_Sample(ALfloat val)
129 { return ALshort_Sample(val)+32768; }
131 static inline ALint ALint_Sample(ALfloat val)
132 { return fastf2i(clampf(val*16777216.0f, -16777216.0f, 16777215.0f)) << 7; }
133 static inline ALuint ALuint_Sample(ALfloat val)
134 { return ALint_Sample(val)+INT_MAX+1; }
136 static inline ALfloat ALfloat_Sample(ALfloat val)
137 { return val; }
139 #define DECL_TEMPLATE(T) \
140 static inline void Store_##T(T *restrict dst, const ALfloat *restrict src, \
141 ALint dststep, ALsizei samples) \
143 ALsizei i; \
144 for(i = 0;i < samples;i++) \
145 dst[i*dststep] = T##_Sample(src[i]); \
148 DECL_TEMPLATE(ALbyte)
149 DECL_TEMPLATE(ALubyte)
150 DECL_TEMPLATE(ALshort)
151 DECL_TEMPLATE(ALushort)
152 DECL_TEMPLATE(ALint)
153 DECL_TEMPLATE(ALuint)
154 DECL_TEMPLATE(ALfloat)
156 #undef DECL_TEMPLATE
158 static void StoreSamples(ALvoid *dst, const ALfloat *src, ALint dststep, enum DevFmtType dsttype, ALsizei samples)
160 switch(dsttype)
162 case DevFmtByte:
163 Store_ALbyte(dst, src, dststep, samples);
164 break;
165 case DevFmtUByte:
166 Store_ALubyte(dst, src, dststep, samples);
167 break;
168 case DevFmtShort:
169 Store_ALshort(dst, src, dststep, samples);
170 break;
171 case DevFmtUShort:
172 Store_ALushort(dst, src, dststep, samples);
173 break;
174 case DevFmtInt:
175 Store_ALint(dst, src, dststep, samples);
176 break;
177 case DevFmtUInt:
178 Store_ALuint(dst, src, dststep, samples);
179 break;
180 case DevFmtFloat:
181 Store_ALfloat(dst, src, dststep, samples);
182 break;
187 ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes)
189 ALint prepcount = converter->mSrcPrepCount;
190 ALsizei increment = converter->mIncrement;
191 ALsizei DataPosFrac = converter->mFracOffset;
192 ALuint64 DataSize64;
194 if(prepcount < 0)
196 /* Negative prepcount means we need to skip that many input samples. */
197 if(-prepcount >= srcframes)
198 return 0;
199 srcframes += prepcount;
200 prepcount = 0;
203 if(srcframes < 1)
205 /* No output samples if there's no input samples. */
206 return 0;
209 if(prepcount < MAX_RESAMPLE_PADDING*2 &&
210 MAX_RESAMPLE_PADDING*2 - prepcount >= srcframes)
212 /* Not enough input samples to generate an output sample. */
213 return 0;
216 DataSize64 = prepcount;
217 DataSize64 += srcframes;
218 DataSize64 -= MAX_RESAMPLE_PADDING*2;
219 DataSize64 <<= FRACTIONBITS;
220 DataSize64 -= DataPosFrac;
222 /* If we have a full prep, we can generate at least one sample. */
223 return (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE);
227 ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes)
229 const ALsizei SrcFrameSize = converter->mNumChannels * converter->mSrcTypeSize;
230 const ALsizei DstFrameSize = converter->mNumChannels * converter->mDstTypeSize;
231 const ALsizei increment = converter->mIncrement;
232 ALsizei pos = 0;
234 START_MIXER_MODE();
235 while(pos < dstframes && *srcframes > 0)
237 ALfloat *restrict SrcData = ASSUME_ALIGNED(converter->mSrcSamples, 16);
238 ALfloat *restrict DstData = ASSUME_ALIGNED(converter->mDstSamples, 16);
239 ALint prepcount = converter->mSrcPrepCount;
240 ALsizei DataPosFrac = converter->mFracOffset;
241 ALuint64 DataSize64;
242 ALsizei DstSize;
243 ALint toread;
244 ALsizei chan;
246 if(prepcount < 0)
248 /* Negative prepcount means we need to skip that many input samples. */
249 if(-prepcount >= *srcframes)
251 converter->mSrcPrepCount = prepcount + *srcframes;
252 *srcframes = 0;
253 break;
255 *src = (const ALbyte*)*src + SrcFrameSize*-prepcount;
256 *srcframes += prepcount;
257 converter->mSrcPrepCount = 0;
258 continue;
260 toread = mini(*srcframes, BUFFERSIZE - MAX_RESAMPLE_PADDING*2);
262 if(prepcount < MAX_RESAMPLE_PADDING*2 &&
263 MAX_RESAMPLE_PADDING*2 - prepcount >= toread)
265 /* Not enough input samples to generate an output sample. Store
266 * what we're given for later.
268 for(chan = 0;chan < converter->mNumChannels;chan++)
269 LoadSamples(&converter->Chan[chan].mPrevSamples[prepcount],
270 (const ALbyte*)*src + converter->mSrcTypeSize*chan,
271 converter->mNumChannels, converter->mSrcType, toread
274 converter->mSrcPrepCount = prepcount + toread;
275 *srcframes = 0;
276 break;
279 DataSize64 = prepcount;
280 DataSize64 += toread;
281 DataSize64 -= MAX_RESAMPLE_PADDING*2;
282 DataSize64 <<= FRACTIONBITS;
283 DataSize64 -= DataPosFrac;
285 /* If we have a full prep, we can generate at least one sample. */
286 DstSize = (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE);
287 DstSize = mini(DstSize, dstframes-pos);
289 for(chan = 0;chan < converter->mNumChannels;chan++)
291 const ALbyte *SrcSamples = (const ALbyte*)*src + converter->mSrcTypeSize*chan;
292 ALbyte *DstSamples = (ALbyte*)dst + converter->mDstTypeSize*chan;
293 const ALfloat *ResampledData;
294 ALsizei SrcDataEnd;
296 /* Load the previous samples into the source data first, then the
297 * new samples from the input buffer.
299 memcpy(SrcData, converter->Chan[chan].mPrevSamples,
300 prepcount*sizeof(ALfloat));
301 LoadSamples(SrcData + prepcount, SrcSamples,
302 converter->mNumChannels, converter->mSrcType, toread
305 /* Store as many prep samples for next time as possible, given the
306 * number of output samples being generated.
308 SrcDataEnd = (DataPosFrac + increment*DstSize)>>FRACTIONBITS;
309 if(SrcDataEnd >= prepcount+toread)
310 memset(converter->Chan[chan].mPrevSamples, 0,
311 sizeof(converter->Chan[chan].mPrevSamples));
312 else
314 size_t len = mini(MAX_RESAMPLE_PADDING*2, prepcount+toread-SrcDataEnd);
315 memcpy(converter->Chan[chan].mPrevSamples, &SrcData[SrcDataEnd],
316 len*sizeof(ALfloat));
317 memset(converter->Chan[chan].mPrevSamples+len, 0,
318 sizeof(converter->Chan[chan].mPrevSamples) - len*sizeof(ALfloat));
321 /* Now resample, and store the result in the output buffer. */
322 ResampledData = converter->mResample(&converter->mState,
323 SrcData+MAX_RESAMPLE_PADDING, DataPosFrac, increment,
324 DstData, DstSize
327 StoreSamples(DstSamples, ResampledData, converter->mNumChannels,
328 converter->mDstType, DstSize);
331 /* Update the number of prep samples still available, as well as the
332 * fractional offset.
334 DataPosFrac += increment*DstSize;
335 converter->mSrcPrepCount = mini(prepcount + toread - (DataPosFrac>>FRACTIONBITS),
336 MAX_RESAMPLE_PADDING*2);
337 converter->mFracOffset = DataPosFrac & FRACTIONMASK;
339 /* Update the src and dst pointers in case there's still more to do. */
340 *src = (const ALbyte*)*src + SrcFrameSize*(DataPosFrac>>FRACTIONBITS);
341 *srcframes -= mini(*srcframes, (DataPosFrac>>FRACTIONBITS));
343 dst = (ALbyte*)dst + DstFrameSize*DstSize;
344 pos += DstSize;
346 END_MIXER_MODE();
348 return pos;
352 ChannelConverter *CreateChannelConverter(enum DevFmtType srcType, enum DevFmtChannels srcChans, enum DevFmtChannels dstChans)
354 ChannelConverter *converter;
356 if(srcChans != dstChans && !((srcChans == DevFmtMono && dstChans == DevFmtStereo) ||
357 (srcChans == DevFmtStereo && dstChans == DevFmtMono)))
358 return NULL;
360 converter = al_calloc(DEF_ALIGN, sizeof(*converter));
361 converter->mSrcType = srcType;
362 converter->mSrcChans = srcChans;
363 converter->mDstChans = dstChans;
365 return converter;
368 void DestroyChannelConverter(ChannelConverter **converter)
370 if(converter)
372 al_free(*converter);
373 *converter = NULL;
378 #define DECL_TEMPLATE(T) \
379 static void Mono2Stereo##T(ALfloat *restrict dst, const T *src, ALsizei frames)\
381 ALsizei i; \
382 for(i = 0;i < frames;i++) \
383 dst[i*2 + 1] = dst[i*2 + 0] = Sample_##T(src[i]) * 0.707106781187f; \
386 static void Stereo2Mono##T(ALfloat *restrict dst, const T *src, ALsizei frames)\
388 ALsizei i; \
389 for(i = 0;i < frames;i++) \
390 dst[i] = (Sample_##T(src[i*2 + 0])+Sample_##T(src[i*2 + 1])) * \
391 0.707106781187f; \
394 DECL_TEMPLATE(ALbyte)
395 DECL_TEMPLATE(ALubyte)
396 DECL_TEMPLATE(ALshort)
397 DECL_TEMPLATE(ALushort)
398 DECL_TEMPLATE(ALint)
399 DECL_TEMPLATE(ALuint)
400 DECL_TEMPLATE(ALfloat)
402 #undef DECL_TEMPLATE
404 void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, ALsizei frames)
406 if(converter->mSrcChans == converter->mDstChans)
408 LoadSamples(dst, src, 1, converter->mSrcType,
409 frames*ChannelsFromDevFmt(converter->mSrcChans, 0));
410 return;
413 if(converter->mSrcChans == DevFmtStereo && converter->mDstChans == DevFmtMono)
415 switch(converter->mSrcType)
417 case DevFmtByte:
418 Stereo2MonoALbyte(dst, src, frames);
419 break;
420 case DevFmtUByte:
421 Stereo2MonoALubyte(dst, src, frames);
422 break;
423 case DevFmtShort:
424 Stereo2MonoALshort(dst, src, frames);
425 break;
426 case DevFmtUShort:
427 Stereo2MonoALushort(dst, src, frames);
428 break;
429 case DevFmtInt:
430 Stereo2MonoALint(dst, src, frames);
431 break;
432 case DevFmtUInt:
433 Stereo2MonoALuint(dst, src, frames);
434 break;
435 case DevFmtFloat:
436 Stereo2MonoALfloat(dst, src, frames);
437 break;
440 else /*if(converter->mSrcChans == DevFmtMono && converter->mDstChans == DevFmtStereo)*/
442 switch(converter->mSrcType)
444 case DevFmtByte:
445 Mono2StereoALbyte(dst, src, frames);
446 break;
447 case DevFmtUByte:
448 Mono2StereoALubyte(dst, src, frames);
449 break;
450 case DevFmtShort:
451 Mono2StereoALshort(dst, src, frames);
452 break;
453 case DevFmtUShort:
454 Mono2StereoALushort(dst, src, frames);
455 break;
456 case DevFmtInt:
457 Mono2StereoALint(dst, src, frames);
458 break;
459 case DevFmtUInt:
460 Mono2StereoALuint(dst, src, frames);
461 break;
462 case DevFmtFloat:
463 Mono2StereoALfloat(dst, src, frames);
464 break;