From e479c238a227520fb82eb001b5b26b4f8c2675dd Mon Sep 17 00:00:00 2001 From: stechyo Date: Sat, 23 Dec 2023 17:12:29 +0000 Subject: [PATCH 1/2] Expose some AudioStreamPlayback methods. Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> --- doc/classes/AudioStreamPlayback.xml | 41 +++++++++++++++++++++++++++++ servers/audio/audio_stream.cpp | 31 ++++++++++++++++++++++ servers/audio/audio_stream.h | 4 +++ 3 files changed, 76 insertions(+) diff --git a/doc/classes/AudioStreamPlayback.xml b/doc/classes/AudioStreamPlayback.xml index 02f3407f79b..d7f6ef20145 100644 --- a/doc/classes/AudioStreamPlayback.xml +++ b/doc/classes/AudioStreamPlayback.xml @@ -79,12 +79,40 @@ Overridable method. Called whenever the audio stream is mixed if the playback is active and [method AudioServer.set_enable_tagging_used_audio_streams] has been set to [code]true[/code]. Editor plugins may use this method to "tag" the current position along the audio stream and display it in a preview. + + + + Returns the number of times the stream has looped. + + + + + + Returns the current position in the stream, in seconds. + + Returns the [AudioSamplePlayback] associated with this [AudioStreamPlayback] for playing back the audio sample of this stream. + + + + Returns [code]true[/code] if the stream is playing. + + + + + + + + Mixes up to [param frames] of audio from the stream from the current position, at a rate of [param rate_scale], advancing the stream. + Returns a [PackedVector2Array] where each element holds the left and right channel volume levels of each frame. + [b]Note:[/b] Can return fewer frames than requested, make sure to use the size of the return value. + + @@ -92,5 +120,18 @@ Associates [AudioSamplePlayback] to this [AudioStreamPlayback] for playing back the audio sample of this stream. + + + + + Starts the stream from the given [param from_pos], in seconds. + + + + + + Stops the stream. + + diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 7ab150c1413..4921299ef15 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -76,6 +76,31 @@ int AudioStreamPlayback::mix(AudioFrame *p_buffer, float p_rate_scale, int p_fra return ret; } +PackedVector2Array AudioStreamPlayback::mix_audio(float p_rate_scale, int p_frames) { + Vector frames_in; + frames_in.resize(p_frames); + + int frames = mix(frames_in.ptrw(), p_rate_scale, p_frames); + + PackedVector2Array res; + res.resize(frames); + + Vector2 *res_ptrw = res.ptrw(); + for (int i = 0; i < frames; i++) { + res_ptrw[i] = Vector2(frames_in[i].left, frames_in[i].right); + } + + return res; +} + +void AudioStreamPlayback::start_playback(double p_from_pos) { + start(); +} + +void AudioStreamPlayback::stop_playback() { + stop(); +} + void AudioStreamPlayback::tag_used_streams() { GDVIRTUAL_CALL(_tag_used_streams); } @@ -108,6 +133,12 @@ void AudioStreamPlayback::_bind_methods() { ClassDB::bind_method(D_METHOD("set_sample_playback", "playback_sample"), &AudioStreamPlayback::set_sample_playback); ClassDB::bind_method(D_METHOD("get_sample_playback"), &AudioStreamPlayback::get_sample_playback); + ClassDB::bind_method(D_METHOD("mix_audio", "rate_scale", "frames"), &AudioStreamPlayback::mix_audio); + ClassDB::bind_method(D_METHOD("start", "from_pos"), &AudioStreamPlayback::start_playback, DEFVAL(0.0)); + ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayback::stop_playback); + ClassDB::bind_method(D_METHOD("get_loop_count"), &AudioStreamPlayback::get_loop_count); + ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayback::get_playback_position); + ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayback::is_playing); } AudioStreamPlayback::AudioStreamPlayback() {} diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index 0ca4777d5c4..31fd94038ea 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -116,6 +116,10 @@ public: AudioStreamPlayback(); ~AudioStreamPlayback(); + + PackedVector2Array mix_audio(float p_rate_scale, int p_frames); + void start_playback(double p_from_pos = 0.0); + void stop_playback(); }; class AudioStreamPlaybackResampled : public AudioStreamPlayback { From c32f72f7fa9aadd960d8d445535e8a7065539a79 Mon Sep 17 00:00:00 2001 From: stechyo Date: Mon, 26 Aug 2024 19:42:42 +0100 Subject: [PATCH 2/2] Post-code review changes. --- doc/classes/AudioStreamPlayback.xml | 7 +++++++ servers/audio/audio_stream.cpp | 32 ++++++++++++++++++++--------- servers/audio/audio_stream.h | 4 +++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/doc/classes/AudioStreamPlayback.xml b/doc/classes/AudioStreamPlayback.xml index d7f6ef20145..f01406d0f1d 100644 --- a/doc/classes/AudioStreamPlayback.xml +++ b/doc/classes/AudioStreamPlayback.xml @@ -113,6 +113,13 @@ [b]Note:[/b] Can return fewer frames than requested, make sure to use the size of the return value. + + + + + Seeks the stream at the given [param time], in seconds. + + diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 4921299ef15..13ba139f316 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -76,31 +76,42 @@ int AudioStreamPlayback::mix(AudioFrame *p_buffer, float p_rate_scale, int p_fra return ret; } -PackedVector2Array AudioStreamPlayback::mix_audio(float p_rate_scale, int p_frames) { - Vector frames_in; - frames_in.resize(p_frames); - - int frames = mix(frames_in.ptrw(), p_rate_scale, p_frames); +PackedVector2Array AudioStreamPlayback::_mix_audio_bind(float p_rate_scale, int p_frames) { + Vector frames = mix_audio(p_rate_scale, p_frames); PackedVector2Array res; - res.resize(frames); + res.resize(frames.size()); Vector2 *res_ptrw = res.ptrw(); - for (int i = 0; i < frames; i++) { - res_ptrw[i] = Vector2(frames_in[i].left, frames_in[i].right); + for (int i = 0; i < frames.size(); i++) { + res_ptrw[i] = Vector2(frames[i].left, frames[i].right); } return res; } +Vector AudioStreamPlayback::mix_audio(float p_rate_scale, int p_frames) { + Vector res; + res.resize(p_frames); + + int frames = mix(res.ptrw(), p_rate_scale, p_frames); + res.resize(frames); + + return res; +} + void AudioStreamPlayback::start_playback(double p_from_pos) { - start(); + start(p_from_pos); } void AudioStreamPlayback::stop_playback() { stop(); } +void AudioStreamPlayback::seek_playback(double p_time) { + seek(p_time); +} + void AudioStreamPlayback::tag_used_streams() { GDVIRTUAL_CALL(_tag_used_streams); } @@ -133,8 +144,9 @@ void AudioStreamPlayback::_bind_methods() { ClassDB::bind_method(D_METHOD("set_sample_playback", "playback_sample"), &AudioStreamPlayback::set_sample_playback); ClassDB::bind_method(D_METHOD("get_sample_playback"), &AudioStreamPlayback::get_sample_playback); - ClassDB::bind_method(D_METHOD("mix_audio", "rate_scale", "frames"), &AudioStreamPlayback::mix_audio); + ClassDB::bind_method(D_METHOD("mix_audio", "rate_scale", "frames"), &AudioStreamPlayback::_mix_audio_bind); ClassDB::bind_method(D_METHOD("start", "from_pos"), &AudioStreamPlayback::start_playback, DEFVAL(0.0)); + ClassDB::bind_method(D_METHOD("seek", "time"), &AudioStreamPlayback::seek_playback, DEFVAL(0.0)); ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayback::stop_playback); ClassDB::bind_method(D_METHOD("get_loop_count"), &AudioStreamPlayback::get_loop_count); ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayback::get_playback_position); diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index 31fd94038ea..f49eb8af015 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -81,6 +81,7 @@ class AudioStreamPlayback : public RefCounted { protected: static void _bind_methods(); + PackedVector2Array _mix_audio_bind(float p_rate_scale, int p_frames); GDVIRTUAL1(_start, double) GDVIRTUAL0(_stop) GDVIRTUAL0RC(bool, _is_playing) @@ -117,9 +118,10 @@ public: AudioStreamPlayback(); ~AudioStreamPlayback(); - PackedVector2Array mix_audio(float p_rate_scale, int p_frames); + Vector mix_audio(float p_rate_scale, int p_frames); void start_playback(double p_from_pos = 0.0); void stop_playback(); + void seek_playback(double p_time); }; class AudioStreamPlaybackResampled : public AudioStreamPlayback {