-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.c
105 lines (90 loc) · 2.67 KB
/
main.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
#include "stdio.h"
typedef unsigned char ID[4];
typedef struct
{
ID chunkID; /* {'f', 'm', 't', ' '} */
long chunkSize;
short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
/* Note: there may be additional fields here,
depending upon wFormatTag. */
} FormatChunk;
typedef struct
{
ID chunkID; /* {'d', 'a', 't', 'a'} */
long chunkSize;
unsigned char waveformData[];
} DataChunk;
void usage(char *command)
{
printf("usage:\n"
"\t%s pcmfile wavfile channel samplerate bitspersample\n", command);
}
int main(int argc, char *argv[])
{
FILE *pcmfile, *wavfile;
long pcmfile_size, chunk_size;
FormatChunk formatchunk;
DataChunk datachunk;
int i, read_len;
char buf[1024];
short tmp;
if (argc != 6) {
usage(argv[0]);
return 1;
}
pcmfile = fopen(argv[1], "rb");
if (pcmfile == NULL) {
printf("!Error: Can't open pcmfile.\n");
return 1;
}
fseek(pcmfile, 0, SEEK_END);
pcmfile_size = ftell(pcmfile);
fseek(pcmfile, 0, SEEK_SET);
wavfile = fopen(argv[2], "wb");
if (wavfile == NULL) {
printf("!Error: Can't create wavfile.\n");
return 1;
}
fwrite("RIFF", 1, 4, wavfile);
fwrite("xxxx", 1, 4, wavfile); //reserved for the total chunk size
fwrite("WAVE", 1, 4, wavfile);
formatchunk.chunkID[0] = 'f';
formatchunk.chunkID[1] = 'm';
formatchunk.chunkID[2] = 't';
formatchunk.chunkID[3] = ' ';
formatchunk.chunkSize = sizeof(FormatChunk) - sizeof(ID) - sizeof(long);
formatchunk.wFormatTag = 1; /* uncompressed */
formatchunk.wChannels = atoi(argv[3]);
formatchunk.dwSamplesPerSec = atoi(argv[4]);
formatchunk.wBitsPerSample = atoi(argv[5]);
formatchunk.wBlockAlign = formatchunk.wChannels * (formatchunk.wBitsPerSample >> 3);
formatchunk.dwAvgBytesPerSec = formatchunk.wBlockAlign * formatchunk.dwSamplesPerSec;
fwrite(&formatchunk, 1, sizeof(formatchunk), wavfile);
datachunk.chunkID[0] = 'd';
datachunk.chunkID[1] = 'a';
datachunk.chunkID[2] = 't';
datachunk.chunkID[3] = 'a';
datachunk.chunkSize = pcmfile_size;
fwrite(&datachunk, 1, sizeof(ID)+sizeof(long), wavfile);
while((read_len = fread(buf, 1, sizeof(buf), pcmfile)) != 0) {
/* revert the endiean */
#if 0
for (i=0; i<read_len; i+=2) {
tmp = buf[i];
buf[i] = buf[i+1];
buf[i+1] = tmp;
}
#endif
fwrite(buf, 1, read_len, wavfile);
}
fseek(wavfile, 4, SEEK_SET);
chunk_size = 4 + (8 + formatchunk.chunkSize) + (8 + datachunk.chunkSize);
fwrite(&chunk_size, 1, 4, wavfile);
fclose(pcmfile);
fclose(wavfile);
}