-
Notifications
You must be signed in to change notification settings - Fork 45
/
volume.c
50 lines (39 loc) · 1.17 KB
/
volume.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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[])
{
FILE *input, *output;
uint8_t header[HEADER_SIZE];
int16_t buffer;
// Check command-line arguments
if (argc != 4) {
printf("Usage: ./volume input.wav output.wav factor\n");
return 1;
}
// Open files and determine scaling factor
if ((input = fopen(argv[1], "r")) == NULL) {
printf("Could not open file.\n");
return 1;
}
if ((output = fopen(argv[2], "w")) == NULL) {
printf("Could not open file.\n");
return 1;
}
float factor = atof(argv[3]);
// Copy header from input file to output file
fread(header, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(header, sizeof(uint8_t), HEADER_SIZE, output);
// Read samples from input file and write updated data to output file
while (fread(&buffer, sizeof(int16_t), 1, input)) {
buffer *= factor;
fwrite(&buffer, sizeof(int16_t), 1, output);
}
// Close files
fclose(input);
fclose(output);
return 0;
}