1 | /* |
---|
2 | Copyright (C) 2014 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
6 | aubio is free software: you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation, either version 3 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | aubio is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #ifdef HAVE_WAVREAD |
---|
24 | |
---|
25 | #include "aubio_priv.h" |
---|
26 | #include "fvec.h" |
---|
27 | #include "fmat.h" |
---|
28 | #include "source_wavread.h" |
---|
29 | |
---|
30 | #include <errno.h> |
---|
31 | |
---|
32 | #define AUBIO_WAVREAD_BUFSIZE 1024 |
---|
33 | |
---|
34 | #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05) |
---|
35 | |
---|
36 | struct _aubio_source_wavread_t { |
---|
37 | uint_t hop_size; |
---|
38 | uint_t samplerate; |
---|
39 | uint_t channels; |
---|
40 | |
---|
41 | // some data about the file |
---|
42 | char_t *path; |
---|
43 | uint_t input_samplerate; |
---|
44 | uint_t input_channels; |
---|
45 | |
---|
46 | // internal stuff |
---|
47 | FILE *fid; |
---|
48 | |
---|
49 | uint_t read_samples; |
---|
50 | uint_t blockalign; |
---|
51 | uint_t bitspersample; |
---|
52 | uint_t read_index; |
---|
53 | uint_t eof; |
---|
54 | |
---|
55 | uint_t duration; |
---|
56 | |
---|
57 | size_t seek_start; |
---|
58 | |
---|
59 | unsigned char *short_output; |
---|
60 | fmat_t *output; |
---|
61 | }; |
---|
62 | |
---|
63 | unsigned int read_little_endian (unsigned char *buf, unsigned int length); |
---|
64 | unsigned int read_little_endian (unsigned char *buf, unsigned int length) { |
---|
65 | uint_t i, ret = 0; |
---|
66 | for (i = 0; i < length; i++) { |
---|
67 | ret += buf[i] << (i * 8); |
---|
68 | } |
---|
69 | return ret; |
---|
70 | } |
---|
71 | |
---|
72 | aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
73 | aubio_source_wavread_t * s = AUBIO_NEW(aubio_source_wavread_t); |
---|
74 | size_t bytes_read = 0, bytes_junk = 0, bytes_expected = 44; |
---|
75 | unsigned char buf[5]; |
---|
76 | unsigned int format, channels, sr, byterate, blockalign, duration, bitspersample;//, data_size; |
---|
77 | |
---|
78 | if (path == NULL) { |
---|
79 | AUBIO_ERR("source_wavread: Aborted opening null path\n"); |
---|
80 | goto beach; |
---|
81 | } |
---|
82 | if ((sint_t)samplerate < 0) { |
---|
83 | AUBIO_ERR("source_wavread: Can not open %s with samplerate %d\n", path, samplerate); |
---|
84 | goto beach; |
---|
85 | } |
---|
86 | if ((sint_t)hop_size <= 0) { |
---|
87 | AUBIO_ERR("source_wavread: Can not open %s with hop_size %d\n", path, hop_size); |
---|
88 | goto beach; |
---|
89 | } |
---|
90 | |
---|
91 | if (s->path) AUBIO_FREE(s->path); |
---|
92 | s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1); |
---|
93 | strncpy(s->path, path, strnlen(path, PATH_MAX) + 1); |
---|
94 | |
---|
95 | s->samplerate = samplerate; |
---|
96 | s->hop_size = hop_size; |
---|
97 | |
---|
98 | s->fid = fopen((const char *)path, "rb"); |
---|
99 | if (!s->fid) { |
---|
100 | AUBIO_ERR("source_wavread: Failed opening %s (System error: %s)\n", s->path, strerror(errno)); |
---|
101 | goto beach; |
---|
102 | } |
---|
103 | |
---|
104 | // ChunkID |
---|
105 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
106 | buf[4] = '\0'; |
---|
107 | if ( strcmp((const char *)buf, "RIFF") != 0 ) { |
---|
108 | AUBIO_ERR("source_wavread: could not find RIFF header in %s\n", s->path); |
---|
109 | goto beach; |
---|
110 | } |
---|
111 | |
---|
112 | // ChunkSize |
---|
113 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
114 | |
---|
115 | // Format |
---|
116 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
117 | buf[4] = '\0'; |
---|
118 | if ( strcmp((const char *)buf, "WAVE") != 0 ) { |
---|
119 | AUBIO_ERR("source_wavread: wrong format in RIFF header in %s\n", s->path); |
---|
120 | goto beach; |
---|
121 | } |
---|
122 | |
---|
123 | // Subchunk1ID |
---|
124 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
125 | buf[4] = '\0'; |
---|
126 | |
---|
127 | // check if we have a JUNK Chunk |
---|
128 | if ( strcmp((const char *)buf, "JUNK") == 0 ) { |
---|
129 | bytes_junk = fread(buf, 1, 4, s->fid); |
---|
130 | buf[4] = '\0'; |
---|
131 | bytes_junk += read_little_endian(buf, 4); |
---|
132 | if (fseek(s->fid, bytes_read + bytes_junk, SEEK_SET) != 0) { |
---|
133 | AUBIO_ERR("source_wavread: could not seek past JUNK Chunk in %s (%s)\n", |
---|
134 | s->path, strerror(errno)); |
---|
135 | goto beach; |
---|
136 | } |
---|
137 | bytes_read += bytes_junk; |
---|
138 | bytes_expected += bytes_junk; |
---|
139 | // now really read the fmt chunk |
---|
140 | fread(buf, 1, 4, s->fid); |
---|
141 | buf[4] = '\0'; |
---|
142 | } |
---|
143 | |
---|
144 | // get the fmt chunk |
---|
145 | if ( strcmp((const char *)buf, "fmt ") != 0 ) { |
---|
146 | AUBIO_ERR("source_wavread: failed finding fmt RIFF header in %s\n", s->path); |
---|
147 | goto beach; |
---|
148 | } |
---|
149 | |
---|
150 | // Subchunk1Size |
---|
151 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
152 | format = read_little_endian(buf, 4); |
---|
153 | if ( format != 16 ) { |
---|
154 | // TODO accept format 18 |
---|
155 | AUBIO_ERR("source_wavread: file %s is not encoded with PCM\n", s->path); |
---|
156 | goto beach; |
---|
157 | } |
---|
158 | if ( buf[1] || buf[2] | buf[3] ) { |
---|
159 | AUBIO_ERR("source_wavread: Subchunk1Size should be 0, in %s\n", s->path); |
---|
160 | goto beach; |
---|
161 | } |
---|
162 | |
---|
163 | // AudioFormat |
---|
164 | bytes_read += fread(buf, 1, 2, s->fid); |
---|
165 | if ( buf[0] != 1 || buf[1] != 0) { |
---|
166 | AUBIO_ERR("source_wavread: AudioFormat should be PCM, in %s\n", s->path); |
---|
167 | goto beach; |
---|
168 | } |
---|
169 | |
---|
170 | // NumChannels |
---|
171 | bytes_read += fread(buf, 1, 2, s->fid); |
---|
172 | channels = read_little_endian(buf, 2); |
---|
173 | |
---|
174 | // SampleRate |
---|
175 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
176 | sr = read_little_endian(buf, 4); |
---|
177 | |
---|
178 | // ByteRate |
---|
179 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
180 | byterate = read_little_endian(buf, 4); |
---|
181 | |
---|
182 | // BlockAlign |
---|
183 | bytes_read += fread(buf, 1, 2, s->fid); |
---|
184 | blockalign = read_little_endian(buf, 2); |
---|
185 | |
---|
186 | // BitsPerSample |
---|
187 | bytes_read += fread(buf, 1, 2, s->fid); |
---|
188 | bitspersample = read_little_endian(buf, 2); |
---|
189 | #if 0 |
---|
190 | if ( bitspersample != 16 ) { |
---|
191 | AUBIO_ERR("source_wavread: can not process %dbit file %s\n", |
---|
192 | bitspersample, s->path); |
---|
193 | goto beach; |
---|
194 | } |
---|
195 | #endif |
---|
196 | |
---|
197 | if ( byterate * 8 != sr * channels * bitspersample ) { |
---|
198 | AUBIO_ERR("source_wavread: wrong byterate in %s\n", s->path); |
---|
199 | goto beach; |
---|
200 | } |
---|
201 | |
---|
202 | if ( blockalign * 8 != channels * bitspersample ) { |
---|
203 | AUBIO_ERR("source_wavread: wrong blockalign in %s\n", s->path); |
---|
204 | goto beach; |
---|
205 | } |
---|
206 | |
---|
207 | s->input_samplerate = sr; |
---|
208 | s->input_channels = channels; |
---|
209 | |
---|
210 | #if 0 |
---|
211 | AUBIO_DBG("channels %d\n", channels); |
---|
212 | AUBIO_DBG("sr %d\n", sr); |
---|
213 | AUBIO_DBG("byterate %d\n", byterate); |
---|
214 | AUBIO_DBG("blockalign %d\n", blockalign); |
---|
215 | AUBIO_DBG("bitspersample %d\n", bitspersample); |
---|
216 | |
---|
217 | AUBIO_DBG("found %d channels in %s\n", s->input_channels, s->path); |
---|
218 | AUBIO_DBG("found %d samplerate in %s\n", s->input_samplerate, s->path); |
---|
219 | #endif |
---|
220 | |
---|
221 | if (samplerate == 0) { |
---|
222 | s->samplerate = s->input_samplerate; |
---|
223 | } else if (samplerate != s->input_samplerate) { |
---|
224 | AUBIO_ERR("source_wavread: can not resample %s from %d to %dHz\n", |
---|
225 | s->path, s->input_samplerate, samplerate); |
---|
226 | goto beach; |
---|
227 | } |
---|
228 | |
---|
229 | // Subchunk2ID |
---|
230 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
231 | buf[4] = '\0'; |
---|
232 | if ( strcmp((const char *)buf, "data") != 0 ) { |
---|
233 | AUBIO_ERR("source_wavread: data RIFF header not found in %s\n", s->path); |
---|
234 | goto beach; |
---|
235 | } |
---|
236 | |
---|
237 | // Subchunk2Size |
---|
238 | bytes_read += fread(buf, 1, 4, s->fid); |
---|
239 | duration = read_little_endian(buf, 4) / blockalign; |
---|
240 | |
---|
241 | //data_size = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24); |
---|
242 | //AUBIO_MSG("found %d frames in %s\n", 8 * data_size / bitspersample / channels, s->path); |
---|
243 | |
---|
244 | // check the total number of bytes read is correct |
---|
245 | if ( bytes_read != bytes_expected ) { |
---|
246 | #ifndef HAVE_WIN_HACKS |
---|
247 | AUBIO_ERR("source_wavread: short read (%zd instead of %zd) in %s\n", |
---|
248 | bytes_read, bytes_expected, s->path); |
---|
249 | #else // mingw does not know about %zd... |
---|
250 | AUBIO_ERR("source_wavread: short read (%d instead of %d) in %s\n", |
---|
251 | (int)bytes_read, (int)bytes_expected, s->path); |
---|
252 | #endif |
---|
253 | goto beach; |
---|
254 | } |
---|
255 | s->seek_start = bytes_read; |
---|
256 | |
---|
257 | s->output = new_fmat(s->input_channels, AUBIO_WAVREAD_BUFSIZE); |
---|
258 | s->blockalign= blockalign; |
---|
259 | s->bitspersample = bitspersample; |
---|
260 | |
---|
261 | s->duration = duration; |
---|
262 | |
---|
263 | s->short_output = (unsigned char *)calloc(s->blockalign, AUBIO_WAVREAD_BUFSIZE); |
---|
264 | s->read_index = 0; |
---|
265 | s->read_samples = 0; |
---|
266 | s->eof = 0; |
---|
267 | |
---|
268 | return s; |
---|
269 | |
---|
270 | beach: |
---|
271 | //AUBIO_ERR("source_wavread: can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
272 | // s->path, s->samplerate, s->hop_size); |
---|
273 | del_aubio_source_wavread(s); |
---|
274 | return NULL; |
---|
275 | } |
---|
276 | |
---|
277 | void aubio_source_wavread_readframe(aubio_source_wavread_t *s, uint_t *wavread_read); |
---|
278 | |
---|
279 | void aubio_source_wavread_readframe(aubio_source_wavread_t *s, uint_t *wavread_read) { |
---|
280 | unsigned char *short_ptr = s->short_output; |
---|
281 | size_t read = fread(short_ptr, s->blockalign, AUBIO_WAVREAD_BUFSIZE, s->fid); |
---|
282 | uint_t i, j, b, bitspersample = s->bitspersample; |
---|
283 | uint_t wrap_at = (1 << ( bitspersample - 1 ) ); |
---|
284 | uint_t wrap_with = (1 << bitspersample); |
---|
285 | smpl_t scaler = 1. / wrap_at; |
---|
286 | int signed_val = 0; |
---|
287 | unsigned int unsigned_val = 0; |
---|
288 | |
---|
289 | for (j = 0; j < read; j++) { |
---|
290 | for (i = 0; i < s->input_channels; i++) { |
---|
291 | unsigned_val = 0; |
---|
292 | for (b = 0; b < bitspersample; b+=8 ) { |
---|
293 | unsigned_val += *(short_ptr) << b; |
---|
294 | short_ptr++; |
---|
295 | } |
---|
296 | signed_val = unsigned_val; |
---|
297 | // FIXME why does 8 bit conversion maps [0;255] to [-128;127] |
---|
298 | // instead of [0;127] to [0;127] and [128;255] to [-128;-1] |
---|
299 | if (bitspersample == 8) signed_val -= wrap_at; |
---|
300 | else if (unsigned_val >= wrap_at) signed_val = unsigned_val - wrap_with; |
---|
301 | s->output->data[i][j] = signed_val * scaler; |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | *wavread_read = read; |
---|
306 | |
---|
307 | if (read == 0) s->eof = 1; |
---|
308 | } |
---|
309 | |
---|
310 | void aubio_source_wavread_do(aubio_source_wavread_t * s, fvec_t * read_data, uint_t * read){ |
---|
311 | uint_t i, j; |
---|
312 | uint_t end = 0; |
---|
313 | uint_t total_wrote = 0; |
---|
314 | while (total_wrote < s->hop_size) { |
---|
315 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
316 | for (i = 0; i < end; i++) { |
---|
317 | read_data->data[i + total_wrote] = 0; |
---|
318 | for (j = 0; j < s->input_channels; j++ ) { |
---|
319 | read_data->data[i + total_wrote] += s->output->data[j][i + s->read_index]; |
---|
320 | } |
---|
321 | read_data->data[i + total_wrote] /= (smpl_t)(s->input_channels); |
---|
322 | } |
---|
323 | total_wrote += end; |
---|
324 | if (total_wrote < s->hop_size) { |
---|
325 | uint_t wavread_read = 0; |
---|
326 | aubio_source_wavread_readframe(s, &wavread_read); |
---|
327 | s->read_samples = wavread_read; |
---|
328 | s->read_index = 0; |
---|
329 | if (s->eof) { |
---|
330 | break; |
---|
331 | } |
---|
332 | } else { |
---|
333 | s->read_index += end; |
---|
334 | } |
---|
335 | } |
---|
336 | if (total_wrote < s->hop_size) { |
---|
337 | for (i = end; i < s->hop_size; i++) { |
---|
338 | read_data->data[i] = 0.; |
---|
339 | } |
---|
340 | } |
---|
341 | *read = total_wrote; |
---|
342 | } |
---|
343 | |
---|
344 | void aubio_source_wavread_do_multi(aubio_source_wavread_t * s, fmat_t * read_data, uint_t * read){ |
---|
345 | uint_t i,j; |
---|
346 | uint_t end = 0; |
---|
347 | uint_t total_wrote = 0; |
---|
348 | while (total_wrote < s->hop_size) { |
---|
349 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
350 | for (j = 0; j < read_data->height; j++) { |
---|
351 | for (i = 0; i < end; i++) { |
---|
352 | read_data->data[j][i + total_wrote] = s->output->data[j][i]; |
---|
353 | } |
---|
354 | } |
---|
355 | total_wrote += end; |
---|
356 | if (total_wrote < s->hop_size) { |
---|
357 | uint_t wavread_read = 0; |
---|
358 | aubio_source_wavread_readframe(s, &wavread_read); |
---|
359 | s->read_samples = wavread_read; |
---|
360 | s->read_index = 0; |
---|
361 | if (s->eof) { |
---|
362 | break; |
---|
363 | } |
---|
364 | } else { |
---|
365 | s->read_index += end; |
---|
366 | } |
---|
367 | } |
---|
368 | if (total_wrote < s->hop_size) { |
---|
369 | for (j = 0; j < read_data->height; j++) { |
---|
370 | for (i = end; i < s->hop_size; i++) { |
---|
371 | read_data->data[j][i] = 0.; |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | *read = total_wrote; |
---|
376 | } |
---|
377 | |
---|
378 | uint_t aubio_source_wavread_get_samplerate(aubio_source_wavread_t * s) { |
---|
379 | return s->samplerate; |
---|
380 | } |
---|
381 | |
---|
382 | uint_t aubio_source_wavread_get_channels(aubio_source_wavread_t * s) { |
---|
383 | return s->input_channels; |
---|
384 | } |
---|
385 | |
---|
386 | uint_t aubio_source_wavread_seek (aubio_source_wavread_t * s, uint_t pos) { |
---|
387 | uint_t ret = 0; |
---|
388 | if ((sint_t)pos < 0) { |
---|
389 | return AUBIO_FAIL; |
---|
390 | } |
---|
391 | ret = fseek(s->fid, s->seek_start + pos * s->blockalign, SEEK_SET); |
---|
392 | if (ret != 0) { |
---|
393 | AUBIO_ERR("source_wavread: could not seek %s at %d (%s)\n", s->path, pos, strerror(errno)); |
---|
394 | return AUBIO_FAIL; |
---|
395 | } |
---|
396 | // reset some values |
---|
397 | s->eof = 0; |
---|
398 | s->read_index = 0; |
---|
399 | return AUBIO_OK; |
---|
400 | } |
---|
401 | |
---|
402 | uint_t aubio_source_wavread_get_duration (const aubio_source_wavread_t * s) { |
---|
403 | if (s && s->duration) { |
---|
404 | return s->duration; |
---|
405 | } |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | uint_t aubio_source_wavread_close (aubio_source_wavread_t * s) { |
---|
410 | if (!s->fid) { |
---|
411 | return AUBIO_FAIL; |
---|
412 | } |
---|
413 | if (fclose(s->fid)) { |
---|
414 | AUBIO_ERR("source_wavread: could not close %s (%s)\n", s->path, strerror(errno)); |
---|
415 | return AUBIO_FAIL; |
---|
416 | } |
---|
417 | s->fid = NULL; |
---|
418 | return AUBIO_OK; |
---|
419 | } |
---|
420 | |
---|
421 | void del_aubio_source_wavread(aubio_source_wavread_t * s) { |
---|
422 | if (!s) return; |
---|
423 | aubio_source_wavread_close(s); |
---|
424 | if (s->short_output) AUBIO_FREE(s->short_output); |
---|
425 | if (s->output) del_fmat(s->output); |
---|
426 | if (s->path) AUBIO_FREE(s->path); |
---|
427 | AUBIO_FREE(s); |
---|
428 | } |
---|
429 | |
---|
430 | #endif /* HAVE_WAVREAD */ |
---|