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