【发布时间】:2011-08-23 14:53:30
【问题描述】:
我已经编写了简单的程序来交换 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;
}
最好的问候!
【问题讨论】:
-
endian_swap()是如何实现的? -
对不起,我之前没有添加endian_swap()
-
结合了这两个错误(由 Oli Charlesworth 和 mtrw 提到),我很惊讶这两个频道看起来都不错。
标签: c++ audio endianness pcm