]> Repos - portaudio/commitdiff
implemented 'Int24_To_Int16_Dither' converter to allow 24-bit audio be played (before...
authordmitrykos <dmitrykos@0f58301d-fd10-0410-b4af-bbb618454e57>
Fri, 16 Apr 2010 19:08:00 +0000 (19:08 +0000)
committerdmitrykos <dmitrykos@0f58301d-fd10-0410-b4af-bbb618454e57>
Fri, 16 Apr 2010 19:08:00 +0000 (19:08 +0000)
src/common/pa_converters.c

index 75cb276fcf1195074c0fa03a5163046e48e17b6c..abbc080941452c675ea4b874b7c618f04653b090 100644 (file)
@@ -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(