source: src/io/sink_wavwrite.c @ b40c149

feature/autosinkfeature/constantqfeature/pitchshiftfeature/timestretch
Last change on this file since b40c149 was b03f1bf, checked in by Paul Brossier <piem@piem.org>, 11 months ago

[sink_wavwrite] use STRERR macro

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/*
2  Copyright (C) 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_WAVWRITE
25
26#include "fvec.h"
27#include "fmat.h"
28#include "io/sink_wavwrite.h"
29#include "io/ioutils.h"
30
31#define MAX_SIZE 4096
32
33#define FLOAT_TO_SHORT(x) (short)(x * 32768)
34
35// swap endian of a short
36#define SWAPS(x) ((x & 0xff) << 8) | ((x & 0xff00) >> 8)
37
38// swap host to little endian
39#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
40#define HTOLES(x) SWAPS(x)
41#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
42#define HTOLES(x) x
43#else
44#ifdef HAVE_WIN_HACKS
45#define HTOLES(x) x
46#else
47#define HTOLES(x) SWAPS(htons(x))
48#endif
49#endif
50
51uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s);
52
53struct _aubio_sink_wavwrite_t {
54  char_t *path;
55  uint_t samplerate;
56  uint_t channels;
57  uint_t bitspersample;
58  uint_t total_frames_written;
59
60  FILE *fid;
61
62  uint_t max_size;
63
64  uint_t scratch_size;
65  unsigned short *scratch_data;
66};
67
68static unsigned char *write_little_endian (unsigned int s, unsigned char *str,
69    unsigned int length);
70
71static unsigned char *write_little_endian (unsigned int s, unsigned char *str,
72    unsigned int length)
73{
74  uint_t i;
75  for (i = 0; i < length; i++) {
76    str[i] = s >> (i * 8);
77  }
78  return str;
79}
80
81aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) {
82  aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t);
83
84  if (path == NULL) {
85    AUBIO_ERR("sink_wavwrite: Aborted opening null path\n");
86    goto beach;
87  }
88
89  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
90  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
91
92  s->max_size = MAX_SIZE;
93  s->bitspersample = 16;
94  s->total_frames_written = 0;
95
96  s->samplerate = 0;
97  s->channels = 0;
98
99  // zero samplerate given. do not open yet
100  if ((sint_t)samplerate == 0) {
101    return s;
102  }
103  // invalid samplerate given, abort
104  if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
105    goto beach;
106  }
107
108  s->samplerate = samplerate;
109  s->channels = 1;
110
111  if (aubio_sink_wavwrite_open(s) != AUBIO_OK) {
112    // open failed, abort
113    goto beach;
114  }
115
116  return s;
117beach:
118  //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
119  //    s->path, s->samplerate);
120  del_aubio_sink_wavwrite(s);
121  return NULL;
122}
123
124uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
125{
126  if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
127    return AUBIO_FAIL;
128  }
129  s->samplerate = samplerate;
130  // automatically open when both samplerate and channels have been set
131  if (/* s->samplerate != 0 && */ s->channels != 0) {
132    return aubio_sink_wavwrite_open(s);
133  }
134  return AUBIO_OK;
135}
136
137uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
138{
139  if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) {
140    return AUBIO_FAIL;
141  }
142  s->channels = channels;
143  // automatically open when both samplerate and channels have been set
144  if (s->samplerate != 0 /* && s->channels != 0 */) {
145    return aubio_sink_wavwrite_open(s);
146  }
147  return AUBIO_OK;
148}
149
150uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s)
151{
152  return s->samplerate;
153}
154
155uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s)
156{
157  return s->channels;
158}
159
160uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) {
161  unsigned char buf[5];
162  uint_t byterate, blockalign;
163  size_t written = 0;
164
165  /* open output file */
166  s->fid = fopen((const char *)s->path, "wb");
167  if (!s->fid) {
168    AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
169    goto beach;
170  }
171
172  // ChunkID
173  written += fwrite("RIFF", 4, 1, s->fid);
174
175  // ChunkSize (0 for now, actual size will be written in _close)
176  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
177
178  // Format
179  written += fwrite("WAVE", 4, 1, s->fid);
180
181  // Subchunk1ID
182  written += fwrite("fmt ", 4, 1, s->fid);
183
184  // Subchunk1Size
185  written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
186
187  // AudioFormat
188  written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
189
190  // NumChannels
191  written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
192
193  // SampleRate
194  written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
195
196  // ByteRate
197  byterate = s->samplerate * s->channels * s->bitspersample / 8;
198  written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
199
200  // BlockAlign
201  blockalign = s->channels * s->bitspersample / 8;
202  written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
203
204  // BitsPerSample
205  written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
206
207  // Subchunk2ID
208  written += fwrite("data", 4, 1, s->fid);
209
210  // Subchunk1Size (0 for now, actual size will be written in _close)
211  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
212
213  // fwrite(*, *, 1, s->fid) was called 13 times, check success
214  if (written != 13) {
215    AUBIO_STRERR("sink_wavwrite: writing header to %s failed, expected %d"
216        " write but got only %d (%s)\n", s->path, 13, written, errorstr);
217    return AUBIO_FAIL;
218  }
219
220  s->scratch_size = s->max_size * s->channels;
221  /* allocate data for de/interleaving reallocated when needed. */
222  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
223    AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
224        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
225    goto beach;
226  }
227  s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
228
229  return AUBIO_OK;
230
231beach:
232  return AUBIO_FAIL;
233}
234
235static
236void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write)
237{
238  uint_t written_frames = 0;
239
240  written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid);
241
242  if (written_frames != write) {
243    AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d"
244        " could be written (%s)\n", write, s->path, written_frames, errorstr);
245  }
246  s->total_frames_written += written_frames;
247}
248
249void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
250  uint_t c = 0, i = 0;
251  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
252      s->max_size, write_data->length, write);
253
254  for (c = 0; c < s->channels; c++) {
255    for (i = 0; i < length; i++) {
256      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
257    }
258  }
259
260  aubio_sink_wavwrite_write_frames(s, length);
261}
262
263void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
264  uint_t c = 0, i = 0;
265
266  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
267      s->channels, write_data->height);
268  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
269      s->max_size, write_data->length, write);
270
271  for (c = 0; c < channels; c++) {
272    for (i = 0; i < length; i++) {
273      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
274    }
275  }
276
277  aubio_sink_wavwrite_write_frames(s, length);
278}
279
280uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
281  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
282  unsigned char buf[5];
283  size_t written = 0, err = 0;
284  if (!s->fid) return AUBIO_FAIL;
285  // ChunkSize
286  err += fseek(s->fid, 4, SEEK_SET);
287  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
288  // Subchunk2Size
289  err += fseek(s->fid, 40, SEEK_SET);
290  written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
291  if (written != 2 || err != 0) {
292    AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d"
293        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
294  }
295  // close file
296  if (fclose(s->fid)) {
297    AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
298  }
299  s->fid = NULL;
300  return AUBIO_OK;
301}
302
303void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
304  AUBIO_ASSERT(s);
305  if (s->fid)
306    aubio_sink_wavwrite_close(s);
307  if (s->path)
308    AUBIO_FREE(s->path);
309  if (s->scratch_data)
310    AUBIO_FREE(s->scratch_data);
311  AUBIO_FREE(s);
312}
313
314#endif /* HAVE_WAVWRITE */
Note: See TracBrowser for help on using the repository browser.