2 Copyright (C) 2012 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
29 #include "source_sndfile.h"
31 #include "temporal/resampler.h"
34 #define MAX_SAMPLES AUBIO_MAX_CHANNELS * MAX_SIZE
36 #if !HAVE_AUBIO_DOUBLE
37 #define aubio_sf_read_smpl sf_read_float
38 #else /* HAVE_AUBIO_DOUBLE */
39 #define aubio_sf_read_smpl sf_read_double
40 #endif /* HAVE_AUBIO_DOUBLE */
42 struct _aubio_source_sndfile_t {
47 // some data about the file
57 uint_t input_hop_size;
58 #ifdef HAVE_SAMPLERATE
59 aubio_resampler_t **resamplers;
62 #endif /* HAVE_SAMPLERATE */
64 // some temporary memory for sndfile to write at
69 aubio_source_sndfile_t * new_aubio_source_sndfile(const char_t * path, uint_t samplerate, uint_t hop_size) {
70 aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
74 AUBIO_ERR("source_sndfile: Aborted opening null path\n");
77 if ((sint_t)samplerate < 0) {
78 AUBIO_ERR("source_sndfile: Can not open %s with samplerate %d\n", path, samplerate);
81 if ((sint_t)hop_size <= 0) {
82 AUBIO_ERR("source_sndfile: Can not open %s with hop_size %d\n", path, hop_size);
86 s->hop_size = hop_size;
89 if (s->path) AUBIO_FREE(s->path);
90 s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
91 strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
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);
97 if (s->handle == NULL) {
98 /* show libsndfile err msg */
99 AUBIO_ERR("source_sndfile: Failed opening %s (%s)\n", s->path,
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;
110 if (samplerate == 0) {
111 s->samplerate = s->input_samplerate;
112 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
114 s->samplerate = samplerate;
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);
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);
126 #ifdef HAVE_SAMPLERATE
127 s->input_data = NULL;
129 s->resamplers = NULL;
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);
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);
145 AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
146 s->input_samplerate, s->samplerate);
148 s->duration = (uint_t)FLOOR(s->duration * s->ratio);
152 AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
155 #endif /* HAVE_SAMPLERATE */
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);
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);
170 void 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 sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
175 /* where to store de-interleaved data */
177 #ifdef HAVE_SAMPLERATE
179 ptr_data = s->input_data->data;
181 #endif /* HAVE_SAMPLERATE */
183 ptr_data = read_data->data;
186 /* de-interleaving and down-mixing data */
187 for (j = 0; j < read_samples / input_channels; j++) {
189 for (i = 0; i < input_channels; i++) {
190 ptr_data[j] += s->scratch_data[input_channels*j+i];
192 ptr_data[j] /= (smpl_t)input_channels;
195 #ifdef HAVE_SAMPLERATE
197 aubio_resampler_do(s->resamplers[0], s->input_data, read_data);
199 #endif /* HAVE_SAMPLERATE */
201 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
203 if (*read < s->hop_size) {
204 for (j = *read; j < s->hop_size; j++) {
205 read_data->data[j] = 0;
211 void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
212 uint_t i,j, input_channels = s->input_channels;
213 /* do actual reading */
214 sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
216 /* where to store de-interleaved data */
218 #ifdef HAVE_SAMPLERATE
220 ptr_data = s->input_mat->data;
222 #endif /* HAVE_SAMPLERATE */
224 ptr_data = read_data->data;
227 if (read_data->height < input_channels) {
228 // destination matrix has less channels than the file; copy only first
229 // channels of the file, de-interleaving data
230 for (j = 0; j < read_samples / input_channels; j++) {
231 for (i = 0; i < read_data->height; i++) {
232 ptr_data[i][j] = s->scratch_data[j * input_channels + i];
236 // destination matrix has as many or more channels than the file; copy each
237 // channel from the file to the destination matrix, de-interleaving data
238 for (j = 0; j < read_samples / input_channels; j++) {
239 for (i = 0; i < input_channels; i++) {
240 ptr_data[i][j] = s->scratch_data[j * input_channels + i];
245 if (read_data->height > input_channels) {
246 // destination matrix has more channels than the file; copy last channel
247 // of the file to each additional channels, de-interleaving data
248 for (j = 0; j < read_samples / input_channels; j++) {
249 for (i = input_channels; i < read_data->height; i++) {
250 ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
255 #ifdef HAVE_SAMPLERATE
257 for (i = 0; i < input_channels; i++) {
258 fvec_t input_chan, read_chan;
259 input_chan.data = s->input_mat->data[i];
260 input_chan.length = s->input_mat->length;
261 read_chan.data = read_data->data[i];
262 read_chan.length = read_data->length;
263 aubio_resampler_do(s->resamplers[i], &input_chan, &read_chan);
266 #endif /* HAVE_SAMPLERATE */
268 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
270 if (*read < s->hop_size) {
271 for (i = 0; i < read_data->height; i++) {
272 for (j = *read; j < s->hop_size; j++) {
273 read_data->data[i][j] = 0.;
280 uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
281 return s->samplerate;
284 uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
285 return s->input_channels;
288 uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) {
289 if (s && s->duration) {
295 uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
296 uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
297 sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
298 if (s->handle == NULL) {
299 AUBIO_ERR("source_sndfile: failed seeking in %s (file not opened?)\n",
304 AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
307 if (sf_ret != resampled_pos) {
308 AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n",
309 s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL));
315 uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
319 if(sf_close(s->handle)) {
320 AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
327 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
329 aubio_source_sndfile_close(s);
330 #ifdef HAVE_SAMPLERATE
331 if (s->resamplers != NULL) {
332 uint_t i = 0, input_channels = s->input_channels;
333 for (i = 0; i < input_channels; i ++) {
334 if (s->resamplers[i] != NULL) {
335 del_aubio_resampler(s->resamplers[i]);
338 AUBIO_FREE(s->resamplers);
341 del_fvec(s->input_data);
344 del_fmat(s->input_mat);
346 #endif /* HAVE_SAMPLERATE */
347 if (s->path) AUBIO_FREE(s->path);
348 AUBIO_FREE(s->scratch_data);
352 #endif /* HAVE_SNDFILE */