/*
- Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+ Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
This file is part of aubio.
#include <sndfile.h>
#include "aubio_priv.h"
-#include "sink_sndfile.h"
#include "fvec.h"
+#include "fmat.h"
+#include "io/sink_sndfile.h"
#define MAX_CHANNELS 6
#define MAX_SIZE 4096
smpl_t *scratch_data;
};
+uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
+
aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) {
aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
+ s->max_size = MAX_SIZE;
+ s->path = path;
if (path == NULL) {
- AUBIO_ERR("Aborted opening null path\n");
+ AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
return NULL;
}
+ s->samplerate = 0;
+ s->channels = 0;
+
+ // negative samplerate given, abort
+ if ((sint_t)samplerate < 0) goto beach;
+ // zero samplerate given. do not open yet
+ if ((sint_t)samplerate == 0) return s;
+
s->samplerate = samplerate;
- s->max_size = MAX_SIZE;
s->channels = 1;
- s->path = path;
+ if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
+ goto beach;
+ }
+ return s;
+
+beach:
+ del_aubio_sink_sndfile(s);
+ return NULL;
+}
+
+uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
+{
+ if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
+ s->samplerate = samplerate;
+ // automatically open when both samplerate and channels have been set
+ if (s->samplerate != 0 && s->channels != 0) {
+ return aubio_sink_sndfile_open(s);
+ }
+ return AUBIO_OK;
+}
+
+uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
+{
+ if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
+ s->channels = channels;
+ // automatically open when both samplerate and channels have been set
+ if (s->samplerate != 0 && s->channels != 0) {
+ return aubio_sink_sndfile_open(s);
+ }
+ return AUBIO_OK;
+}
+
+uint_t aubio_sink_sndfile_get_samplerate(aubio_sink_sndfile_t *s)
+{
+ return s->samplerate;
+}
+
+uint_t aubio_sink_sndfile_get_channels(aubio_sink_sndfile_t *s)
+{
+ return s->channels;
+}
+
+uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
/* set output format */
SF_INFO sfinfo;
AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
if (s->handle == NULL) {
/* show libsndfile err msg */
- AUBIO_ERR("Failed opening %s. %s\n", s->path, sf_strerror (NULL));
- return NULL;
+ AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL));
+ return AUBIO_FAIL;
}
s->scratch_size = s->max_size*s->channels;
/* allocate data for de/interleaving reallocated when needed. */
if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
- AUBIO_ERR("%d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
+ AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
- return NULL;
+ return AUBIO_FAIL;
}
s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
- return s;
+ return AUBIO_OK;
}
void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
- sf_count_t written_frames = 0;
- int i, j, channels = s->channels;
- int nsamples = channels*write;
+ uint_t i, j, channels = s->channels;
+ int nsamples = 0;
smpl_t *pwrite;
+ sf_count_t written_frames;
if (write > s->max_size) {
- AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
+ AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
write, s->max_size);
write = s->max_size;
}
+ nsamples = channels * write;
+
/* interleaving data */
for ( i = 0; i < channels; i++) {
pwrite = (smpl_t *)write_data->data;
- for (j=0; j < write; j++) {
+ for (j = 0; j < write; j++) {
s->scratch_data[channels*j+i] = pwrite[j];
}
}
- uint_t written = sf_write_float (s->handle, s->scratch_data, nsamples);
- if (written/channels != write) {
- AUBIO_WRN("trying to write %d frames to %s, but only %d could be written",
- write, s->path, written);
+ written_frames = sf_write_float (s->handle, s->scratch_data, nsamples);
+ if (written_frames/channels != write) {
+ AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
+ write, s->path, (uint_t)written_frames);
}
return;
}
-void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
- if (!s) return;
+void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
+ uint_t i, j, channels = s->channels;
+ int nsamples = 0;
+ smpl_t *pwrite;
+ sf_count_t written_frames;
+
+ if (write > s->max_size) {
+ AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
+ write, s->max_size);
+ write = s->max_size;
+ }
+
+ nsamples = channels * write;
+
+ /* interleaving data */
+ for ( i = 0; i < write_data->height; i++) {
+ pwrite = (smpl_t *)write_data->data[i];
+ for (j = 0; j < write; j++) {
+ s->scratch_data[channels*j+i] = pwrite[j];
+ }
+ }
+
+ written_frames = sf_write_float (s->handle, s->scratch_data, nsamples);
+ if (written_frames/channels != write) {
+ AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
+ write, s->path, (uint_t)written_frames);
+ }
+ return;
+}
+
+uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
+ if (!s->handle) {
+ return AUBIO_FAIL;
+ }
if (sf_close(s->handle)) {
- AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
+ AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
+ return AUBIO_FAIL;
}
+ s->handle = NULL;
+ return AUBIO_OK;
+}
+
+void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
+ if (!s) return;
+ aubio_sink_sndfile_close(s);
AUBIO_FREE(s->scratch_data);
AUBIO_FREE(s);
}