From ee577ad13d0d38e84d67fdc9abf00612ba7aff0a Mon Sep 17 00:00:00 2001 From: kukemuna Date: Tue, 5 Mar 2024 10:30:27 +0200 Subject: [PATCH] automated commit by check50 [check50=True] --- volume.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 volume.c diff --git a/volume.c b/volume.c new file mode 100644 index 0000000..a3c75cd --- /dev/null +++ b/volume.c @@ -0,0 +1,55 @@ +// Modifies the volume of an audio file + +#include +#include +#include + +// 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 header[HEADER_SIZE]; + + fread(header, HEADER_SIZE, 1, input); + fwrite(header, HEADER_SIZE, 1, output); + + // TODO: Read samples from input file and write updated data to output file + int16_t *buffer = malloc(sizeof(int16_t)); + + while (fread(buffer, sizeof(int16_t), 1, input)) + { + *buffer *= factor; + fwrite(buffer, sizeof(int16_t), 1, output); + } + free(buffer); + + // Close files + fclose(input); + fclose(output); +}