src/io/source_sndfile.c
[aubio.git] / src / io / source_sndfile.c
1 /*
2   Copyright (C) 2012 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 "source_sndfile.h"
32
33 #include "temporal/resampler.h"
34
35 #define MAX_SIZE 4096
36 #define MAX_SAMPLES AUBIO_MAX_CHANNELS * MAX_SIZE
37
38 #if !HAVE_AUBIO_DOUBLE
39 #define aubio_sf_read_smpl sf_read_float
40 #else /* HAVE_AUBIO_DOUBLE */
41 #define aubio_sf_read_smpl sf_read_double
42 #endif /* HAVE_AUBIO_DOUBLE */
43
44 struct _aubio_source_sndfile_t {
45   uint_t hop_size;
46   uint_t samplerate;
47   uint_t channels;
48
49   // some data about the file
50   char_t *path;
51   SNDFILE *handle;
52   int input_samplerate;
53   int input_channels;
54   int input_format;
55   int duration;
56
57   // resampling stuff
58   smpl_t ratio;
59   uint_t input_hop_size;
60 #ifdef HAVE_SAMPLERATE
61   aubio_resampler_t **resamplers;
62   fvec_t *input_data;
63   fmat_t *input_mat;
64 #endif /* HAVE_SAMPLERATE */
65
66   // some temporary memory for sndfile to write at
67   uint_t scratch_size;
68   smpl_t *scratch_data;
69 };
70
71 aubio_source_sndfile_t * new_aubio_source_sndfile(const char_t * path, uint_t samplerate, uint_t hop_size) {
72   aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
73   SF_INFO sfinfo;
74
75   if (path == NULL) {
76     AUBIO_ERR("source_sndfile: Aborted opening null path\n");
77     goto beach;
78   }
79   if ((sint_t)samplerate < 0) {
80     AUBIO_ERR("source_sndfile: Can not open %s with samplerate %d\n", path, samplerate);
81     goto beach;
82   }
83   if ((sint_t)hop_size <= 0) {
84     AUBIO_ERR("source_sndfile: Can not open %s with hop_size %d\n", path, hop_size);
85     goto beach;
86   }
87
88   s->hop_size = hop_size;
89   s->channels = 1;
90
91   if (s->path) AUBIO_FREE(s->path);
92   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
93   strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
94
95   // try opening the file, getting the info in sfinfo
96   AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
97   s->handle = sf_open (s->path, SFM_READ, &sfinfo);
98
99   if (s->handle == NULL) {
100     /* show libsndfile err msg */
101     AUBIO_ERR("source_sndfile: Failed opening %s (%s)\n", s->path,
102         sf_strerror (NULL));
103     goto beach;
104   }
105
106   /* get input specs */
107   s->input_samplerate = sfinfo.samplerate;
108   s->input_channels   = sfinfo.channels;
109   s->input_format     = sfinfo.format;
110   s->duration         = sfinfo.frames;
111
112   if (samplerate == 0) {
113     s->samplerate = s->input_samplerate;
114     //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
115   } else {
116     s->samplerate = samplerate;
117   }
118   /* compute input block size required before resampling */
119   s->ratio = s->samplerate/(smpl_t)s->input_samplerate;
120   s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
121
122   if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
123     AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n",
124         MAX_SAMPLES / s->input_channels, s->input_channels);
125     goto beach;
126   }
127
128 #ifdef HAVE_SAMPLERATE
129   s->input_data = NULL;
130   s->input_mat = NULL;
131   s->resamplers = NULL;
132   if (s->ratio != 1) {
133     uint_t i;
134     s->resamplers = AUBIO_ARRAY(aubio_resampler_t*, s->input_channels);
135     s->input_data = new_fvec(s->input_hop_size);
136     s->input_mat = new_fmat(s->input_channels, s->input_hop_size);
137     for (i = 0; i < (uint_t)s->input_channels; i++) {
138       s->resamplers[i] = new_aubio_resampler(s->ratio, 4);
139     }
140     if (s->ratio > 1) {
141       // we would need to add a ring buffer for these
142       if ( (uint_t)FLOOR(s->input_hop_size * s->ratio + .5)  != s->hop_size ) {
143         AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path,
144             s->input_samplerate, s->samplerate);
145         goto beach;
146       }
147       AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path,
148           s->input_samplerate, s->samplerate);
149     }
150     s->duration = (uint_t)FLOOR(s->duration * s->ratio);
151   }
152 #else
153   if (s->ratio != 1) {
154     AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n");
155     goto beach;
156   }
157 #endif /* HAVE_SAMPLERATE */
158
159   /* allocate data for de/interleaving reallocated when needed. */
160   s->scratch_size = s->input_hop_size * s->input_channels;
161   s->scratch_data = AUBIO_ARRAY(smpl_t, s->scratch_size);
162
163   return s;
164
165 beach:
166   //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
167   //    s->path, s->samplerate, s->hop_size);
168   del_aubio_source_sndfile(s);
169   return NULL;
170 }
171
172 void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
173   uint_t i,j, input_channels = s->input_channels;
174   /* read from file into scratch_data */
175   sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
176
177   /* where to store de-interleaved data */
178   smpl_t *ptr_data;
179 #ifdef HAVE_SAMPLERATE
180   if (s->ratio != 1) {
181     ptr_data = s->input_data->data;
182   } else
183 #endif /* HAVE_SAMPLERATE */
184   {
185     ptr_data = read_data->data;
186   }
187
188   /* de-interleaving and down-mixing data  */
189   for (j = 0; j < read_samples / input_channels; j++) {
190     ptr_data[j] = 0;
191     for (i = 0; i < input_channels; i++) {
192       ptr_data[j] += s->scratch_data[input_channels*j+i];
193     }
194     ptr_data[j] /= (smpl_t)input_channels;
195   }
196
197 #ifdef HAVE_SAMPLERATE
198   if (s->resamplers) {
199     aubio_resampler_do(s->resamplers[0], s->input_data, read_data);
200   }
201 #endif /* HAVE_SAMPLERATE */
202
203   *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
204
205   if (*read < s->hop_size) {
206     for (j = *read; j < s->hop_size; j++) {
207       read_data->data[j] = 0;
208     }
209   }
210
211 }
212
213 void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
214   uint_t i,j, input_channels = s->input_channels;
215   /* do actual reading */
216   sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
217
218   /* where to store de-interleaved data */
219   smpl_t **ptr_data;
220 #ifdef HAVE_SAMPLERATE
221   if (s->ratio != 1) {
222     ptr_data = s->input_mat->data;
223   } else
224 #endif /* HAVE_SAMPLERATE */
225   {
226     ptr_data = read_data->data;
227   }
228
229   if (read_data->height < input_channels) {
230     // destination matrix has less channels than the file; copy only first
231     // channels of the file, de-interleaving data
232     for (j = 0; j < read_samples / input_channels; j++) {
233       for (i = 0; i < read_data->height; i++) {
234         ptr_data[i][j] = s->scratch_data[j * input_channels + i];
235       }
236     }
237   } else {
238     // destination matrix has as many or more channels than the file; copy each
239     // channel from the file to the destination matrix, de-interleaving data
240     for (j = 0; j < read_samples / input_channels; j++) {
241       for (i = 0; i < input_channels; i++) {
242         ptr_data[i][j] = s->scratch_data[j * input_channels + i];
243       }
244     }
245   }
246
247   if (read_data->height > input_channels) {
248     // destination matrix has more channels than the file; copy last channel
249     // of the file to each additional channels, de-interleaving data
250     for (j = 0; j < read_samples / input_channels; j++) {
251       for (i = input_channels; i < read_data->height; i++) {
252         ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
253       }
254     }
255   }
256
257 #ifdef HAVE_SAMPLERATE
258   if (s->resamplers) {
259     for (i = 0; i < input_channels; i++) {
260       fvec_t input_chan, read_chan;
261       input_chan.data = s->input_mat->data[i];
262       input_chan.length = s->input_mat->length;
263       read_chan.data = read_data->data[i];
264       read_chan.length = read_data->length;
265       aubio_resampler_do(s->resamplers[i], &input_chan, &read_chan);
266     }
267   }
268 #endif /* HAVE_SAMPLERATE */
269
270   *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
271
272   if (*read < s->hop_size) {
273     for (i = 0; i < read_data->height; i++) {
274       for (j = *read; j < s->hop_size; j++) {
275         read_data->data[i][j] = 0.;
276       }
277     }
278   }
279
280 }
281
282 uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
283   return s->samplerate;
284 }
285
286 uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
287   return s->input_channels;
288 }
289
290 uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) {
291   if (s && s->duration) {
292     return s->duration;
293   }
294   return 0;
295 }
296
297 uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
298   uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
299   sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
300   if (sf_ret == -1) {
301     AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
302     return AUBIO_FAIL;
303   }
304   if (sf_ret != resampled_pos) {
305     AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n",
306         s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL));
307     return AUBIO_FAIL;
308   }
309   return AUBIO_OK;
310 }
311
312 uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
313   if (!s->handle) {
314     return AUBIO_FAIL;
315   }
316   if(sf_close(s->handle)) {
317     AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
318     return AUBIO_FAIL;
319   }
320   s->handle = NULL;
321   return AUBIO_OK;
322 }
323
324 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
325   if (!s) return;
326   aubio_source_sndfile_close(s);
327 #ifdef HAVE_SAMPLERATE
328   if (s->resamplers != NULL) {
329     uint_t i = 0, input_channels = s->input_channels;
330     for (i = 0; i < input_channels; i ++) {
331       if (s->resamplers[i] != NULL) {
332         del_aubio_resampler(s->resamplers[i]);
333       }
334     }
335     AUBIO_FREE(s->resamplers);
336   }
337   if (s->input_data) {
338     del_fvec(s->input_data);
339   }
340   if (s->input_mat) {
341     del_fmat(s->input_mat);
342   }
343 #endif /* HAVE_SAMPLERATE */
344   if (s->path) AUBIO_FREE(s->path);
345   AUBIO_FREE(s->scratch_data);
346   AUBIO_FREE(s);
347 }
348
349 #endif /* HAVE_SNDFILE */