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

sampler
Last change on this file since 0a756ea was d2be104, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/io/: also copy null ending char

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