source: src/io/sink_sndfile.c @ 1cbd27c

feature/crepe
Last change on this file since 1cbd27c was 5fc6e81, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[sink_sndfile] preset_format does not fail on empty format string

  • Property mode set to 100644
File size: 7.3 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    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
140    if (fmt && strnlen(fmt, PATH_MAX))  {
141      AUBIO_WRN("sink_sndfile: could not guess format %s for %s,"
142          " using default (wav)\n", fmt, s->path);
143      return AUBIO_FAIL;
144    }
145  }
146  return AUBIO_OK;
147}
148
149uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
150{
151  return s->samplerate;
152}
153
154uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
155{
156  return s->channels;
157}
158
159uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
160  /* set output format */
161  SF_INFO sfinfo;
162  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
163  sfinfo.samplerate = s->samplerate;
164  sfinfo.channels   = s->channels;
165  sfinfo.format     = s->format;
166
167  /* try creating the file */
168  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
169
170  if (s->handle == NULL) {
171    /* show libsndfile err msg */
172    AUBIO_ERR("sink_sndfile: Failed opening \"%s\" with %d channels, %dHz: %s\n",
173        s->path, s->channels, s->samplerate, sf_strerror (NULL));
174    return AUBIO_FAIL;
175  }
176
177  s->scratch_size = s->max_size*s->channels;
178  /* allocate data for de/interleaving reallocated when needed. */
179  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
180    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum buffer size %d\n",
181        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
182    return AUBIO_FAIL;
183  }
184  s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
185
186  return AUBIO_OK;
187}
188
189void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
190  uint_t i, j;
191  sf_count_t written_frames;
192  uint_t channels = s->channels;
193  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
194      s->max_size, write_data->length, write);
195  int nsamples = channels * length;
196
197  /* interleaving data  */
198  for ( i = 0; i < channels; i++) {
199    for (j = 0; j < length; j++) {
200      s->scratch_data[channels*j+i] = write_data->data[j];
201    }
202  }
203
204  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
205  if (written_frames/channels != write) {
206    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
207      write, s->path, (uint_t)written_frames);
208  }
209  return;
210}
211
212void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
213  uint_t i, j;
214  sf_count_t written_frames;
215  uint_t channels = aubio_sink_validate_input_channels("sink_sndfile", s->path,
216      s->channels, write_data->height);
217  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
218      s->max_size, write_data->length, write);
219  int nsamples = channels * length;
220
221  /* interleaving data  */
222  for ( i = 0; i < channels; i++) {
223    for (j = 0; j < length; j++) {
224      s->scratch_data[channels*j+i] = write_data->data[i][j];
225    }
226  }
227
228  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
229  if (written_frames/channels != write) {
230    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
231      write, s->path, (uint_t)written_frames);
232  }
233  return;
234}
235
236uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
237  if (!s->handle) {
238    return AUBIO_FAIL;
239  }
240  if (sf_close(s->handle)) {
241    AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
242    return AUBIO_FAIL;
243  }
244  s->handle = NULL;
245  return AUBIO_OK;
246}
247
248void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
249  AUBIO_ASSERT(s);
250  if (s->handle)
251    aubio_sink_sndfile_close(s);
252  if (s->path)
253    AUBIO_FREE(s->path);
254  if (s->scratch_data)
255    AUBIO_FREE(s->scratch_data);
256  AUBIO_FREE(s);
257}
258
259#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.