source: src/io/source_wavread.c @ 2510248

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 2510248 was 6a253e8, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[source_wavread] use padding helpers

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