source: src/io/sink_wavwrite.c @ fdeba11

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

src/io/sink_wavwrite.c: assume windows is little endian to build with mingw32

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