c - 设置/清除音频样本的 LSB

标签 c audio fgets lsb

我只是想知道是否有人可以帮助我设置/清除音频样本的 LSB

下面的代码遍历一个包含 24 个元素的数组,每个元素都添加到文件中,并在后面添加一个新行。

FILE *fp;
fp = fopen(EmbedFile, "w");
    for (int i = 0; i < 24; i++){
        fprintf(fp, "%d\n", Bits[i]);
    }
   fclose(fp);

当我打开文件时,所有内容都已按照我希望的方式写入。

我想做的是,读取该行并比较该值,如果它是 0,则清除音频样本的 LSB,否则将其设置为 1,代码如下:

FILE *embedfile = fopen(EmbedFile, "r");
    int line = 0;
    char input[12];
    char *zero = "0";
    char *one = "1";

while (fgets(input, 12, embedfile))
{
    //duplicates the key sample prior to lsb modification
    outputFrames[frame] = inputFrames[frame];

   //sets the lsb of the audio sample to match the current line being read from the text file.
    if (strcmp(input, zero) == 0)
    {
        //clear the LSB
        outputFrames[frame] &= ~1;
        printf("%u bit inserted\n", outputFrames[frame] &= ~1);
    }

    else
    {
        //set the LSB
        outputFrames[frame] |= 1;
        printf("%u bit inserted\n", outputFrames[frame] |= 1);

    }
    //next frame
    frame++;
}

打印输出没有显示我想象的内容,相反,这是我得到的:

1 bit inserted
1 bit inserted
4294967295 bit inserted
4294967295 bit inserted
1 bit inserted
3 bit inserted
1 bit inserted

.txt 文件具有这些值作为开头,因此如果我正确执行了条件,打印输出应该与它们匹配。

0
0
1
0
0
0
1

如果有人能指出我哪里出错了,我真的很感激,我只是不知道为什么输出不是我所期望的。

谢谢

最佳答案

经过一番查看,我发现了我的代码的问题。

FILE *fp;
fp = fopen(EmbedFile, "w");
    for (int i = 0; i < 24; i++){
        fprintf(fp, "%d\n", Bits[i]);
    }
   fclose(fp);

我什至在我原来的帖子中提到,每次插入后都会有一个新行。

但是,要比较的字符没有考虑这些新行。

char *zero = "0";
char *one = "1";

将它们更改为下面的代码后,输出现在是正确的。

char *zero = "0\n";
char *one = "1\n";

@undwind 谢谢你的建议,看了之后,你的方法更有意义了。

谢谢。

关于c - 设置/清除音频样本的 LSB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43067095/

相关文章:

c - Makefile:扩展依赖项

android - Android 麦克风到扬声器直接流媒体应用程序的音频延迟

android - 如何在libPd中播放WAV文件?

c - 如何在换行符 "\t"之前将字符 "\n"添加到字符串中

c - C 语言中 fgets 不返回 NULL

c - 在没有任何外部库或头文件(如 math.h)的情况下查找数字的 n 次方根的程序

c - sendto 在发送时没有给出错误

c - 在 glib 的 "g_print()"中强制使用 UTF-8 编码

javascript - 禁用背景音乐控制

c - fgets 输出奇怪的垃圾字符