nonipal

Lab - 4.1

Sep 3rd, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 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.     // TODO: Copy header from input file to output file
  37.    
  38.     typedef uint8_t BYTE;
  39.    
  40.     BYTE bytes[44];
  41.    
  42.     fread(bytes, sizeof(BYTE), 44, input);
  43.    
  44.     fwrite(bytes, sizeof(BYTE), 44, output);
  45.    
  46.  
  47.     // TODO: Read samples from input file and write updated data to output file
  48.    
  49.     typedef int16_t KYTE;
  50.    
  51.     KYTE buffer;
  52.     while (fread(&buffer, sizeof(KYTE), 1, input))
  53.     {
  54.         fwrite(&buffer, sizeof(KYTE), 1, output)*factor;
  55.     }
  56.    
  57.     // Close files
  58.     fclose(input);
  59.     fclose(output);
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment