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);
}

Tags : , ,
Categories : Software, C

Comments

comments powered by Disqus
Copyright © 2000-2022 - Eric Abouaf