source: src/io/source_wavread.c @ ea7428a

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

src/io/source_wavread.h: add _get_duration

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