source: src/io/source_sndfile.c @ b643a33

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

src/io/*.c: take a copy of const char* path

  • Property mode set to 100644
File size: 9.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
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
57  // resampling stuff
58  smpl_t ratio;
59  uint_t input_hop_size;
60#ifdef HAVE_SAMPLERATE
61  aubio_resampler_t *resampler;
62  fvec_t *input_data;
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  if (s->path) AUBIO_FREE(s->path);
91  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX));
92  strncpy(s->path, path, strnlen(path, PATH_MAX));
93
94  // try opening the file, getting the info in sfinfo
95  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
96  s->handle = sf_open (s->path, SFM_READ, &sfinfo);
97
98  if (s->handle == NULL) {
99    /* show libsndfile err msg */
100    AUBIO_ERR("source_sndfile: Failed opening %s: %s\n", s->path, 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
109  if (samplerate == 0) {
110    samplerate = s->input_samplerate;
111    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
112  }
113  s->samplerate = samplerate;
114  /* compute input block size required before resampling */
115  s->ratio = s->samplerate/(smpl_t)s->input_samplerate;
116  s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
117
118  if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
119    AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n",
120        MAX_SAMPLES / s->input_channels, s->input_channels);
121    goto beach;
122  }
123
124#ifdef HAVE_SAMPLERATE
125  s->resampler = NULL;
126  s->input_data = NULL;
127  if (s->ratio != 1) {
128    s->input_data = new_fvec(s->input_hop_size);
129    s->resampler = new_aubio_resampler(s->ratio, 4);
130    if (s->ratio > 1) {
131      // we would need to add a ring buffer for these
132      if ( (uint_t)(s->input_hop_size * s->ratio + .5)  != s->hop_size ) {
133        AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path,
134            s->input_samplerate, s->samplerate);
135        goto beach;
136      }
137      AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
138          s->input_samplerate, s->samplerate);
139    }
140  }
141#else
142  if (s->ratio != 1) {
143    AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
144    goto beach;
145  }
146#endif /* HAVE_SAMPLERATE */
147
148  /* allocate data for de/interleaving reallocated when needed. */
149  s->scratch_size = s->input_hop_size * s->input_channels;
150  s->scratch_data = AUBIO_ARRAY(smpl_t, s->scratch_size);
151
152  return s;
153
154beach:
155  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
156  //    s->path, s->samplerate, s->hop_size);
157  del_aubio_source_sndfile(s);
158  return NULL;
159}
160
161void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
162  uint_t i,j, input_channels = s->input_channels;
163  /* read from file into scratch_data */
164  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
165
166  /* where to store de-interleaved data */
167  smpl_t *ptr_data;
168#ifdef HAVE_SAMPLERATE
169  if (s->ratio != 1) {
170    ptr_data = s->input_data->data;
171  } else
172#endif /* HAVE_SAMPLERATE */
173  {
174    ptr_data = read_data->data;
175  }
176
177  /* de-interleaving and down-mixing data  */
178  for (j = 0; j < read_samples / input_channels; j++) {
179    ptr_data[j] = 0;
180    for (i = 0; i < input_channels; i++) {
181      ptr_data[j] += s->scratch_data[input_channels*j+i];
182    }
183    ptr_data[j] /= (smpl_t)input_channels;
184  }
185
186#ifdef HAVE_SAMPLERATE
187  if (s->resampler) {
188    aubio_resampler_do(s->resampler, s->input_data, read_data);
189  }
190#endif /* HAVE_SAMPLERATE */
191
192  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
193
194  if (*read < s->hop_size) {
195    for (j = *read; j < s->hop_size; j++) {
196      read_data->data[j] = 0;
197    }
198  }
199
200}
201
202void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
203  uint_t i,j, input_channels = s->input_channels;
204  /* do actual reading */
205  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
206
207  /* where to store de-interleaved data */
208  smpl_t **ptr_data;
209#ifdef HAVE_SAMPLERATE
210  if (s->ratio != 1) {
211    AUBIO_ERR("source_sndfile: no multi channel resampling yet\n");
212    return;
213    //ptr_data = s->input_data->data;
214  } else
215#endif /* HAVE_SAMPLERATE */
216  {
217    ptr_data = read_data->data;
218  }
219
220  if (read_data->height < input_channels) {
221    // destination matrix has less channels than the file; copy only first
222    // channels of the file, de-interleaving data
223    for (j = 0; j < read_samples / input_channels; j++) {
224      for (i = 0; i < read_data->height; i++) {
225        ptr_data[i][j] = s->scratch_data[j * input_channels + i];
226      }
227    }
228  } else {
229    // destination matrix has as many or more channels than the file; copy each
230    // channel from the file to the destination matrix, de-interleaving data
231    for (j = 0; j < read_samples / input_channels; j++) {
232      for (i = 0; i < input_channels; i++) {
233        ptr_data[i][j] = s->scratch_data[j * input_channels + i];
234      }
235    }
236  }
237
238  if (read_data->height > input_channels) {
239    // destination matrix has more channels than the file; copy last channel
240    // of the file to each additional channels, de-interleaving data
241    for (j = 0; j < read_samples / input_channels; j++) {
242      for (i = input_channels; i < read_data->height; i++) {
243        ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
244      }
245    }
246  }
247
248#ifdef HAVE_SAMPLERATE
249  if (s->resampler) {
250    //aubio_resampler_do(s->resampler, s->input_data, read_data);
251  }
252#endif /* HAVE_SAMPLERATE */
253
254  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
255
256  if (*read < s->hop_size) {
257    for (i = 0; i < read_data->height; i++) {
258      for (j = *read; j < s->hop_size; j++) {
259        read_data->data[i][j] = 0.;
260      }
261    }
262  }
263
264}
265
266uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
267  return s->samplerate;
268}
269
270uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
271  return s->input_channels;
272}
273
274uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
275  uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
276  sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
277  if (sf_ret == -1) {
278    AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
279    return AUBIO_FAIL;
280  }
281  if (sf_ret != resampled_pos) {
282    AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n",
283        s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL));
284    return AUBIO_FAIL;
285  }
286  return AUBIO_OK;
287}
288
289uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
290  if (!s->handle) {
291    return AUBIO_FAIL;
292  }
293  if(sf_close(s->handle)) {
294    AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
295    return AUBIO_FAIL;
296  }
297  return AUBIO_OK;
298}
299
300void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
301  if (!s) return;
302  aubio_source_sndfile_close(s);
303#ifdef HAVE_SAMPLERATE
304  if (s->resampler != NULL) {
305    del_aubio_resampler(s->resampler);
306  }
307  if (s->input_data) {
308    del_fvec(s->input_data);
309  }
310#endif /* HAVE_SAMPLERATE */
311  if (s->path) AUBIO_FREE(s->path);
312  AUBIO_FREE(s->scratch_data);
313  AUBIO_FREE(s);
314}
315
316#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.