source: src/io/source_sndfile.c @ f107e12

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

src/io/: removed methods duplicated in merge

  • Property mode set to 100644
File size: 5.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
21
22#include "config.h"
23
24#ifdef HAVE_SNDFILE
25
26#include <sndfile.h>
27
28#include "aubio_priv.h"
29#include "source_sndfile.h"
30#include "fvec.h"
31
[87dfc3f]32#include "temporal/resampler.h"
33
[afbd7e7]34#define MAX_CHANNELS 6
35#define MAX_SIZE 4096
[18c6b20]36#define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE
[afbd7e7]37
38struct _aubio_source_sndfile_t {
39  uint_t hop_size;
40  uint_t samplerate;
41  uint_t channels;
[18c6b20]42
43  // some data about the file
44  char_t *path;
45  SNDFILE *handle;
[afbd7e7]46  int input_samplerate;
47  int input_channels;
48  int input_format;
[87dfc3f]49
[18c6b20]50  // resampling stuff
[87dfc3f]51  smpl_t ratio;
[18c6b20]52  uint_t input_hop_size;
[87dfc3f]53#ifdef HAVE_SAMPLERATE
54  aubio_resampler_t *resampler;
[18c6b20]55  fvec_t *input_data;
[87dfc3f]56#endif /* HAVE_SAMPLERATE */
[18c6b20]57
58  // some temporary memory for sndfile to write at
59  uint_t scratch_size;
60  smpl_t *scratch_data;
[afbd7e7]61};
62
63aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) {
64  aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
65
66  if (path == NULL) {
67    AUBIO_ERR("Aborted opening null path\n");
68    return NULL;
69  }
70
71  s->hop_size = hop_size;
72  s->channels = 1;
73  s->path = path;
74
75  // try opening the file, geting the info in sfinfo
76  SF_INFO sfinfo;
77  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
78  s->handle = sf_open (s->path, SFM_READ, &sfinfo);
79
80  if (s->handle == NULL) {
81    /* show libsndfile err msg */
82    AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL));
[18c6b20]83    goto beach;
[afbd7e7]84  }     
85
86  /* get input specs */
87  s->input_samplerate = sfinfo.samplerate;
88  s->input_channels   = sfinfo.channels;
89  s->input_format     = sfinfo.format;
90
[c8f411b]91  if (samplerate == 0) {
92    samplerate = s->input_samplerate;
[4db8eab]93    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
[c8f411b]94  }
95  s->samplerate = samplerate;
[18c6b20]96  /* compute input block size required before resampling */
[87dfc3f]97  s->ratio = s->samplerate/(float)s->input_samplerate;
[18c6b20]98  s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
99
100  if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
101    AUBIO_ERR("Not able to process more than %d frames of %d channels\n",
102        MAX_SAMPLES / s->input_channels, s->input_channels);
103    goto beach;
104  }
[87dfc3f]105
106#ifdef HAVE_SAMPLERATE
[18c6b20]107  s->resampler = NULL;
108  s->input_data = NULL;
109  if (s->ratio != 1) {
110    s->input_data = new_fvec(s->input_hop_size);
[6fd8d7e]111    s->resampler = new_aubio_resampler(s->ratio, 4);
[d22cc42]112    if (s->ratio > 1) {
113      // we would need to add a ring buffer for these
114      if ( (uint_t)(s->input_hop_size * s->ratio + .5)  != s->hop_size ) {
115        AUBIO_ERR("can not upsample from %d to %d\n", s->input_samplerate, s->samplerate);
116        goto beach;
117      }
[18c6b20]118      AUBIO_WRN("upsampling %s from %d to % d\n", s->path, s->input_samplerate, s->samplerate);
[87dfc3f]119    }
[18c6b20]120  }
[87dfc3f]121#else
[18c6b20]122  if (s->ratio != 1) {
[87dfc3f]123    AUBIO_ERR("aubio was compiled without aubio_resampler\n");
[18c6b20]124    goto beach;
[afbd7e7]125  }
[18c6b20]126#endif /* HAVE_SAMPLERATE */
[87dfc3f]127
[afbd7e7]128  /* allocate data for de/interleaving reallocated when needed. */
[18c6b20]129  s->scratch_size = s->input_hop_size * s->input_channels;
[af86999]130  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
[afbd7e7]131
132  return s;
[18c6b20]133
134beach:
[d22cc42]135  AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
[18c6b20]136      s->path, s->samplerate, s->hop_size);
137  del_aubio_source_sndfile(s);
138  return NULL;
[afbd7e7]139}
140
141void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
[18c6b20]142  uint_t i,j, input_channels = s->input_channels;
[afbd7e7]143  /* do actual reading */
[18c6b20]144  sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
[afbd7e7]145
[87dfc3f]146  smpl_t *data;
147
148#ifdef HAVE_SAMPLERATE
[18c6b20]149  if (s->ratio != 1) {
150    data = s->input_data->data;
[87dfc3f]151  } else
152#endif /* HAVE_SAMPLERATE */
153  {
154    data = read_data->data;
155  }
[afbd7e7]156
157  /* de-interleaving and down-mixing data  */
[18c6b20]158  for (j = 0; j < read_samples / input_channels; j++) {
[87dfc3f]159    data[j] = 0;
[afbd7e7]160    for (i = 0; i < input_channels; i++) {
[87dfc3f]161      data[j] += (smpl_t)s->scratch_data[input_channels*j+i];
[afbd7e7]162    }
[87dfc3f]163    data[j] /= (smpl_t)input_channels;
164  }
165
166#ifdef HAVE_SAMPLERATE
167  if (s->resampler) {
[18c6b20]168    aubio_resampler_do(s->resampler, s->input_data, read_data);
[afbd7e7]169  }
[87dfc3f]170#endif /* HAVE_SAMPLERATE */
171
[18c6b20]172  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
[afbd7e7]173}
174
[063fa5a]175uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
176  return s->samplerate;
177}
178
[afbd7e7]179void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
[420962e]180  if (!s) return;
[afbd7e7]181  if (sf_close(s->handle)) {
182    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
183  }
[87dfc3f]184#ifdef HAVE_SAMPLERATE
[18c6b20]185  if (s->resampler != NULL) {
[87dfc3f]186    del_aubio_resampler(s->resampler);
187  }
[18c6b20]188  if (s->input_data) {
189    del_fvec(s->input_data);
[87dfc3f]190  }
191#endif /* HAVE_SAMPLERATE */
[afbd7e7]192  AUBIO_FREE(s->scratch_data);
193  AUBIO_FREE(s);
194}
195
196#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.