From b122784103caaf55887949b0f9f67def8fee12a1 Mon Sep 17 00:00:00 2001 From: Rudy Date: Sun, 19 Mar 2023 00:58:34 -0400 Subject: [PATCH 1/6] Implemented function "Int32_To_Int24_Dither". --- src/common/pa_converters.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index 29bbf2b3f..c1be8b2b5 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -935,13 +935,20 @@ static void Int32_To_Int24_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 */ + + PaInt32 *src = (PaInt32*)sourceBuffer; + signed char *dest = (signed char*)destinationBuffer; + PaInt32 dither; + + while ( count-- ) + { + dither = PaUtil_Generate16BitTriangularDither(ditherGenerator); + *dest = (signed char) ((((*src) >> 1) + dither) >> 7); + + src += sourceStride; + dest += destinationStride; + } } /* -------------------------------------------------------------------------- */ From 806d4b90ac82082efed5c24650d003d004930957 Mon Sep 17 00:00:00 2001 From: Rudy Date: Sun, 19 Mar 2023 22:51:32 -0400 Subject: [PATCH 2/6] added review comment --- src/common/pa_converters.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index c1be8b2b5..119e66d3f 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -943,6 +943,7 @@ static void Int32_To_Int24_Dither( while ( count-- ) { + /* REVIEW */ dither = PaUtil_Generate16BitTriangularDither(ditherGenerator); *dest = (signed char) ((((*src) >> 1) + dither) >> 7); From f1733cc39041337122507002a701af5e94631354 Mon Sep 17 00:00:00 2001 From: Rudy Date: Thu, 11 May 2023 02:27:54 -0400 Subject: [PATCH 3/6] amended Int32_To_Int24_Dither following feedback; first attempt at implementing PaUtil_Generate24BitTriangularDither --- src/common/pa_converters.c | 16 +++++++++++++--- src/common/pa_dither.c | 25 +++++++++++++++++++++++++ src/common/pa_dither.h | 11 +++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index 119e66d3f..90142486d 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -939,16 +939,26 @@ static void Int32_To_Int24_Dither( PaInt32 *src = (PaInt32*)sourceBuffer; signed char *dest = (signed char*)destinationBuffer; + signed char *scaledDitherResult = (signed char*)destinationBuffer; PaInt32 dither; while ( count-- ) { /* REVIEW */ dither = PaUtil_Generate16BitTriangularDither(ditherGenerator); - *dest = (signed char) ((((*src) >> 1) + dither) >> 7); - + *scaledDitherResult = (signed char) ((((*src) >> 1) + (dither >> 8)) >> 7); + +#if defined(PA_LITTLE_ENDIAN) + dest[0] = (unsigned char)(*scaledDitherResult >> 8); + dest[1] = (unsigned char)(*scaledDitherResult >> 16); + dest[2] = (unsigned char)(*scaledDitherResult >> 24); +#elif defined(PA_BIG_ENDIAN) + dest[0] = (unsigned char)(*scaledDitherResult >> 24); + dest[1] = (unsigned char)(*scaledDitherResult >> 16); + dest[2] = (unsigned char)(*scaledDitherResult >> 8); +#endif src += sourceStride; - dest += destinationStride; + dest += destinationStride * 3; } } diff --git a/src/common/pa_dither.c b/src/common/pa_dither.c index 0d1666a77..19b97b8e6 100644 --- a/src/common/pa_dither.c +++ b/src/common/pa_dither.c @@ -86,6 +86,31 @@ PaInt32 PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *s } +PaInt32 PaUtil_Generate24BitTriangularDither(PaUtilTriangularDitherGenerator* state) +{ + PaInt32 current, highPass; + + /* Generate two random numbers. */ + state->randSeed1 = (state->randSeed1 * 196314165) + 907633515; + state->randSeed2 = (state->randSeed2 * 196314165) + 907633515; + + /* Generate triangular distribution about 0. + * Shift before adding to prevent overflow which would skew the distribution. + * Also shift an extra bit for the high pass filter. + */ +#define PA_DITHER_BITS_24_BITS (23) +#define DITHER_SHIFT_24_BITS_ ((sizeof(PaInt32)*8 - PA_DITHER_BITS_24_BITS) + 1) + + current = (((PaInt32)state->randSeed1) >> DITHER_SHIFT_24_BITS_) + + (((PaInt32)state->randSeed2) >> DITHER_SHIFT_24_BITS_); + + /* High pass filter to reduce audibility. */ + highPass = current - state->previous; + state->previous = current; + return highPass; +} + + /* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */ #define PA_FLOAT_DITHER_SCALE_ (1.0f / ((1<>7. + @return + A signed 32-bit integer with a range of +32767 to -32768 +*/ +PaInt32 PaUtil_Generate24BitTriangularDither(PaUtilTriangularDitherGenerator* ditherState); + + /** @brief Calculate 2 LSB dither signal with a triangular distribution. Ranged for adding to a pre-scaled float. From dea6d9860d19e9ba1caa5a8a2435856529288b18 Mon Sep 17 00:00:00 2001 From: Rudy Date: Thu, 11 May 2023 14:26:53 -0400 Subject: [PATCH 4/6] removed dither scaling --- src/common/pa_converters.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index 90142486d..f06d356b3 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -946,7 +946,7 @@ static void Int32_To_Int24_Dither( { /* REVIEW */ dither = PaUtil_Generate16BitTriangularDither(ditherGenerator); - *scaledDitherResult = (signed char) ((((*src) >> 1) + (dither >> 8)) >> 7); + *scaledDitherResult = (signed char) ((((*src) >> 1) + dither) >> 7); #if defined(PA_LITTLE_ENDIAN) dest[0] = (unsigned char)(*scaledDitherResult >> 8); From 8dc7928522934bcbbf48376fc8d1f33840819857 Mon Sep 17 00:00:00 2001 From: Rudy Date: Sat, 13 May 2023 15:20:44 -0400 Subject: [PATCH 5/6] removed PaUtil_Generate24BitTriangularDither; implemented Phil's feedback on Int32_To_Int24_Dither --- src/common/pa_converters.c | 18 +++++++++--------- src/common/pa_dither.c | 25 ------------------------- src/common/pa_dither.h | 11 ----------- 3 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index f06d356b3..c5f775a16 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -938,24 +938,24 @@ static void Int32_To_Int24_Dither( (void) ditherGenerator; /* unused parameters */ PaInt32 *src = (PaInt32*)sourceBuffer; - signed char *dest = (signed char*)destinationBuffer; - signed char *scaledDitherResult = (signed char*)destinationBuffer; + unsigned char *dest = (unsigned char*)destinationBuffer; + PaInt32 *scaledDitherResult = (PaInt32*)destinationBuffer; PaInt32 dither; while ( count-- ) { /* REVIEW */ dither = PaUtil_Generate16BitTriangularDither(ditherGenerator); - *scaledDitherResult = (signed char) ((((*src) >> 1) + dither) >> 7); + *scaledDitherResult = (PaInt32) ((((*src) >> 1) + dither) >> 7); #if defined(PA_LITTLE_ENDIAN) - dest[0] = (unsigned char)(*scaledDitherResult >> 8); - dest[1] = (unsigned char)(*scaledDitherResult >> 16); - dest[2] = (unsigned char)(*scaledDitherResult >> 24); + dest[0] = (unsigned char)(*scaledDitherResult); + dest[1] = (unsigned char)(*scaledDitherResult >> 8); + dest[2] = (unsigned char)(*scaledDitherResult >> 16); #elif defined(PA_BIG_ENDIAN) - dest[0] = (unsigned char)(*scaledDitherResult >> 24); - dest[1] = (unsigned char)(*scaledDitherResult >> 16); - dest[2] = (unsigned char)(*scaledDitherResult >> 8); + dest[0] = (unsigned char)(*scaledDitherResult >> 16); + dest[1] = (unsigned char)(*scaledDitherResult >> 8); + dest[2] = (unsigned char)(*scaledDitherResult); #endif src += sourceStride; dest += destinationStride * 3; diff --git a/src/common/pa_dither.c b/src/common/pa_dither.c index 19b97b8e6..0d1666a77 100644 --- a/src/common/pa_dither.c +++ b/src/common/pa_dither.c @@ -86,31 +86,6 @@ PaInt32 PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *s } -PaInt32 PaUtil_Generate24BitTriangularDither(PaUtilTriangularDitherGenerator* state) -{ - PaInt32 current, highPass; - - /* Generate two random numbers. */ - state->randSeed1 = (state->randSeed1 * 196314165) + 907633515; - state->randSeed2 = (state->randSeed2 * 196314165) + 907633515; - - /* Generate triangular distribution about 0. - * Shift before adding to prevent overflow which would skew the distribution. - * Also shift an extra bit for the high pass filter. - */ -#define PA_DITHER_BITS_24_BITS (23) -#define DITHER_SHIFT_24_BITS_ ((sizeof(PaInt32)*8 - PA_DITHER_BITS_24_BITS) + 1) - - current = (((PaInt32)state->randSeed1) >> DITHER_SHIFT_24_BITS_) + - (((PaInt32)state->randSeed2) >> DITHER_SHIFT_24_BITS_); - - /* High pass filter to reduce audibility. */ - highPass = current - state->previous; - state->previous = current; - return highPass; -} - - /* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */ #define PA_FLOAT_DITHER_SCALE_ (1.0f / ((1<>7. - @return - A signed 32-bit integer with a range of +32767 to -32768 -*/ -PaInt32 PaUtil_Generate24BitTriangularDither(PaUtilTriangularDitherGenerator* ditherState); - - /** @brief Calculate 2 LSB dither signal with a triangular distribution. Ranged for adding to a pre-scaled float. From 76c10e3d5ab399ecf742f9ac952fd322a1c2ae9a Mon Sep 17 00:00:00 2001 From: Rudy Date: Sat, 13 May 2023 15:42:30 -0400 Subject: [PATCH 6/6] removed unnecessary "unused parameter" from Int32_To_Int24_Dither; initial implementation of Int32_To_UInt8_Dither --- src/common/pa_converters.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/common/pa_converters.c b/src/common/pa_converters.c index c5f775a16..ae5003e38 100644 --- a/src/common/pa_converters.c +++ b/src/common/pa_converters.c @@ -935,7 +935,6 @@ static void Int32_To_Int24_Dither( void *sourceBuffer, signed int sourceStride, unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator ) { - (void) ditherGenerator; /* unused parameters */ PaInt32 *src = (PaInt32*)sourceBuffer; unsigned char *dest = (unsigned char*)destinationBuffer; @@ -1073,16 +1072,19 @@ static void Int32_To_UInt8_Dither( void *sourceBuffer, signed int sourceStride, unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator ) { - /* PaInt32 *src = (PaInt32*)sourceBuffer; - unsigned char *dest = (unsigned char*)destinationBuffer; */ - (void)ditherGenerator; /* unused parameter */ + PaInt32 *src = (PaInt32*)sourceBuffer; + unsigned char *dest = (unsigned char*)destinationBuffer; + PaInt32 dither; while( count-- ) { - /* IMPLEMENT ME */ + /* REVIEW */ + dither = PaUtil_Generate16BitTriangularDither( ditherGenerator ); - /* src += sourceStride; - dest += destinationStride; */ + *dest = (unsigned char)((((*src) >> 1) + dither) >> 23); + + src += sourceStride; + dest += destinationStride; } }