source: src/io/sink_sndfile.c @ a028a04

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchsampleryinfft+
Last change on this file since a028a04 was a028a04, checked in by Paul Brossier <piem@piem.org>, 3 years ago

src/io/sink_{sndfile,wavwrite}.c: use AUBIO_MAX_CHANNELS, fix error message

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