-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaud2arr.c
94 lines (88 loc) · 2.24 KB
/
aud2arr.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Parses a wav file and prints an array of 16-bit integers to the console
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int fd;
size_t dsize;
size_t fsize;
void parse_header(char* header) {
//RIFF
printf("%.*s\n", 4, header);
header+=4;
//File size
fsize = (size_t)*((int*)header);
printf("File size: %ld\n", fsize);
header+=4;
//WAVE
printf("%.*s\n", 4, header);
header+=4;
//fmt
printf("%.*s\n", 4, header);
header+=4;
//Length of format data
printf("Len of fmt: %d\n", *((int*)header));
header+=4;
//Type of format (1 is PCM) – 2 byte integer
printf("Type of format: %d\n", *((short*)header));
header+=2;
//Number of Channels
printf("Number of channels: %d\n", *((short*)header));
header+=2;
//Sample Rate
printf("Sample rate: %d\n", *((int*)header));
header+=10;
//Bits per sample
printf("Bits per sample: %d\n", *((short*)header));
header+=2;
//data
printf("%.*s\n", 4, header);
header+=4;
//File size
dsize = (size_t)*((unsigned int*)header);
printf("%ld\n", dsize);
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Error: No file specified.\n");
return EXIT_FAILURE;
}
int opt;
char compress = 0;
while ((opt = getopt(argc, argv, "c")) != -1) {
switch (opt) {
case 'c':
//compress the array to 8-bit samples
compress = 1;
break;
default:
fprintf(stderr, "Usage: %s [-c] [file...]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (compress) {
fd = open(argv[2], O_RDONLY);
} else {
fd = open(argv[1], O_RDONLY);
}
char* header = malloc(44);
read(fd, header, 44);
parse_header(header);
free(header);
short *arr = calloc(2, dsize);
read(fd, arr, dsize);
char * comma = "";
for (int i = 0; i < dsize/2; i++) {
short sample = (short)arr[i];
if (compress) {
sample = sample / 128;
}
printf("%s%d", comma, sample);
comma = ", ";
}
printf("\n");
free(arr);
close(fd);
return EXIT_SUCCESS;
}