Skip to content

Commit

Permalink
amended Int32_To_Int24_Dither following feedback; first attempt at im…
Browse files Browse the repository at this point in the history
…plementing PaUtil_Generate24BitTriangularDither
  • Loading branch information
whyismynamerudy committed May 11, 2023
1 parent 806d4b9 commit f1733cc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/common/pa_converters.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/common/pa_dither.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<<PA_DITHER_BITS_)-1))
static const float const_float_dither_scale_ = PA_FLOAT_DITHER_SCALE_;
Expand Down
11 changes: 11 additions & 0 deletions src/common/pa_dither.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *di
PaInt32 PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *ditherState );


/* REVEIW */
/**
@brief Calculate 3 LSB dither signal with a triangular distribution.
Ranged for adding to a 1 bit right-shifted 32 bit integer
prior to >>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.
Expand Down

0 comments on commit f1733cc

Please sign in to comment.