source: src/io/source_sndfile.c @ 5958d1e

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

src/io/source_sndfile.c: also check parameters here, synchronise tests

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