IOS - Decode PCM byte array

我的未来我决定 提交于 2019-12-11 09:39:08

问题


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!



来源:https://stackoverflow.com/questions/11413781/ios-decode-pcm-byte-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!