在pcm音频中交换端点

时间:2011-08-23 14:53:30

标签: c++ audio endianness pcm

我做了简单的程序来交换PCM音频中的endian(2个通道,48kHz,24位),但只有一个通道正确交换,第二个仍然是小Endian(我在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;
}

祝你好运!

2 个答案:

答案 0 :(得分:2)

您一次只能在文件int中阅读。但是int可能是16位或32位。你说你有24位音频。

您应该修改代码,一次读取三个charchar [3]数组。然后,您将修改swap_endian函数以对char [3]进行操作(这很简单;只需交换数组的第一个和最后一个元素的内容!)

答案 1 :(得分:1)

您宣布short x。尝试声明unsigned int x