source: src/io/source_sndfile.c @ dab4a4c

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

src/io/source_sndfile.c: add support for multi-channel resampling

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