Expose some AudioStreamPlayback methods.

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
This commit is contained in:
stechyo 2023-12-23 17:12:29 +00:00
parent 77dcf97d82
commit e479c238a2
3 changed files with 76 additions and 0 deletions

View File

@ -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.
</description>
</method>
<method name="get_loop_count" qualifiers="const">
<return type="int" />
<description>
Returns the number of times the stream has looped.
</description>
</method>
<method name="get_playback_position" qualifiers="const">
<return type="float" />
<description>
Returns the current position in the stream, in seconds.
</description>
</method>
<method name="get_sample_playback" qualifiers="const" experimental="">
<return type="AudioSamplePlayback" />
<description>
Returns the [AudioSamplePlayback] associated with this [AudioStreamPlayback] for playing back the audio sample of this stream.
</description>
</method>
<method name="is_playing" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the stream is playing.
</description>
</method>
<method name="mix_audio">
<return type="PackedVector2Array" />
<param index="0" name="rate_scale" type="float" />
<param index="1" name="frames" type="int" />
<description>
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.
</description>
</method>
<method name="set_sample_playback" experimental="">
<return type="void" />
<param index="0" name="playback_sample" type="AudioSamplePlayback" />
@ -92,5 +120,18 @@
Associates [AudioSamplePlayback] to this [AudioStreamPlayback] for playing back the audio sample of this stream.
</description>
</method>
<method name="start">
<return type="void" />
<param index="0" name="from_pos" type="float" default="0.0" />
<description>
Starts the stream from the given [param from_pos], in seconds.
</description>
</method>
<method name="stop">
<return type="void" />
<description>
Stops the stream.
</description>
</method>
</methods>
</class>

View File

@ -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<AudioFrame> 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() {}

View File

@ -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 {