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