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/>.
28 #include "aubio_priv.h"
31 #include "source_sndfile.h"
33 #include "temporal/resampler.h"
35 #define MAX_CHANNELS 6
37 #define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE
39 struct _aubio_source_sndfile_t {
44 // some data about the file
53 uint_t input_hop_size;
54 #ifdef HAVE_SAMPLERATE
55 aubio_resampler_t *resampler;
57 #endif /* HAVE_SAMPLERATE */
59 // some temporary memory for sndfile to write at
64 aubio_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);
68 AUBIO_ERR("Aborted opening null path\n");
72 s->hop_size = hop_size;
76 // try opening the file, getting the info in sfinfo
78 AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
79 s->handle = sf_open (s->path, SFM_READ, &sfinfo);
81 if (s->handle == NULL) {
82 /* show libsndfile err msg */
83 AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL));
88 s->input_samplerate = sfinfo.samplerate;
89 s->input_channels = sfinfo.channels;
90 s->input_format = sfinfo.format;
92 if (samplerate == 0) {
93 samplerate = s->input_samplerate;
94 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
96 s->samplerate = samplerate;
97 /* compute input block size required before resampling */
98 s->ratio = s->samplerate/(float)s->input_samplerate;
99 s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
101 if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
102 AUBIO_ERR("Not able to process more than %d frames of %d channels\n",
103 MAX_SAMPLES / s->input_channels, s->input_channels);
107 #ifdef HAVE_SAMPLERATE
109 s->input_data = NULL;
111 s->input_data = new_fvec(s->input_hop_size);
112 s->resampler = new_aubio_resampler(s->ratio, 4);
114 // we would need to add a ring buffer for these
115 if ( (uint_t)(s->input_hop_size * s->ratio + .5) != s->hop_size ) {
116 AUBIO_ERR("can not upsample from %d to %d\n", s->input_samplerate, s->samplerate);
119 AUBIO_WRN("upsampling %s from %d to % d\n", s->path, s->input_samplerate, s->samplerate);
124 AUBIO_ERR("aubio was compiled without aubio_resampler\n");
127 #endif /* HAVE_SAMPLERATE */
129 /* allocate data for de/interleaving reallocated when needed. */
130 s->scratch_size = s->input_hop_size * s->input_channels;
131 s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
136 AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
137 s->path, s->samplerate, s->hop_size);
138 del_aubio_source_sndfile(s);
142 void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
143 uint_t i,j, input_channels = s->input_channels;
144 /* do actual reading */
145 sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
149 #ifdef HAVE_SAMPLERATE
151 data = s->input_data->data;
153 #endif /* HAVE_SAMPLERATE */
155 data = read_data->data;
158 /* de-interleaving and down-mixing data */
159 for (j = 0; j < read_samples / input_channels; j++) {
161 for (i = 0; i < input_channels; i++) {
162 data[j] += s->scratch_data[input_channels*j+i];
164 data[j] /= (smpl_t)input_channels;
167 #ifdef HAVE_SAMPLERATE
169 aubio_resampler_do(s->resampler, s->input_data, read_data);
171 #endif /* HAVE_SAMPLERATE */
173 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
175 if (*read < s->hop_size) {
176 for (j = *read; j < s->hop_size; j++) {
183 void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
184 uint_t i,j, input_channels = s->input_channels;
185 /* do actual reading */
186 sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
190 #ifdef HAVE_SAMPLERATE
192 AUBIO_ERR("source_sndfile: no multi channel resampling yet");
194 //data = s->input_data->data;
196 #endif /* HAVE_SAMPLERATE */
198 data = read_data->data;
201 if (read_data->height < input_channels) {
202 // destination matrix has less channels than the file; copy only first
203 // channels of the file, de-interleaving data
204 for (j = 0; j < read_samples / input_channels; j++) {
205 for (i = 0; i < read_data->height; i++) {
206 data[i][j] = (smpl_t)s->scratch_data[j * input_channels + i];
210 // destination matrix has as many or more channels than the file; copy each
211 // channel from the file to the destination matrix, de-interleaving data
212 for (j = 0; j < read_samples / input_channels; j++) {
213 for (i = 0; i < input_channels; i++) {
214 data[i][j] = (smpl_t)s->scratch_data[j * input_channels + i];
219 if (read_data->height > input_channels) {
220 // destination matrix has more channels than the file; copy last channel
221 // of the file to each additional channels, de-interleaving data
222 for (j = 0; j < read_samples / input_channels; j++) {
223 for (i = input_channels; i < read_data->height; i++) {
224 data[i][j] = (smpl_t)s->scratch_data[j * input_channels + (input_channels - 1)];
229 #ifdef HAVE_SAMPLERATE
231 //aubio_resampler_do(s->resampler, s->input_data, read_data);
233 #endif /* HAVE_SAMPLERATE */
235 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
237 if (*read < s->hop_size) {
238 for (i = 0; i < read_data->height; i++) {
239 for (j = *read; j < s->hop_size; j++) {
247 uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
248 return s->samplerate;
251 uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
252 return s->input_channels;
255 uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
256 uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate);
257 return sf_seek (s->handle, resampled_pos, SEEK_SET);
260 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
262 if (sf_close(s->handle)) {
263 AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
265 #ifdef HAVE_SAMPLERATE
266 if (s->resampler != NULL) {
267 del_aubio_resampler(s->resampler);
270 del_fvec(s->input_data);
272 #endif /* HAVE_SAMPLERATE */
273 AUBIO_FREE(s->scratch_data);
277 #endif /* HAVE_SNDFILE */