My code is just copying the header but doesn't change the volume at the second loop.
// TODO: Copy header from the input file to the output file
uint8_t header[HEADER_SIZE];
uint8_t* header_p=malloc(HEADER_SIZE * sizeof(uint8_t));
while(fread(&header_p,sizeof(header[HEADER_SIZE]),1,input))
{
fwrite(&header_p,sizeof(header[HEADER_SIZE]),1,output);
}
// TODO: Read samples from the input file and write updated data to the output file
int16_t buffer;
while(fread(&buffer,sizeof(buffer),1,input))
{
buffer =(int16_t)(buffer*factor);
fwrite(&buffer,sizeof(buffer),1,output);
}
If a particular WAV file consists only of a header followed by 16-bit signed values representing the sound data, your error would be in the first while loop. This loop continues to copy sizeof(header[HEADER_SIZE])
(which is actually the size of one element in header which is 1) chunks from input to output until the end of the file has reached, the second while loop will not be entered.
Just copy the header once, and use a more appropriate scaling algorithm to fix some other issues. For example:
uint8_t header[HEADER_SIZE];
/* There is only one header, so parse it just once */
if (fread(&header[0], sizeof(header), 1, input)) {
fwrite(&header[0], sizeof(header), 1, output);
}
/* Assuming 16-bit signed integer PCM payload */
int16_t sample;
while(fread(&sample, sizeof(sample), 1, input)) {
/* Round value to nearest number */
double scaledValue = floor((sample * factor) + 0.5);
/* Clamp new value */
if (scaledValue >= INT16_MAX) {
sample = INT16_MAX;
} else if (scaledValue <= INT16_MIN) {
sample = INT16_MIN;
} else {
sample = (int16_t)scaledValue;
}
fwrite(&sample, sizeof(sample), 1, output);
}
However the .wav file format is not as simple as that and has multiple variations, 16-bit signed PCM values is just one of them. Also the size and the format of the sample data is given in header, you will need to parse the header carefully and act accordingly. For more information on the .wav file format, I guess these are a good starting point: