source: src/io/sink_wavwrite.c @ b40c149

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since b40c149 was b03f1bf, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[sink_wavwrite] use STRERR macro

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[52ca8a3]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
[33d0242]22#include "aubio_priv.h"
[52ca8a3]23
24#ifdef HAVE_WAVWRITE
25
26#include "fvec.h"
[870ad70]27#include "fmat.h"
28#include "io/sink_wavwrite.h"
[cf19b8a]29#include "io/ioutils.h"
[52ca8a3]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
[fdeba11]44#ifdef HAVE_WIN_HACKS
45#define HTOLES(x) x
46#else
[52ca8a3]47#define HTOLES(x) SWAPS(htons(x))
48#endif
[fdeba11]49#endif
[52ca8a3]50
[870ad70]51uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s);
52
[52ca8a3]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
[80d0083]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{
[52ca8a3]74  uint_t i;
75  for (i = 0; i < length; i++) {
76    str[i] = s >> (i * 8);
77  }
78  return str;
79}
80
[ae5d58a]81aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) {
[52ca8a3]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
[d2be104]89  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
90  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[b643a33]91
[52ca8a3]92  s->max_size = MAX_SIZE;
93  s->bitspersample = 16;
94  s->total_frames_written = 0;
95
[870ad70]96  s->samplerate = 0;
97  s->channels = 0;
98
99  // zero samplerate given. do not open yet
[cf19b8a]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  }
[870ad70]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{
[cf19b8a]126  if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
127    return AUBIO_FAIL;
128  }
[870ad70]129  s->samplerate = samplerate;
130  // automatically open when both samplerate and channels have been set
[3e1c482]131  if (/* s->samplerate != 0 && */ s->channels != 0) {
[870ad70]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{
[cf19b8a]139  if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) {
140    return AUBIO_FAIL;
141  }
[870ad70]142  s->channels = channels;
143  // automatically open when both samplerate and channels have been set
[3e1c482]144  if (s->samplerate != 0 /* && s->channels != 0 */) {
[870ad70]145    return aubio_sink_wavwrite_open(s);
146  }
147  return AUBIO_OK;
148}
149
[ae5d58a]150uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s)
[870ad70]151{
152  return s->samplerate;
153}
154
[ae5d58a]155uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s)
[870ad70]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;
[14a5b9a]163  size_t written = 0;
[870ad70]164
165  /* open output file */
166  s->fid = fopen((const char *)s->path, "wb");
[52ca8a3]167  if (!s->fid) {
[b03f1bf]168    AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
[52ca8a3]169    goto beach;
170  }
171
172  // ChunkID
[14a5b9a]173  written += fwrite("RIFF", 4, 1, s->fid);
[52ca8a3]174
175  // ChunkSize (0 for now, actual size will be written in _close)
[14a5b9a]176  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
[52ca8a3]177
178  // Format
[14a5b9a]179  written += fwrite("WAVE", 4, 1, s->fid);
[52ca8a3]180
181  // Subchunk1ID
[14a5b9a]182  written += fwrite("fmt ", 4, 1, s->fid);
[52ca8a3]183
184  // Subchunk1Size
[14a5b9a]185  written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
[52ca8a3]186
187  // AudioFormat
[14a5b9a]188  written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
[52ca8a3]189
190  // NumChannels
[14a5b9a]191  written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
[52ca8a3]192
193  // SampleRate
[14a5b9a]194  written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
[52ca8a3]195
196  // ByteRate
197  byterate = s->samplerate * s->channels * s->bitspersample / 8;
[14a5b9a]198  written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
[52ca8a3]199
200  // BlockAlign
201  blockalign = s->channels * s->bitspersample / 8;
[14a5b9a]202  written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
[52ca8a3]203
204  // BitsPerSample
[14a5b9a]205  written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
[52ca8a3]206
207  // Subchunk2ID
[14a5b9a]208  written += fwrite("data", 4, 1, s->fid);
[52ca8a3]209
210  // Subchunk1Size (0 for now, actual size will be written in _close)
[14a5b9a]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) {
[b03f1bf]215    AUBIO_STRERR("sink_wavwrite: writing header to %s failed, expected %d"
[14a5b9a]216        " write but got only %d (%s)\n", s->path, 13, written, errorstr);
217    return AUBIO_FAIL;
218  }
[52ca8a3]219
220  s->scratch_size = s->max_size * s->channels;
221  /* allocate data for de/interleaving reallocated when needed. */
[a028a04]222  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
[870ad70]223    AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
[a028a04]224        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
[870ad70]225    goto beach;
[52ca8a3]226  }
227  s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
228
[870ad70]229  return AUBIO_OK;
[52ca8a3]230
231beach:
[870ad70]232  return AUBIO_FAIL;
[52ca8a3]233}
234
[a2b7187]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) {
[b03f1bf]243    AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d"
[a2b7187]244        " could be written (%s)\n", write, s->path, written_frames, errorstr);
245  }
246  s->total_frames_written += written_frames;
247}
[52ca8a3]248
249void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
[a2b7187]250  uint_t c = 0, i = 0;
[e4c6c76]251  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
252      s->max_size, write_data->length, write);
[52ca8a3]253
[a97eb17]254  for (c = 0; c < s->channels; c++) {
[e4c6c76]255    for (i = 0; i < length; i++) {
[a97eb17]256      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
257    }
[52ca8a3]258  }
259
[a2b7187]260  aubio_sink_wavwrite_write_frames(s, length);
[52ca8a3]261}
262
[870ad70]263void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
[a2b7187]264  uint_t c = 0, i = 0;
[870ad70]265
[e4c6c76]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);
[870ad70]270
[e4c6c76]271  for (c = 0; c < channels; c++) {
272    for (i = 0; i < length; i++) {
[870ad70]273      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
274    }
275  }
276
[a2b7187]277  aubio_sink_wavwrite_write_frames(s, length);
[870ad70]278}
279
[a9fd272]280uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
[52ca8a3]281  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
282  unsigned char buf[5];
[f5a97ed]283  size_t written = 0, err = 0;
[a9fd272]284  if (!s->fid) return AUBIO_FAIL;
[52ca8a3]285  // ChunkSize
[f5a97ed]286  err += fseek(s->fid, 4, SEEK_SET);
287  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
[52ca8a3]288  // Subchunk2Size
[f5a97ed]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) {
[b03f1bf]292    AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d"
[f5a97ed]293        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
294  }
[52ca8a3]295  // close file
296  if (fclose(s->fid)) {
[b03f1bf]297    AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
[52ca8a3]298  }
299  s->fid = NULL;
[a9fd272]300  return AUBIO_OK;
[52ca8a3]301}
302
303void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
[3e1c482]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);
[52ca8a3]311  AUBIO_FREE(s);
312}
313
314#endif /* HAVE_WAVWRITE */
Note: See TracBrowser for help on using the repository browser.