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