source: src/io/source_wavread.c @ 9498c88

feature/crepe_org
Last change on this file since 9498c88 was b40c149, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[source_wavread] comment unused macro

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