From: dmitrykos Date: Fri, 16 Apr 2010 19:08:00 +0000 (+0000) Subject: implemented 'Int24_To_Int16_Dither' converter to allow 24-bit audio be played (before... X-Git-Tag: pa_stable_v19_20110326_r1647~135 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=bd2bfe985e302e7a320699abc8ef934f8c0bb6c0;p=portaudio implemented 'Int24_To_Int16_Dither' converter to allow 24-bit audio be played (before was silence for WMME and DirectSound) --- diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index 75cb276..abbc080 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -1163,15 +1163,39 @@ static void Int24_To_Int16_Dither( void *sourceBuffer, signed int sourceStride, unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator ) { - (void) destinationBuffer; /* unused parameters */ - (void) destinationStride; /* unused parameters */ - (void) sourceBuffer; /* unused parameters */ - (void) sourceStride; /* unused parameters */ - (void) count; /* unused parameters */ - (void) ditherGenerator; /* unused parameters */ - /* IMPLEMENT ME */ + #define _PA_CNV_RESCALE(__max_from,__max_to,g) ((1.0f/(float)(__max_from/2+1))*((float)((__max_to/2+1)+g))) +#ifndef PA_BIG_ENDIAN + #define _PA_INT24_TO_INT32(v) ((int)(((int)v[2] << 24)|((int)v[1] << 16)|((int)v[0] << 8)) >> 8) +#else + #define _PA_INT24_TO_INT32(v) ((int)(((int)v[0] << 24)|((int)v[1] << 16)|((int)v[2] << 8)) >> 8) +#endif + #define _PA_INT24_TO_FLOAT(v,g) ((float)(_PA_INT24_TO_INT32(v)) * _PA_CNV_RESCALE(0xffffff,0xffff,g)) + + unsigned char *src = (unsigned char *)sourceBuffer; + PaInt16 *dest = (PaInt16 *)destinationBuffer; + float dither, dithered; + + while( count-- ) + { + dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator ); + + /* upscale 24-bit int to 16-bit float, decrease scaler by 2 to leave space for dither + in order to not overflow + */ + dithered = _PA_INT24_TO_FLOAT(src, -2.0f) + dither; + +#ifdef PA_USE_C99_LRINTF + *dest = lrintf(dithered-0.5f); +#else + *dest = (PaInt16) dithered; +#endif + + src += sourceStride * 3; + dest += destinationStride; + } } + /* -------------------------------------------------------------------------- */ static void Int24_To_Int8(