source: src/io/source_sndfile.c @ 9630fa8

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 9630fa8 was c0a1906, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[source] simplify and avoid unrequired checks

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