source: src/io/source_sndfile.c @ e136c9f

feature/autosinkfeature/constantqfeature/pitchshiftfeature/timestretch
Last change on this file since e136c9f was e136c9f, checked in by Paul Brossier <piem@piem.org>, 20 months ago

[source_sndfile] avoid declaration after statement

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