source: src/io/source_sndfile.c @ 857f8871

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

src/io/source_sndfile.h: add _get_duration

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