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