source: src/io/sink_sndfile.c @ 0da5208

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 0da5208 was 0da5208, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[io] sink_sndfile: try guessing format according to file extension

  • Property mode set to 100644
File size: 7.2 KB
Line 
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
41struct _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  int format;
52};
53
54uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
55
56uint_t aubio_str_extension_matches(const char_t *ext,
57    const char_t *pattern);
58const char_t *aubio_str_get_extension(const char_t *filename);
59
60aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
61  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
62  s->max_size = MAX_SIZE;
63
64  if (path == NULL) {
65    AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
66    goto beach;
67  }
68
69  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
70  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
71
72  s->samplerate = 0;
73  s->channels = 0;
74
75  aubio_sink_sndfile_preset_format(s, aubio_str_get_extension(path));
76
77  // zero samplerate given. do not open yet
78  if ((sint_t)samplerate == 0) {
79    return s;
80  }
81  // invalid samplerate given, abort
82  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
83    goto beach;
84  }
85
86  s->samplerate = samplerate;
87  s->channels = 1;
88
89  if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
90    goto beach;
91  }
92  return s;
93
94beach:
95  del_aubio_sink_sndfile(s);
96  return NULL;
97}
98
99uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
100{
101  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
102    return AUBIO_FAIL;
103  }
104  s->samplerate = samplerate;
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);
108  }
109  return AUBIO_OK;
110}
111
112uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
113{
114  if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
115    return AUBIO_FAIL;
116  }
117  s->channels = channels;
118  // automatically open when both samplerate and channels have been set
119  if (s->samplerate != 0 /* && s->channels != 0 */) {
120    return aubio_sink_sndfile_open(s);
121  }
122  return AUBIO_OK;
123}
124
125uint_t aubio_sink_sndfile_preset_format(aubio_sink_sndfile_t *s,
126    const char_t *fmt)
127{
128  if (aubio_str_extension_matches(fmt, "wav")) {
129    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
130  } else if (aubio_str_extension_matches(fmt, "aiff")) {
131    s->format = SF_FORMAT_AIFF | SF_FORMAT_PCM_16;
132  } else if (aubio_str_extension_matches(fmt, "flac")) {
133    s->format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16;
134  } else if (aubio_str_extension_matches(fmt, "ogg")) {
135    s->format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
136  } else if (atoi(fmt) > 0x010000) {
137    s->format = atoi(fmt);
138  } else {
139    AUBIO_WRN("sink_sndfile: could not guess format for %s,"
140       " using default (wav)\n", s->path);
141    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
142    return AUBIO_FAIL;
143  }
144  return AUBIO_OK;
145}
146
147uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
148{
149  return s->samplerate;
150}
151
152uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
153{
154  return s->channels;
155}
156
157uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
158  /* set output format */
159  SF_INFO sfinfo;
160  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
161  sfinfo.samplerate = s->samplerate;
162  sfinfo.channels   = s->channels;
163  sfinfo.format     = s->format;
164
165  /* try creating the file */
166  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
167
168  if (s->handle == NULL) {
169    /* show libsndfile err msg */
170    AUBIO_ERR("sink_sndfile: Failed opening \"%s\" with %d channels, %dHz: %s\n",
171        s->path, s->channels, s->samplerate, sf_strerror (NULL));
172    return AUBIO_FAIL;
173  }
174
175  s->scratch_size = s->max_size*s->channels;
176  /* allocate data for de/interleaving reallocated when needed. */
177  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
178    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum buffer size %d\n",
179        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
180    return AUBIO_FAIL;
181  }
182  s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
183
184  return AUBIO_OK;
185}
186
187void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
188  uint_t i, j;
189  sf_count_t written_frames;
190  uint_t channels = s->channels;
191  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
192      s->max_size, write_data->length, write);
193  int nsamples = channels * length;
194
195  /* interleaving data  */
196  for ( i = 0; i < channels; i++) {
197    for (j = 0; j < length; j++) {
198      s->scratch_data[channels*j+i] = write_data->data[j];
199    }
200  }
201
202  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
203  if (written_frames/channels != write) {
204    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
205      write, s->path, (uint_t)written_frames);
206  }
207  return;
208}
209
210void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
211  uint_t i, j;
212  sf_count_t written_frames;
213  uint_t channels = aubio_sink_validate_input_channels("sink_sndfile", s->path,
214      s->channels, write_data->height);
215  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
216      s->max_size, write_data->length, write);
217  int nsamples = channels * length;
218
219  /* interleaving data  */
220  for ( i = 0; i < channels; i++) {
221    for (j = 0; j < length; j++) {
222      s->scratch_data[channels*j+i] = write_data->data[i][j];
223    }
224  }
225
226  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
227  if (written_frames/channels != write) {
228    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
229      write, s->path, (uint_t)written_frames);
230  }
231  return;
232}
233
234uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
235  if (!s->handle) {
236    return AUBIO_FAIL;
237  }
238  if (sf_close(s->handle)) {
239    AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
240    return AUBIO_FAIL;
241  }
242  s->handle = NULL;
243  return AUBIO_OK;
244}
245
246void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
247  AUBIO_ASSERT(s);
248  if (s->handle)
249    aubio_sink_sndfile_close(s);
250  if (s->path)
251    AUBIO_FREE(s->path);
252  if (s->scratch_data)
253    AUBIO_FREE(s->scratch_data);
254  AUBIO_FREE(s);
255}
256
257#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.