我在我的 objective-c 应用程序上遇到了一个问题。
我正在从向我发送 PCM 编码声音的服务器 (Socket c#) 读取一个字节数组,我目前正在寻找一个示例代码来为我解码这个字节数组 (NSData) 并播放它。
有人知道解决办法吗?或者如何阅读 u-Law 音频?
非常感谢! :D
最佳答案
此链接包含有关 mu-law 编码和解码的信息:
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));
}
当您拥有未压缩的音频时,您应该能够使用 Audio Queue API 播放它。
祝你好运!
关于IOS - 解码 PCM 字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11413781/