0

// 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 uint8_t buffer_header[HEADER_SIZE]; fread(buffer_header,sizeof(uint8_t),HEADER_SIZE,input); fwrite(buffer_header,sizeof(uint8_t),HEADER_SIZE,output); // writes the header to the output file // TODO: Read samples from input file and write updated data to output file 

fseek(input,0,SEEK_END); // moving the cursor in the file to the end to determine the total size of the file using ftell

int len= ftell(input);
int samplelen= len - HEADER_SIZE; // we find the size of the samples by subtracting the total size by the header size

fseek(input,0,SEEK_SET); fseek(input,HEADER_SIZE,SEEK_SET); // we move the cursor at the header size

int16_t buffer_samples[samplelen]; fread(buffer_samples,sizeof(int16_t),samplelen,input); // read the samples and store them in buffer for (int i=0;i<samplelen;i++) { buffer_samples[i]=buffer_samples[i]*factor; //iterate over the buffer array and multipy it by the factor } fseek(output,HEADER_SIZE,SEEK_SET); // set the cursor in the output file to the header size fwrite(buffer_samples,sizeof(int16_t),samplelen,output); // write the samples into the output file // Close files fclose(input); fclose(output); 

}

1 Answer 1

0

Remember, sizeof returns the size in bytes of the argument. int16_t is 2 bytes long.

This

fread(buffer_samples,sizeof(int16_t),samplelen,input); // read the samples and store them in buffer 

is reading 2 * samplelen bytes. Similarly, fwrite will write 2 * samplelen bytes.

Look at the size of the output file as with ls -al; it will be 2 * the size of the input file. It should be the same size.

Only read (and write) sampleln bytes. The loop will operate on each 2-byte sample because of buffer_samples declaration.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.