source: src/io/sink_sndfile.c @ 695e171

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 695e171 was 385a06e2, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/io/sink_sndfile.c: fix for double precision

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