0

I am taking a .wav file (input) and changing its volume by (value) and then outputing that into a new .wav file (output). The first 44 bytes of a .wav are the header and I am reading then writing to the new .wav file.

uint8_t header[HEADER_SIZE];
fread(header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(header, sizeof(uint8_t), HEADER_SIZE, output);

Then I am reading input, multiplying by (value), then writing to output.

int16_t buffer;
while (fread(&buffer, sizeof(int16_t), 1, input ))
{
    buffer = buffer * factor;
    fwrite(&buffer, sizeof(int16_t), 1, output);
}

Initially I tried to pass through (input + 44) and (output + 44) thinking I would point to the start out the file then go 44 more bytes. This didnt work so when I finally looked at the solution I changed my code to the above.

My question is how does this not (try to) multiply the header files by value? Or How does the program know to start multiplying after the header?

  • 2
    Files are streams of data. Each call to `fread()` continues from where the last one ended. – Barmar Feb 03 '22 at 19:00
  • @barmar Thanks! After reading your comment I was able to change my search terms and found a similar question that had an explaination. https://stackoverflow.com/questions/10696845/does-fread-move-the-file-pointer Do you want to make your comment into an answer so I can mark my question as answered? – Ben Schrock Feb 03 '22 at 19:08
  • FYI, you will gain a lot of efficiency by reading blocks of data at a time instead of a single value. Perhaps 256 at a time, or more. – SGeorgiades Feb 03 '22 at 19:10
  • @SGeorgiades If I read a block of data all at once (multiple samples in a .wav) how would I multiply those individual pieces by (value)? Maybe this is just outside the scope of what I have learned so far. – Ben Schrock Feb 03 '22 at 19:14
  • Aside: you haven't shown how you open the files, but on some systems it is worth explicitly opening them in binary mode, such as `"rb"` and `"wb"`. Also, if you are multiplying the data values by a factor, you should do it via `int32_t` and clip the value to the min and max that `int16_t` can hold. – Weather Vane Feb 03 '22 at 19:15
  • @Schrock Just use a simple loop: `for (int i = 0; i < BUF_SIZE; i++) { buffer[i] *= factor; }` – SGeorgiades Feb 03 '22 at 19:20

0 Answers0