source: src/io/sink_wavwrite.c @ 30cb440e

feature/crepe_org
Last change on this file since 30cb440e was 65a4fb4, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[sink_wavwrite] call fflush in open

This ensures the file header was actually written correctly, and fails
otherwise, for instance when the target disk-system is full.

  • 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
[65a4fb4]214  if (written != 13 || fflush(s->fid)) {
215    AUBIO_STRERR("sink_wavwrite: writing header to %s failed"
216        " (wrote %d/%d, %s)\n", s->path, written, 13, errorstr);
217    fclose(s->fid);
218    s->fid = NULL;
[14a5b9a]219    return AUBIO_FAIL;
220  }
[52ca8a3]221
222  s->scratch_size = s->max_size * s->channels;
223  /* allocate data for de/interleaving reallocated when needed. */
[a028a04]224  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
[870ad70]225    AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
[a028a04]226        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
[870ad70]227    goto beach;
[52ca8a3]228  }
229  s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
230
[870ad70]231  return AUBIO_OK;
[52ca8a3]232
233beach:
[870ad70]234  return AUBIO_FAIL;
[52ca8a3]235}
236
[a2b7187]237static
238void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write)
239{
240  uint_t written_frames = 0;
241
242  written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid);
243
244  if (written_frames != write) {
[b03f1bf]245    AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d"
[a2b7187]246        " could be written (%s)\n", write, s->path, written_frames, errorstr);
247  }
248  s->total_frames_written += written_frames;
249}
[52ca8a3]250
251void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
[a2b7187]252  uint_t c = 0, i = 0;
[e4c6c76]253  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
254      s->max_size, write_data->length, write);
[52ca8a3]255
[a97eb17]256  for (c = 0; c < s->channels; c++) {
[e4c6c76]257    for (i = 0; i < length; i++) {
[a97eb17]258      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
259    }
[52ca8a3]260  }
261
[a2b7187]262  aubio_sink_wavwrite_write_frames(s, length);
[52ca8a3]263}
264
[870ad70]265void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
[a2b7187]266  uint_t c = 0, i = 0;
[870ad70]267
[e4c6c76]268  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
269      s->channels, write_data->height);
270  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
271      s->max_size, write_data->length, write);
[870ad70]272
[e4c6c76]273  for (c = 0; c < channels; c++) {
274    for (i = 0; i < length; i++) {
[870ad70]275      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
276    }
277  }
278
[a2b7187]279  aubio_sink_wavwrite_write_frames(s, length);
[870ad70]280}
281
[a9fd272]282uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
[52ca8a3]283  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
284  unsigned char buf[5];
[f5a97ed]285  size_t written = 0, err = 0;
[a9fd272]286  if (!s->fid) return AUBIO_FAIL;
[52ca8a3]287  // ChunkSize
[f5a97ed]288  err += fseek(s->fid, 4, SEEK_SET);
289  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
[52ca8a3]290  // Subchunk2Size
[f5a97ed]291  err += fseek(s->fid, 40, SEEK_SET);
292  written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
293  if (written != 2 || err != 0) {
[b03f1bf]294    AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d"
[f5a97ed]295        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
296  }
[52ca8a3]297  // close file
298  if (fclose(s->fid)) {
[b03f1bf]299    AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
[52ca8a3]300  }
301  s->fid = NULL;
[a9fd272]302  return AUBIO_OK;
[52ca8a3]303}
304
305void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
[3e1c482]306  AUBIO_ASSERT(s);
307  if (s->fid)
308    aubio_sink_wavwrite_close(s);
309  if (s->path)
310    AUBIO_FREE(s->path);
311  if (s->scratch_data)
312    AUBIO_FREE(s->scratch_data);
[52ca8a3]313  AUBIO_FREE(s);
314}
315
316#endif /* HAVE_WAVWRITE */
Note: See TracBrowser for help on using the repository browser.