Decoding mu-law audio stream to PCM

Axis network video cameras (such Axis 214) provide an API to retrieve audio data over HTTP.
Different content-types are available. If the MIME type is “audio/basic”, the audio data is encoded using G.711 mu-law at 64kbits/s.
This compression transforms 2 bytes of PCM data to 1 byte.

Here is an easy-way to convert the encoded audio data to PCM (to convert to WAV for example) :

unsigned short mulaw_decode(unsigned char mulaw) { mulaw = ~mulaw;
  int sign = mulaw & 0x80;
  int exponent = (mulaw & 0x70) >> 4;
  int data = mulaw & 0x0f;
 data |= 0x10;
  data <<= 1;
  data += 1;
  data <<= exponent + 2;
  data -= 0x84;
  return (short)(sign == 0 ? data : -data);
}
This entry was posted in Programming and tagged , , . Bookmark the permalink.

2 Responses to Decoding mu-law audio stream to PCM

  1. Krmen Kolev says:

    That is great code.
    I need C\C++ code for the opposite conversion – from PCM to mu-law. The PCM audio is 8 bit x 8KHz. The resulting mu-law should be 8 bit x 8KHz, 64 Kbit sampling rate. Thank you.

  2. neyric says:

    Don’t have it… this is getting old :)
    sox can do it: http://sox.sourceforge.net/

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>