2 Copyright (C) 2012-2014 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 "io/sink_sndfile.h"
33 #define MAX_CHANNELS 6
36 #if !HAVE_AUBIO_DOUBLE
37 #define aubio_sf_write_smpl sf_write_float
38 #else /* HAVE_AUBIO_DOUBLE */
39 #define aubio_sf_write_smpl sf_write_double
40 #endif /* HAVE_AUBIO_DOUBLE */
42 struct _aubio_sink_sndfile_t {
54 uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
56 aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
57 aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
58 s->max_size = MAX_SIZE;
61 AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
65 if (s->path) AUBIO_FREE(s->path);
66 s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
67 strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
72 // negative samplerate given, abort
73 if ((sint_t)samplerate < 0) goto beach;
74 // zero samplerate given. do not open yet
75 if ((sint_t)samplerate == 0) return s;
77 s->samplerate = samplerate;
80 if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
86 del_aubio_sink_sndfile(s);
90 uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
92 if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
93 s->samplerate = samplerate;
94 // automatically open when both samplerate and channels have been set
95 if (s->samplerate != 0 && s->channels != 0) {
96 return aubio_sink_sndfile_open(s);
101 uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
103 if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
104 s->channels = channels;
105 // automatically open when both samplerate and channels have been set
106 if (s->samplerate != 0 && s->channels != 0) {
107 return aubio_sink_sndfile_open(s);
112 uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
114 return s->samplerate;
117 uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
122 uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
123 /* set output format */
125 AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
126 sfinfo.samplerate = s->samplerate;
127 sfinfo.channels = s->channels;
128 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
130 /* try creating the file */
131 s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
133 if (s->handle == NULL) {
134 /* show libsndfile err msg */
135 AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL));
139 s->scratch_size = s->max_size*s->channels;
140 /* allocate data for de/interleaving reallocated when needed. */
141 if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
142 AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
143 s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
146 s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
151 void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
152 uint_t i, j, channels = s->channels;
155 sf_count_t written_frames;
157 if (write > s->max_size) {
158 AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
163 nsamples = channels * write;
165 /* interleaving data */
166 for ( i = 0; i < channels; i++) {
167 pwrite = (smpl_t *)write_data->data;
168 for (j = 0; j < write; j++) {
169 s->scratch_data[channels*j+i] = pwrite[j];
173 written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
174 if (written_frames/channels != write) {
175 AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
176 write, s->path, (uint_t)written_frames);
181 void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
182 uint_t i, j, channels = s->channels;
185 sf_count_t written_frames;
187 if (write > s->max_size) {
188 AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
193 nsamples = channels * write;
195 /* interleaving data */
196 for ( i = 0; i < write_data->height; i++) {
197 pwrite = (smpl_t *)write_data->data[i];
198 for (j = 0; j < write; j++) {
199 s->scratch_data[channels*j+i] = pwrite[j];
203 written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
204 if (written_frames/channels != write) {
205 AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
206 write, s->path, (uint_t)written_frames);
211 uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
215 if (sf_close(s->handle)) {
216 AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
223 void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
225 if (s->path) AUBIO_FREE(s->path);
226 aubio_sink_sndfile_close(s);
227 AUBIO_FREE(s->scratch_data);
231 #endif /* HAVE_SNDFILE */