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);
69 AUBIO_ERR("source_sndfile: Aborted opening null path\n");
72 if ((sint_t)samplerate < 0) {
73 AUBIO_ERR("source_sndfile: Can not open %s with samplerate %d\n", path, samplerate);
76 if ((sint_t)hop_size <= 0) {
77 AUBIO_ERR("source_sndfile: Can not open %s with hop_size %d\n", path, hop_size);
81 s->hop_size = hop_size;
85 // try opening the file, getting the info in sfinfo
86 AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
87 s->handle = sf_open (s->path, SFM_READ, &sfinfo);
89 if (s->handle == NULL) {
90 /* show libsndfile err msg */
91 AUBIO_ERR("source_sndfile: Failed opening %s: %s\n", s->path, sf_strerror (NULL));
96 s->input_samplerate = sfinfo.samplerate;
97 s->input_channels = sfinfo.channels;
98 s->input_format = sfinfo.format;
100 if (samplerate == 0) {
101 samplerate = s->input_samplerate;
102 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
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);
109 if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
110 AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n",
111 MAX_SAMPLES / s->input_channels, s->input_channels);
115 #ifdef HAVE_SAMPLERATE
117 s->input_data = NULL;
119 s->input_data = new_fvec(s->input_hop_size);
120 s->resampler = new_aubio_resampler(s->ratio, 4);
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("source_sndfile: can not upsample %s from %d to %d\n", s->path,
125 s->input_samplerate, s->samplerate);
128 AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
129 s->input_samplerate, s->samplerate);
134 AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
137 #endif /* HAVE_SAMPLERATE */
139 /* allocate data for de/interleaving reallocated when needed. */
140 s->scratch_size = s->input_hop_size * s->input_channels;
141 s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
146 //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
147 // s->path, s->samplerate, s->hop_size);
148 del_aubio_source_sndfile(s);
152 void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
153 uint_t i,j, input_channels = s->input_channels;
154 /* read from file into scratch_data */
155 sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
157 /* where to store de-interleaved data */
159 #ifdef HAVE_SAMPLERATE
161 ptr_data = s->input_data->data;
163 #endif /* HAVE_SAMPLERATE */
165 ptr_data = read_data->data;
168 /* de-interleaving and down-mixing data */
169 for (j = 0; j < read_samples / input_channels; j++) {
171 for (i = 0; i < input_channels; i++) {
172 ptr_data[j] += s->scratch_data[input_channels*j+i];
174 ptr_data[j] /= (smpl_t)input_channels;
177 #ifdef HAVE_SAMPLERATE
179 aubio_resampler_do(s->resampler, s->input_data, read_data);
181 #endif /* HAVE_SAMPLERATE */
183 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
185 if (*read < s->hop_size) {
186 for (j = *read; j < s->hop_size; j++) {
187 read_data->data[j] = 0;
193 void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
194 uint_t i,j, input_channels = s->input_channels;
195 /* do actual reading */
196 sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
198 /* where to store de-interleaved data */
200 #ifdef HAVE_SAMPLERATE
202 AUBIO_ERR("source_sndfile: no multi channel resampling yet");
204 //ptr_data = s->input_data->data;
206 #endif /* HAVE_SAMPLERATE */
208 ptr_data = read_data->data;
211 if (read_data->height < input_channels) {
212 // destination matrix has less channels than the file; copy only first
213 // channels of the file, de-interleaving data
214 for (j = 0; j < read_samples / input_channels; j++) {
215 for (i = 0; i < read_data->height; i++) {
216 ptr_data[i][j] = (smpl_t)s->scratch_data[j * input_channels + i];
220 // destination matrix has as many or more channels than the file; copy each
221 // channel from the file to the destination matrix, de-interleaving data
222 for (j = 0; j < read_samples / input_channels; j++) {
223 for (i = 0; i < input_channels; i++) {
224 ptr_data[i][j] = (smpl_t)s->scratch_data[j * input_channels + i];
229 if (read_data->height > input_channels) {
230 // destination matrix has more channels than the file; copy last channel
231 // of the file to each additional channels, de-interleaving data
232 for (j = 0; j < read_samples / input_channels; j++) {
233 for (i = input_channels; i < read_data->height; i++) {
234 ptr_data[i][j] = (smpl_t)s->scratch_data[j * input_channels + (input_channels - 1)];
239 #ifdef HAVE_SAMPLERATE
241 //aubio_resampler_do(s->resampler, s->input_data, read_data);
243 #endif /* HAVE_SAMPLERATE */
245 *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
247 if (*read < s->hop_size) {
248 for (i = 0; i < read_data->height; i++) {
249 for (j = *read; j < s->hop_size; j++) {
250 read_data->data[i][j] = 0.;
257 uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
258 return s->samplerate;
261 uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
262 return s->input_channels;
265 uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
266 uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
267 return sf_seek (s->handle, resampled_pos, SEEK_SET);
270 uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
274 if(sf_close(s->handle)) {
275 AUBIO_ERR("source_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
281 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
283 aubio_source_sndfile_close(s);
284 #ifdef HAVE_SAMPLERATE
285 if (s->resampler != NULL) {
286 del_aubio_resampler(s->resampler);
289 del_fvec(s->input_data);
291 #endif /* HAVE_SAMPLERATE */
292 AUBIO_FREE(s->scratch_data);
296 #endif /* HAVE_SNDFILE */