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 #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 */
45 struct _aubio_source_sndfile_t {
50 // some data about the file
59 uint_t input_hop_size;
60 #ifdef HAVE_SAMPLERATE
61 aubio_resampler_t *resampler;
63 #endif /* HAVE_SAMPLERATE */
65 // some temporary memory for sndfile to write at
70 aubio_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);
75 AUBIO_ERR("source_sndfile: Aborted opening null path\n");
78 if ((sint_t)samplerate < 0) {
79 AUBIO_ERR("source_sndfile: Can not open %s with samplerate %d\n", path, samplerate);
82 if ((sint_t)hop_size <= 0) {
83 AUBIO_ERR("source_sndfile: Can not open %s with hop_size %d\n", path, hop_size);
87 s->hop_size = hop_size;
91 // try opening the file, getting the info in sfinfo
92 AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
93 s->handle = sf_open (s->path, SFM_READ, &sfinfo);
95 if (s->handle == NULL) {
96 /* show libsndfile err msg */
97 AUBIO_ERR("source_sndfile: Failed opening %s: %s\n", s->path, sf_strerror (NULL));
101 /* get input specs */
102 s->input_samplerate = sfinfo.samplerate;
103 s->input_channels = sfinfo.channels;
104 s->input_format = sfinfo.format;
106 if (samplerate == 0) {
107 samplerate = s->input_samplerate;
108 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
110 s->samplerate = samplerate;
111 /* compute input block size required before resampling */
112 s->ratio = s->samplerate/(smpl_t)s->input_samplerate;
113 s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
115 if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
116 AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n",
117 MAX_SAMPLES / s->input_channels, s->input_channels);
121 #ifdef HAVE_SAMPLERATE
123 s->input_data = NULL;
125 s->input_data = new_fvec(s->input_hop_size);
126 s->resampler = new_aubio_resampler(s->ratio, 4);
128 // we would need to add a ring buffer for these
129 if ( (uint_t)(s->input_hop_size * s->ratio + .5) != s->hop_size ) {
130 AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path,
131 s->input_samplerate, s->samplerate);
134 AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
135 s->input_samplerate, s->samplerate);
140 AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
143 #endif /* HAVE_SAMPLERATE */
145 /* allocate data for de/interleaving reallocated when needed. */
146 s->scratch_size = s->input_hop_size * s->input_channels;
147 s->scratch_data = AUBIO_ARRAY(smpl_t, s->scratch_size);
152 //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
153 // s->path, s->samplerate, s->hop_size);
154 del_aubio_source_sndfile(s);
158 void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
159 uint_t i,j, input_channels = s->input_channels;
160 /* read from file into scratch_data */
161 sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
163 /* where to store de-interleaved data */
165 #ifdef HAVE_SAMPLERATE
167 ptr_data = s->input_data->data;
169 #endif /* HAVE_SAMPLERATE */
171 ptr_data = read_data->data;
174 /* de-interleaving and down-mixing data */
175 for (j = 0; j < read_samples / input_channels; j++) {
177 for (i = 0; i < input_channels; i++) {
178 ptr_data[j] += s->scratch_data[input_channels*j+i];
180 ptr_data[j] /= (smpl_t)input_channels;
183 #ifdef HAVE_SAMPLERATE
185 aubio_resampler_do(s->resampler, s->input_data, read_data);
187 #endif /* HAVE_SAMPLERATE */
189 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
191 if (*read < s->hop_size) {
192 for (j = *read; j < s->hop_size; j++) {
193 read_data->data[j] = 0;
199 void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
200 uint_t i,j, input_channels = s->input_channels;
201 /* do actual reading */
202 sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
204 /* where to store de-interleaved data */
206 #ifdef HAVE_SAMPLERATE
208 AUBIO_ERR("source_sndfile: no multi channel resampling yet\n");
210 //ptr_data = s->input_data->data;
212 #endif /* HAVE_SAMPLERATE */
214 ptr_data = read_data->data;
217 if (read_data->height < input_channels) {
218 // destination matrix has less channels than the file; copy only first
219 // channels of the file, de-interleaving data
220 for (j = 0; j < read_samples / input_channels; j++) {
221 for (i = 0; i < read_data->height; i++) {
222 ptr_data[i][j] = s->scratch_data[j * input_channels + i];
226 // destination matrix has as many or more channels than the file; copy each
227 // channel from the file to the destination matrix, de-interleaving data
228 for (j = 0; j < read_samples / input_channels; j++) {
229 for (i = 0; i < input_channels; i++) {
230 ptr_data[i][j] = s->scratch_data[j * input_channels + i];
235 if (read_data->height > input_channels) {
236 // destination matrix has more channels than the file; copy last channel
237 // of the file to each additional channels, de-interleaving data
238 for (j = 0; j < read_samples / input_channels; j++) {
239 for (i = input_channels; i < read_data->height; i++) {
240 ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
245 #ifdef HAVE_SAMPLERATE
247 //aubio_resampler_do(s->resampler, s->input_data, read_data);
249 #endif /* HAVE_SAMPLERATE */
251 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
253 if (*read < s->hop_size) {
254 for (i = 0; i < read_data->height; i++) {
255 for (j = *read; j < s->hop_size; j++) {
256 read_data->data[i][j] = 0.;
263 uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
264 return s->samplerate;
267 uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
268 return s->input_channels;
271 uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
272 uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
273 sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
275 AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
278 if (sf_ret != resampled_pos) {
279 AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n",
280 s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL));
286 uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
290 if(sf_close(s->handle)) {
291 AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
297 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
299 aubio_source_sndfile_close(s);
300 #ifdef HAVE_SAMPLERATE
301 if (s->resampler != NULL) {
302 del_aubio_resampler(s->resampler);
305 del_fvec(s->input_data);
307 #endif /* HAVE_SAMPLERATE */
308 AUBIO_FREE(s->scratch_data);
312 #endif /* HAVE_SNDFILE */