source: src/io/source_sndfile.c @ 2da7526

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

src/io/{sink,source}_sndfile.c: remove spaces and tabs

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