[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 | if ((sint_t)samplerate < 0) { |
---|
| 91 | AUBIO_ERR("sink_wavwrite: Can not create %s with samplerate %d\n", path, samplerate); |
---|
| 92 | goto beach; |
---|
| 93 | } |
---|
| 94 | |
---|
[b643a33] | 95 | if (s->path) AUBIO_FREE(s->path); |
---|
[d2be104] | 96 | s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1); |
---|
| 97 | strncpy(s->path, path, strnlen(path, PATH_MAX) + 1); |
---|
[b643a33] | 98 | |
---|
[52ca8a3] | 99 | s->max_size = MAX_SIZE; |
---|
| 100 | s->bitspersample = 16; |
---|
| 101 | s->total_frames_written = 0; |
---|
| 102 | |
---|
[870ad70] | 103 | s->samplerate = 0; |
---|
| 104 | s->channels = 0; |
---|
| 105 | |
---|
| 106 | // zero samplerate given. do not open yet |
---|
[cf19b8a] | 107 | if ((sint_t)samplerate == 0) { |
---|
| 108 | return s; |
---|
| 109 | } |
---|
| 110 | // invalid samplerate given, abort |
---|
| 111 | if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) { |
---|
| 112 | goto beach; |
---|
| 113 | } |
---|
[870ad70] | 114 | |
---|
| 115 | s->samplerate = samplerate; |
---|
| 116 | s->channels = 1; |
---|
| 117 | |
---|
| 118 | if (aubio_sink_wavwrite_open(s) != AUBIO_OK) { |
---|
| 119 | // open failed, abort |
---|
| 120 | goto beach; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return s; |
---|
| 124 | beach: |
---|
| 125 | //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n", |
---|
| 126 | // s->path, s->samplerate); |
---|
| 127 | del_aubio_sink_wavwrite(s); |
---|
| 128 | return NULL; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate) |
---|
| 132 | { |
---|
[cf19b8a] | 133 | if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) { |
---|
| 134 | return AUBIO_FAIL; |
---|
| 135 | } |
---|
[870ad70] | 136 | s->samplerate = samplerate; |
---|
| 137 | // automatically open when both samplerate and channels have been set |
---|
| 138 | if (s->samplerate != 0 && s->channels != 0) { |
---|
| 139 | return aubio_sink_wavwrite_open(s); |
---|
| 140 | } |
---|
| 141 | return AUBIO_OK; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels) |
---|
| 145 | { |
---|
[cf19b8a] | 146 | if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) { |
---|
| 147 | return AUBIO_FAIL; |
---|
| 148 | } |
---|
[870ad70] | 149 | s->channels = channels; |
---|
| 150 | // automatically open when both samplerate and channels have been set |
---|
| 151 | if (s->samplerate != 0 && s->channels != 0) { |
---|
| 152 | return aubio_sink_wavwrite_open(s); |
---|
| 153 | } |
---|
| 154 | return AUBIO_OK; |
---|
| 155 | } |
---|
| 156 | |
---|
[ae5d58a] | 157 | uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s) |
---|
[870ad70] | 158 | { |
---|
| 159 | return s->samplerate; |
---|
| 160 | } |
---|
| 161 | |
---|
[ae5d58a] | 162 | uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s) |
---|
[870ad70] | 163 | { |
---|
| 164 | return s->channels; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) { |
---|
| 168 | unsigned char buf[5]; |
---|
| 169 | uint_t byterate, blockalign; |
---|
| 170 | |
---|
| 171 | /* open output file */ |
---|
| 172 | s->fid = fopen((const char *)s->path, "wb"); |
---|
[52ca8a3] | 173 | if (!s->fid) { |
---|
| 174 | AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno)); |
---|
| 175 | goto beach; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | // ChunkID |
---|
| 179 | fwrite("RIFF", 4, 1, s->fid); |
---|
| 180 | |
---|
| 181 | // ChunkSize (0 for now, actual size will be written in _close) |
---|
| 182 | fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); |
---|
| 183 | |
---|
| 184 | // Format |
---|
| 185 | fwrite("WAVE", 4, 1, s->fid); |
---|
| 186 | |
---|
| 187 | // Subchunk1ID |
---|
| 188 | fwrite("fmt ", 4, 1, s->fid); |
---|
| 189 | |
---|
| 190 | // Subchunk1Size |
---|
| 191 | fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid); |
---|
| 192 | |
---|
| 193 | // AudioFormat |
---|
| 194 | fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid); |
---|
| 195 | |
---|
| 196 | // NumChannels |
---|
| 197 | fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid); |
---|
| 198 | |
---|
| 199 | // SampleRate |
---|
| 200 | fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid); |
---|
| 201 | |
---|
| 202 | // ByteRate |
---|
| 203 | byterate = s->samplerate * s->channels * s->bitspersample / 8; |
---|
| 204 | fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid); |
---|
| 205 | |
---|
| 206 | // BlockAlign |
---|
| 207 | blockalign = s->channels * s->bitspersample / 8; |
---|
| 208 | fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid); |
---|
| 209 | |
---|
| 210 | // BitsPerSample |
---|
| 211 | fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid); |
---|
| 212 | |
---|
| 213 | // Subchunk2ID |
---|
| 214 | fwrite("data", 4, 1, s->fid); |
---|
| 215 | |
---|
| 216 | // Subchunk1Size (0 for now, actual size will be written in _close) |
---|
| 217 | fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); |
---|
| 218 | |
---|
| 219 | s->scratch_size = s->max_size * s->channels; |
---|
| 220 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
[a028a04] | 221 | if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) { |
---|
[870ad70] | 222 | AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n", |
---|
[a028a04] | 223 | s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS); |
---|
[870ad70] | 224 | goto beach; |
---|
[52ca8a3] | 225 | } |
---|
| 226 | s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size); |
---|
| 227 | |
---|
[870ad70] | 228 | return AUBIO_OK; |
---|
[52ca8a3] | 229 | |
---|
| 230 | beach: |
---|
[870ad70] | 231 | return AUBIO_FAIL; |
---|
[52ca8a3] | 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){ |
---|
| 236 | uint_t i = 0, written_frames = 0; |
---|
| 237 | |
---|
| 238 | if (write > s->max_size) { |
---|
| 239 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 240 | "but only %d can be written at a time\n", write, s->path, s->max_size); |
---|
| 241 | write = s->max_size; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | for (i = 0; i < write; i++) { |
---|
| 245 | s->scratch_data[i] = HTOLES(FLOAT_TO_SHORT(write_data->data[i])); |
---|
| 246 | } |
---|
| 247 | written_frames = fwrite(s->scratch_data, 2, write, s->fid); |
---|
| 248 | |
---|
| 249 | if (written_frames != write) { |
---|
| 250 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 251 | "but only %d could be written\n", write, s->path, written_frames); |
---|
| 252 | } |
---|
| 253 | s->total_frames_written += written_frames; |
---|
| 254 | return; |
---|
| 255 | } |
---|
| 256 | |
---|
[870ad70] | 257 | void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){ |
---|
| 258 | uint_t c = 0, i = 0, written_frames = 0; |
---|
| 259 | |
---|
| 260 | if (write > s->max_size) { |
---|
| 261 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 262 | "but only %d can be written at a time\n", write, s->path, s->max_size); |
---|
| 263 | write = s->max_size; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | for (c = 0; c < s->channels; c++) { |
---|
| 267 | for (i = 0; i < write; i++) { |
---|
| 268 | s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i])); |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid); |
---|
| 272 | |
---|
| 273 | if (written_frames != write * s->channels) { |
---|
| 274 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 275 | "but only %d could be written\n", write, s->path, written_frames / s->channels); |
---|
| 276 | } |
---|
| 277 | s->total_frames_written += written_frames; |
---|
| 278 | return; |
---|
| 279 | } |
---|
| 280 | |
---|
[a9fd272] | 281 | uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) { |
---|
[52ca8a3] | 282 | uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8; |
---|
| 283 | unsigned char buf[5]; |
---|
[a9fd272] | 284 | if (!s->fid) return AUBIO_FAIL; |
---|
[52ca8a3] | 285 | // ChunkSize |
---|
| 286 | fseek(s->fid, 4, SEEK_SET); |
---|
| 287 | fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid); |
---|
| 288 | // Subchunk2Size |
---|
| 289 | fseek(s->fid, 40, SEEK_SET); |
---|
| 290 | fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); |
---|
| 291 | // close file |
---|
| 292 | if (fclose(s->fid)) { |
---|
| 293 | AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno)); |
---|
| 294 | } |
---|
| 295 | s->fid = NULL; |
---|
[a9fd272] | 296 | return AUBIO_OK; |
---|
[52ca8a3] | 297 | } |
---|
| 298 | |
---|
| 299 | void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){ |
---|
| 300 | if (!s) return; |
---|
| 301 | aubio_sink_wavwrite_close(s); |
---|
[b643a33] | 302 | if (s->path) AUBIO_FREE(s->path); |
---|
[52ca8a3] | 303 | AUBIO_FREE(s->scratch_data); |
---|
| 304 | AUBIO_FREE(s); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | #endif /* HAVE_WAVWRITE */ |
---|