IOS - Decode PCM byte array

2019-07-30 17:15发布

问题:

Im stuck on an issue on my objective C App.

I'm reading a byte array from a serveur (Socket c#) who send me an PCM encoded sound, and i'm currently looking for a sample code that decode for me this byte array (NSData), and play it.

Does anyone know a solution ? Or how can I read a u-Law audio?

Thanks a lot ! :D

回答1:

This link has information about mu-law encoding and decoding:

http://dystopiancode.blogspot.com.es/2012/02/pcm-law-and-u-law-companding-algorithms.html

#define MULAW_BIAS 33
/*
 * Description:
 *  Decodes an 8-bit unsigned integer using the mu-Law.
 * Parameters:
 *  number - the number who will be decoded
 * Returns:
 *  The decoded number
 */
int16_t MuLaw_Decode(int8_t number)
{
 uint8_t sign = 0, position = 0;
 int16_t decoded = 0;
 number=~number;
 if(number&0x80)
 {
  number&=~(1<<7);
  sign = -1;
 }
 position = ((number & 0xF0) >>4) + 5;
 decoded = ((1<<position)|((number&0x0F)<<(position-4))|(1<<(position-5)))
            - MULAW_BIAS;
 return (sign==0)?(decoded):(-(decoded));
}

When you have the uncompressed audio you should be able to play it using the Audio Queue API.

Good luck!