source: src/io/source_sndfile.c @ ae5d58a

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

src/io/*.{c,h}: added const qualifiers to unmodified pointers

  • Property mode set to 100644
File size: 8.9 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;
[87dfc3f]56
[18c6b20]57  // resampling stuff
[87dfc3f]58  smpl_t ratio;
[18c6b20]59  uint_t input_hop_size;
[87dfc3f]60#ifdef HAVE_SAMPLERATE
61  aubio_resampler_t *resampler;
[18c6b20]62  fvec_t *input_data;
[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;
89  s->path = path;
90
[3d1ca81]91  // try opening the file, getting the info in sfinfo
[afbd7e7]92  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
93  s->handle = sf_open (s->path, SFM_READ, &sfinfo);
94
95  if (s->handle == NULL) {
96    /* show libsndfile err msg */
[491e6ea]97    AUBIO_ERR("source_sndfile: Failed opening %s: %s\n", s->path, sf_strerror (NULL));
[18c6b20]98    goto beach;
[a65d37a]99  }
[afbd7e7]100
101  /* get input specs */
102  s->input_samplerate = sfinfo.samplerate;
103  s->input_channels   = sfinfo.channels;
104  s->input_format     = sfinfo.format;
105
[c8f411b]106  if (samplerate == 0) {
107    samplerate = s->input_samplerate;
[4db8eab]108    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
[c8f411b]109  }
110  s->samplerate = samplerate;
[18c6b20]111  /* compute input block size required before resampling */
[5bd806d]112  s->ratio = s->samplerate/(smpl_t)s->input_samplerate;
[18c6b20]113  s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
114
115  if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
[491e6ea]116    AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n",
[18c6b20]117        MAX_SAMPLES / s->input_channels, s->input_channels);
118    goto beach;
119  }
[87dfc3f]120
121#ifdef HAVE_SAMPLERATE
[18c6b20]122  s->resampler = NULL;
123  s->input_data = NULL;
124  if (s->ratio != 1) {
125    s->input_data = new_fvec(s->input_hop_size);
[6fd8d7e]126    s->resampler = new_aubio_resampler(s->ratio, 4);
[d22cc42]127    if (s->ratio > 1) {
128      // we would need to add a ring buffer for these
129      if ( (uint_t)(s->input_hop_size * s->ratio + .5)  != s->hop_size ) {
[491e6ea]130        AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path,
[018e511]131            s->input_samplerate, s->samplerate);
[d22cc42]132        goto beach;
133      }
[491e6ea]134      AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
[018e511]135          s->input_samplerate, s->samplerate);
[87dfc3f]136    }
[18c6b20]137  }
[87dfc3f]138#else
[18c6b20]139  if (s->ratio != 1) {
[491e6ea]140    AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
[18c6b20]141    goto beach;
[afbd7e7]142  }
[18c6b20]143#endif /* HAVE_SAMPLERATE */
[87dfc3f]144
[afbd7e7]145  /* allocate data for de/interleaving reallocated when needed. */
[18c6b20]146  s->scratch_size = s->input_hop_size * s->input_channels;
[5bd806d]147  s->scratch_data = AUBIO_ARRAY(smpl_t, s->scratch_size);
[afbd7e7]148
149  return s;
[18c6b20]150
151beach:
[447c673]152  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
153  //    s->path, s->samplerate, s->hop_size);
[18c6b20]154  del_aubio_source_sndfile(s);
155  return NULL;
[afbd7e7]156}
157
158void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
[18c6b20]159  uint_t i,j, input_channels = s->input_channels;
[2722dc7]160  /* read from file into scratch_data */
[5bd806d]161  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
[afbd7e7]162
[2722dc7]163  /* where to store de-interleaved data */
164  smpl_t *ptr_data;
[87dfc3f]165#ifdef HAVE_SAMPLERATE
[18c6b20]166  if (s->ratio != 1) {
[2722dc7]167    ptr_data = s->input_data->data;
[87dfc3f]168  } else
169#endif /* HAVE_SAMPLERATE */
170  {
[2722dc7]171    ptr_data = read_data->data;
[87dfc3f]172  }
[afbd7e7]173
174  /* de-interleaving and down-mixing data  */
[18c6b20]175  for (j = 0; j < read_samples / input_channels; j++) {
[2722dc7]176    ptr_data[j] = 0;
[afbd7e7]177    for (i = 0; i < input_channels; i++) {
[2722dc7]178      ptr_data[j] += s->scratch_data[input_channels*j+i];
[afbd7e7]179    }
[2722dc7]180    ptr_data[j] /= (smpl_t)input_channels;
[87dfc3f]181  }
182
183#ifdef HAVE_SAMPLERATE
184  if (s->resampler) {
[18c6b20]185    aubio_resampler_do(s->resampler, s->input_data, read_data);
[afbd7e7]186  }
[87dfc3f]187#endif /* HAVE_SAMPLERATE */
188
[18c6b20]189  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
[18a378e]190
191  if (*read < s->hop_size) {
192    for (j = *read; j < s->hop_size; j++) {
[45f48576]193      read_data->data[j] = 0;
[18a378e]194    }
195  }
196
[afbd7e7]197}
198
[4865e4b]199void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
200  uint_t i,j, input_channels = s->input_channels;
201  /* do actual reading */
[5bd806d]202  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
[4865e4b]203
[2722dc7]204  /* where to store de-interleaved data */
205  smpl_t **ptr_data;
[4865e4b]206#ifdef HAVE_SAMPLERATE
207  if (s->ratio != 1) {
[f33ab63]208    AUBIO_ERR("source_sndfile: no multi channel resampling yet\n");
[4865e4b]209    return;
[2722dc7]210    //ptr_data = s->input_data->data;
[4865e4b]211  } else
212#endif /* HAVE_SAMPLERATE */
213  {
[2722dc7]214    ptr_data = read_data->data;
[4865e4b]215  }
216
[50e10a9]217  if (read_data->height < input_channels) {
218    // destination matrix has less channels than the file; copy only first
219    // channels of the file, de-interleaving data
220    for (j = 0; j < read_samples / input_channels; j++) {
221      for (i = 0; i < read_data->height; i++) {
[5bd806d]222        ptr_data[i][j] = s->scratch_data[j * input_channels + i];
[50e10a9]223      }
224    }
225  } else {
226    // destination matrix has as many or more channels than the file; copy each
227    // channel from the file to the destination matrix, de-interleaving data
228    for (j = 0; j < read_samples / input_channels; j++) {
229      for (i = 0; i < input_channels; i++) {
[5bd806d]230        ptr_data[i][j] = s->scratch_data[j * input_channels + i];
[50e10a9]231      }
[4865e4b]232    }
233  }
[50e10a9]234
[6510866]235  if (read_data->height > input_channels) {
[50e10a9]236    // destination matrix has more channels than the file; copy last channel
237    // of the file to each additional channels, de-interleaving data
[6510866]238    for (j = 0; j < read_samples / input_channels; j++) {
[58c24e1]239      for (i = input_channels; i < read_data->height; i++) {
[5bd806d]240        ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
[6510866]241      }
242    }
243  }
[4865e4b]244
245#ifdef HAVE_SAMPLERATE
246  if (s->resampler) {
[3d2fe26]247    //aubio_resampler_do(s->resampler, s->input_data, read_data);
[4865e4b]248  }
249#endif /* HAVE_SAMPLERATE */
250
251  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
[18a378e]252
253  if (*read < s->hop_size) {
[69440b8]254    for (i = 0; i < read_data->height; i++) {
[18a378e]255      for (j = *read; j < s->hop_size; j++) {
[45f48576]256        read_data->data[i][j] = 0.;
[18a378e]257      }
258    }
259  }
260
[4865e4b]261}
262
[063fa5a]263uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
264  return s->samplerate;
265}
266
[4865e4b]267uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
268  return s->input_channels;
269}
270
[7982203]271uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
[7bb5cef]272  uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
[f33ab63]273  sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
274  if (sf_ret == -1) {
275    AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
276    return AUBIO_FAIL;
277  }
278  if (sf_ret != resampled_pos) {
[74dcddb]279    AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n",
280        s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL));
[f33ab63]281    return AUBIO_FAIL;
282  }
283  return AUBIO_OK;
[7982203]284}
285
[422452b]286uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
287  if (!s->handle) {
288    return AUBIO_FAIL;
[c038740]289  }
[422452b]290  if(sf_close(s->handle)) {
[f33ab63]291    AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
[422452b]292    return AUBIO_FAIL;
[afbd7e7]293  }
[422452b]294  return AUBIO_OK;
295}
296
297void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
298  if (!s) return;
[c038740]299  aubio_source_sndfile_close(s);
[87dfc3f]300#ifdef HAVE_SAMPLERATE
[18c6b20]301  if (s->resampler != NULL) {
[87dfc3f]302    del_aubio_resampler(s->resampler);
303  }
[18c6b20]304  if (s->input_data) {
305    del_fvec(s->input_data);
[87dfc3f]306  }
307#endif /* HAVE_SAMPLERATE */
[afbd7e7]308  AUBIO_FREE(s->scratch_data);
309  AUBIO_FREE(s);
310}
311
312#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.