source: src/io/source_wavread.c @ 9492ec88

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

src/io/source_wavread.c: initialize buffer

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