*/
-
-#include "config.h"
+#include "aubio_priv.h"
#ifdef HAVE_SNDFILE
#include <sndfile.h>
-#include "aubio_priv.h"
#include "fvec.h"
#include "fmat.h"
#include "source_sndfile.h"
#include "temporal/resampler.h"
-#define MAX_CHANNELS 6
#define MAX_SIZE 4096
-#define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE
+#define MAX_SAMPLES AUBIO_MAX_CHANNELS * MAX_SIZE
#if !HAVE_AUBIO_DOUBLE
#define aubio_sf_read_smpl sf_read_float
int input_samplerate;
int input_channels;
int input_format;
+ int duration;
// resampling stuff
smpl_t ratio;
uint_t input_hop_size;
#ifdef HAVE_SAMPLERATE
- aubio_resampler_t *resampler;
+ aubio_resampler_t **resamplers;
fvec_t *input_data;
+ fmat_t *input_mat;
#endif /* HAVE_SAMPLERATE */
// some temporary memory for sndfile to write at
s->hop_size = hop_size;
s->channels = 1;
- if (s->path) AUBIO_FREE(s->path);
- s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX));
- strncpy(s->path, path, strnlen(path, PATH_MAX));
+ s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
+ strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
// try opening the file, getting the info in sfinfo
AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
if (s->handle == NULL) {
/* show libsndfile err msg */
- AUBIO_ERR("source_sndfile: Failed opening %s: %s\n", s->path, sf_strerror (NULL));
+ AUBIO_ERR("source_sndfile: Failed opening %s (%s)\n", s->path,
+ sf_strerror (NULL));
goto beach;
}
s->input_samplerate = sfinfo.samplerate;
s->input_channels = sfinfo.channels;
s->input_format = sfinfo.format;
+ s->duration = sfinfo.frames;
if (samplerate == 0) {
s->samplerate = s->input_samplerate;
}
#ifdef HAVE_SAMPLERATE
- s->resampler = NULL;
s->input_data = NULL;
+ s->input_mat = NULL;
+ s->resamplers = NULL;
if (s->ratio != 1) {
+ uint_t i;
+ s->resamplers = AUBIO_ARRAY(aubio_resampler_t*, s->input_channels);
s->input_data = new_fvec(s->input_hop_size);
- s->resampler = new_aubio_resampler(s->ratio, 4);
+ s->input_mat = new_fmat(s->input_channels, s->input_hop_size);
+ for (i = 0; i < (uint_t)s->input_channels; i++) {
+ s->resamplers[i] = new_aubio_resampler(s->ratio, 4);
+ }
if (s->ratio > 1) {
// we would need to add a ring buffer for these
- if ( (uint_t)(s->input_hop_size * s->ratio + .5) != s->hop_size ) {
+ if ( (uint_t)FLOOR(s->input_hop_size * s->ratio + .5) != s->hop_size ) {
AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path,
s->input_samplerate, s->samplerate);
goto beach;
AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
s->input_samplerate, s->samplerate);
}
+ s->duration = (uint_t)FLOOR(s->duration * s->ratio);
}
#else
if (s->ratio != 1) {
}
#ifdef HAVE_SAMPLERATE
- if (s->resampler) {
- aubio_resampler_do(s->resampler, s->input_data, read_data);
+ if (s->resamplers) {
+ aubio_resampler_do(s->resamplers[0], s->input_data, read_data);
}
#endif /* HAVE_SAMPLERATE */
smpl_t **ptr_data;
#ifdef HAVE_SAMPLERATE
if (s->ratio != 1) {
- AUBIO_ERR("source_sndfile: no multi channel resampling yet\n");
- return;
- //ptr_data = s->input_data->data;
+ ptr_data = s->input_mat->data;
} else
#endif /* HAVE_SAMPLERATE */
{
}
#ifdef HAVE_SAMPLERATE
- if (s->resampler) {
- //aubio_resampler_do(s->resampler, s->input_data, read_data);
+ if (s->resamplers) {
+ for (i = 0; i < input_channels; i++) {
+ fvec_t input_chan, read_chan;
+ input_chan.data = s->input_mat->data[i];
+ input_chan.length = s->input_mat->length;
+ read_chan.data = read_data->data[i];
+ read_chan.length = read_data->length;
+ aubio_resampler_do(s->resamplers[i], &input_chan, &read_chan);
+ }
}
#endif /* HAVE_SAMPLERATE */
return s->input_channels;
}
+uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) {
+ if (s && s->duration) {
+ return s->duration;
+ }
+ return 0;
+}
+
uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
- sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
+ sf_count_t sf_ret;
+ if (s->handle == NULL) {
+ AUBIO_ERR("source_sndfile: failed seeking in %s (file not opened?)\n",
+ s->path);
+ return AUBIO_FAIL;
+ }
+ if ((sint_t)pos < 0) {
+ AUBIO_ERR("source_sndfile: could not seek %s at %d (seeking position"
+ " should be >= 0)\n", s->path, pos);
+ return AUBIO_FAIL;
+ }
+ sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
if (sf_ret == -1) {
AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
return AUBIO_FAIL;
uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
if (!s->handle) {
- return AUBIO_FAIL;
+ return AUBIO_OK;
}
if(sf_close(s->handle)) {
AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
return AUBIO_FAIL;
}
+ s->handle = NULL;
return AUBIO_OK;
}
void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
- if (!s) return;
+ AUBIO_ASSERT(s);
aubio_source_sndfile_close(s);
#ifdef HAVE_SAMPLERATE
- if (s->resampler != NULL) {
- del_aubio_resampler(s->resampler);
+ if (s->resamplers != NULL) {
+ uint_t i = 0, input_channels = s->input_channels;
+ for (i = 0; i < input_channels; i ++) {
+ if (s->resamplers[i] != NULL) {
+ del_aubio_resampler(s->resamplers[i]);
+ }
+ }
+ AUBIO_FREE(s->resamplers);
}
if (s->input_data) {
del_fvec(s->input_data);
}
+ if (s->input_mat) {
+ del_fmat(s->input_mat);
+ }
#endif /* HAVE_SAMPLERATE */
if (s->path) AUBIO_FREE(s->path);
AUBIO_FREE(s->scratch_data);