交换 pcm 音频中的字节序
我编写了一个简单的程序来交换 PCM 音频中的字节序(2 个通道,48kHz,24 位),但只有一个通道正确交换,第二个通道仍然是小字节序(我检查了 CoolEdit 2000 中生成的输出)。有人可以给我一些指导,我的代码有什么问题吗?
inline int endian_swap(unsigned int x)
{
unsigned char c1, c2, c3, c4;
c1 = x & 255;
c2 = (x >> 8) & 255;
c3 = (x >> 16) & 255;
c4 = (x >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
int main()
{
FILE *fpIn, *fpOut;
short x;
fpIn = fopen("audio.pcm", "rb");
fpOut = fopen("out.pcm", "wb");
int test = sizeof(short);
int count = 0;
int swaped = 0;
while( fread(&x, sizeof(int), 1, fpIn) == 1 )
{
swaped = endian_swap(x);
fwrite(&swaped, sizeof(int), 1, fpOut);
}
system("pause");
return 0;
}
此致!
I've made simply program to swap endian in PCM audio (2 channels, 48kHz, 24 bit), but only one channel is swapped correctly, second one is still little Endian (i've checked generated output in CoolEdit 2000). Could anybody give me some guidance what's wrong in my code?
inline int endian_swap(unsigned int x)
{
unsigned char c1, c2, c3, c4;
c1 = x & 255;
c2 = (x >> 8) & 255;
c3 = (x >> 16) & 255;
c4 = (x >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
int main()
{
FILE *fpIn, *fpOut;
short x;
fpIn = fopen("audio.pcm", "rb");
fpOut = fopen("out.pcm", "wb");
int test = sizeof(short);
int count = 0;
int swaped = 0;
while( fread(&x, sizeof(int), 1, fpIn) == 1 )
{
swaped = endian_swap(x);
fwrite(&swaped, sizeof(int), 1, fpOut);
}
system("pause");
return 0;
}
Best regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您一次在文件中读取一个
int
。但int
可能是 16 位或 32 位。你说你有24位音频。您应该修改代码以一次读取三个
char
到char [3]
数组中。然后,您也将修改swap_endian
函数以对char [3]
进行操作(这很简单;只需交换数组第一个和最后一个元素的内容即可!)You are reading in the file one
int
at a time. But anint
is probably either 16-bit or 32-bit. You say you have 24-bit audio.You should modify your code to read three
char
at a time, into achar [3]
array. You will then too modify yourswap_endian
function to operate on achar [3]
(this is easy; just swap the contents of the first and last elements of the array!)您声明了
short x
。尝试声明unsigned int x
。You declared
short x
. Try declaringunsigned int x
.