Advertisement
jamestha3d

cs50 volume 2022

Oct 3rd, 2022
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. // Modifies the volume of an audio file
  2.  
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. // Number of bytes in .wav header
  8. const int HEADER_SIZE = 44;
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     // Check command-line arguments
  13.     if (argc != 4)
  14.     {
  15.         printf("Usage: ./volume input.wav output.wav factor\n");
  16.         return 1;
  17.     }
  18.  
  19.     // Open files and determine scaling factor
  20.     FILE *input = fopen(argv[1], "r");
  21.     if (input == NULL)
  22.     {
  23.         printf("Could not open file.\n");
  24.         return 1;
  25.     }
  26.  
  27.     FILE *output = fopen(argv[2], "w");
  28.     if (output == NULL)
  29.     {
  30.         printf("Could not open file.\n");
  31.         return 1;
  32.     }
  33.  
  34.     float factor = atof(argv[3]);
  35.  
  36.     uint8_t *header = malloc(sizeof(uint8_t) * HEADER_SIZE);
  37.     int16_t *body = malloc(sizeof(int16_t));
  38.  
  39.     fread(header, sizeof(uint8_t), HEADER_SIZE, input);
  40.     fwrite(header, sizeof(uint8_t), HEADER_SIZE, output);
  41.  
  42.     //int ptr = fread(body, sizeof(int16_t), 1, input);
  43.  
  44.     while (fread(body, sizeof(int16_t), 1, input))
  45.     {
  46.         *body = *body * factor;
  47.         fwrite(body, sizeof(int16_t), 1, output);
  48.     }
  49.  
  50.     // TODO: Copy header from input file to output file
  51.  
  52.     // TODO: Read samples from input file and write updated data to output file
  53.  
  54.     // Close files
  55.     fclose(input);
  56.     fclose(output);
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement