I have looked at all the solutions here on the exchange. I have two issues. Firstly:
const int HEADER_SIZE = 44;
This does not work for me in the context of initializing a header array. I either have to use the number 44 or make use of malloc(HEADER_SIZE * sizeof(uint8_t))
If I don't, and use uint8_t header[HEADER_SIZE];
as per the solution video, I get an error at compile time "expression must have a constant value". Assuming this has to do with my specific compiler? I'm using MVSC Cl.exe.
Secondly, even after copying the solution from the video word for word, my code still does not reproduce the correct output file. It's always an output file of around 5kb and length 0.00 seconds. Something is obviously wrong. Can anyone look at the code and clue me in as to what might be going wrong? My only thought is that it has something to do with the compiler being for both c and c++. Thanks!
FULL CODE
// Modifies the volume of an audio file
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
// Number of bytes in .wav header
const int HEADER_SIZE = 44;
int main(int argc, char *argv[])
{
// Check command-line arguments
if (argc != 4)
{
printf("Usage: ./volume input.wav output.wav factor\n");
return 1;
}
// Open files and determine scaling factor
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open file.\n");
return 1;
}
FILE *output = fopen(argv[2], "w");
if (output == NULL)
{
printf("Could not open file.\n");
return 1;
}
float factor = atof(argv[3]);
// TODO: Copy header from input file to output file
// make room for header bytes
uint8_t header[44]; // HEADER_SIZE causes "error C2057: expected constant expression"
fread(header, 44, 1, input);
fwrite(header, 44, 1, output);
// TODO: Read samples from input file and write updated data to output file
int16_t buffer;
while(fread(&buffer, sizeof(int16_t), 1, input)) {
printf("%d\n", buffer);
buffer *= factor;
fwrite(&buffer, sizeof(int16_t), 1, output);
}
// Close files
fclose(input);
fclose(output);
}
The farthest I got was using malloc()
to allocate space for the header, writing to that space with fgetc()
and then looping back over the space and writing one character at a time with fputc()
. This at least got me an output file of size 44 bytes, which makes sense. Body code did nothing.