Path: blob/master/scene/audio/audio_stream_player_internal.cpp
20888 views
/**************************************************************************/1/* audio_stream_player_internal.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_stream_player_internal.h"3132#include "scene/main/node.h"33#include "servers/audio/audio_stream.h"3435void AudioStreamPlayerInternal::_set_process(bool p_enabled) {36if (physical) {37node->set_physics_process_internal(p_enabled);38} else {39node->set_process_internal(p_enabled);40}41}4243void AudioStreamPlayerInternal::_update_stream_parameters() {44if (stream.is_null()) {45return;46}4748List<AudioStream::Parameter> parameters;49stream->get_parameter_list(¶meters);50for (const AudioStream::Parameter &K : parameters) {51const PropertyInfo &pi = K.property;52StringName key = PARAM_PREFIX + pi.name;53if (!playback_parameters.has(key)) {54ParameterData pd;55pd.path = pi.name;56pd.value = K.default_value;57playback_parameters.insert(key, pd);58}59}60}6162void AudioStreamPlayerInternal::process() {63Vector<Ref<AudioStreamPlayback>> playbacks_to_remove;64for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {65if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {66playbacks_to_remove.push_back(playback);67}68}69// Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.70for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) {71stream_playbacks.erase(playback);72}73if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {74// This node is no longer actively playing audio.75active.clear();76_set_process(false);77}78if (!playbacks_to_remove.is_empty()) {79node->emit_signal(SceneStringName(finished));80}81}8283void AudioStreamPlayerInternal::ensure_playback_limit() {84while (stream_playbacks.size() > max_polyphony) {85AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);86stream_playbacks.remove_at(0);87}88}8990void AudioStreamPlayerInternal::notification(int p_what) {91switch (p_what) {92case Node::NOTIFICATION_ENTER_TREE: {93if (autoplay && !Engine::get_singleton()->is_editor_hint()) {94play_callable.call(0.0);95}96set_stream_paused(!node->can_process());97} break;9899case Node::NOTIFICATION_EXIT_TREE: {100set_stream_paused(true);101} break;102103case Node::NOTIFICATION_INTERNAL_PROCESS: {104process();105} break;106107case Node::NOTIFICATION_PREDELETE: {108for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {109AudioServer::get_singleton()->stop_playback_stream(playback);110}111stream_playbacks.clear();112} break;113114case Node::NOTIFICATION_SUSPENDED:115case Node::NOTIFICATION_PAUSED: {116bool can_process = node->is_inside_tree() && node->can_process();117if (!can_process) {118// Node can't process so we start fading out to silence119set_stream_paused(true);120}121} break;122123case Node::NOTIFICATION_UNSUSPENDED: {124if (node->get_tree()->is_paused()) {125break;126}127[[fallthrough]];128}129130case Node::NOTIFICATION_UNPAUSED: {131set_stream_paused(false);132} break;133}134}135136Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {137Ref<AudioStreamPlayback> stream_playback;138if (stream.is_null()) {139return stream_playback;140}141ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");142if (stream->is_monophonic() && is_playing()) {143stop_callable.call();144}145stream_playback = stream->instantiate_playback();146ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");147148for (const KeyValue<StringName, ParameterData> &K : playback_parameters) {149stream_playback->set_parameter(K.value.path, K.value.value);150}151152// Sample handling.153if (_is_sample()) {154if (stream->can_be_sampled()) {155stream_playback->set_is_sample(true);156if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_null()) {157if (!AudioServer::get_singleton()->is_stream_registered_as_sample(stream)) {158AudioServer::get_singleton()->register_stream_as_sample(stream);159}160Ref<AudioSamplePlayback> sample_playback;161sample_playback.instantiate();162sample_playback->stream = stream;163sample_playback->pitch_scale = pitch_scale;164stream_playback->set_sample_playback(sample_playback);165}166} else if (!stream->is_meta_stream()) {167WARN_PRINT(vformat(R"(%s is trying to play a sample from a stream that cannot be sampled.)", node->get_path()));168}169}170171stream_playbacks.push_back(stream_playback);172active.set();173_set_process(true);174return stream_playback;175}176177void AudioStreamPlayerInternal::set_stream_paused(bool p_pause) {178// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.179for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {180AudioServer::get_singleton()->set_playback_paused(playback, p_pause);181if (_is_sample() && playback->get_sample_playback().is_valid()) {182AudioServer::get_singleton()->set_sample_playback_pause(playback->get_sample_playback(), p_pause);183}184}185}186187bool AudioStreamPlayerInternal::get_stream_paused() const {188// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.189if (!stream_playbacks.is_empty()) {190return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);191}192return false;193}194195void AudioStreamPlayerInternal::validate_property(PropertyInfo &p_property) const {196if (!Engine::get_singleton()->is_editor_hint()) {197return;198}199if (p_property.name == "bus") {200String options;201for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {202if (i > 0) {203options += ",";204}205String name = AudioServer::get_singleton()->get_bus_name(i);206options += name;207}208209p_property.hint_string = options;210}211}212213bool AudioStreamPlayerInternal::set(const StringName &p_name, const Variant &p_value) {214ParameterData *pd = playback_parameters.getptr(p_name);215if (!pd) {216return false;217}218pd->value = p_value;219for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {220playback->set_parameter(pd->path, pd->value);221}222return true;223}224225bool AudioStreamPlayerInternal::get(const StringName &p_name, Variant &r_ret) const {226const ParameterData *pd = playback_parameters.getptr(p_name);227if (!pd) {228return false;229}230r_ret = pd->value;231return true;232}233234void AudioStreamPlayerInternal::get_property_list(List<PropertyInfo> *p_list) const {235if (stream.is_null()) {236return;237}238List<AudioStream::Parameter> parameters;239stream->get_parameter_list(¶meters);240for (const AudioStream::Parameter &K : parameters) {241PropertyInfo pi = K.property;242pi.name = PARAM_PREFIX + pi.name;243244const ParameterData *pd = playback_parameters.getptr(pi.name);245if (pd && pd->value == K.default_value) {246pi.usage &= ~PROPERTY_USAGE_STORAGE;247}248249p_list->push_back(pi);250}251}252253void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {254if (stream.is_valid()) {255stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));256}257stop_callable.call();258stream = p_stream;259_update_stream_parameters();260if (stream.is_valid()) {261stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));262}263node->notify_property_list_changed();264}265266void AudioStreamPlayerInternal::seek(float p_seconds) {267if (is_playing()) {268stop_callable.call();269play_callable.call(p_seconds);270}271}272273void AudioStreamPlayerInternal::stop_basic() {274for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {275AudioServer::get_singleton()->stop_playback_stream(playback);276}277stream_playbacks.clear();278279active.clear();280_set_process(false);281}282283bool AudioStreamPlayerInternal::is_playing() const {284for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {285if (AudioServer::get_singleton()->is_playback_active(playback)) {286return true;287}288}289return false;290}291292float AudioStreamPlayerInternal::get_playback_position() {293// Return the playback position of the most recently started playback stream.294if (!stream_playbacks.is_empty()) {295return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);296}297return 0;298}299300void AudioStreamPlayerInternal::set_playing(bool p_enable) {301if (p_enable) {302play_callable.call(0.0);303} else {304stop_callable.call();305}306}307308bool AudioStreamPlayerInternal::is_active() const {309return active.is_set();310}311312void AudioStreamPlayerInternal::set_pitch_scale(float p_pitch_scale) {313ERR_FAIL_COND(p_pitch_scale <= 0.0);314pitch_scale = p_pitch_scale;315316for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {317AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);318}319}320321void AudioStreamPlayerInternal::set_max_polyphony(int p_max_polyphony) {322if (p_max_polyphony > 0) {323max_polyphony = p_max_polyphony;324}325}326327bool AudioStreamPlayerInternal::has_stream_playback() {328return !stream_playbacks.is_empty();329}330331Ref<AudioStreamPlayback> AudioStreamPlayerInternal::get_stream_playback() {332ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");333return stream_playbacks[stream_playbacks.size() - 1];334}335336void AudioStreamPlayerInternal::set_playback_type(AudioServer::PlaybackType p_playback_type) {337playback_type = p_playback_type;338}339340AudioServer::PlaybackType AudioStreamPlayerInternal::get_playback_type() const {341return playback_type;342}343344StringName AudioStreamPlayerInternal::get_bus() const {345const String bus_name = bus;346for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {347if (AudioServer::get_singleton()->get_bus_name(i) == bus_name) {348return bus;349}350}351return SceneStringName(Master);352}353354AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {355node = p_node;356play_callable = p_play_callable;357stop_callable = p_stop_callable;358physical = p_physical;359bus = SceneStringName(Master);360361AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp((Object *)node, &Object::notify_property_list_changed));362AudioServer::get_singleton()->connect("bus_renamed", callable_mp((Object *)node, &Object::notify_property_list_changed).unbind(3));363}364365366