-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_wav.cpp
249 lines (234 loc) · 5.63 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
#include <sys/stat.h>
#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_in[2] = { NULL, NULL };
FILE *f_out = NULL;
/* combine two wavs */
f_in[0] = fopen(in1, "r");
if(!f_in[0]) {
syslog(LOG_ERR,"File [%s] cannot be opened for read.\n", in1);
return 1;
}
if(in2 != NULL) {
f_in[1] = fopen(in2, "r");
if(!f_in[1]) {
fclose(f_in[0]);
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;
spooldir_mkdir(out);
*pointToLastDirSeparator = '/';
} else {
break;
}
}
f_out = fopen(out, "w");
if(f_out) {
spooldir_file_chmod_own(f_out);
break;
}
}
if(!f_out) {
if(f_in[0] != NULL)
fclose(f_in[0]);
if(f_in[1] != NULL)
fclose(f_in[1]);
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);
unsigned buff_length = 1024 * 1024;
char *buff[2] = { NULL, NULL };
unsigned read_length[2] = { 0, 0 };
unsigned buff_pos[2] = { 0, 0 };
char *p[2] = { NULL, NULL };
for (unsigned i = 0; i < 2; i++) {
if (f_in[i]) {
buff[i] = new FILE_LINE(0) char[buff_length];
read_length[i] = fread(buff[i], 1, buff_length, f_in[i]);
if (read_length[i]) {
p[i] = buff[i];
}
}
}
short int zero = 0;
while (p[0] || p[1]) {
if (p[0] && p[1]) {
if(stereo) {
/* stereo */
if(swap) {
fwrite(p[1], 2, 1, f_out);
fwrite(p[0], 2, 1, f_out);
} else {
fwrite(p[0], 2, 1, f_out);
fwrite(p[1], 2, 1, f_out);
}
} else {
/* mono */
slinear_saturated_add((short int*)p[0], (short int*)p[1]);
fwrite(p[0], 2, 1, f_out);
}
buff_pos[0] += 2;
buff_pos[1] += 2;
} else if (p[0]) {
if(swap) {
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
fwrite(p[0], 2, 1, f_out);
} else {
fwrite(p[0], 2, 1, f_out);
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
}
buff_pos[0] += 2;
} else if (p[1]) {
if(swap) {
fwrite(p[1], 2, 1, f_out);
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
} else {
if(stereo) {
fwrite(&zero, 2, 1, f_out);
}
fwrite(p[1], 2, 1, f_out);
}
buff_pos[1] += 2;
}
for (unsigned i = 0; i < 2; i++) {
if (read_length[i] > 0 && buff_pos[i] >= read_length[i]) {
read_length[i] = fread(buff[i], 1, buff_length, f_in[i]);
buff_pos[i] = 0;
}
if (read_length[i] > 0) {
p[i] = buff[i] + buff_pos[i];
} else {
p[i] = NULL;
}
}
}
wav_update_header(f_out);
fclose(f_out);
for(unsigned i = 0; i < 2; i++) {
if(f_in[i]) {
fclose(f_in[i]);
}
if(buff[i]) {
delete [] buff[i];
}
}
return 0;
}