0

I'm just wondering if anyone could help me out with setting/clearing the LSB of an audio sample

The code below goes through an array of 24 elements and each element is added to the file with a new line that follows.

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

When I open the file everything has been written the way I want them to.

What I'm trying to do is, read the line and compare the value, if it's 0 clear the LSB of the audio sample, else set it to 1, code below:

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++;
}

The print outs aren't showing what I thought they would, instead this is what I get:

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

The .txt file has these values to start with so the print out should match them if I did the conditions correctly.

0
0
1
0
0
0
1

If anyone could point out where I am going wrong I would truly appreciate that, I'm just at a loss as to why the outputs aren't what I expected them.

Thanks

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Shouldn't those printouts just match the comments? I.e. instead of the strange side-effecty `printf("...\n", outputFrames[frame] &= ~1);`, just use `printf("...", 0);` (which, of course, doesn't need formatting at all). – unwind Mar 28 '17 at 10:31

1 Answers1

0

After a little more looking I found the issue with my code.

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

I even mentioned it in my original post that a new line follows each insertion.

However my, chars to compare don't take into account these new lines.

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

After changing them to the code below, the outputs are now correct.

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

@undwind Thank you for the suggestion, after looking at it, your way makes more sense.

Thank you.