[dc46037b] | 1 | /* |
---|
| 2 | Copyright (C) 2018 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 | |
---|
[7e93013] | 21 | /* |
---|
| 22 | This file is largely inspired by `examples/encoder_example.c` in the |
---|
| 23 | libvorbis source package (versions 1.3.5 and later) available online at |
---|
| 24 | https://xiph.org/vorbis/ |
---|
| 25 | */ |
---|
| 26 | |
---|
[dc46037b] | 27 | #include "aubio_priv.h" |
---|
| 28 | |
---|
| 29 | #ifdef HAVE_VORBISENC |
---|
| 30 | |
---|
| 31 | #include "io/ioutils.h" |
---|
| 32 | #include "fmat.h" |
---|
| 33 | |
---|
| 34 | #include <vorbis/vorbisenc.h> |
---|
| 35 | #include <string.h> // strerror |
---|
| 36 | #include <errno.h> // errno |
---|
| 37 | #include <time.h> // time |
---|
| 38 | |
---|
[dc72476] | 39 | #define MAX_SIZE 2048 |
---|
| 40 | |
---|
[dc46037b] | 41 | struct _aubio_sink_vorbis_t { |
---|
| 42 | FILE *fid; // file id |
---|
| 43 | ogg_stream_state os; // stream |
---|
| 44 | ogg_page og; // page |
---|
| 45 | ogg_packet op; // data packet |
---|
| 46 | vorbis_info vi; // vorbis bitstream settings |
---|
| 47 | vorbis_comment vc; // user comment |
---|
| 48 | vorbis_dsp_state vd; // working state |
---|
| 49 | vorbis_block vb; // working space |
---|
| 50 | |
---|
| 51 | uint_t samplerate; |
---|
| 52 | uint_t channels; |
---|
| 53 | char_t *path; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | typedef struct _aubio_sink_vorbis_t aubio_sink_vorbis_t; |
---|
| 57 | |
---|
| 58 | uint_t aubio_sink_vorbis_preset_channels(aubio_sink_vorbis_t *s, |
---|
| 59 | uint_t channels); |
---|
| 60 | uint_t aubio_sink_vorbis_preset_samplerate(aubio_sink_vorbis_t *s, |
---|
| 61 | uint_t samplerate); |
---|
| 62 | uint_t aubio_sink_vorbis_open(aubio_sink_vorbis_t *s); |
---|
| 63 | uint_t aubio_sink_vorbis_close (aubio_sink_vorbis_t *s); |
---|
| 64 | void del_aubio_sink_vorbis (aubio_sink_vorbis_t *s); |
---|
| 65 | |
---|
| 66 | aubio_sink_vorbis_t * new_aubio_sink_vorbis (const char_t *uri, |
---|
| 67 | uint_t samplerate) |
---|
| 68 | { |
---|
| 69 | aubio_sink_vorbis_t * s = AUBIO_NEW(aubio_sink_vorbis_t); |
---|
| 70 | |
---|
[4c37e87] | 71 | if (!uri) { |
---|
[0e6ad10] | 72 | AUBIO_ERROR("sink_vorbis: Aborted opening null path\n"); |
---|
[4c37e87] | 73 | goto failure; |
---|
| 74 | } |
---|
| 75 | |
---|
[dc46037b] | 76 | s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1); |
---|
| 77 | strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1); |
---|
[be63940] | 78 | s->path[strnlen(uri, PATH_MAX)] = '\0'; |
---|
[dc46037b] | 79 | |
---|
| 80 | s->channels = 0; |
---|
| 81 | |
---|
| 82 | if ((sint_t)samplerate == 0) |
---|
| 83 | return s; |
---|
| 84 | |
---|
| 85 | aubio_sink_vorbis_preset_samplerate(s, samplerate); |
---|
| 86 | s->channels = 1; |
---|
| 87 | |
---|
| 88 | if (aubio_sink_vorbis_open(s) != AUBIO_OK) |
---|
| 89 | goto failure; |
---|
| 90 | |
---|
| 91 | return s; |
---|
| 92 | |
---|
| 93 | failure: |
---|
| 94 | del_aubio_sink_vorbis(s); |
---|
| 95 | return NULL; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void del_aubio_sink_vorbis (aubio_sink_vorbis_t *s) |
---|
| 99 | { |
---|
| 100 | if (s->fid) aubio_sink_vorbis_close(s); |
---|
| 101 | // clean up |
---|
| 102 | ogg_stream_clear(&s->os); |
---|
| 103 | vorbis_block_clear(&s->vb); |
---|
| 104 | vorbis_dsp_clear(&s->vd); |
---|
| 105 | vorbis_comment_clear(&s->vc); |
---|
| 106 | vorbis_info_clear(&s->vi); |
---|
| 107 | |
---|
[be63940] | 108 | if (s->path) AUBIO_FREE(s->path); |
---|
[dc46037b] | 109 | AUBIO_FREE(s); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | uint_t aubio_sink_vorbis_open(aubio_sink_vorbis_t *s) |
---|
| 113 | { |
---|
| 114 | float quality_mode = .9; |
---|
| 115 | |
---|
[6031419] | 116 | if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL; |
---|
[dc46037b] | 117 | |
---|
| 118 | s->fid = fopen((const char *)s->path, "wb"); |
---|
[0850e54] | 119 | if (!s->fid) { |
---|
| 120 | AUBIO_ERR("sink_vorbis: Error opening file %s (%s)\n", |
---|
| 121 | s->path, strerror(errno)); |
---|
| 122 | return AUBIO_FAIL; |
---|
| 123 | } |
---|
[dc46037b] | 124 | |
---|
| 125 | vorbis_info_init(&s->vi); |
---|
| 126 | if (vorbis_encode_init_vbr(&s->vi, s->channels, s->samplerate, quality_mode)) |
---|
| 127 | { |
---|
| 128 | AUBIO_ERR("sink_vorbis: vorbis_encode_init_vbr failed\n"); |
---|
| 129 | return AUBIO_FAIL; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // add comment |
---|
| 133 | vorbis_comment_init(&s->vc); |
---|
| 134 | vorbis_comment_add_tag(&s->vc, "ENCODER", "aubio"); |
---|
| 135 | |
---|
| 136 | // initalise analysis and block |
---|
| 137 | vorbis_analysis_init(&s->vd, &s->vi); |
---|
| 138 | vorbis_block_init(&s->vd, &s->vb); |
---|
| 139 | |
---|
| 140 | // pick randome serial number |
---|
| 141 | srand(time(NULL)); |
---|
| 142 | ogg_stream_init(&s->os, rand()); |
---|
| 143 | |
---|
| 144 | // write header |
---|
| 145 | { |
---|
| 146 | int ret = 0; |
---|
[0850e54] | 147 | size_t wrote; |
---|
[dc46037b] | 148 | ogg_packet header; |
---|
| 149 | ogg_packet header_comm; |
---|
| 150 | ogg_packet header_code; |
---|
| 151 | |
---|
| 152 | vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm, |
---|
| 153 | &header_code); |
---|
| 154 | |
---|
| 155 | ogg_stream_packetin(&s->os, &header); |
---|
| 156 | ogg_stream_packetin(&s->os, &header_comm); |
---|
| 157 | ogg_stream_packetin(&s->os, &header_code); |
---|
| 158 | |
---|
| 159 | // make sure audio data will start on a new page |
---|
| 160 | while (1) |
---|
| 161 | { |
---|
| 162 | ret = ogg_stream_flush(&s->os, &s->og); |
---|
| 163 | if (ret==0) break; |
---|
[0850e54] | 164 | wrote = fwrite(s->og.header, 1, s->og.header_len, s->fid); |
---|
| 165 | ret = (wrote == (unsigned)s->og.header_len); |
---|
| 166 | wrote = fwrite(s->og.body, 1, s->og.body_len, s->fid); |
---|
| 167 | ret &= (wrote == (unsigned)s->og.body_len); |
---|
| 168 | if (ret == 0) { |
---|
| 169 | AUBIO_ERR("sink_vorbis: failed writing \'%s\' to disk (%s)\n", |
---|
| 170 | s->path, strerror(errno)); |
---|
| 171 | return AUBIO_FAIL; |
---|
| 172 | } |
---|
[dc46037b] | 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return AUBIO_OK; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | uint_t aubio_sink_vorbis_preset_samplerate(aubio_sink_vorbis_t *s, |
---|
| 180 | uint_t samplerate) |
---|
| 181 | { |
---|
| 182 | if (aubio_io_validate_samplerate("sink_vorbis", s->path, samplerate)) |
---|
| 183 | return AUBIO_FAIL; |
---|
| 184 | s->samplerate = samplerate; |
---|
[252f585] | 185 | if (/* s->samplerate != 0 && */ s->channels != 0) |
---|
[dc46037b] | 186 | return aubio_sink_vorbis_open(s); |
---|
| 187 | return AUBIO_OK; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | uint_t aubio_sink_vorbis_preset_channels(aubio_sink_vorbis_t *s, |
---|
| 191 | uint_t channels) |
---|
| 192 | { |
---|
| 193 | if (aubio_io_validate_channels("sink_vorbis", s->path, channels)) { |
---|
| 194 | return AUBIO_FAIL; |
---|
| 195 | } |
---|
| 196 | s->channels = channels; |
---|
| 197 | // automatically open when both samplerate and channels have been set |
---|
[252f585] | 198 | if (s->samplerate != 0 /* && s->channels != 0 */) { |
---|
[dc46037b] | 199 | return aubio_sink_vorbis_open(s); |
---|
| 200 | } |
---|
| 201 | return AUBIO_OK; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | uint_t aubio_sink_vorbis_get_samplerate(const aubio_sink_vorbis_t *s) |
---|
| 205 | { |
---|
| 206 | return s->samplerate; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | uint_t aubio_sink_vorbis_get_channels(const aubio_sink_vorbis_t *s) |
---|
| 210 | { |
---|
| 211 | return s->channels; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | void aubio_sink_vorbis_write(aubio_sink_vorbis_t *s) |
---|
| 215 | { |
---|
[0850e54] | 216 | int result; |
---|
| 217 | size_t wrote; |
---|
[dc46037b] | 218 | // pre-analysis |
---|
| 219 | while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) { |
---|
| 220 | |
---|
| 221 | vorbis_analysis(&s->vb, NULL); |
---|
| 222 | vorbis_bitrate_addblock(&s->vb); |
---|
| 223 | |
---|
| 224 | while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) |
---|
| 225 | { |
---|
| 226 | ogg_stream_packetin(&s->os, &s->op); |
---|
| 227 | |
---|
| 228 | while (1) { |
---|
[0850e54] | 229 | result = ogg_stream_pageout (&s->os, &s->og); |
---|
[dc46037b] | 230 | if (result == 0) break; |
---|
[0850e54] | 231 | wrote = fwrite(s->og.header, 1, s->og.header_len, s->fid); |
---|
| 232 | result = (wrote == (unsigned)s->og.header_len); |
---|
| 233 | wrote = fwrite(s->og.body, 1, s->og.body_len, s->fid); |
---|
| 234 | result &= (wrote == (unsigned)s->og.body_len); |
---|
| 235 | if (result == 0) { |
---|
| 236 | AUBIO_WRN("sink_vorbis: failed writing \'%s\' to disk (%s)\n", |
---|
| 237 | s->path, strerror(errno)); |
---|
| 238 | } |
---|
[dc46037b] | 239 | if (ogg_page_eos(&s->og)) break; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | void aubio_sink_vorbis_do(aubio_sink_vorbis_t *s, fvec_t *write_data, |
---|
| 246 | uint_t write) |
---|
| 247 | { |
---|
| 248 | uint_t c, v; |
---|
[dc72476] | 249 | uint_t length = aubio_sink_validate_input_length("sink_vorbis", s->path, |
---|
| 250 | MAX_SIZE, write_data->length, write); |
---|
| 251 | float **buffer = vorbis_analysis_buffer(&s->vd, (long)length); |
---|
[dc46037b] | 252 | // fill buffer |
---|
| 253 | if (!write) { |
---|
| 254 | return; |
---|
| 255 | } else if (!buffer) { |
---|
| 256 | AUBIO_WRN("sink_vorbis: failed fetching buffer of size %d\n", write); |
---|
[4b9481f9] | 257 | return; |
---|
[dc46037b] | 258 | } else { |
---|
| 259 | for (c = 0; c < s->channels; c++) { |
---|
[dc72476] | 260 | for (v = 0; v < length; v++) { |
---|
[dc46037b] | 261 | buffer[c][v] = write_data->data[v]; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | // tell vorbis how many frames were written |
---|
[dc72476] | 265 | vorbis_analysis_wrote(&s->vd, (long)length); |
---|
[dc46037b] | 266 | } |
---|
| 267 | // write to file |
---|
| 268 | aubio_sink_vorbis_write(s); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | void aubio_sink_vorbis_do_multi(aubio_sink_vorbis_t *s, fmat_t *write_data, |
---|
| 272 | uint_t write) |
---|
| 273 | { |
---|
| 274 | uint_t c, v; |
---|
[dc72476] | 275 | uint_t channels = aubio_sink_validate_input_channels("sink_vorbis", s->path, |
---|
| 276 | s->channels, write_data->height); |
---|
| 277 | uint_t length = aubio_sink_validate_input_length("sink_vorbis", s->path, |
---|
| 278 | MAX_SIZE, write_data->length, write); |
---|
| 279 | float **buffer = vorbis_analysis_buffer(&s->vd, (long)length); |
---|
[dc46037b] | 280 | // fill buffer |
---|
| 281 | if (!write) { |
---|
| 282 | return; |
---|
| 283 | } else if (!buffer) { |
---|
| 284 | AUBIO_WRN("sink_vorbis: failed fetching buffer of size %d\n", write); |
---|
[4b9481f9] | 285 | return; |
---|
[dc46037b] | 286 | } else { |
---|
[6031419] | 287 | for (c = 0; c < channels; c++) { |
---|
[dc72476] | 288 | for (v = 0; v < length; v++) { |
---|
[dc46037b] | 289 | buffer[c][v] = write_data->data[c][v]; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | // tell vorbis how many frames were written |
---|
| 293 | vorbis_analysis_wrote(&s->vd, (long)write); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | aubio_sink_vorbis_write(s); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | uint_t aubio_sink_vorbis_close (aubio_sink_vorbis_t *s) |
---|
| 300 | { |
---|
[56fa1e0] | 301 | if (!s->fid) return AUBIO_FAIL; |
---|
[dc46037b] | 302 | //mark the end of stream |
---|
| 303 | vorbis_analysis_wrote(&s->vd, 0); |
---|
| 304 | |
---|
| 305 | aubio_sink_vorbis_write(s); |
---|
| 306 | |
---|
[7107ed9] | 307 | if (s->fid && fclose(s->fid)) { |
---|
[dc46037b] | 308 | AUBIO_ERR("sink_vorbis: Error closing file %s (%s)\n", |
---|
| 309 | s->path, strerror(errno)); |
---|
| 310 | return AUBIO_FAIL; |
---|
| 311 | } |
---|
[7107ed9] | 312 | s->fid = NULL; |
---|
[dc46037b] | 313 | return AUBIO_OK; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | #endif /* HAVE_VORBISENC */ |
---|