source: src/io/source_wavread.c @ fa5d8ad

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since fa5d8ad was fa5d8ad, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/io/source_wavread.c: skip chunks until data is found, or abort

  • Property mode set to 100644
File size: 12.5 KB
Line 
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
36struct _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
63unsigned int read_little_endian (unsigned char *buf, unsigned int length);
64unsigned 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
72aubio_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 + 4;
139    // now really read the fmt chunk
140    bytes_read += 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  while ( strcmp((const char *)buf, "data") != 0 ) {
233    if (feof(s->fid) || ferror(s->fid)) {
234      AUBIO_ERR("source_wavread: no data RIFF header found in %s\n", s->path);
235      goto beach;
236    }
237    bytes_junk = fread(buf, 1, 4, s->fid);
238    buf[4] = '\0';
239    bytes_junk += read_little_endian(buf, 4);
240    if (fseek(s->fid, bytes_read + bytes_junk, SEEK_SET) != 0) {
241      AUBIO_ERR("source_wavread: could not seek past unknown chunk in %s (%s)\n",
242          s->path, strerror(errno));
243      goto beach;
244    }
245    bytes_read += bytes_junk;
246    bytes_expected += bytes_junk+ 4;
247    bytes_read += fread(buf, 1, 4, s->fid);
248    buf[4] = '\0';
249  }
250
251  // Subchunk2Size
252  bytes_read += fread(buf, 1, 4, s->fid);
253  duration = read_little_endian(buf, 4) / blockalign;
254
255  //data_size = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
256  //AUBIO_MSG("found %d frames in %s\n", 8 * data_size / bitspersample / channels, s->path);
257
258  // check the total number of bytes read is correct
259  if ( bytes_read != bytes_expected ) {
260#ifndef HAVE_WIN_HACKS
261    AUBIO_ERR("source_wavread: short read (%zd instead of %zd) in %s\n",
262        bytes_read, bytes_expected, s->path);
263#else // mingw does not know about %zd...
264    AUBIO_ERR("source_wavread: short read (%d instead of %d) in %s\n",
265        (int)bytes_read, (int)bytes_expected, s->path);
266#endif
267    goto beach;
268  }
269  s->seek_start = bytes_read;
270
271  s->output = new_fmat(s->input_channels, AUBIO_WAVREAD_BUFSIZE);
272  s->blockalign= blockalign;
273  s->bitspersample = bitspersample;
274
275  s->duration = duration;
276
277  s->short_output = (unsigned char *)calloc(s->blockalign, AUBIO_WAVREAD_BUFSIZE);
278  s->read_index = 0;
279  s->read_samples = 0;
280  s->eof = 0;
281
282  return s;
283
284beach:
285  //AUBIO_ERR("source_wavread: can not read %s at samplerate %dHz with a hop_size of %d\n",
286  //    s->path, s->samplerate, s->hop_size);
287  del_aubio_source_wavread(s);
288  return NULL;
289}
290
291void aubio_source_wavread_readframe(aubio_source_wavread_t *s, uint_t *wavread_read);
292
293void aubio_source_wavread_readframe(aubio_source_wavread_t *s, uint_t *wavread_read) {
294  unsigned char *short_ptr = s->short_output;
295  size_t read = fread(short_ptr, s->blockalign, AUBIO_WAVREAD_BUFSIZE, s->fid);
296  uint_t i, j, b, bitspersample = s->bitspersample;
297  uint_t wrap_at = (1 << ( bitspersample - 1 ) );
298  uint_t wrap_with = (1 << bitspersample);
299  smpl_t scaler = 1. / wrap_at;
300  int signed_val = 0;
301  unsigned int unsigned_val = 0;
302
303  for (j = 0; j < read; j++) {
304    for (i = 0; i < s->input_channels; i++) {
305      unsigned_val = 0;
306      for (b = 0; b < bitspersample; b+=8 ) {
307        unsigned_val += *(short_ptr) << b;
308        short_ptr++;
309      }
310      signed_val = unsigned_val;
311      // FIXME why does 8 bit conversion maps [0;255] to [-128;127]
312      // instead of [0;127] to [0;127] and [128;255] to [-128;-1]
313      if (bitspersample == 8) signed_val -= wrap_at;
314      else if (unsigned_val >= wrap_at) signed_val = unsigned_val - wrap_with;
315      s->output->data[i][j] = signed_val * scaler;
316    }
317  }
318
319  *wavread_read = read;
320
321  if (read == 0) s->eof = 1;
322}
323
324void aubio_source_wavread_do(aubio_source_wavread_t * s, fvec_t * read_data, uint_t * read){
325  uint_t i, j;
326  uint_t end = 0;
327  uint_t total_wrote = 0;
328  while (total_wrote < s->hop_size) {
329    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
330    for (i = 0; i < end; i++) {
331      read_data->data[i + total_wrote] = 0;
332      for (j = 0; j < s->input_channels; j++ ) {
333        read_data->data[i + total_wrote] += s->output->data[j][i + s->read_index];
334      }
335      read_data->data[i + total_wrote] /= (smpl_t)(s->input_channels);
336    }
337    total_wrote += end;
338    if (total_wrote < s->hop_size) {
339      uint_t wavread_read = 0;
340      aubio_source_wavread_readframe(s, &wavread_read);
341      s->read_samples = wavread_read;
342      s->read_index = 0;
343      if (s->eof) {
344        break;
345      }
346    } else {
347      s->read_index += end;
348    }
349  }
350  if (total_wrote < s->hop_size) {
351    for (i = end; i < s->hop_size; i++) {
352      read_data->data[i] = 0.;
353    }
354  }
355  *read = total_wrote;
356}
357
358void aubio_source_wavread_do_multi(aubio_source_wavread_t * s, fmat_t * read_data, uint_t * read){
359  uint_t i,j;
360  uint_t end = 0;
361  uint_t total_wrote = 0;
362  while (total_wrote < s->hop_size) {
363    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
364    for (j = 0; j < read_data->height; j++) {
365      for (i = 0; i < end; i++) {
366        read_data->data[j][i + total_wrote] = s->output->data[j][i];
367      }
368    }
369    total_wrote += end;
370    if (total_wrote < s->hop_size) {
371      uint_t wavread_read = 0;
372      aubio_source_wavread_readframe(s, &wavread_read);
373      s->read_samples = wavread_read;
374      s->read_index = 0;
375      if (s->eof) {
376        break;
377      }
378    } else {
379      s->read_index += end;
380    }
381  }
382  if (total_wrote < s->hop_size) {
383    for (j = 0; j < read_data->height; j++) {
384      for (i = end; i < s->hop_size; i++) {
385        read_data->data[j][i] = 0.;
386      }
387    }
388  }
389  *read = total_wrote;
390}
391
392uint_t aubio_source_wavread_get_samplerate(aubio_source_wavread_t * s) {
393  return s->samplerate;
394}
395
396uint_t aubio_source_wavread_get_channels(aubio_source_wavread_t * s) {
397  return s->input_channels;
398}
399
400uint_t aubio_source_wavread_seek (aubio_source_wavread_t * s, uint_t pos) {
401  uint_t ret = 0;
402  if ((sint_t)pos < 0) {
403    return AUBIO_FAIL;
404  }
405  ret = fseek(s->fid, s->seek_start + pos * s->blockalign, SEEK_SET);
406  if (ret != 0) {
407    AUBIO_ERR("source_wavread: could not seek %s at %d (%s)\n", s->path, pos, strerror(errno));
408    return AUBIO_FAIL;
409  }
410  // reset some values
411  s->eof = 0;
412  s->read_index = 0;
413  return AUBIO_OK;
414}
415
416uint_t aubio_source_wavread_get_duration (const aubio_source_wavread_t * s) {
417  if (s && s->duration) {
418    return s->duration;
419  }
420  return 0;
421}
422
423uint_t aubio_source_wavread_close (aubio_source_wavread_t * s) {
424  if (!s->fid) {
425    return AUBIO_FAIL;
426  }
427  if (fclose(s->fid)) {
428    AUBIO_ERR("source_wavread: could not close %s (%s)\n", s->path, strerror(errno));
429    return AUBIO_FAIL;
430  }
431  s->fid = NULL;
432  return AUBIO_OK;
433}
434
435void del_aubio_source_wavread(aubio_source_wavread_t * s) {
436  if (!s) return;
437  aubio_source_wavread_close(s);
438  if (s->short_output) AUBIO_FREE(s->short_output);
439  if (s->output) del_fmat(s->output);
440  if (s->path) AUBIO_FREE(s->path);
441  AUBIO_FREE(s);
442}
443
444#endif /* HAVE_WAVREAD */
Note: See TracBrowser for help on using the repository browser.