交换 pcm 音频中的字节序

发布于 2024-12-01 03:44:22 字数 793 浏览 4 评论 0原文

我编写了一个简单的程序来交换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-12-08 03:44:22

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

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

You are reading in the file one int at a time. But an int 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 a char [3] array. You will then too modify your swap_endian function to operate on a char [3] (this is easy; just swap the contents of the first and last elements of the array!)

ㄖ落Θ余辉 2024-12-08 03:44:22

您声明了 short x。尝试声明unsigned int x

You declared short x. Try declaring unsigned int x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文