src/io/ioutils.h: add functions to check samplerate and channels, use in sink_*.c
[aubio.git] / src / io / sink_sndfile.c
1 /*
2   Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "config.h"
23
24 #ifdef HAVE_SNDFILE
25
26 #include <sndfile.h>
27
28 #include "aubio_priv.h"
29 #include "fvec.h"
30 #include "fmat.h"
31 #include "io/sink_sndfile.h"
32 #include "io/ioutils.h"
33
34 #define MAX_CHANNELS 6
35 #define MAX_SIZE 4096
36
37 #if !HAVE_AUBIO_DOUBLE
38 #define aubio_sf_write_smpl sf_write_float
39 #else /* HAVE_AUBIO_DOUBLE */
40 #define aubio_sf_write_smpl sf_write_double
41 #endif /* HAVE_AUBIO_DOUBLE */
42
43 struct _aubio_sink_sndfile_t {
44   uint_t samplerate;
45   uint_t channels;
46   char_t *path;
47
48   uint_t max_size;
49
50   SNDFILE *handle;
51   uint_t scratch_size;
52   smpl_t *scratch_data;
53 };
54
55 uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
56
57 aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
58   aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
59   s->max_size = MAX_SIZE;
60
61   if (path == NULL) {
62     AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
63     return NULL;
64   }
65
66   if (s->path) AUBIO_FREE(s->path);
67   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
68   strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
69
70   s->samplerate = 0;
71   s->channels = 0;
72
73   // zero samplerate given. do not open yet
74   if ((sint_t)samplerate == 0) {
75     return s;
76   }
77   // invalid samplerate given, abort
78   if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
79     goto beach;
80   }
81
82   s->samplerate = samplerate;
83   s->channels = 1;
84
85   if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
86     goto beach;
87   }
88   return s;
89
90 beach:
91   del_aubio_sink_sndfile(s);
92   return NULL;
93 }
94
95 uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
96 {
97   if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
98     return AUBIO_FAIL;
99   }
100   s->samplerate = samplerate;
101   // automatically open when both samplerate and channels have been set
102   if (s->samplerate != 0 && s->channels != 0) {
103     return aubio_sink_sndfile_open(s);
104   }
105   return AUBIO_OK;
106 }
107
108 uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
109 {
110   if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
111     return AUBIO_FAIL;
112   }
113   s->channels = channels;
114   // automatically open when both samplerate and channels have been set
115   if (s->samplerate != 0 && s->channels != 0) {
116     return aubio_sink_sndfile_open(s);
117   }
118   return AUBIO_OK;
119 }
120
121 uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
122 {
123   return s->samplerate;
124 }
125
126 uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
127 {
128   return s->channels;
129 }
130
131 uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
132   /* set output format */
133   SF_INFO sfinfo;
134   AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
135   sfinfo.samplerate = s->samplerate;
136   sfinfo.channels   = s->channels;
137   sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
138
139   /* try creating the file */
140   s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
141
142   if (s->handle == NULL) {
143     /* show libsndfile err msg */
144     AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL));
145     return AUBIO_FAIL;
146   }
147
148   s->scratch_size = s->max_size*s->channels;
149   /* allocate data for de/interleaving reallocated when needed. */
150   if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
151     AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
152         s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
153     return AUBIO_FAIL;
154   }
155   s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
156
157   return AUBIO_OK;
158 }
159
160 void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
161   uint_t i, j, channels = s->channels;
162   int nsamples = 0;
163   smpl_t *pwrite;
164   sf_count_t written_frames;
165
166   if (write > s->max_size) {
167     AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
168       write, s->max_size);
169     write = s->max_size;
170   }
171
172   nsamples = channels * write;
173
174   /* interleaving data  */
175   for ( i = 0; i < channels; i++) {
176     pwrite = (smpl_t *)write_data->data;
177     for (j = 0; j < write; j++) {
178       s->scratch_data[channels*j+i] = pwrite[j];
179     }
180   }
181
182   written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
183   if (written_frames/channels != write) {
184     AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
185       write, s->path, (uint_t)written_frames);
186   }
187   return;
188 }
189
190 void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
191   uint_t i, j, channels = s->channels;
192   int nsamples = 0;
193   smpl_t *pwrite;
194   sf_count_t written_frames;
195
196   if (write > s->max_size) {
197     AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
198       write, s->max_size);
199     write = s->max_size;
200   }
201
202   nsamples = channels * write;
203
204   /* interleaving data  */
205   for ( i = 0; i < write_data->height; i++) {
206     pwrite = (smpl_t *)write_data->data[i];
207     for (j = 0; j < write; j++) {
208       s->scratch_data[channels*j+i] = pwrite[j];
209     }
210   }
211
212   written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
213   if (written_frames/channels != write) {
214     AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
215       write, s->path, (uint_t)written_frames);
216   }
217   return;
218 }
219
220 uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
221   if (!s->handle) {
222     return AUBIO_FAIL;
223   }
224   if (sf_close(s->handle)) {
225     AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
226     return AUBIO_FAIL;
227   }
228   s->handle = NULL;
229   return AUBIO_OK;
230 }
231
232 void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
233   if (!s) return;
234   if (s->path) AUBIO_FREE(s->path);
235   aubio_sink_sndfile_close(s);
236   AUBIO_FREE(s->scratch_data);
237   AUBIO_FREE(s);
238 }
239
240 #endif /* HAVE_SNDFILE */