[music-dsp] Bit depth conversions?

Jon Watte hplus at mindcontrol.org
Thu Apr 11 13:23:08 EDT 2002

Un-tested code that might work:

// This code will down-convert and dither a 16-bit signed short
// mono signal into an 8-bit unsigned char signal, using a first
// order forward-feeding error term dither.

#define uchar unsigned char

void dither_one_channel_16_to_8( short * input, uchar * output, int count,
int * memory )
  int m = *memory;
  while( count-- > 0 ) {
    int i = *input++;
    i += m;
    int j = i + 32768 - 128;
    uchar o;
    if( j < 0 ) {
      o = 0;
    else if( j > 65535 ) {
      o = 255;
    else {
      o = (uchar)((j>>8)&0xff);
    m = ((j-32768+128)-i);
    *output++ = o;
  *memory = m;

Note that you might not want to carry forward the full difference for
infinity. It's probably likely that the worst performance hit comes
from the saturation conditionals, which can be avoided with appropriate
instructions on many DSPs and integer SIMD type instructions, or CMOV.

Anyway, this should give you something to go on.

If all you want is that wonderful lo-tek crunchy sound, try this macro
on each sample:

short keep_bits_from_16( short input, int keepBits ) {
  return (input & (-1 << (16-keepBits)));

You may also wish to replicate every N:th sample N times for some of
that lo-fi aliasing goodness found in products like "The Amazing Singing
Barney Doll (feat. N-Sync and Britney Spears)"


				/ h+

> -----Original Message-----
> From: owner-music-dsp at shoko.calarts.edu
> [mailto:owner-music-dsp at shoko.calarts.edu]On Behalf Of Chris H
> Sent: Thursday, April 11, 2002 7:54 AM
> To: music-dsp at shoko.calarts.edu
> Subject: RE: [music-dsp] Bit depth conversions?
> Does anybody have any code examples for that?  I'm
> currently building a prototype degrader/bit crush
> plugin and I'd be interested in incorporating
> something like that

dupswapdrop -- the music-dsp mailing list and website: subscription info,
FAQ, source code archive, list archive, book reviews, dsp links

More information about the music-dsp mailing list