source: src/io/source_wavread.c @ d2be104

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since d2be104 was d2be104, checked in by Paul Brossier <piem@piem.org>, 4 years ago

src/io/: also copy null ending char

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