Revert "Added loop_begin and loop_end to OggVorbisStream"
This commit is contained in:
parent
4d5b82811f
commit
2e28bd602c
3 changed files with 22 additions and 72 deletions
|
@ -36,54 +36,36 @@
|
||||||
#include "thirdparty/misc/stb_vorbis.c"
|
#include "thirdparty/misc/stb_vorbis.c"
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
#ifndef CLAMP
|
|
||||||
#define CLAMP(m_a, m_min, m_max) (((m_a) < (m_min)) ? (m_min) : (((m_a) > (m_max)) ? m_max : m_a))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
void AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
||||||
|
|
||||||
ERR_FAIL_COND(!active);
|
ERR_FAIL_COND(!active);
|
||||||
|
|
||||||
int todo = p_frames;
|
int todo = p_frames;
|
||||||
|
|
||||||
int start_buffer = 0;
|
while (todo && active) {
|
||||||
|
|
||||||
while (todo > 0 && active) {
|
int mixed = stb_vorbis_get_samples_float_interleaved(ogg_stream, 2, (float *)p_buffer, todo * 2);
|
||||||
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);
|
|
||||||
if (vorbis_stream->channels == 1 && mixed > 0) {
|
if (vorbis_stream->channels == 1 && mixed > 0) {
|
||||||
//mix mono to stereo
|
//mix mono to stereo
|
||||||
for (int i = start_buffer; i < mixed; i++) {
|
for (int i = 0; i < mixed; i++) {
|
||||||
p_buffer[i].r = p_buffer[i].l;
|
p_buffer[i].r = p_buffer[i].l;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
todo -= mixed;
|
todo -= mixed;
|
||||||
frames_mixed += mixed;
|
frames_mixed += mixed;
|
||||||
|
|
||||||
if (todo > 0) {
|
if (todo) {
|
||||||
//end of file!
|
//end of file!
|
||||||
if (vorbis_stream->loop) {
|
if (vorbis_stream->loop) {
|
||||||
//loop to the loop_beginning
|
//loop
|
||||||
seek(vorbis_stream->loop_begin);
|
seek(vorbis_stream->loop_offset);
|
||||||
loops++;
|
loops++;
|
||||||
// we still have buffer to fill, start from this element in the next iteration.
|
|
||||||
start_buffer = p_frames - todo;
|
|
||||||
} else {
|
} else {
|
||||||
for (int i = p_frames - todo; i < p_frames; i++) {
|
for (int i = mixed; i < p_frames; i++) {
|
||||||
p_buffer[i] = AudioFrame(0, 0);
|
p_buffer[i] = AudioFrame(0, 0);
|
||||||
}
|
}
|
||||||
active = false;
|
active = false;
|
||||||
todo = 0;
|
|
||||||
}
|
}
|
||||||
} else if (vorbis_stream->loop && frames_mixed >= vorbis_stream->loop_end_frames) {
|
|
||||||
// We reached loop_end. Loop to loop_begin plus whatever extra length we already mixed
|
|
||||||
uint32_t frames_to_advance = uint32_t(frames_mixed - vorbis_stream->loop_end_frames);
|
|
||||||
float start_loop = vorbis_stream->loop_begin + (float(frames_to_advance) / vorbis_stream->sample_rate);
|
|
||||||
seek(start_loop);
|
|
||||||
loops++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,9 +199,6 @@ void AudioStreamOGGVorbis::set_data(const PoolVector<uint8_t> &p_data) {
|
||||||
//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));
|
//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));
|
||||||
|
|
||||||
length = stb_vorbis_stream_length_in_seconds(ogg_stream);
|
length = stb_vorbis_stream_length_in_seconds(ogg_stream);
|
||||||
if (loop_end == 0) {
|
|
||||||
set_loop_end(length);
|
|
||||||
}
|
|
||||||
stb_vorbis_close(ogg_stream);
|
stb_vorbis_close(ogg_stream);
|
||||||
|
|
||||||
data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar.ptr());
|
data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar.ptr());
|
||||||
|
@ -254,24 +233,12 @@ bool AudioStreamOGGVorbis::has_loop() const {
|
||||||
return loop;
|
return loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioStreamOGGVorbis::set_loop_begin(float p_seconds) {
|
void AudioStreamOGGVorbis::set_loop_offset(float p_seconds) {
|
||||||
p_seconds = CLAMP(p_seconds, 0, length);
|
loop_offset = p_seconds;
|
||||||
loop_begin = p_seconds;
|
|
||||||
loop_begin_frames = uint32_t(sample_rate * p_seconds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float AudioStreamOGGVorbis::get_loop_begin() const {
|
float AudioStreamOGGVorbis::get_loop_offset() const {
|
||||||
return loop_begin;
|
return loop_offset;
|
||||||
}
|
|
||||||
|
|
||||||
void AudioStreamOGGVorbis::set_loop_end(float p_seconds) {
|
|
||||||
p_seconds = CLAMP(p_seconds, 0, length);
|
|
||||||
loop_end = p_seconds;
|
|
||||||
loop_end_frames = uint32_t(sample_rate * p_seconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
float AudioStreamOGGVorbis::get_loop_end() const {
|
|
||||||
return loop_end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioStreamOGGVorbis::_bind_methods() {
|
void AudioStreamOGGVorbis::_bind_methods() {
|
||||||
|
@ -282,16 +249,12 @@ void AudioStreamOGGVorbis::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOGGVorbis::set_loop);
|
ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOGGVorbis::set_loop);
|
||||||
ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOGGVorbis::has_loop);
|
ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOGGVorbis::has_loop);
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("set_loop_begin", "seconds"), &AudioStreamOGGVorbis::set_loop_begin);
|
ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamOGGVorbis::set_loop_offset);
|
||||||
ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamOGGVorbis::get_loop_begin);
|
ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamOGGVorbis::get_loop_offset);
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("set_loop_end", "seconds"), &AudioStreamOGGVorbis::set_loop_end);
|
|
||||||
ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamOGGVorbis::get_loop_end);
|
|
||||||
|
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
|
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");
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop", "has_loop");
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "loop_begin", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop_begin", "get_loop_begin");
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "loop_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop_offset", "get_loop_offset");
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "loop_end", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop_end", "get_loop_end");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
|
AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
|
||||||
|
@ -300,10 +263,7 @@ AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
|
||||||
length = 0;
|
length = 0;
|
||||||
sample_rate = 1;
|
sample_rate = 1;
|
||||||
channels = 1;
|
channels = 1;
|
||||||
loop_begin = 0;
|
loop_offset = 0;
|
||||||
loop_end = 0;
|
|
||||||
loop_begin_frames = 0;
|
|
||||||
loop_end_frames = 0;
|
|
||||||
decode_mem_size = 0;
|
decode_mem_size = 0;
|
||||||
loop = true;
|
loop = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,11 +92,7 @@ class AudioStreamOGGVorbis : public AudioStream {
|
||||||
int channels;
|
int channels;
|
||||||
float length;
|
float length;
|
||||||
bool loop;
|
bool loop;
|
||||||
float loop_begin;
|
float loop_offset;
|
||||||
float loop_end;
|
|
||||||
|
|
||||||
uint32_t loop_begin_frames;
|
|
||||||
uint32_t loop_end_frames;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
@ -105,11 +101,8 @@ public:
|
||||||
void set_loop(bool p_enable);
|
void set_loop(bool p_enable);
|
||||||
bool has_loop() const;
|
bool has_loop() const;
|
||||||
|
|
||||||
void set_loop_begin(float p_seconds);
|
void set_loop_offset(float p_seconds);
|
||||||
float get_loop_begin() const;
|
float get_loop_offset() const;
|
||||||
|
|
||||||
void set_loop_end(float p_seconds);
|
|
||||||
float get_loop_end() const;
|
|
||||||
|
|
||||||
virtual Ref<AudioStreamPlayback> instance_playback();
|
virtual Ref<AudioStreamPlayback> instance_playback();
|
||||||
virtual String get_stream_name() const;
|
virtual String get_stream_name() const;
|
||||||
|
|
|
@ -72,15 +72,13 @@ String ResourceImporterOGGVorbis::get_preset_name(int p_idx) const {
|
||||||
void ResourceImporterOGGVorbis::get_import_options(List<ImportOption> *r_options, int p_preset) const {
|
void ResourceImporterOGGVorbis::get_import_options(List<ImportOption> *r_options, int p_preset) const {
|
||||||
|
|
||||||
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), true));
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), true));
|
||||||
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "loop_begin"), 0));
|
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "loop_offset"), 0));
|
||||||
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "loop_end"), 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Error ResourceImporterOGGVorbis::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
|
Error ResourceImporterOGGVorbis::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
|
||||||
|
|
||||||
bool loop = p_options["loop"];
|
bool loop = p_options["loop"];
|
||||||
float loop_begin = p_options["loop_begin"];
|
float loop_offset = p_options["loop_offset"];
|
||||||
float loop_end = p_options["loop_end"];
|
|
||||||
|
|
||||||
FileAccess *f = FileAccess::open(p_source_file, FileAccess::READ);
|
FileAccess *f = FileAccess::open(p_source_file, FileAccess::READ);
|
||||||
if (!f) {
|
if (!f) {
|
||||||
|
@ -102,8 +100,7 @@ Error ResourceImporterOGGVorbis::import(const String &p_source_file, const Strin
|
||||||
|
|
||||||
ogg_stream->set_data(data);
|
ogg_stream->set_data(data);
|
||||||
ogg_stream->set_loop(loop);
|
ogg_stream->set_loop(loop);
|
||||||
ogg_stream->set_loop_begin(loop_begin);
|
ogg_stream->set_loop_offset(loop_offset);
|
||||||
if (loop_end > 0) ogg_stream->set_loop_end(loop_end);
|
|
||||||
|
|
||||||
return ResourceSaver::save(p_save_path + ".oggstr", ogg_stream);
|
return ResourceSaver::save(p_save_path + ".oggstr", ogg_stream);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue