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"
29 #include "sink_sndfile.h"
32 #define MAX_CHANNELS 6
35 struct _aubio_sink_sndfile_t {
47 aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) {
48 aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
51 AUBIO_ERR("Aborted opening null path\n");
55 s->samplerate = samplerate;
56 s->max_size = MAX_SIZE;
60 /* set output format */
62 AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
63 sfinfo.samplerate = s->samplerate;
64 sfinfo.channels = s->channels;
65 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
67 /* try creating the file */
68 s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
70 if (s->handle == NULL) {
71 /* show libsndfile err msg */
72 AUBIO_ERR("Failed opening %s. %s\n", s->path, sf_strerror (NULL));
76 s->scratch_size = s->max_size*s->channels;
77 /* allocate data for de/interleaving reallocated when needed. */
78 if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
79 AUBIO_ERR("%d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
80 s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
83 s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
88 void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
89 uint_t i, j, channels = s->channels;
90 int nsamples = channels*write;
93 if (write > s->max_size) {
94 AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
99 /* interleaving data */
100 for ( i = 0; i < channels; i++) {
101 pwrite = (smpl_t *)write_data->data;
102 for (j = 0; j < write; j++) {
103 s->scratch_data[channels*j+i] = pwrite[j];
107 sf_count_t written_frames = sf_write_float (s->handle, s->scratch_data, nsamples);
108 if (written_frames/channels != write) {
109 AUBIO_WRN("trying to write %d frames to %s, but only %d could be written",
110 write, s->path, (uint_t)written_frames);
115 void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
117 if (sf_close(s->handle)) {
118 AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
120 AUBIO_FREE(s->scratch_data);
124 #endif /* HAVE_SNDFILE */