initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

6
scene/audio/SCsub Normal file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
env.add_source_files(env.scene_sources, "*.cpp")

View File

@@ -0,0 +1,41 @@
/**************************************************************************/
/* audio_stream_player.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#ifndef DISABLE_DEPRECATED
bool AudioStreamPlayer::_is_autoplay_enabled_bind_compat_86907() {
return is_autoplay_enabled();
}
void AudioStreamPlayer::_bind_compatibility_methods() {
ClassDB::bind_compatibility_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::_is_autoplay_enabled_bind_compat_86907);
}
#endif // DISABLE_DEPRECATED

View File

@@ -0,0 +1,305 @@
/**************************************************************************/
/* audio_stream_player.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#include "audio_stream_player.h"
#include "audio_stream_player.compat.inc"
#include "scene/audio/audio_stream_player_internal.h"
#include "servers/audio/audio_stream.h"
void AudioStreamPlayer::_notification(int p_what) {
if (p_what == NOTIFICATION_ACCESSIBILITY_UPDATE) {
RID ae = get_accessibility_element();
ERR_FAIL_COND(ae.is_null());
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_AUDIO);
} else {
internal->notification(p_what);
}
}
void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {
internal->set_stream(p_stream);
}
bool AudioStreamPlayer::_set(const StringName &p_name, const Variant &p_value) {
return internal->set(p_name, p_value);
}
bool AudioStreamPlayer::_get(const StringName &p_name, Variant &r_ret) const {
return internal->get(p_name, r_ret);
}
void AudioStreamPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
internal->get_property_list(p_list);
}
Ref<AudioStream> AudioStreamPlayer::get_stream() const {
return internal->stream;
}
void AudioStreamPlayer::set_volume_db(float p_volume) {
ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");
internal->volume_db = p_volume;
Vector<AudioFrame> volume_vector = _get_volume_vector();
for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);
}
}
float AudioStreamPlayer::get_volume_db() const {
return internal->volume_db;
}
void AudioStreamPlayer::set_volume_linear(float p_volume) {
set_volume_db(Math::linear_to_db(p_volume));
}
float AudioStreamPlayer::get_volume_linear() const {
return Math::db_to_linear(get_volume_db());
}
void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {
internal->set_pitch_scale(p_pitch_scale);
}
float AudioStreamPlayer::get_pitch_scale() const {
return internal->pitch_scale;
}
void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) {
internal->set_max_polyphony(p_max_polyphony);
}
int AudioStreamPlayer::get_max_polyphony() const {
return internal->max_polyphony;
}
void AudioStreamPlayer::play(float p_from_pos) {
Ref<AudioStreamPlayback> stream_playback = internal->play_basic();
if (stream_playback.is_null()) {
return;
}
AudioServer::get_singleton()->start_playback_stream(stream_playback, internal->bus, _get_volume_vector(), p_from_pos, internal->pitch_scale);
internal->ensure_playback_limit();
// Sample handling.
if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) {
Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();
sample_playback->offset = p_from_pos;
sample_playback->volume_vector = _get_volume_vector();
sample_playback->bus = get_bus();
AudioServer::get_singleton()->start_sample_playback(sample_playback);
}
}
void AudioStreamPlayer::seek(float p_seconds) {
internal->seek(p_seconds);
}
void AudioStreamPlayer::stop() {
internal->stop_basic();
}
bool AudioStreamPlayer::is_playing() const {
return internal->is_playing();
}
float AudioStreamPlayer::get_playback_position() {
return internal->get_playback_position();
}
void AudioStreamPlayer::set_bus(const StringName &p_bus) {
internal->bus = p_bus;
for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());
}
}
StringName AudioStreamPlayer::get_bus() const {
return internal->get_bus();
}
void AudioStreamPlayer::set_autoplay(bool p_enable) {
internal->autoplay = p_enable;
}
bool AudioStreamPlayer::is_autoplay_enabled() const {
return internal->autoplay;
}
void AudioStreamPlayer::set_mix_target(MixTarget p_target) {
mix_target = p_target;
}
AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {
return mix_target;
}
void AudioStreamPlayer::_set_playing(bool p_enable) {
internal->set_playing(p_enable);
}
void AudioStreamPlayer::set_stream_paused(bool p_pause) {
internal->set_stream_paused(p_pause);
}
bool AudioStreamPlayer::get_stream_paused() const {
return internal->get_stream_paused();
}
Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {
Vector<AudioFrame> volume_vector;
// We need at most four stereo pairs (for 7.1 systems).
volume_vector.resize(4);
// Initialize the volume vector to zero.
for (AudioFrame &channel_volume_db : volume_vector) {
channel_volume_db = AudioFrame(0, 0);
}
float volume_linear = Math::db_to_linear(internal->volume_db);
// Set the volume vector up according to the speaker mode and mix target.
// TODO do we need to scale the volume down when we output to more channels?
if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
} else {
switch (mix_target) {
case MIX_TARGET_STEREO: {
volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
} break;
case MIX_TARGET_SURROUND: {
// TODO Make sure this is right.
volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);
volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);
} break;
case MIX_TARGET_CENTER: {
// TODO Make sure this is right.
volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
} break;
}
}
return volume_vector;
}
void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {
internal->validate_property(p_property);
}
bool AudioStreamPlayer::has_stream_playback() {
return internal->has_stream_playback();
}
Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {
return internal->get_stream_playback();
}
AudioServer::PlaybackType AudioStreamPlayer::get_playback_type() const {
return internal->get_playback_type();
}
void AudioStreamPlayer::set_playback_type(AudioServer::PlaybackType p_playback_type) {
internal->set_playback_type(p_playback_type);
}
void AudioStreamPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);
ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);
ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
ClassDB::bind_method(D_METHOD("set_volume_linear", "volume_linear"), &AudioStreamPlayer::set_volume_linear);
ClassDB::bind_method(D_METHOD("get_volume_linear"), &AudioStreamPlayer::get_volume_linear);
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);
ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);
ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);
ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);
ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);
ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);
ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);
ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);
ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer::_set_playing);
ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);
ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);
ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);
ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);
ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);
ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);
ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer::set_playback_type);
ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer::get_playback_type);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_linear", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_volume_linear", "get_volume_linear");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");
ADD_SIGNAL(MethodInfo("finished"));
BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
}
AudioStreamPlayer::AudioStreamPlayer() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false));
}
AudioStreamPlayer::~AudioStreamPlayer() {
memdelete(internal);
}

View File

@@ -0,0 +1,119 @@
/**************************************************************************/
/* audio_stream_player.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "scene/main/node.h"
#include "servers/audio_server.h"
struct AudioFrame;
class AudioStream;
class AudioStreamPlayback;
class AudioStreamPlayerInternal;
class AudioStreamPlayer : public Node {
GDCLASS(AudioStreamPlayer, Node);
public:
enum MixTarget {
MIX_TARGET_STEREO,
MIX_TARGET_SURROUND,
MIX_TARGET_CENTER
};
private:
AudioStreamPlayerInternal *internal = nullptr;
MixTarget mix_target = MIX_TARGET_STEREO;
void _set_playing(bool p_enable);
bool _is_active() const;
Vector<AudioFrame> _get_volume_vector();
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;
#ifndef DISABLE_DEPRECATED
bool _is_autoplay_enabled_bind_compat_86907();
static void _bind_compatibility_methods();
#endif // DISABLE_DEPRECATED
public:
void set_stream(Ref<AudioStream> p_stream);
Ref<AudioStream> get_stream() const;
void set_volume_db(float p_volume);
float get_volume_db() const;
void set_volume_linear(float p_volume);
float get_volume_linear() const;
void set_pitch_scale(float p_pitch_scale);
float get_pitch_scale() const;
void set_max_polyphony(int p_max_polyphony);
int get_max_polyphony() const;
void play(float p_from_pos = 0.0);
void seek(float p_seconds);
void stop();
bool is_playing() const;
float get_playback_position();
void set_bus(const StringName &p_bus);
StringName get_bus() const;
void set_autoplay(bool p_enable);
bool is_autoplay_enabled() const;
void set_mix_target(MixTarget p_target);
MixTarget get_mix_target() const;
void set_stream_paused(bool p_pause);
bool get_stream_paused() const;
bool has_stream_playback();
Ref<AudioStreamPlayback> get_stream_playback();
AudioServer::PlaybackType get_playback_type() const;
void set_playback_type(AudioServer::PlaybackType p_playback_type);
AudioStreamPlayer();
~AudioStreamPlayer();
};
VARIANT_ENUM_CAST(AudioStreamPlayer::MixTarget)

View File

@@ -0,0 +1,363 @@
/**************************************************************************/
/* audio_stream_player_internal.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#include "audio_stream_player_internal.h"
#include "scene/main/node.h"
#include "servers/audio/audio_stream.h"
void AudioStreamPlayerInternal::_set_process(bool p_enabled) {
if (physical) {
node->set_physics_process_internal(p_enabled);
} else {
node->set_process_internal(p_enabled);
}
}
void AudioStreamPlayerInternal::_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 AudioStreamPlayerInternal::process() {
Vector<Ref<AudioStreamPlayback>> playbacks_to_remove;
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {
playbacks_to_remove.push_back(playback);
}
}
// Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) {
stream_playbacks.erase(playback);
}
if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {
// This node is no longer actively playing audio.
active.clear();
_set_process(false);
}
if (!playbacks_to_remove.is_empty()) {
node->emit_signal(SceneStringName(finished));
}
}
void AudioStreamPlayerInternal::ensure_playback_limit() {
while (stream_playbacks.size() > max_polyphony) {
AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);
stream_playbacks.remove_at(0);
}
}
void AudioStreamPlayerInternal::notification(int p_what) {
switch (p_what) {
case Node::NOTIFICATION_ENTER_TREE: {
if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
play_callable.call(0.0);
}
set_stream_paused(!node->can_process());
} break;
case Node::NOTIFICATION_EXIT_TREE: {
set_stream_paused(true);
} break;
case Node::NOTIFICATION_INTERNAL_PROCESS: {
process();
} break;
case Node::NOTIFICATION_PREDELETE: {
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback);
}
stream_playbacks.clear();
} break;
case Node::NOTIFICATION_SUSPENDED:
case Node::NOTIFICATION_PAUSED: {
if (!node->can_process()) {
// Node can't process so we start fading out to silence
set_stream_paused(true);
}
} break;
case Node::NOTIFICATION_UNSUSPENDED: {
if (node->get_tree()->is_paused()) {
break;
}
[[fallthrough]];
}
case Node::NOTIFICATION_UNPAUSED: {
set_stream_paused(false);
} break;
}
}
Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
Ref<AudioStreamPlayback> stream_playback;
if (stream.is_null()) {
return stream_playback;
}
ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");
if (stream->is_monophonic() && is_playing()) {
stop_callable.call();
}
stream_playback = stream->instantiate_playback();
ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");
for (const KeyValue<StringName, ParameterData> &K : playback_parameters) {
stream_playback->set_parameter(K.value.path, K.value.value);
}
// Sample handling.
if (_is_sample()) {
if (stream->can_be_sampled()) {
stream_playback->set_is_sample(true);
if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_null()) {
if (!AudioServer::get_singleton()->is_stream_registered_as_sample(stream)) {
AudioServer::get_singleton()->register_stream_as_sample(stream);
}
Ref<AudioSamplePlayback> sample_playback;
sample_playback.instantiate();
sample_playback->stream = stream;
sample_playback->pitch_scale = pitch_scale;
stream_playback->set_sample_playback(sample_playback);
}
} else if (!stream->is_meta_stream()) {
WARN_PRINT(vformat(R"(%s is trying to play a sample from a stream that cannot be sampled.)", node->get_path()));
}
}
stream_playbacks.push_back(stream_playback);
active.set();
_set_process(true);
return stream_playback;
}
void AudioStreamPlayerInternal::set_stream_paused(bool p_pause) {
// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
if (_is_sample() && playback->get_sample_playback().is_valid()) {
AudioServer::get_singleton()->set_sample_playback_pause(playback->get_sample_playback(), p_pause);
}
}
}
bool AudioStreamPlayerInternal::get_stream_paused() const {
// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
}
return false;
}
void AudioStreamPlayerInternal::validate_property(PropertyInfo &p_property) const {
if (!Engine::get_singleton()->is_editor_hint()) {
return;
}
if (p_property.name == "bus") {
String options;
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
if (i > 0) {
options += ",";
}
String name = AudioServer::get_singleton()->get_bus_name(i);
options += name;
}
p_property.hint_string = options;
}
}
bool AudioStreamPlayerInternal::set(const StringName &p_name, const Variant &p_value) {
ParameterData *pd = playback_parameters.getptr(p_name);
if (!pd) {
return false;
}
pd->value = p_value;
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
playback->set_parameter(pd->path, pd->value);
}
return true;
}
bool AudioStreamPlayerInternal::get(const StringName &p_name, Variant &r_ret) const {
const ParameterData *pd = playback_parameters.getptr(p_name);
if (!pd) {
return false;
}
r_ret = pd->value;
return true;
}
void AudioStreamPlayerInternal::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;
const ParameterData *pd = playback_parameters.getptr(pi.name);
if (pd && pd->value == K.default_value) {
pi.usage &= ~PROPERTY_USAGE_STORAGE;
}
p_list->push_back(pi);
}
}
void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {
if (stream.is_valid()) {
stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
}
stop_callable.call();
stream = p_stream;
_update_stream_parameters();
if (stream.is_valid()) {
stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
}
node->notify_property_list_changed();
}
void AudioStreamPlayerInternal::seek(float p_seconds) {
if (is_playing()) {
stop_callable.call();
play_callable.call(p_seconds);
}
}
void AudioStreamPlayerInternal::stop_basic() {
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback);
}
stream_playbacks.clear();
active.clear();
_set_process(false);
}
bool AudioStreamPlayerInternal::is_playing() const {
for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
if (AudioServer::get_singleton()->is_playback_active(playback)) {
return true;
}
}
return false;
}
float AudioStreamPlayerInternal::get_playback_position() {
// Return the playback position of the most recently started playback stream.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
}
return 0;
}
void AudioStreamPlayerInternal::set_playing(bool p_enable) {
if (p_enable) {
play_callable.call(0.0);
} else {
stop_callable.call();
}
}
bool AudioStreamPlayerInternal::is_active() const {
return active.is_set();
}
void AudioStreamPlayerInternal::set_pitch_scale(float p_pitch_scale) {
ERR_FAIL_COND(p_pitch_scale <= 0.0);
pitch_scale = p_pitch_scale;
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
}
}
void AudioStreamPlayerInternal::set_max_polyphony(int p_max_polyphony) {
if (p_max_polyphony > 0) {
max_polyphony = p_max_polyphony;
}
}
bool AudioStreamPlayerInternal::has_stream_playback() {
return !stream_playbacks.is_empty();
}
Ref<AudioStreamPlayback> AudioStreamPlayerInternal::get_stream_playback() {
ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");
return stream_playbacks[stream_playbacks.size() - 1];
}
void AudioStreamPlayerInternal::set_playback_type(AudioServer::PlaybackType p_playback_type) {
playback_type = p_playback_type;
}
AudioServer::PlaybackType AudioStreamPlayerInternal::get_playback_type() const {
return playback_type;
}
StringName AudioStreamPlayerInternal::get_bus() const {
const String bus_name = bus;
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
if (AudioServer::get_singleton()->get_bus_name(i) == bus_name) {
return bus;
}
}
return SceneStringName(Master);
}
AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {
node = p_node;
play_callable = p_play_callable;
stop_callable = p_stop_callable;
physical = p_physical;
bus = SceneStringName(Master);
AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp((Object *)node, &Object::notify_property_list_changed));
AudioServer::get_singleton()->connect("bus_renamed", callable_mp((Object *)node, &Object::notify_property_list_changed).unbind(3));
}

View File

@@ -0,0 +1,114 @@
/**************************************************************************/
/* audio_stream_player_internal.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "core/object/ref_counted.h"
#include "core/templates/safe_refcount.h"
#include "servers/audio_server.h"
class AudioStream;
class AudioStreamPlayback;
class AudioSamplePlayback;
class Node;
class AudioStreamPlayerInternal : public Object {
GDCLASS(AudioStreamPlayerInternal, Object);
private:
struct ParameterData {
StringName path;
Variant value;
};
static inline const String PARAM_PREFIX = "parameters/";
Node *node = nullptr;
Callable play_callable;
Callable stop_callable;
bool physical = false;
AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT;
HashMap<StringName, ParameterData> playback_parameters;
void _set_process(bool p_enabled);
void _update_stream_parameters();
_FORCE_INLINE_ bool _is_sample() {
return (AudioServer::get_singleton()->get_default_playback_type() == AudioServer::PlaybackType::PLAYBACK_TYPE_SAMPLE && get_playback_type() == AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT) || get_playback_type() == AudioServer::PlaybackType::PLAYBACK_TYPE_SAMPLE;
}
public:
Vector<Ref<AudioStreamPlayback>> stream_playbacks;
Ref<AudioStream> stream;
SafeFlag active;
float pitch_scale = 1.0;
float volume_db = 0.0;
bool autoplay = false;
StringName bus;
int max_polyphony = 1;
void process();
void ensure_playback_limit();
void notification(int p_what);
void validate_property(PropertyInfo &p_property) const;
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;
void set_stream(Ref<AudioStream> p_stream);
void set_pitch_scale(float p_pitch_scale);
void set_max_polyphony(int p_max_polyphony);
StringName get_bus() const;
Ref<AudioStreamPlayback> play_basic();
void seek(float p_seconds);
void stop_basic();
bool is_playing() const;
float get_playback_position();
void set_playing(bool p_enable);
bool is_active() const;
void set_stream_paused(bool p_pause);
bool get_stream_paused() const;
bool has_stream_playback();
Ref<AudioStreamPlayback> get_stream_playback();
void set_playback_type(AudioServer::PlaybackType p_playback_type);
AudioServer::PlaybackType get_playback_type() const;
AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical);
};