-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav64.c
117 lines (93 loc) · 2.88 KB
/
wav64.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
FILE* infile;
FILE* outfile;
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: wav64 infile outfile\n");
exit(-1);
}
printf("infile = %s\n", argv[1]);
printf("outfile = %s\n", argv[2]);
infile = fopen(argv[1], "rb");
char riff[5] = { 0 };
fread(riff, 4, 1, infile);
printf("%s\n", riff);
unsigned int filesize;
fread(&filesize, 4, 1, infile);
printf("filesize = 0x%08X (%d)\n", filesize, filesize);
char type[5] = { 0 };
fread(type, 4, 1, infile);
printf("%s\n", type);
char marker[5] = { 0 };
fread(marker, 4, 1, infile);
printf("%s\n", marker);
unsigned int formatlength;
fread(&formatlength, 4, 1, infile);
printf("formatlength = 0x%08X (%d)\n", formatlength, formatlength);
unsigned short formattype;
fread(&formattype, 2, 1, infile);
printf("formattype = 0x%04X (%d)\n", formattype, formattype);
unsigned short channels;
fread(&channels, 2, 1, infile);
printf("channels = 0x%04X (%d)\n", channels, channels);
unsigned int samplerate;
fread(&samplerate, 4, 1, infile);
printf("samplerate = 0x%08X (%d)\n", samplerate, samplerate);
unsigned int bytespersecond;
fread(&bytespersecond, 4, 1, infile);
printf("bytespersecond = 0x%08X (%d)\n", bytespersecond, bytespersecond);
// bytespersample = 1 : 8-bit mono
// bytespersample = 2 : 8-bit stereo/16-bit mono
// bytespersample = 4 : 16-bit stereo
unsigned short bytespersample;
fread(&bytespersample, 2, 1, infile);
printf("bytespersample = 0x%04X (%d)\n", bytespersample, bytespersample);
unsigned short bitspersample;
fread(&bitspersample, 2, 1, infile);
printf("bitspersample = 0x%04X (%d)\n", bitspersample, bitspersample);
char datamarker[5] = { 0 };
fread(datamarker, 4, 1, infile);
printf("%s\n", datamarker);
unsigned int datasize;
fread(&datasize, 4, 1, infile);
printf("datasize = 0x%08X (%d)\n", datasize, datasize);
outfile = fopen(argv[2], "wb");
unsigned short len = datasize / bytespersample / 4;
printf("outlength = %d\n", len);
fwrite(&len, 2, 1, outfile);
short sample;
int filter;
unsigned int fsize = 2;
for (int k = 0; k < datasize; k += bytespersample)
{
fread(&sample, 2, 1, infile);
filter = sample;
fread(&sample, 2, 1, infile);
filter += sample;
fread(&sample, 2, 1, infile);
filter += sample;
fread(&sample, 2, 1, infile);
filter += sample;
sample = filter / 4;
sample >>= 12; // convert sample to 4-bits :)
sample += 8;
sample &= 0x0F;
fputc(sample, outfile);
fsize++;
k += bytespersample; // skip in fours to quarter the sample rate from 44100Hz to 11025Hz
k += bytespersample;
k += bytespersample;
}
// pad out file so that it is exactly 0x2000 (8192 bytes) in size
while (fsize < 8192)
{
fputc(0, outfile);
fsize++;
}
fclose(outfile);
fclose(infile);
return 0;
}