Implement audio stream playback parameters.

Implements a way for audio stream playback to be configured via parameters
directly in the edited AudioStreamPlayer[2D/3D].

Currently, configuring the playback stream is not possible (or is sometimes hacky
as the user has to obtain the currently played stream, which is not always immediately available).

This PR only implements this new feature to control looping in stream playback instances (a commonly requested feature, which was lost in the transition from Godot 2 to Godot 3).
But the idea is that it can do a lot more:

* If effects are bundled to the stream, control per playback instance parameters such as cutoff or resoance, or any other exposed effect parameter per playback instance.
* For the upcoming interactive music PR (#64488), this exposes an easy way to change the active clip, which was not possible before.
* For the upcoming parametrizable audio support (https://github.com/godotengine/godot-proposals/issues/3394) this allows editing and animating audio graph parameters.

In any case, this PR is required to complete #64488.

Update modules/vorbis/audio_stream_ogg_vorbis.h

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>

Update modules/minimp3/audio_stream_mp3.h

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>

Update modules/minimp3/audio_stream_mp3.h

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>

Update modules/vorbis/audio_stream_ogg_vorbis.h

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>

Update doc/classes/AudioStream.xml

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
This commit is contained in:
Juan Linietsky
2023-12-23 17:30:32 +01:00
parent 13a0d6e9b2
commit a40fe16866
14 changed files with 382 additions and 6 deletions

View File

@@ -34,6 +34,8 @@
#include "core/math/audio_frame.h"
#include "servers/audio_server.h"
#define PARAM_PREFIX "parameters/"
void AudioStreamPlayer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
@@ -88,9 +90,71 @@ void AudioStreamPlayer::_notification(int p_what) {
}
}
void AudioStreamPlayer::_update_stream_parameters() {
if (stream.is_null()) {
return;
}
List<AudioStream::Parameter> parameters;
stream->get_parameter_list(&parameters);
for (const AudioStream::Parameter &K : parameters) {
const PropertyInfo &pi = K.property;
StringName key = PARAM_PREFIX + pi.name;
if (!playback_parameters.has(key)) {
ParameterData pd;
pd.path = pi.name;
pd.value = K.default_value;
playback_parameters.insert(key, pd);
}
}
}
void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {
if (stream.is_valid()) {
stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayer::_update_stream_parameters));
}
stop();
stream = p_stream;
_update_stream_parameters();
if (stream.is_valid()) {
stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayer::_update_stream_parameters));
}
notify_property_list_changed();
}
bool AudioStreamPlayer::_set(const StringName &p_name, const Variant &p_value) {
HashMap<StringName, ParameterData>::Iterator I = playback_parameters.find(p_name);
if (!I) {
return false;
}
ParameterData &pd = I->value;
pd.value = p_value;
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
playback->set_parameter(pd.path, pd.value);
}
return true;
}
bool AudioStreamPlayer::_get(const StringName &p_name, Variant &r_ret) const {
HashMap<StringName, ParameterData>::ConstIterator I = playback_parameters.find(p_name);
if (!I) {
return false;
}
r_ret = I->value.value;
return true;
}
void AudioStreamPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
if (stream.is_null()) {
return;
}
List<AudioStream::Parameter> parameters;
stream->get_parameter_list(&parameters);
for (const AudioStream::Parameter &K : parameters) {
PropertyInfo pi = K.property;
pi.name = PARAM_PREFIX + pi.name;
p_list->push_back(pi);
}
}
Ref<AudioStream> AudioStreamPlayer::get_stream() const {
@@ -144,6 +208,10 @@ void AudioStreamPlayer::play(float p_from_pos) {
Ref<AudioStreamPlayback> stream_playback = stream->instantiate_playback();
ERR_FAIL_COND_MSG(stream_playback.is_null(), "Failed to instantiate playback.");
for (const KeyValue<StringName, ParameterData> &K : playback_parameters) {
stream_playback->set_parameter(K.value.path, K.value.value);
}
AudioServer::get_singleton()->start_playback_stream(stream_playback, bus, _get_volume_vector(), p_from_pos, pitch_scale);
stream_playbacks.push_back(stream_playback);
active.set();

View File

@@ -68,11 +68,23 @@ private:
Vector<AudioFrame> _get_volume_vector();
struct ParameterData {
StringName path;
Variant value;
};
HashMap<StringName, ParameterData> playback_parameters;
void _update_stream_parameters();
protected:
void _validate_property(PropertyInfo &p_property) const;
void _notification(int p_what);
static void _bind_methods();
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
public:
void set_stream(Ref<AudioStream> p_stream);
Ref<AudioStream> get_stream() const;