2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream_ogg_vorbis.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-01-21 22:00:25 +00:00
|
|
|
#include "audio_stream_ogg_vorbis.h"
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/file_access.h"
|
2017-04-28 17:28:21 +00:00
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
|
|
|
ERR_FAIL_COND_V(!active, 0);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int todo = p_frames;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-12-17 21:30:28 +00:00
|
|
|
int start_buffer = 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int frames_mixed_this_step = p_frames;
|
|
|
|
|
2017-12-17 21:30:28 +00:00
|
|
|
while (todo && active) {
|
|
|
|
float *buffer = (float *)p_buffer;
|
|
|
|
if (start_buffer > 0) {
|
|
|
|
buffer = (buffer + start_buffer * 2);
|
|
|
|
}
|
|
|
|
int mixed = stb_vorbis_get_samples_float_interleaved(ogg_stream, 2, buffer, todo * 2);
|
2017-09-07 02:13:57 +00:00
|
|
|
if (vorbis_stream->channels == 1 && mixed > 0) {
|
|
|
|
//mix mono to stereo
|
2021-02-16 04:42:45 +00:00
|
|
|
for (int i = start_buffer; i < start_buffer + mixed; i++) {
|
2017-09-07 02:13:57 +00:00
|
|
|
p_buffer[i].r = p_buffer[i].l;
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
todo -= mixed;
|
2017-04-25 12:02:45 +00:00
|
|
|
frames_mixed += mixed;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-11-20 13:11:08 +00:00
|
|
|
if (todo) {
|
2017-01-21 22:00:25 +00:00
|
|
|
//end of file!
|
2020-03-02 21:36:01 +00:00
|
|
|
bool is_not_empty = mixed > 0 || stb_vorbis_stream_length_in_samples(ogg_stream) > 0;
|
|
|
|
if (vorbis_stream->loop && is_not_empty) {
|
2017-11-20 13:11:08 +00:00
|
|
|
//loop
|
|
|
|
seek(vorbis_stream->loop_offset);
|
2017-01-21 22:00:25 +00:00
|
|
|
loops++;
|
2017-12-17 21:30:28 +00:00
|
|
|
// we still have buffer to fill, start from this element in the next iteration.
|
|
|
|
start_buffer = p_frames - todo;
|
2017-01-21 22:00:25 +00:00
|
|
|
} else {
|
2021-08-27 17:27:58 +00:00
|
|
|
frames_mixed_this_step = p_frames - todo;
|
2017-12-17 21:30:28 +00:00
|
|
|
for (int i = p_frames - todo; i < p_frames; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_buffer[i] = AudioFrame(0, 0);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
active = false;
|
2017-12-17 21:30:28 +00:00
|
|
|
todo = 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 17:27:58 +00:00
|
|
|
return frames_mixed_this_step;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlaybackOGGVorbis::get_stream_sampling_rate() {
|
|
|
|
return vorbis_stream->sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackOGGVorbis::start(float p_from_pos) {
|
2017-03-05 15:44:50 +00:00
|
|
|
active = true;
|
2017-09-21 03:31:36 +00:00
|
|
|
seek(p_from_pos);
|
2017-03-05 15:44:50 +00:00
|
|
|
loops = 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
_begin_resample();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackOGGVorbis::stop() {
|
2017-03-05 15:44:50 +00:00
|
|
|
active = false;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-01-21 22:00:25 +00:00
|
|
|
bool AudioStreamPlaybackOGGVorbis::is_playing() const {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
|
|
|
|
return loops;
|
|
|
|
}
|
|
|
|
|
2017-09-21 03:31:36 +00:00
|
|
|
float AudioStreamPlaybackOGGVorbis::get_playback_position() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return float(frames_mixed) / vorbis_stream->sample_rate;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-09-21 03:31:36 +00:00
|
|
|
void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!active) {
|
2017-01-21 22:00:25 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2018-01-22 19:35:33 +00:00
|
|
|
if (p_time >= vorbis_stream->get_length()) {
|
2017-04-25 12:02:45 +00:00
|
|
|
p_time = 0;
|
|
|
|
}
|
|
|
|
frames_mixed = uint32_t(vorbis_stream->sample_rate * p_time);
|
|
|
|
|
|
|
|
stb_vorbis_seek(ogg_stream, frames_mixed);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
|
|
|
|
if (ogg_alloc.alloc_buffer) {
|
|
|
|
stb_vorbis_close(ogg_stream);
|
2020-03-27 19:14:19 +00:00
|
|
|
memfree(ogg_alloc.alloc_buffer);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
|
|
|
|
Ref<AudioStreamPlaybackOGGVorbis> ovs;
|
|
|
|
|
2020-05-07 20:02:28 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(data == nullptr, ovs,
|
|
|
|
"This AudioStreamOGGVorbis does not have an audio file assigned "
|
|
|
|
"to it. AudioStreamOGGVorbis should not be created from the "
|
|
|
|
"inspector or with `.new()`. Instead, load an audio file.");
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
ovs.instantiate();
|
2017-03-05 15:44:50 +00:00
|
|
|
ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
|
2020-03-27 19:14:19 +00:00
|
|
|
ovs->ogg_alloc.alloc_buffer = (char *)memalloc(decode_mem_size);
|
2017-03-05 15:44:50 +00:00
|
|
|
ovs->ogg_alloc.alloc_buffer_length_in_bytes = decode_mem_size;
|
|
|
|
ovs->frames_mixed = 0;
|
|
|
|
ovs->active = false;
|
|
|
|
ovs->loops = 0;
|
|
|
|
int error;
|
|
|
|
ovs->ogg_stream = stb_vorbis_open_memory((const unsigned char *)data, data_len, &error, &ovs->ogg_alloc);
|
2017-01-21 22:00:25 +00:00
|
|
|
if (!ovs->ogg_stream) {
|
2020-03-27 19:14:19 +00:00
|
|
|
memfree(ovs->ogg_alloc.alloc_buffer);
|
2020-04-01 23:20:12 +00:00
|
|
|
ovs->ogg_alloc.alloc_buffer = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ovs;
|
|
|
|
}
|
|
|
|
|
|
|
|
String AudioStreamOGGVorbis::get_stream_name() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return ""; //return stream_name;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 16:47:37 +00:00
|
|
|
void AudioStreamOGGVorbis::clear_data() {
|
|
|
|
if (data) {
|
2020-03-27 19:14:19 +00:00
|
|
|
memfree(data);
|
2020-04-01 23:20:12 +00:00
|
|
|
data = nullptr;
|
2017-12-31 16:47:37 +00:00
|
|
|
data_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
void AudioStreamOGGVorbis::set_data(const Vector<uint8_t> &p_data) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int src_data_len = p_data.size();
|
|
|
|
uint32_t alloc_try = 1024;
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<char> alloc_mem;
|
|
|
|
char *w;
|
2020-04-01 23:20:12 +00:00
|
|
|
stb_vorbis *ogg_stream = nullptr;
|
2017-01-21 22:00:25 +00:00
|
|
|
stb_vorbis_alloc ogg_alloc;
|
|
|
|
|
2020-09-09 18:46:46 +00:00
|
|
|
// Vorbis comments may be up to UINT32_MAX, but that's arguably pretty rare.
|
|
|
|
// Let's go with 2^30 so we don't risk going out of bounds.
|
|
|
|
const uint32_t MAX_TEST_MEM = 1 << 30;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (alloc_try < MAX_TEST_MEM) {
|
2017-01-21 22:00:25 +00:00
|
|
|
alloc_mem.resize(alloc_try);
|
2020-02-17 21:06:54 +00:00
|
|
|
w = alloc_mem.ptrw();
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
ogg_alloc.alloc_buffer = w;
|
2017-03-05 15:44:50 +00:00
|
|
|
ogg_alloc.alloc_buffer_length_in_bytes = alloc_try;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
const uint8_t *src_datar = p_data.ptr();
|
2017-02-03 11:55:29 +00:00
|
|
|
|
2017-01-21 22:00:25 +00:00
|
|
|
int error;
|
2020-02-17 21:06:54 +00:00
|
|
|
ogg_stream = stb_vorbis_open_memory((const unsigned char *)src_datar, src_data_len, &error, &ogg_alloc);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!ogg_stream && error == VORBIS_outofmem) {
|
|
|
|
alloc_try *= 2;
|
2017-01-21 22:00:25 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(alloc_try == MAX_TEST_MEM);
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND(ogg_stream == nullptr);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
stb_vorbis_info info = stb_vorbis_get_info(ogg_stream);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
channels = info.channels;
|
|
|
|
sample_rate = info.sample_rate;
|
|
|
|
decode_mem_size = alloc_try;
|
|
|
|
//does this work? (it's less mem..)
|
|
|
|
//decode_mem_size = ogg_alloc.alloc_buffer_length_in_bytes + info.setup_memory_required + info.temp_memory_required + info.max_frame_size;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
length = stb_vorbis_stream_length_in_seconds(ogg_stream);
|
2017-02-03 11:55:29 +00:00
|
|
|
stb_vorbis_close(ogg_stream);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-12-31 16:47:37 +00:00
|
|
|
// free any existing data
|
|
|
|
clear_data();
|
|
|
|
|
2020-03-27 19:14:19 +00:00
|
|
|
data = memalloc(src_data_len);
|
2021-04-27 14:19:21 +00:00
|
|
|
memcpy(data, src_datar, src_data_len);
|
2017-03-05 15:44:50 +00:00
|
|
|
data_len = src_data_len;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-09 18:46:46 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND_MSG(alloc_try == MAX_TEST_MEM, vformat("Couldn't set vorbis data even with an alloc buffer of %d bytes, report bug.", MAX_TEST_MEM));
|
2017-02-03 11:55:29 +00:00
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> AudioStreamOGGVorbis::get_data() const {
|
|
|
|
Vector<uint8_t> vdata;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
if (data_len && data) {
|
|
|
|
vdata.resize(data_len);
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = vdata.ptrw();
|
2021-04-27 14:19:21 +00:00
|
|
|
memcpy(w, data, data_len);
|
2017-02-03 11:55:29 +00:00
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
return vdata;
|
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
void AudioStreamOGGVorbis::set_loop(bool p_enable) {
|
2017-03-05 15:44:50 +00:00
|
|
|
loop = p_enable;
|
2017-02-03 11:55:29 +00:00
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
bool AudioStreamOGGVorbis::has_loop() const {
|
|
|
|
return loop;
|
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-11-20 13:11:08 +00:00
|
|
|
void AudioStreamOGGVorbis::set_loop_offset(float p_seconds) {
|
|
|
|
loop_offset = p_seconds;
|
2017-08-22 14:47:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 13:11:08 +00:00
|
|
|
float AudioStreamOGGVorbis::get_loop_offset() const {
|
|
|
|
return loop_offset;
|
2017-08-22 14:47:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:35:33 +00:00
|
|
|
float AudioStreamOGGVorbis::get_length() const {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:55:29 +00:00
|
|
|
void AudioStreamOGGVorbis::_bind_methods() {
|
2018-04-25 19:04:03 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamOGGVorbis::set_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamOGGVorbis::get_data);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOGGVorbis::set_loop);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOGGVorbis::has_loop);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-11-20 13:11:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamOGGVorbis::set_loop_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamOGGVorbis::get_loop_offset);
|
2017-08-22 14:47:24 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
|
2019-11-11 14:14:37 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 09:57:18 +00:00
|
|
|
AudioStreamOGGVorbis::AudioStreamOGGVorbis() {}
|
2017-12-31 16:47:37 +00:00
|
|
|
|
|
|
|
AudioStreamOGGVorbis::~AudioStreamOGGVorbis() {
|
|
|
|
clear_data();
|
|
|
|
}
|