source: src/io/source_sndfile.c @ a280ed1

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

[source_sndfile] set error message when reading after close

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[afbd7e7]1/*
2  Copyright (C) 2012 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"
[afbd7e7]22
23#ifdef HAVE_SNDFILE
24
25#include <sndfile.h>
26
27#include "fvec.h"
[3d2fe26]28#include "fmat.h"
[2589ea9]29#include "ioutils.h"
[3d2fe26]30#include "source_sndfile.h"
[afbd7e7]31
[87dfc3f]32#include "temporal/resampler.h"
33
[afbd7e7]34#define MAX_SIZE 4096
[18a96aa]35#define MAX_SAMPLES AUBIO_MAX_CHANNELS * MAX_SIZE
[afbd7e7]36
[5bd806d]37#if !HAVE_AUBIO_DOUBLE
38#define aubio_sf_read_smpl sf_read_float
39#else /* HAVE_AUBIO_DOUBLE */
40#define aubio_sf_read_smpl sf_read_double
41#endif /* HAVE_AUBIO_DOUBLE */
42
[afbd7e7]43struct _aubio_source_sndfile_t {
44  uint_t hop_size;
45  uint_t samplerate;
46  uint_t channels;
[18c6b20]47
48  // some data about the file
49  char_t *path;
50  SNDFILE *handle;
[afbd7e7]51  int input_samplerate;
52  int input_channels;
53  int input_format;
[c6e7ba1]54  int duration;
[87dfc3f]55
[18c6b20]56  // resampling stuff
[87dfc3f]57  smpl_t ratio;
[18c6b20]58  uint_t input_hop_size;
[87dfc3f]59#ifdef HAVE_SAMPLERATE
[1d01e515]60  aubio_resampler_t **resamplers;
[18c6b20]61  fvec_t *input_data;
[1d01e515]62  fmat_t *input_mat;
[87dfc3f]63#endif /* HAVE_SAMPLERATE */
[18c6b20]64
65  // some temporary memory for sndfile to write at
66  uint_t scratch_size;
[5bd806d]67  smpl_t *scratch_data;
[afbd7e7]68};
69
[ae5d58a]70aubio_source_sndfile_t * new_aubio_source_sndfile(const char_t * path, uint_t samplerate, uint_t hop_size) {
[afbd7e7]71  aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
[67b05b4]72  SF_INFO sfinfo;
[afbd7e7]73
74  if (path == NULL) {
[491e6ea]75    AUBIO_ERR("source_sndfile: Aborted opening null path\n");
[447c673]76    goto beach;
77  }
78  if ((sint_t)samplerate < 0) {
[491e6ea]79    AUBIO_ERR("source_sndfile: Can not open %s with samplerate %d\n", path, samplerate);
[447c673]80    goto beach;
81  }
82  if ((sint_t)hop_size <= 0) {
[491e6ea]83    AUBIO_ERR("source_sndfile: Can not open %s with hop_size %d\n", path, hop_size);
[447c673]84    goto beach;
[afbd7e7]85  }
86
87  s->hop_size = hop_size;
88  s->channels = 1;
[b643a33]89
[d2be104]90  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
91  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[afbd7e7]92
[3d1ca81]93  // try opening the file, getting the info in sfinfo
[afbd7e7]94  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
95  s->handle = sf_open (s->path, SFM_READ, &sfinfo);
96
97  if (s->handle == NULL) {
98    /* show libsndfile err msg */
[17d0f0e]99    AUBIO_ERR("source_sndfile: Failed opening %s (%s)\n", s->path,
100        sf_strerror (NULL));
[18c6b20]101    goto beach;
[a65d37a]102  }
[afbd7e7]103
104  /* get input specs */
105  s->input_samplerate = sfinfo.samplerate;
106  s->input_channels   = sfinfo.channels;
107  s->input_format     = sfinfo.format;
[c6e7ba1]108  s->duration         = sfinfo.frames;
[afbd7e7]109
[c8f411b]110  if (samplerate == 0) {
[26a6af7]111    s->samplerate = s->input_samplerate;
[4db8eab]112    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
[26a6af7]113  } else {
114    s->samplerate = samplerate;
[c8f411b]115  }
[18c6b20]116  /* compute input block size required before resampling */
[5bd806d]117  s->ratio = s->samplerate/(smpl_t)s->input_samplerate;
[18c6b20]118  s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
119
120  if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
[491e6ea]121    AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n",
[18c6b20]122        MAX_SAMPLES / s->input_channels, s->input_channels);
123    goto beach;
124  }
[87dfc3f]125
126#ifdef HAVE_SAMPLERATE
[18c6b20]127  s->input_data = NULL;
[1d01e515]128  s->input_mat = NULL;
129  s->resamplers = NULL;
[18c6b20]130  if (s->ratio != 1) {
[1d01e515]131    uint_t i;
132    s->resamplers = AUBIO_ARRAY(aubio_resampler_t*, s->input_channels);
[18c6b20]133    s->input_data = new_fvec(s->input_hop_size);
[1d01e515]134    s->input_mat = new_fmat(s->input_channels, s->input_hop_size);
135    for (i = 0; i < (uint_t)s->input_channels; i++) {
136      s->resamplers[i] = new_aubio_resampler(s->ratio, 4);
137    }
[d22cc42]138    if (s->ratio > 1) {
139      // we would need to add a ring buffer for these
[cad7e91]140      if ( (uint_t)FLOOR(s->input_hop_size * s->ratio + .5)  != s->hop_size ) {
[491e6ea]141        AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path,
[018e511]142            s->input_samplerate, s->samplerate);
[d22cc42]143        goto beach;
144      }
[491e6ea]145      AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
[018e511]146          s->input_samplerate, s->samplerate);
[87dfc3f]147    }
[985d5c4]148    s->duration = (uint_t)FLOOR(s->duration * s->ratio);
[18c6b20]149  }
[87dfc3f]150#else
[18c6b20]151  if (s->ratio != 1) {
[491e6ea]152    AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
[18c6b20]153    goto beach;
[afbd7e7]154  }
[18c6b20]155#endif /* HAVE_SAMPLERATE */
[87dfc3f]156
[afbd7e7]157  /* allocate data for de/interleaving reallocated when needed. */
[18c6b20]158  s->scratch_size = s->input_hop_size * s->input_channels;
[5bd806d]159  s->scratch_data = AUBIO_ARRAY(smpl_t, s->scratch_size);
[afbd7e7]160
161  return s;
[18c6b20]162
163beach:
[447c673]164  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
165  //    s->path, s->samplerate, s->hop_size);
[18c6b20]166  del_aubio_source_sndfile(s);
167  return NULL;
[afbd7e7]168}
169
170void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
[18c6b20]171  uint_t i,j, input_channels = s->input_channels;
[2722dc7]172  /* read from file into scratch_data */
[2589ea9]173  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
174      s->hop_size, read_data->length);
175  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
[ebfa80d]176      s->scratch_size);
[4b16721]177  if (!s->handle) {
178    AUBIO_ERR("source_sndfile: could not read from %s (file was closed)\n",
179        s->path);
180    *read = 0;
181    return;
182  }
[2589ea9]183
[ebfa80d]184  uint_t read_length = read_samples / s->input_channels;
[afbd7e7]185
[2722dc7]186  /* where to store de-interleaved data */
187  smpl_t *ptr_data;
[87dfc3f]188#ifdef HAVE_SAMPLERATE
[18c6b20]189  if (s->ratio != 1) {
[2722dc7]190    ptr_data = s->input_data->data;
[87dfc3f]191  } else
192#endif /* HAVE_SAMPLERATE */
193  {
[ebfa80d]194    read_length = MIN(length, read_length);
[2722dc7]195    ptr_data = read_data->data;
[87dfc3f]196  }
[afbd7e7]197
198  /* de-interleaving and down-mixing data  */
[ebfa80d]199  for (j = 0; j < read_length; j++) {
[2722dc7]200    ptr_data[j] = 0;
[afbd7e7]201    for (i = 0; i < input_channels; i++) {
[2722dc7]202      ptr_data[j] += s->scratch_data[input_channels*j+i];
[afbd7e7]203    }
[2722dc7]204    ptr_data[j] /= (smpl_t)input_channels;
[87dfc3f]205  }
206
207#ifdef HAVE_SAMPLERATE
[1d01e515]208  if (s->resamplers) {
209    aubio_resampler_do(s->resamplers[0], s->input_data, read_data);
[afbd7e7]210  }
[87dfc3f]211#endif /* HAVE_SAMPLERATE */
212
[ebfa80d]213  *read = MIN(length, (uint_t)FLOOR(s->ratio * read_length + .5));
[18a378e]214
[7dea72f]215  aubio_source_pad_output (read_data, *read);
[18a378e]216
[afbd7e7]217}
218
[4865e4b]219void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
220  uint_t i,j, input_channels = s->input_channels;
221  /* do actual reading */
[2589ea9]222  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
223      s->hop_size, read_data->length);
224  uint_t channels = aubio_source_validate_input_channels("source_sndfile",
225      s->path, s->input_channels, read_data->height);
226  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
[ebfa80d]227      s->scratch_size);
[4b16721]228  if (!s->handle) {
229    AUBIO_ERR("source_sndfile: could not read from %s (file was closed)\n",
230        s->path);
231    *read = 0;
232    return;
233  }
[2589ea9]234
[ebfa80d]235  uint_t read_length = read_samples / s->input_channels;
[4865e4b]236
[2722dc7]237  /* where to store de-interleaved data */
238  smpl_t **ptr_data;
[4865e4b]239#ifdef HAVE_SAMPLERATE
240  if (s->ratio != 1) {
[1d01e515]241    ptr_data = s->input_mat->data;
[4865e4b]242  } else
243#endif /* HAVE_SAMPLERATE */
244  {
[ebfa80d]245    read_length = MIN(read_length, length);
[2722dc7]246    ptr_data = read_data->data;
[4865e4b]247  }
248
[ebfa80d]249  for (j = 0; j < read_length; j++) {
[2589ea9]250    for (i = 0; i < channels; i++) {
251      ptr_data[i][j] = s->scratch_data[j * input_channels + i];
[6510866]252    }
253  }
[4865e4b]254
255#ifdef HAVE_SAMPLERATE
[1d01e515]256  if (s->resamplers) {
257    for (i = 0; i < input_channels; i++) {
258      fvec_t input_chan, read_chan;
259      input_chan.data = s->input_mat->data[i];
260      input_chan.length = s->input_mat->length;
261      read_chan.data = read_data->data[i];
262      read_chan.length = read_data->length;
263      aubio_resampler_do(s->resamplers[i], &input_chan, &read_chan);
264    }
[4865e4b]265  }
266#endif /* HAVE_SAMPLERATE */
267
[ebfa80d]268  *read = MIN(length, (uint_t)FLOOR(s->ratio * read_length + .5));
[18a378e]269
[7dea72f]270  aubio_source_pad_multi_output(read_data, input_channels, *read);
[4865e4b]271}
272
[063fa5a]273uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
274  return s->samplerate;
275}
276
[4865e4b]277uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
278  return s->input_channels;
279}
280
[c6e7ba1]281uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) {
282  if (s && s->duration) {
283    return s->duration;
284  }
285  return 0;
286}
287
[7982203]288uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
[7bb5cef]289  uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
[53bb4da]290  sf_count_t sf_ret;
[0eeba03]291  if (s->handle == NULL) {
292    AUBIO_ERR("source_sndfile: failed seeking in %s (file not opened?)\n",
293        s->path);
294    return AUBIO_FAIL;
295  }
[05a8c18]296  if ((sint_t)pos < 0) {
297    AUBIO_ERR("source_sndfile: could not seek %s at %d (seeking position"
298       " should be >= 0)\n", s->path, pos);
299    return AUBIO_FAIL;
300  }
[53bb4da]301  sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
[f33ab63]302  if (sf_ret == -1) {
303    AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
304    return AUBIO_FAIL;
305  }
306  if (sf_ret != resampled_pos) {
[74dcddb]307    AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n",
308        s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL));
[f33ab63]309    return AUBIO_FAIL;
310  }
311  return AUBIO_OK;
[7982203]312}
313
[422452b]314uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
315  if (!s->handle) {
[48ebc3b]316    return AUBIO_OK;
[c038740]317  }
[422452b]318  if(sf_close(s->handle)) {
[f33ab63]319    AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
[422452b]320    return AUBIO_FAIL;
[afbd7e7]321  }
[5210563]322  s->handle = NULL;
[422452b]323  return AUBIO_OK;
324}
325
326void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
[c0a1906]327  AUBIO_ASSERT(s);
[c038740]328  aubio_source_sndfile_close(s);
[87dfc3f]329#ifdef HAVE_SAMPLERATE
[1d01e515]330  if (s->resamplers != NULL) {
331    uint_t i = 0, input_channels = s->input_channels;
332    for (i = 0; i < input_channels; i ++) {
333      if (s->resamplers[i] != NULL) {
334        del_aubio_resampler(s->resamplers[i]);
335      }
336    }
337    AUBIO_FREE(s->resamplers);
[87dfc3f]338  }
[18c6b20]339  if (s->input_data) {
340    del_fvec(s->input_data);
[87dfc3f]341  }
[1d01e515]342  if (s->input_mat) {
343    del_fmat(s->input_mat);
344  }
[87dfc3f]345#endif /* HAVE_SAMPLERATE */
[b643a33]346  if (s->path) AUBIO_FREE(s->path);
[afbd7e7]347  AUBIO_FREE(s->scratch_data);
348  AUBIO_FREE(s);
349}
350
351#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.