2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream_ogg_vorbis.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 15:47:28 +01: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. */
|
|
|
|
/*************************************************************************/
|
2017-01-21 23:00:25 +01:00
|
|
|
#include "audio_stream_ogg_vorbis.h"
|
2017-03-05 15:47:28 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
#include "os/file_access.h"
|
2017-04-28 19:28:21 +02:00
|
|
|
|
|
|
|
#include "thirdparty/misc/stb_vorbis.c"
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!active);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int todo = p_frames;
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-07-29 11:29:51 +02:00
|
|
|
while (todo && active) {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int mixed = stb_vorbis_get_samples_float_interleaved(ogg_stream, 2, (float *)p_buffer, todo * 2);
|
|
|
|
todo -= mixed;
|
2017-04-25 14:02:45 +02:00
|
|
|
frames_mixed += mixed;
|
2017-01-21 23:00:25 +01:00
|
|
|
|
|
|
|
if (todo) {
|
|
|
|
//end of file!
|
2017-02-03 12:55:29 +01:00
|
|
|
if (vorbis_stream->loop) {
|
2017-01-21 23:00:25 +01:00
|
|
|
//loop
|
|
|
|
seek_pos(0);
|
|
|
|
loops++;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = mixed; i < p_frames; i++) {
|
|
|
|
p_buffer[i] = AudioFrame(0, 0);
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
active = false;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlaybackOGGVorbis::get_stream_sampling_rate() {
|
|
|
|
|
|
|
|
return vorbis_stream->sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackOGGVorbis::start(float p_from_pos) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
active = true;
|
2017-04-24 22:57:05 +02:00
|
|
|
seek_pos(p_from_pos);
|
2017-03-05 16:44:50 +01:00
|
|
|
loops = 0;
|
2017-01-21 23:00:25 +01:00
|
|
|
_begin_resample();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackOGGVorbis::stop() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
active = false;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
bool AudioStreamPlaybackOGGVorbis::is_playing() const {
|
|
|
|
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
|
|
|
|
|
|
|
|
return loops;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlaybackOGGVorbis::get_pos() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return float(frames_mixed) / vorbis_stream->sample_rate;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
void AudioStreamPlaybackOGGVorbis::seek_pos(float p_time) {
|
|
|
|
|
|
|
|
if (!active)
|
|
|
|
return;
|
|
|
|
|
2017-04-25 14:02:45 +02:00
|
|
|
if (p_time >= get_length()) {
|
|
|
|
p_time = 0;
|
|
|
|
}
|
|
|
|
frames_mixed = uint32_t(vorbis_stream->sample_rate * p_time);
|
|
|
|
|
|
|
|
stb_vorbis_seek(ogg_stream, frames_mixed);
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlaybackOGGVorbis::get_length() const {
|
|
|
|
|
|
|
|
return vorbis_stream->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
|
|
|
|
if (ogg_alloc.alloc_buffer) {
|
|
|
|
AudioServer::get_singleton()->audio_data_free(ogg_alloc.alloc_buffer);
|
|
|
|
stb_vorbis_close(ogg_stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
|
|
|
|
|
|
|
|
Ref<AudioStreamPlaybackOGGVorbis> ovs;
|
2017-03-05 16:44:50 +01:00
|
|
|
printf("instance at %p, data %p\n", this, data);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(data == NULL, ovs);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
|
|
|
ovs.instance();
|
2017-03-05 16:44:50 +01:00
|
|
|
ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
|
|
|
|
ovs->ogg_alloc.alloc_buffer = (char *)AudioServer::get_singleton()->audio_data_alloc(decode_mem_size);
|
|
|
|
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 23:00:25 +01:00
|
|
|
if (!ovs->ogg_stream) {
|
|
|
|
|
|
|
|
AudioServer::get_singleton()->audio_data_free(ovs->ogg_alloc.alloc_buffer);
|
2017-03-05 16:44:50 +01:00
|
|
|
ovs->ogg_alloc.alloc_buffer = NULL;
|
|
|
|
ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ovs;
|
|
|
|
}
|
|
|
|
|
|
|
|
String AudioStreamOGGVorbis::get_stream_name() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return ""; //return stream_name;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioStreamOGGVorbis::set_data(const PoolVector<uint8_t> &p_data) {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int src_data_len = p_data.size();
|
|
|
|
#define MAX_TEST_MEM (1 << 20)
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t alloc_try = 1024;
|
2017-01-21 23:00:25 +01:00
|
|
|
PoolVector<char> alloc_mem;
|
|
|
|
PoolVector<char>::Write w;
|
2017-03-05 16:44:50 +01:00
|
|
|
stb_vorbis *ogg_stream = NULL;
|
2017-01-21 23:00:25 +01:00
|
|
|
stb_vorbis_alloc ogg_alloc;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (alloc_try < MAX_TEST_MEM) {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
|
|
|
alloc_mem.resize(alloc_try);
|
|
|
|
w = alloc_mem.write();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ogg_alloc.alloc_buffer = w.ptr();
|
|
|
|
ogg_alloc.alloc_buffer_length_in_bytes = alloc_try;
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
PoolVector<uint8_t>::Read src_datar = p_data.read();
|
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
int error;
|
2017-03-05 16:44:50 +01:00
|
|
|
ogg_stream = stb_vorbis_open_memory((const unsigned char *)src_datar.ptr(), src_data_len, &error, &ogg_alloc);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!ogg_stream && error == VORBIS_outofmem) {
|
2017-01-21 23:00:25 +01:00
|
|
|
w = PoolVector<char>::Write();
|
2017-03-05 16:44:50 +01:00
|
|
|
alloc_try *= 2;
|
2017-01-21 23:00:25 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(alloc_try == MAX_TEST_MEM);
|
|
|
|
ERR_FAIL_COND(ogg_stream == NULL);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
stb_vorbis_info info = stb_vorbis_get_info(ogg_stream);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01: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 23:00:25 +01:00
|
|
|
|
2017-03-24 21:45:31 +01:00
|
|
|
//print_line("succeeded "+itos(ogg_alloc.alloc_buffer_length_in_bytes)+" setup "+itos(info.setup_memory_required)+" setup temp "+itos(info.setup_temp_memory_required)+" temp "+itos(info.temp_memory_required)+" maxframe"+itos(info.max_frame_size));
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
length = stb_vorbis_stream_length_in_seconds(ogg_stream);
|
2017-02-03 12:55:29 +01:00
|
|
|
stb_vorbis_close(ogg_stream);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar.ptr());
|
|
|
|
data_len = src_data_len;
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
printf("create at %p, data %p\n", this, data);
|
2017-02-03 12:55:29 +01:00
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
PoolVector<uint8_t> AudioStreamOGGVorbis::get_data() const {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
PoolVector<uint8_t> vdata;
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
if (data_len && data) {
|
|
|
|
vdata.resize(data_len);
|
|
|
|
{
|
|
|
|
PoolVector<uint8_t>::Write w = vdata.write();
|
2017-03-05 16:44:50 +01:00
|
|
|
copymem(w.ptr(), data, data_len);
|
2017-02-03 12:55:29 +01:00
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
return vdata;
|
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
void AudioStreamOGGVorbis::set_loop(bool p_enable) {
|
2017-03-05 16:44:50 +01:00
|
|
|
loop = p_enable;
|
2017-02-03 12:55:29 +01:00
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
bool AudioStreamOGGVorbis::has_loop() const {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
return loop;
|
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
void AudioStreamOGGVorbis::_bind_methods() {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01: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 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01: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 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop", "has_loop");
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 12:55:29 +01:00
|
|
|
AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data = NULL;
|
|
|
|
length = 0;
|
|
|
|
sample_rate = 1;
|
|
|
|
channels = 1;
|
|
|
|
decode_mem_size = 0;
|
|
|
|
loop = false;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|