source: src/io/source_sndfile.c @ 18a96aa

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

src/io/source_sndfile.c

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