forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_wav.cpp
272 lines (254 loc) · 5.96 KB
/
format_wav.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "format_wav.h"
#include "format_slinear.h"
#include "tools.h"
// sample rate 8000, 12000, 16000, 24000
int wav_write_header(FILE *f, int samplerate, int stereo)
{
unsigned int hz=htoll(samplerate);
unsigned int bhz = htoll(samplerate*2*(stereo ? 2 : 1)); // 2 bytes per sample and 2 channels
unsigned int hs = htoll(16); // 16bit
unsigned short fmt = htols(1);
unsigned short chans = htols((stereo ? 2 : 1));
unsigned short bysam = htols(2*(stereo ? 2 : 1));
unsigned short bisam = htols(16);
unsigned int size = htoll(0);
/* Write a wav header, ignoring sizes which will be filled in later */
fseek(f,0,SEEK_SET);
if (fwrite("RIFF", 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&size, 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&hs, 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&fmt, 1, 2, f) != 2) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&chans, 1, 2, f) != 2) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&hz, 1, 4, f) != 4) {
///log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&bhz, 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&bysam, 1, 2, f) != 2) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&bisam, 1, 2, f) != 2) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite("data", 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
if (fwrite(&size, 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to write header\n");
return -1;
}
return 0;
}
int wav_update_header(FILE *f)
{
off_t cur,end;
int datalen,filelen,bytes;
cur = ftello(f);
fseek(f, 0, SEEK_END);
end = ftello(f);
/* data starts 44 bytes in */
bytes = end - 44;
datalen = htoll(bytes);
/* chunk size is bytes of data plus 36 bytes of header */
filelen = htoll(36 + bytes);
if (cur < 0) {
//log(LOG_WARNING, "Unable to find our position\n");
return -1;
}
if (fseek(f, 4, SEEK_SET)) {
//log(LOG_WARNING, "Unable to set our position\n");
return -1;
}
if (fwrite(&filelen, 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to set write file size\n");
return -1;
}
if (fseek(f, 40, SEEK_SET)) {
//log(LOG_WARNING, "Unable to set our position\n");
return -1;
}
if (fwrite(&datalen, 1, 4, f) != 4) {
//log(LOG_WARNING, "Unable to set write datalen\n");
return -1;
}
if (fseeko(f, cur, SEEK_SET)) {
//log(LOG_WARNING, "Unable to return to position\n");
return -1;
}
return 0;
}
int wav_mix(char *in1, char *in2, char *out, int samplerate, int swap, int stereo) {
FILE *f_in1 = NULL;
FILE *f_in2 = NULL;
FILE *f_out = NULL;
char *bitstream_buf1 = NULL;
char *bitstream_buf2 = NULL;
char *p1;
char *f1;
char *p2;
char *f2;
short int zero = 0;
long file_size1;
long file_size2 = 0;
/* combine two wavs */
f_in1 = fopen(in1, "r");
if(!f_in1) {
syslog(LOG_ERR,"File [%s] cannot be opened for read.\n", in1);
return 1;
}
if(in2 != NULL) {
f_in2 = fopen(in2, "r");
if(!f_in2) {
fclose(f_in1);
syslog(LOG_ERR,"File [%s] cannot be opened for read.\n", in2);
return 1;
}
}
for(int passOpen = 0; passOpen < 2; passOpen++) {
if(passOpen == 1) {
char *pointToLastDirSeparator = strrchr(out, '/');
if(pointToLastDirSeparator) {
*pointToLastDirSeparator = 0;
mkdir_r(out, 0777);
*pointToLastDirSeparator = '/';
} else {
break;
}
}
f_out = fopen(out, "w");
if(f_out) {
break;
}
}
if(!f_out) {
if(f_in1 != NULL)
fclose(f_in1);
if(f_in2 != NULL)
fclose(f_in2);
syslog(LOG_ERR,"File [%s] cannot be opened for write.\n", out);
return 1;
}
char f_out_buffer[32768];
setvbuf(f_out, f_out_buffer, _IOFBF, 32768);
wav_write_header(f_out, samplerate, stereo);
fseek(f_in1, 0, SEEK_END);
file_size1 = ftell(f_in1);
fseek(f_in1, 0, SEEK_SET);
if(in2 != NULL) {
fseek(f_in2, 0, SEEK_END);
file_size2 = ftell(f_in2);
fseek(f_in2, 0, SEEK_SET);
}
bitstream_buf1 = (char *)malloc(file_size1);
if(!bitstream_buf1) {
if(f_in1 != NULL)
fclose(f_in1);
if(f_in2 != NULL)
fclose(f_in2);
if(f_out != NULL)
fclose(f_out);
syslog(LOG_ERR,"Cannot malloc bitsream_buf1[%ld]", file_size1);
return 1;
}
if(in2 != NULL) {
bitstream_buf2 = (char *)malloc(file_size2);
if(!bitstream_buf2) {
fclose(f_in1);
fclose(f_in2);
fclose(f_out);
free(bitstream_buf1);
syslog(LOG_ERR,"Cannot malloc bitsream_buf2[%ld]", file_size1);
return 1;
}
}
fread(bitstream_buf1, file_size1, 1, f_in1);
p1 = bitstream_buf1;
f1 = bitstream_buf1 + file_size1;
if(in2 != NULL) {
fread(bitstream_buf2, file_size2, 1, f_in2);
p2 = bitstream_buf2;
f2 = bitstream_buf2 + file_size2;
} else {
p2 = f2 = 0;
}
while(p1 < f1 || p2 < f2 ) {
if(p1 < f1 && p2 < f2) {
if(stereo) {
/* stereo */
if(swap) {
fwrite(p2, 2, 1, f_out);
fwrite(p1, 2, 1, f_out);
} else {
fwrite(p1, 2, 1, f_out);
fwrite(p2, 2, 1, f_out);
}
} else {
/* mono */
slinear_saturated_add((short int*)p1, (short int*)p2);
fwrite(p1, 2, 1, f_out);
}
p1 += 2;
p2 += 2;
} else if ( p1 < f1 ) {
if(swap) {
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
fwrite(p1, 2, 1, f_out);
} else {
fwrite(p1, 2, 1, f_out);
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
}
p1 += 2;
} else if ( p2 < f2 ) {
if(swap) {
fwrite(p2, 2, 1, f_out);
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
} else {
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
fwrite(p2, 2, 1, f_out);
}
p2 += 2;
}
}
wav_update_header(f_out);
if(bitstream_buf1)
free(bitstream_buf1);
if(bitstream_buf2)
free(bitstream_buf2);
fclose(f_out);
fclose(f_in1);
if(f_in2) fclose(f_in2);
return 0;
}