Skip to content

Instantly share code, notes, and snippets.

@moqdm
Created August 22, 2021 19:23
Show Gist options
  • Select an option

  • Save moqdm/4b0c1e8e9fc907e8f48a80c1c598526c to your computer and use it in GitHub Desktop.

Select an option

Save moqdm/4b0c1e8e9fc907e8f48a80c1c598526c to your computer and use it in GitHub Desktop.
it's my Lab 4: Volume solution... *Please just take a look if you couldn't solve it ... Thank you πŸ™‚ #cs50
// Modifies the volume of an audio file
#include <cs50.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.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 header[HEADER_SIZE];
fread(&header, sizeof(header), 1, input);
fwrite(&header, sizeof(header), 1, output);
// TODO: Read samples from input file and write updated data to output file
int16_t buffer;
while (fread(&buffer, sizeof(buffer), 1, input))
{
buffer *= factor;
fwrite(&buffer, sizeof(buffer), 1, output);
}
// Close files
fclose(input);
fclose(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment