| 1 | /**************************************************************************/ |
| 2 | /* audio_stream_ogg_vorbis.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "audio_stream_ogg_vorbis.h" |
| 32 | #include "core/io/file_access.h" |
| 33 | |
| 34 | #include "core/templates/rb_map.h" |
| 35 | |
| 36 | #include <ogg/ogg.h> |
| 37 | |
| 38 | int AudioStreamPlaybackOggVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) { |
| 39 | ERR_FAIL_COND_V(!ready, 0); |
| 40 | |
| 41 | if (!active) { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int todo = p_frames; |
| 46 | |
| 47 | int beat_length_frames = -1; |
| 48 | bool use_loop = looping_override ? looping : vorbis_stream->loop; |
| 49 | |
| 50 | if (use_loop && vorbis_stream->get_bpm() > 0 && vorbis_stream->get_beat_count() > 0) { |
| 51 | beat_length_frames = vorbis_stream->get_beat_count() * vorbis_data->get_sampling_rate() * 60 / vorbis_stream->get_bpm(); |
| 52 | } |
| 53 | |
| 54 | while (todo > 0 && active) { |
| 55 | AudioFrame *buffer = p_buffer; |
| 56 | buffer += p_frames - todo; |
| 57 | |
| 58 | int to_mix = todo; |
| 59 | if (beat_length_frames >= 0 && (beat_length_frames - (int)frames_mixed) < to_mix) { |
| 60 | to_mix = MAX(0, beat_length_frames - (int)frames_mixed); |
| 61 | } |
| 62 | |
| 63 | int mixed = _mix_frames_vorbis(buffer, to_mix); |
| 64 | ERR_FAIL_COND_V(mixed < 0, 0); |
| 65 | todo -= mixed; |
| 66 | frames_mixed += mixed; |
| 67 | |
| 68 | if (loop_fade_remaining < FADE_SIZE) { |
| 69 | int to_fade = loop_fade_remaining + MIN(FADE_SIZE - loop_fade_remaining, mixed); |
| 70 | for (int i = loop_fade_remaining; i < to_fade; i++) { |
| 71 | buffer[i - loop_fade_remaining] += loop_fade[i] * (float(FADE_SIZE - i) / float(FADE_SIZE)); |
| 72 | } |
| 73 | loop_fade_remaining = to_fade; |
| 74 | } |
| 75 | |
| 76 | if (beat_length_frames >= 0) { |
| 77 | /** |
| 78 | * Length determined by beat length |
| 79 | * This code is commented out because, in practice, it is preferred that the fade |
| 80 | * is done by the transitioner and this stream just goes on until it ends while fading out. |
| 81 | * |
| 82 | * End fade implementation is left here for reference in case at some point this feature |
| 83 | * is desired. |
| 84 | |
| 85 | if (!beat_loop && (int)frames_mixed > beat_length_frames - FADE_SIZE) { |
| 86 | print_line("beat length fade/after mix?"); |
| 87 | //No loop, just fade and finish |
| 88 | for (int i = 0; i < mixed; i++) { |
| 89 | int idx = frames_mixed + i - mixed; |
| 90 | buffer[i] *= 1.0 - float(MAX(0, (idx - (beat_length_frames - FADE_SIZE)))) / float(FADE_SIZE); |
| 91 | } |
| 92 | if ((int)frames_mixed == beat_length_frames) { |
| 93 | for (int i = p_frames - todo; i < p_frames; i++) { |
| 94 | p_buffer[i] = AudioFrame(0, 0); |
| 95 | } |
| 96 | active = false; |
| 97 | break; |
| 98 | } |
| 99 | } else |
| 100 | **/ |
| 101 | |
| 102 | if (use_loop && beat_length_frames <= (int)frames_mixed) { |
| 103 | // End of file when doing beat-based looping. <= used instead of == because importer editing |
| 104 | if (!have_packets_left && !have_samples_left) { |
| 105 | //Nothing remaining, so do nothing. |
| 106 | loop_fade_remaining = FADE_SIZE; |
| 107 | } else { |
| 108 | // Add some loop fade; |
| 109 | int faded_mix = _mix_frames_vorbis(loop_fade, FADE_SIZE); |
| 110 | |
| 111 | for (int i = faded_mix; i < FADE_SIZE; i++) { |
| 112 | // In case lesss was mixed, pad with zeros |
| 113 | loop_fade[i] = AudioFrame(0, 0); |
| 114 | } |
| 115 | loop_fade_remaining = 0; |
| 116 | } |
| 117 | |
| 118 | seek(vorbis_stream->loop_offset); |
| 119 | loops++; |
| 120 | // We still have buffer to fill, start from this element in the next iteration. |
| 121 | continue; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (!have_packets_left && !have_samples_left) { |
| 126 | // Actual end of file! |
| 127 | bool is_not_empty = mixed > 0 || vorbis_stream->get_length() > 0; |
| 128 | if (use_loop && is_not_empty) { |
| 129 | //loop |
| 130 | |
| 131 | seek(vorbis_stream->loop_offset); |
| 132 | loops++; |
| 133 | // We still have buffer to fill, start from this element in the next iteration. |
| 134 | |
| 135 | } else { |
| 136 | for (int i = p_frames - todo; i < p_frames; i++) { |
| 137 | p_buffer[i] = AudioFrame(0, 0); |
| 138 | } |
| 139 | active = false; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | return p_frames - todo; |
| 144 | } |
| 145 | |
| 146 | int AudioStreamPlaybackOggVorbis::_mix_frames_vorbis(AudioFrame *p_buffer, int p_frames) { |
| 147 | ERR_FAIL_COND_V(!ready, p_frames); |
| 148 | if (!have_samples_left) { |
| 149 | ogg_packet *packet = nullptr; |
| 150 | int err; |
| 151 | |
| 152 | if (!vorbis_data_playback->next_ogg_packet(&packet)) { |
| 153 | have_packets_left = false; |
| 154 | WARN_PRINT("ran out of packets in stream"); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | err = vorbis_synthesis(&block, packet); |
| 159 | ERR_FAIL_COND_V_MSG(err != 0, p_frames, "Error during vorbis synthesis " + itos(err)); |
| 160 | |
| 161 | err = vorbis_synthesis_blockin(&dsp_state, &block); |
| 162 | ERR_FAIL_COND_V_MSG(err != 0, p_frames, "Error during vorbis block processing " + itos(err)); |
| 163 | |
| 164 | have_packets_left = !packet->e_o_s; |
| 165 | } |
| 166 | |
| 167 | float **pcm; // Accessed with pcm[channel_idx][sample_idx]. |
| 168 | |
| 169 | int frames = vorbis_synthesis_pcmout(&dsp_state, &pcm); |
| 170 | if (frames > p_frames) { |
| 171 | frames = p_frames; |
| 172 | have_samples_left = true; |
| 173 | } else { |
| 174 | have_samples_left = false; |
| 175 | } |
| 176 | |
| 177 | if (info.channels > 1) { |
| 178 | for (int frame = 0; frame < frames; frame++) { |
| 179 | p_buffer[frame].left = pcm[0][frame]; |
| 180 | p_buffer[frame].right = pcm[1][frame]; |
| 181 | } |
| 182 | } else { |
| 183 | for (int frame = 0; frame < frames; frame++) { |
| 184 | p_buffer[frame].left = pcm[0][frame]; |
| 185 | p_buffer[frame].right = pcm[0][frame]; |
| 186 | } |
| 187 | } |
| 188 | vorbis_synthesis_read(&dsp_state, frames); |
| 189 | return frames; |
| 190 | } |
| 191 | |
| 192 | float AudioStreamPlaybackOggVorbis::get_stream_sampling_rate() { |
| 193 | return vorbis_data->get_sampling_rate(); |
| 194 | } |
| 195 | |
| 196 | bool AudioStreamPlaybackOggVorbis::_alloc_vorbis() { |
| 197 | vorbis_info_init(&info); |
| 198 | info_is_allocated = true; |
| 199 | vorbis_comment_init(&comment); |
| 200 | comment_is_allocated = true; |
| 201 | |
| 202 | ERR_FAIL_COND_V(vorbis_data.is_null(), false); |
| 203 | vorbis_data_playback = vorbis_data->instantiate_playback(); |
| 204 | |
| 205 | ogg_packet *packet; |
| 206 | int err; |
| 207 | |
| 208 | for (int i = 0; i < 3; i++) { |
| 209 | if (!vorbis_data_playback->next_ogg_packet(&packet)) { |
| 210 | WARN_PRINT("Not enough packets to parse header"); |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | err = vorbis_synthesis_headerin(&info, &comment, packet); |
| 215 | ERR_FAIL_COND_V_MSG(err != 0, false, "Error parsing header"); |
| 216 | } |
| 217 | |
| 218 | err = vorbis_synthesis_init(&dsp_state, &info); |
| 219 | ERR_FAIL_COND_V_MSG(err != 0, false, "Error initializing dsp state"); |
| 220 | dsp_state_is_allocated = true; |
| 221 | |
| 222 | err = vorbis_block_init(&dsp_state, &block); |
| 223 | ERR_FAIL_COND_V_MSG(err != 0, false, "Error initializing block"); |
| 224 | block_is_allocated = true; |
| 225 | |
| 226 | ready = true; |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | void AudioStreamPlaybackOggVorbis::start(double p_from_pos) { |
| 232 | ERR_FAIL_COND(!ready); |
| 233 | loop_fade_remaining = FADE_SIZE; |
| 234 | active = true; |
| 235 | seek(p_from_pos); |
| 236 | loops = 0; |
| 237 | begin_resample(); |
| 238 | } |
| 239 | |
| 240 | void AudioStreamPlaybackOggVorbis::stop() { |
| 241 | active = false; |
| 242 | } |
| 243 | |
| 244 | bool AudioStreamPlaybackOggVorbis::is_playing() const { |
| 245 | return active; |
| 246 | } |
| 247 | |
| 248 | int AudioStreamPlaybackOggVorbis::get_loop_count() const { |
| 249 | return loops; |
| 250 | } |
| 251 | |
| 252 | double AudioStreamPlaybackOggVorbis::get_playback_position() const { |
| 253 | return double(frames_mixed) / (double)vorbis_data->get_sampling_rate(); |
| 254 | } |
| 255 | |
| 256 | void AudioStreamPlaybackOggVorbis::tag_used_streams() { |
| 257 | vorbis_stream->tag_used(get_playback_position()); |
| 258 | } |
| 259 | |
| 260 | void AudioStreamPlaybackOggVorbis::set_parameter(const StringName &p_name, const Variant &p_value) { |
| 261 | if (p_name == SNAME("looping")) { |
| 262 | if (p_value == Variant()) { |
| 263 | looping_override = false; |
| 264 | looping = false; |
| 265 | } else { |
| 266 | looping_override = true; |
| 267 | looping = p_value; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | Variant AudioStreamPlaybackOggVorbis::get_parameter(const StringName &p_name) const { |
| 273 | if (looping_override && p_name == SNAME("looping")) { |
| 274 | return looping; |
| 275 | } |
| 276 | return Variant(); |
| 277 | } |
| 278 | |
| 279 | void AudioStreamPlaybackOggVorbis::seek(double p_time) { |
| 280 | ERR_FAIL_COND(!ready); |
| 281 | ERR_FAIL_COND(vorbis_stream.is_null()); |
| 282 | if (!active) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (p_time >= vorbis_stream->get_length()) { |
| 287 | p_time = 0; |
| 288 | } |
| 289 | |
| 290 | frames_mixed = uint32_t(vorbis_data->get_sampling_rate() * p_time); |
| 291 | |
| 292 | const int64_t desired_sample = p_time * get_stream_sampling_rate(); |
| 293 | |
| 294 | if (!vorbis_data_playback->seek_page(desired_sample)) { |
| 295 | WARN_PRINT("seek failed"); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // We want to start decoding before the page that we expect the sample to be in (the sample may |
| 300 | // be part of a partial packet across page boundaries). Otherwise, the decoder may not have |
| 301 | // synchronized before reaching the sample. |
| 302 | int64_t start_page_number = vorbis_data_playback->get_page_number() - 1; |
| 303 | if (start_page_number < 0) { |
| 304 | start_page_number = 0; |
| 305 | } |
| 306 | |
| 307 | while (true) { |
| 308 | ogg_packet *packet; |
| 309 | int err; |
| 310 | |
| 311 | // We start at an unknown granule position. |
| 312 | int64_t granule_pos = -1; |
| 313 | |
| 314 | // Decode data until we get to the desired sample or notice that we have read past it. |
| 315 | vorbis_data_playback->set_page_number(start_page_number); |
| 316 | vorbis_synthesis_restart(&dsp_state); |
| 317 | |
| 318 | while (true) { |
| 319 | if (!vorbis_data_playback->next_ogg_packet(&packet)) { |
| 320 | WARN_PRINT_ONCE("Seeking beyond limits"); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | err = vorbis_synthesis(&block, packet); |
| 325 | if (err != OV_ENOTAUDIO) { |
| 326 | ERR_FAIL_COND_MSG(err != 0, "Error during vorbis synthesis " + itos(err) + "."); |
| 327 | |
| 328 | err = vorbis_synthesis_blockin(&dsp_state, &block); |
| 329 | ERR_FAIL_COND_MSG(err != 0, "Error during vorbis block processing " + itos(err) + "."); |
| 330 | |
| 331 | int samples_out = vorbis_synthesis_pcmout(&dsp_state, nullptr); |
| 332 | |
| 333 | if (granule_pos < 0) { |
| 334 | // We don't know where we are yet, so just keep on decoding. |
| 335 | err = vorbis_synthesis_read(&dsp_state, samples_out); |
| 336 | ERR_FAIL_COND_MSG(err != 0, "Error during vorbis read updating " + itos(err) + "."); |
| 337 | } else if (granule_pos + samples_out >= desired_sample) { |
| 338 | // Our sample is in this block. Skip the beginning of the block up to the sample, then |
| 339 | // return. |
| 340 | int skip_samples = (int)(desired_sample - granule_pos); |
| 341 | err = vorbis_synthesis_read(&dsp_state, skip_samples); |
| 342 | ERR_FAIL_COND_MSG(err != 0, "Error during vorbis read updating " + itos(err) + "."); |
| 343 | have_samples_left = skip_samples < samples_out; |
| 344 | have_packets_left = !packet->e_o_s; |
| 345 | return; |
| 346 | } else { |
| 347 | // Our sample is not in this block. Skip it. |
| 348 | err = vorbis_synthesis_read(&dsp_state, samples_out); |
| 349 | ERR_FAIL_COND_MSG(err != 0, "Error during vorbis read updating " + itos(err) + "."); |
| 350 | granule_pos += samples_out; |
| 351 | } |
| 352 | } |
| 353 | if (packet->granulepos != -1) { |
| 354 | // We found an update to our granule position. |
| 355 | granule_pos = packet->granulepos; |
| 356 | if (granule_pos > desired_sample) { |
| 357 | // We've read past our sample. We need to start on an earlier page. |
| 358 | if (start_page_number == 0) { |
| 359 | // We didn't find the sample even reading from the beginning. |
| 360 | have_samples_left = false; |
| 361 | have_packets_left = !packet->e_o_s; |
| 362 | return; |
| 363 | } |
| 364 | start_page_number--; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | if (packet->e_o_s) { |
| 369 | // We've reached the end of the stream and didn't find our sample. |
| 370 | have_samples_left = false; |
| 371 | have_packets_left = false; |
| 372 | return; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | void AudioStreamPlaybackOggVorbis::set_is_sample(bool p_is_sample) { |
| 379 | _is_sample = p_is_sample; |
| 380 | } |
| 381 | |
| 382 | bool AudioStreamPlaybackOggVorbis::get_is_sample() const { |
| 383 | return _is_sample; |
| 384 | } |
| 385 | |
| 386 | Ref<AudioSamplePlayback> AudioStreamPlaybackOggVorbis::get_sample_playback() const { |
| 387 | return sample_playback; |
| 388 | } |
| 389 | |
| 390 | void AudioStreamPlaybackOggVorbis::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { |
| 391 | sample_playback = p_playback; |
| 392 | if (sample_playback.is_valid()) { |
| 393 | sample_playback->stream_playback = Ref<AudioStreamPlayback>(this); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | AudioStreamPlaybackOggVorbis::~AudioStreamPlaybackOggVorbis() { |
| 398 | if (block_is_allocated) { |
| 399 | vorbis_block_clear(&block); |
| 400 | } |
| 401 | if (dsp_state_is_allocated) { |
| 402 | vorbis_dsp_clear(&dsp_state); |
| 403 | } |
| 404 | if (comment_is_allocated) { |
| 405 | vorbis_comment_clear(&comment); |
| 406 | } |
| 407 | if (info_is_allocated) { |
| 408 | vorbis_info_clear(&info); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | Ref<AudioStreamPlayback> AudioStreamOggVorbis::instantiate_playback() { |
| 413 | Ref<AudioStreamPlaybackOggVorbis> ovs; |
| 414 | |
| 415 | ERR_FAIL_COND_V(packet_sequence.is_null(), nullptr); |
| 416 | |
| 417 | ovs.instantiate(); |
| 418 | ovs->vorbis_stream = Ref<AudioStreamOggVorbis>(this); |
| 419 | ovs->vorbis_data = packet_sequence; |
| 420 | ovs->frames_mixed = 0; |
| 421 | ovs->active = false; |
| 422 | ovs->loops = 0; |
| 423 | if (ovs->_alloc_vorbis()) { |
| 424 | return ovs; |
| 425 | } |
| 426 | // Failed to allocate data structures. |
| 427 | return nullptr; |
| 428 | } |
| 429 | |
| 430 | String AudioStreamOggVorbis::get_stream_name() const { |
| 431 | return ""; //return stream_name; |
| 432 | } |
| 433 | |
| 434 | void AudioStreamOggVorbis::maybe_update_info() { |
| 435 | ERR_FAIL_COND(packet_sequence.is_null()); |
| 436 | |
| 437 | vorbis_info info; |
| 438 | vorbis_comment comment; |
| 439 | int err; |
| 440 | |
| 441 | vorbis_info_init(&info); |
| 442 | vorbis_comment_init(&comment); |
| 443 | |
| 444 | Ref<OggPacketSequencePlayback> packet_sequence_playback = packet_sequence->instantiate_playback(); |
| 445 | |
| 446 | for (int i = 0; i < 3; i++) { |
| 447 | ogg_packet *packet; |
| 448 | if (!packet_sequence_playback->next_ogg_packet(&packet)) { |
| 449 | WARN_PRINT("Failed to get header packet"); |
| 450 | break; |
| 451 | } |
| 452 | if (i == 0) { |
| 453 | packet->b_o_s = 1; |
| 454 | |
| 455 | ERR_FAIL_COND(!vorbis_synthesis_idheader(packet)); |
| 456 | } |
| 457 | |
| 458 | err = vorbis_synthesis_headerin(&info, &comment, packet); |
| 459 | ERR_FAIL_COND_MSG(err != 0, "Error parsing header packet " + itos(i) + ": " + itos(err)); |
| 460 | } |
| 461 | |
| 462 | Dictionary dictionary; |
| 463 | // Comments are required by the Vorbis spec to be structured like env vars, i.e. VAR=VALUE. |
| 464 | // This is how tags are stored (artist, album, etc.), and we parse them out for display. |
| 465 | // See https://xiph.org/vorbis/doc/v-comment.html |
| 466 | for (int i = 0; i < comment.comments; i++) { |
| 467 | String c = String::utf8(comment.user_comments[i]); |
| 468 | int equals = c.find_char('='); |
| 469 | |
| 470 | #ifdef TOOLS_ENABLED |
| 471 | if (equals == -1) { |
| 472 | WARN_PRINT(vformat(R"(Invalid comment in Ogg Vorbis file "%s", should contain '=': "%s".)", get_path(), c)); |
| 473 | continue; |
| 474 | } |
| 475 | #endif |
| 476 | |
| 477 | String tag = c.substr(0, equals); |
| 478 | String tag_value = c.substr(equals + 1); |
| 479 | |
| 480 | dictionary[tag.to_lower()] = tag_value; |
| 481 | } |
| 482 | tags = dictionary; |
| 483 | |
| 484 | packet_sequence->set_sampling_rate(info.rate); |
| 485 | |
| 486 | vorbis_comment_clear(&comment); |
| 487 | vorbis_info_clear(&info); |
| 488 | } |
| 489 | |
| 490 | void AudioStreamOggVorbis::set_packet_sequence(Ref<OggPacketSequence> p_packet_sequence) { |
| 491 | packet_sequence = p_packet_sequence; |
| 492 | if (packet_sequence.is_valid()) { |
| 493 | maybe_update_info(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | Ref<OggPacketSequence> AudioStreamOggVorbis::get_packet_sequence() const { |
| 498 | return packet_sequence; |
| 499 | } |
| 500 | |
| 501 | void AudioStreamOggVorbis::set_loop(bool p_enable) { |
| 502 | loop = p_enable; |
| 503 | } |
| 504 | |
| 505 | bool AudioStreamOggVorbis::has_loop() const { |
| 506 | return loop; |
| 507 | } |
| 508 | |
| 509 | void AudioStreamOggVorbis::set_loop_offset(double p_seconds) { |
| 510 | loop_offset = p_seconds; |
| 511 | } |
| 512 | |
| 513 | double AudioStreamOggVorbis::get_loop_offset() const { |
| 514 | return loop_offset; |
| 515 | } |
| 516 | |
| 517 | double AudioStreamOggVorbis::get_length() const { |
| 518 | ERR_FAIL_COND_V(packet_sequence.is_null(), 0); |
| 519 | return packet_sequence->get_length(); |
| 520 | } |
| 521 | |
| 522 | void AudioStreamOggVorbis::set_bpm(double p_bpm) { |
| 523 | ERR_FAIL_COND(p_bpm < 0); |
| 524 | bpm = p_bpm; |
| 525 | emit_changed(); |
| 526 | } |
| 527 | |
| 528 | double AudioStreamOggVorbis::get_bpm() const { |
| 529 | return bpm; |
| 530 | } |
| 531 | |
| 532 | void AudioStreamOggVorbis::set_beat_count(int p_beat_count) { |
| 533 | ERR_FAIL_COND(p_beat_count < 0); |
| 534 | beat_count = p_beat_count; |
| 535 | emit_changed(); |
| 536 | } |
| 537 | |
| 538 | int AudioStreamOggVorbis::get_beat_count() const { |
| 539 | return beat_count; |
| 540 | } |
| 541 | |
| 542 | void AudioStreamOggVorbis::set_bar_beats(int p_bar_beats) { |
| 543 | ERR_FAIL_COND(p_bar_beats < 2); |
| 544 | bar_beats = p_bar_beats; |
| 545 | emit_changed(); |
| 546 | } |
| 547 | |
| 548 | int AudioStreamOggVorbis::get_bar_beats() const { |
| 549 | return bar_beats; |
| 550 | } |
| 551 | |
| 552 | void AudioStreamOggVorbis::set_tags(const Dictionary &p_tags) { |
| 553 | tags = p_tags; |
| 554 | } |
| 555 | |
| 556 | Dictionary AudioStreamOggVorbis::get_tags() const { |
| 557 | return tags; |
| 558 | } |
| 559 | |
| 560 | bool AudioStreamOggVorbis::is_monophonic() const { |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | void AudioStreamOggVorbis::get_parameter_list(List<Parameter> *r_parameters) { |
| 565 | r_parameters->push_back(Parameter(PropertyInfo(Variant::BOOL, "looping", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE), Variant())); |
| 566 | } |
| 567 | |
| 568 | Ref<AudioSample> AudioStreamOggVorbis::generate_sample() const { |
| 569 | Ref<AudioSample> sample; |
| 570 | sample.instantiate(); |
| 571 | sample->stream = this; |
| 572 | sample->loop_mode = loop |
| 573 | ? AudioSample::LoopMode::LOOP_FORWARD |
| 574 | : AudioSample::LoopMode::LOOP_DISABLED; |
| 575 | sample->loop_begin = loop_offset; |
| 576 | sample->loop_end = 0; |
| 577 | return sample; |
| 578 | } |
| 579 | |
| 580 | Ref<AudioStreamOggVorbis> AudioStreamOggVorbis::load_from_buffer(const Vector<uint8_t> &p_stream_data) { |
| 581 | Ref<AudioStreamOggVorbis> ogg_vorbis_stream; |
| 582 | ogg_vorbis_stream.instantiate(); |
| 583 | |
| 584 | Ref<OggPacketSequence> ogg_packet_sequence; |
| 585 | ogg_packet_sequence.instantiate(); |
| 586 | |
| 587 | ogg_stream_state stream_state; |
| 588 | ogg_sync_state sync_state; |
| 589 | ogg_page page; |
| 590 | ogg_packet packet; |
| 591 | bool initialized_stream = false; |
| 592 | |
| 593 | ogg_sync_init(&sync_state); |
| 594 | const long OGG_SYNC_BUFFER_SIZE = 8192; |
| 595 | int err; |
| 596 | size_t cursor = 0; |
| 597 | size_t packet_count = 0; |
| 598 | bool done = false; |
| 599 | while (!done) { |
| 600 | err = ogg_sync_check(&sync_state); |
| 601 | ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err)); |
| 602 | while (ogg_sync_pageout(&sync_state, &page) != 1) { |
| 603 | if (cursor >= size_t(p_stream_data.size())) { |
| 604 | done = true; |
| 605 | break; |
| 606 | } |
| 607 | err = ogg_sync_check(&sync_state); |
| 608 | ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err)); |
| 609 | char *sync_buf = ogg_sync_buffer(&sync_state, OGG_SYNC_BUFFER_SIZE); |
| 610 | err = ogg_sync_check(&sync_state); |
| 611 | ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err)); |
| 612 | size_t copy_size = p_stream_data.size() - cursor; |
| 613 | if (copy_size > OGG_SYNC_BUFFER_SIZE) { |
| 614 | copy_size = OGG_SYNC_BUFFER_SIZE; |
| 615 | } |
| 616 | memcpy(sync_buf, &p_stream_data[cursor], copy_size); |
| 617 | ogg_sync_wrote(&sync_state, copy_size); |
| 618 | cursor += copy_size; |
| 619 | err = ogg_sync_check(&sync_state); |
| 620 | ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err)); |
| 621 | } |
| 622 | if (done) { |
| 623 | break; |
| 624 | } |
| 625 | err = ogg_sync_check(&sync_state); |
| 626 | ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err)); |
| 627 | |
| 628 | // Have a page now. |
| 629 | if (!initialized_stream) { |
| 630 | if (ogg_stream_init(&stream_state, ogg_page_serialno(&page))) { |
| 631 | ERR_FAIL_V_MSG(Ref<AudioStreamOggVorbis>(), "Failed allocating memory for Ogg Vorbis stream."); |
| 632 | } |
| 633 | initialized_stream = true; |
| 634 | } |
| 635 | ogg_stream_pagein(&stream_state, &page); |
| 636 | err = ogg_stream_check(&stream_state); |
| 637 | ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg stream error " + itos(err)); |
| 638 | int desync_iters = 0; |
| 639 | |
| 640 | RBMap<uint64_t, Vector<Vector<uint8_t>>> sorted_packets; |
| 641 | int64_t granule_pos = 0; |
| 642 | |
| 643 | while (true) { |
| 644 | err = ogg_stream_packetout(&stream_state, &packet); |
| 645 | if (err == -1) { |
| 646 | // According to the docs this is usually recoverable, but don't sit here spinning forever. |
| 647 | desync_iters++; |
| 648 | WARN_PRINT_ONCE("Desync during ogg import."); |
| 649 | ERR_FAIL_COND_V_MSG(desync_iters > 100, Ref<AudioStreamOggVorbis>(), "Packet sync issue during Ogg import"); |
| 650 | continue; |
| 651 | } else if (err == 0) { |
| 652 | // Not enough data to fully reconstruct a packet. Go on to the next page. |
| 653 | break; |
| 654 | } |
| 655 | if (packet_count == 0 && vorbis_synthesis_idheader(&packet) == 0) { |
| 656 | print_verbose("Found a non-vorbis-header packet in a header position"); |
| 657 | // Clearly this logical stream is not a vorbis stream, so destroy it and try again with the next page. |
| 658 | if (initialized_stream) { |
| 659 | ogg_stream_clear(&stream_state); |
| 660 | initialized_stream = false; |
| 661 | } |
| 662 | break; |
| 663 | } |
| 664 | if (packet.granulepos > granule_pos) { |
| 665 | granule_pos = packet.granulepos; |
| 666 | } |
| 667 | |
| 668 | if (packet.bytes > 0) { |
| 669 | PackedByteArray data; |
| 670 | data.resize(packet.bytes); |
| 671 | memcpy(data.ptrw(), packet.packet, packet.bytes); |
| 672 | sorted_packets[granule_pos].push_back(data); |
| 673 | packet_count++; |
| 674 | } |
| 675 | } |
| 676 | Vector<Vector<uint8_t>> packet_data; |
| 677 | for (const KeyValue<uint64_t, Vector<Vector<uint8_t>>> &pair : sorted_packets) { |
| 678 | for (const Vector<uint8_t> &packets : pair.value) { |
| 679 | packet_data.push_back(packets); |
| 680 | } |
| 681 | } |
| 682 | if (initialized_stream && packet_data.size() > 0) { |
| 683 | ogg_packet_sequence->push_page(ogg_page_granulepos(&page), packet_data); |
| 684 | } |
| 685 | } |
| 686 | if (initialized_stream) { |
| 687 | ogg_stream_clear(&stream_state); |
| 688 | } |
| 689 | ogg_sync_clear(&sync_state); |
| 690 | |
| 691 | if (ogg_packet_sequence->get_packet_granule_positions().is_empty()) { |
| 692 | ERR_FAIL_V_MSG(Ref<AudioStreamOggVorbis>(), "Ogg Vorbis decoding failed. Check that your data is a valid Ogg Vorbis audio stream."); |
| 693 | } |
| 694 | |
| 695 | ogg_vorbis_stream->set_packet_sequence(ogg_packet_sequence); |
| 696 | |
| 697 | return ogg_vorbis_stream; |
| 698 | } |
| 699 | |
| 700 | Ref<AudioStreamOggVorbis> AudioStreamOggVorbis::load_from_file(const String &p_path) { |
| 701 | const Vector<uint8_t> stream_data = FileAccess::get_file_as_bytes(p_path); |
| 702 | ERR_FAIL_COND_V_MSG(stream_data.is_empty(), Ref<AudioStreamOggVorbis>(), vformat("Cannot open file '%s'.", p_path)); |
| 703 | return load_from_buffer(stream_data); |
| 704 | } |
| 705 | |
| 706 | void AudioStreamOggVorbis::_bind_methods() { |
| 707 | ClassDB::bind_static_method("AudioStreamOggVorbis", D_METHOD("load_from_buffer", "stream_data"), &AudioStreamOggVorbis::load_from_buffer); |
| 708 | ClassDB::bind_static_method("AudioStreamOggVorbis", D_METHOD("load_from_file", "path"), &AudioStreamOggVorbis::load_from_file); |
| 709 | |
| 710 | ClassDB::bind_method(D_METHOD("set_packet_sequence", "packet_sequence"), &AudioStreamOggVorbis::set_packet_sequence); |
| 711 | ClassDB::bind_method(D_METHOD("get_packet_sequence"), &AudioStreamOggVorbis::get_packet_sequence); |
| 712 | |
| 713 | ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOggVorbis::set_loop); |
| 714 | ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOggVorbis::has_loop); |
| 715 | |
| 716 | ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamOggVorbis::set_loop_offset); |
| 717 | ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamOggVorbis::get_loop_offset); |
| 718 | |
| 719 | ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamOggVorbis::set_bpm); |
| 720 | ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamOggVorbis::get_bpm); |
| 721 | |
| 722 | ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamOggVorbis::set_beat_count); |
| 723 | ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamOggVorbis::get_beat_count); |
| 724 | |
| 725 | ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamOggVorbis::set_bar_beats); |
| 726 | ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamOggVorbis::get_bar_beats); |
| 727 | |
| 728 | ClassDB::bind_method(D_METHOD("set_tags", "tags"), &AudioStreamOggVorbis::set_tags); |
| 729 | ClassDB::bind_method(D_METHOD("get_tags"), &AudioStreamOggVorbis::get_tags); |
| 730 | |
| 731 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "packet_sequence", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_packet_sequence", "get_packet_sequence"); |
| 732 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm"); |
| 733 | ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count"); |
| 734 | ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats"); |
| 735 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "tags", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_tags", "get_tags"); |
| 736 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop"); |
| 737 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset"); |
| 738 | } |
| 739 | |
| 740 | AudioStreamOggVorbis::AudioStreamOggVorbis() {} |
| 741 | |
| 742 | AudioStreamOggVorbis::~AudioStreamOggVorbis() {} |