[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 | |
---|
| 22 | #include "config.h" |
---|
| 23 | |
---|
| 24 | #ifdef HAVE_WAVWRITE |
---|
| 25 | |
---|
| 26 | #include "aubio_priv.h" |
---|
| 27 | #include "fvec.h" |
---|
[870ad70] | 28 | #include "fmat.h" |
---|
| 29 | #include "io/sink_wavwrite.h" |
---|
[52ca8a3] | 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 |
---|
[fdeba11] | 47 | #ifdef HAVE_WIN_HACKS |
---|
| 48 | #define HTOLES(x) x |
---|
| 49 | #else |
---|
[52ca8a3] | 50 | #define HTOLES(x) SWAPS(htons(x)) |
---|
| 51 | #endif |
---|
[fdeba11] | 52 | #endif |
---|
[52ca8a3] | 53 | |
---|
[870ad70] | 54 | uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s); |
---|
| 55 | |
---|
[52ca8a3] | 56 | struct _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 | |
---|
[80d0083] | 71 | static unsigned char *write_little_endian (unsigned int s, unsigned char *str, |
---|
| 72 | unsigned int length); |
---|
| 73 | |
---|
| 74 | static unsigned char *write_little_endian (unsigned int s, unsigned char *str, |
---|
| 75 | unsigned int length) |
---|
| 76 | { |
---|
[52ca8a3] | 77 | uint_t i; |
---|
| 78 | for (i = 0; i < length; i++) { |
---|
| 79 | str[i] = s >> (i * 8); |
---|
| 80 | } |
---|
| 81 | return str; |
---|
| 82 | } |
---|
| 83 | |
---|
[ae5d58a] | 84 | aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) { |
---|
[52ca8a3] | 85 | aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t); |
---|
| 86 | |
---|
| 87 | if (path == NULL) { |
---|
| 88 | AUBIO_ERR("sink_wavwrite: Aborted opening null path\n"); |
---|
| 89 | goto beach; |
---|
| 90 | } |
---|
| 91 | if ((sint_t)samplerate < 0) { |
---|
| 92 | AUBIO_ERR("sink_wavwrite: Can not create %s with samplerate %d\n", path, samplerate); |
---|
| 93 | goto beach; |
---|
| 94 | } |
---|
| 95 | |
---|
[b643a33] | 96 | if (s->path) AUBIO_FREE(s->path); |
---|
[d2be104] | 97 | s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1); |
---|
| 98 | strncpy(s->path, path, strnlen(path, PATH_MAX) + 1); |
---|
[b643a33] | 99 | |
---|
[52ca8a3] | 100 | s->max_size = MAX_SIZE; |
---|
| 101 | s->bitspersample = 16; |
---|
| 102 | s->total_frames_written = 0; |
---|
| 103 | |
---|
[870ad70] | 104 | s->samplerate = 0; |
---|
| 105 | s->channels = 0; |
---|
| 106 | |
---|
| 107 | // negative samplerate given, abort |
---|
| 108 | if ((sint_t)samplerate < 0) goto beach; |
---|
| 109 | // zero samplerate given. do not open yet |
---|
| 110 | if ((sint_t)samplerate == 0) return s; |
---|
[4d6024f] | 111 | // samplerate way too large, fail |
---|
| 112 | if ((sint_t)samplerate > 192000 * 4) goto beach; |
---|
[870ad70] | 113 | |
---|
| 114 | s->samplerate = samplerate; |
---|
| 115 | s->channels = 1; |
---|
| 116 | |
---|
| 117 | if (aubio_sink_wavwrite_open(s) != AUBIO_OK) { |
---|
| 118 | // open failed, abort |
---|
| 119 | goto beach; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return s; |
---|
| 123 | beach: |
---|
| 124 | //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n", |
---|
| 125 | // s->path, s->samplerate); |
---|
| 126 | del_aubio_sink_wavwrite(s); |
---|
| 127 | return NULL; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate) |
---|
| 131 | { |
---|
| 132 | if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL; |
---|
| 133 | s->samplerate = samplerate; |
---|
| 134 | // automatically open when both samplerate and channels have been set |
---|
| 135 | if (s->samplerate != 0 && s->channels != 0) { |
---|
| 136 | return aubio_sink_wavwrite_open(s); |
---|
| 137 | } |
---|
| 138 | return AUBIO_OK; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels) |
---|
| 142 | { |
---|
| 143 | if ((sint_t)(channels) <= 0) return AUBIO_FAIL; |
---|
| 144 | s->channels = channels; |
---|
| 145 | // automatically open when both samplerate and channels have been set |
---|
| 146 | if (s->samplerate != 0 && s->channels != 0) { |
---|
| 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; |
---|
| 165 | |
---|
| 166 | /* open output file */ |
---|
| 167 | s->fid = fopen((const char *)s->path, "wb"); |
---|
[52ca8a3] | 168 | if (!s->fid) { |
---|
| 169 | AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno)); |
---|
| 170 | goto beach; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // ChunkID |
---|
| 174 | fwrite("RIFF", 4, 1, s->fid); |
---|
| 175 | |
---|
| 176 | // ChunkSize (0 for now, actual size will be written in _close) |
---|
| 177 | fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); |
---|
| 178 | |
---|
| 179 | // Format |
---|
| 180 | fwrite("WAVE", 4, 1, s->fid); |
---|
| 181 | |
---|
| 182 | // Subchunk1ID |
---|
| 183 | fwrite("fmt ", 4, 1, s->fid); |
---|
| 184 | |
---|
| 185 | // Subchunk1Size |
---|
| 186 | fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid); |
---|
| 187 | |
---|
| 188 | // AudioFormat |
---|
| 189 | fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid); |
---|
| 190 | |
---|
| 191 | // NumChannels |
---|
| 192 | fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid); |
---|
| 193 | |
---|
| 194 | // SampleRate |
---|
| 195 | fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid); |
---|
| 196 | |
---|
| 197 | // ByteRate |
---|
| 198 | byterate = s->samplerate * s->channels * s->bitspersample / 8; |
---|
| 199 | fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid); |
---|
| 200 | |
---|
| 201 | // BlockAlign |
---|
| 202 | blockalign = s->channels * s->bitspersample / 8; |
---|
| 203 | fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid); |
---|
| 204 | |
---|
| 205 | // BitsPerSample |
---|
| 206 | fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid); |
---|
| 207 | |
---|
| 208 | // Subchunk2ID |
---|
| 209 | fwrite("data", 4, 1, s->fid); |
---|
| 210 | |
---|
| 211 | // Subchunk1Size (0 for now, actual size will be written in _close) |
---|
| 212 | fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); |
---|
| 213 | |
---|
| 214 | s->scratch_size = s->max_size * s->channels; |
---|
| 215 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 216 | if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) { |
---|
[870ad70] | 217 | AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n", |
---|
| 218 | s->max_size, s->channels, MAX_SIZE * MAX_CHANNELS); |
---|
| 219 | goto beach; |
---|
[52ca8a3] | 220 | } |
---|
| 221 | s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size); |
---|
| 222 | |
---|
[870ad70] | 223 | return AUBIO_OK; |
---|
[52ca8a3] | 224 | |
---|
| 225 | beach: |
---|
[870ad70] | 226 | return AUBIO_FAIL; |
---|
[52ca8a3] | 227 | } |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){ |
---|
| 231 | uint_t i = 0, written_frames = 0; |
---|
| 232 | |
---|
| 233 | if (write > s->max_size) { |
---|
| 234 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 235 | "but only %d can be written at a time\n", write, s->path, s->max_size); |
---|
| 236 | write = s->max_size; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | for (i = 0; i < write; i++) { |
---|
| 240 | s->scratch_data[i] = HTOLES(FLOAT_TO_SHORT(write_data->data[i])); |
---|
| 241 | } |
---|
| 242 | written_frames = fwrite(s->scratch_data, 2, write, s->fid); |
---|
| 243 | |
---|
| 244 | if (written_frames != write) { |
---|
| 245 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 246 | "but only %d could be written\n", write, s->path, written_frames); |
---|
| 247 | } |
---|
| 248 | s->total_frames_written += written_frames; |
---|
| 249 | return; |
---|
| 250 | } |
---|
| 251 | |
---|
[870ad70] | 252 | void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){ |
---|
| 253 | uint_t c = 0, i = 0, written_frames = 0; |
---|
| 254 | |
---|
| 255 | if (write > s->max_size) { |
---|
| 256 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 257 | "but only %d can be written at a time\n", write, s->path, s->max_size); |
---|
| 258 | write = s->max_size; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | for (c = 0; c < s->channels; c++) { |
---|
| 262 | for (i = 0; i < write; i++) { |
---|
| 263 | s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i])); |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid); |
---|
| 267 | |
---|
| 268 | if (written_frames != write * s->channels) { |
---|
| 269 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
| 270 | "but only %d could be written\n", write, s->path, written_frames / s->channels); |
---|
| 271 | } |
---|
| 272 | s->total_frames_written += written_frames; |
---|
| 273 | return; |
---|
| 274 | } |
---|
| 275 | |
---|
[a9fd272] | 276 | uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) { |
---|
[52ca8a3] | 277 | uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8; |
---|
| 278 | unsigned char buf[5]; |
---|
[a9fd272] | 279 | if (!s->fid) return AUBIO_FAIL; |
---|
[52ca8a3] | 280 | // ChunkSize |
---|
| 281 | fseek(s->fid, 4, SEEK_SET); |
---|
| 282 | fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid); |
---|
| 283 | // Subchunk2Size |
---|
| 284 | fseek(s->fid, 40, SEEK_SET); |
---|
| 285 | fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); |
---|
| 286 | // close file |
---|
| 287 | if (fclose(s->fid)) { |
---|
| 288 | AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno)); |
---|
| 289 | } |
---|
| 290 | s->fid = NULL; |
---|
[a9fd272] | 291 | return AUBIO_OK; |
---|
[52ca8a3] | 292 | } |
---|
| 293 | |
---|
| 294 | void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){ |
---|
| 295 | if (!s) return; |
---|
| 296 | aubio_sink_wavwrite_close(s); |
---|
[b643a33] | 297 | if (s->path) AUBIO_FREE(s->path); |
---|
[52ca8a3] | 298 | AUBIO_FREE(s->scratch_data); |
---|
| 299 | AUBIO_FREE(s); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | #endif /* HAVE_WAVWRITE */ |
---|