Path: blob/master/scene/audio/audio_stream_player_internal.cpp
9903 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: {116if (!node->can_process()) {117// Node can't process so we start fading out to silence118set_stream_paused(true);119}120} break;121122case Node::NOTIFICATION_UNSUSPENDED: {123if (node->get_tree()->is_paused()) {124break;125}126[[fallthrough]];127}128129case Node::NOTIFICATION_UNPAUSED: {130set_stream_paused(false);131} break;132}133}134135Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {136Ref<AudioStreamPlayback> stream_playback;137if (stream.is_null()) {138return stream_playback;139}140ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");141if (stream->is_monophonic() && is_playing()) {142stop_callable.call();143}144stream_playback = stream->instantiate_playback();145ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");146147for (const KeyValue<StringName, ParameterData> &K : playback_parameters) {148stream_playback->set_parameter(K.value.path, K.value.value);149}150151// Sample handling.152if (_is_sample()) {153if (stream->can_be_sampled()) {154stream_playback->set_is_sample(true);155if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_null()) {156if (!AudioServer::get_singleton()->is_stream_registered_as_sample(stream)) {157AudioServer::get_singleton()->register_stream_as_sample(stream);158}159Ref<AudioSamplePlayback> sample_playback;160sample_playback.instantiate();161sample_playback->stream = stream;162sample_playback->pitch_scale = pitch_scale;163stream_playback->set_sample_playback(sample_playback);164}165} else if (!stream->is_meta_stream()) {166WARN_PRINT(vformat(R"(%s is trying to play a sample from a stream that cannot be sampled.)", node->get_path()));167}168}169170stream_playbacks.push_back(stream_playback);171active.set();172_set_process(true);173return stream_playback;174}175176void AudioStreamPlayerInternal::set_stream_paused(bool p_pause) {177// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.178for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {179AudioServer::get_singleton()->set_playback_paused(playback, p_pause);180if (_is_sample() && playback->get_sample_playback().is_valid()) {181AudioServer::get_singleton()->set_sample_playback_pause(playback->get_sample_playback(), p_pause);182}183}184}185186bool AudioStreamPlayerInternal::get_stream_paused() const {187// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.188if (!stream_playbacks.is_empty()) {189return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);190}191return false;192}193194void AudioStreamPlayerInternal::validate_property(PropertyInfo &p_property) const {195if (!Engine::get_singleton()->is_editor_hint()) {196return;197}198if (p_property.name == "bus") {199String options;200for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {201if (i > 0) {202options += ",";203}204String name = AudioServer::get_singleton()->get_bus_name(i);205options += name;206}207208p_property.hint_string = options;209}210}211212bool AudioStreamPlayerInternal::set(const StringName &p_name, const Variant &p_value) {213ParameterData *pd = playback_parameters.getptr(p_name);214if (!pd) {215return false;216}217pd->value = p_value;218for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {219playback->set_parameter(pd->path, pd->value);220}221return true;222}223224bool AudioStreamPlayerInternal::get(const StringName &p_name, Variant &r_ret) const {225const ParameterData *pd = playback_parameters.getptr(p_name);226if (!pd) {227return false;228}229r_ret = pd->value;230return true;231}232233void AudioStreamPlayerInternal::get_property_list(List<PropertyInfo> *p_list) const {234if (stream.is_null()) {235return;236}237List<AudioStream::Parameter> parameters;238stream->get_parameter_list(¶meters);239for (const AudioStream::Parameter &K : parameters) {240PropertyInfo pi = K.property;241pi.name = PARAM_PREFIX + pi.name;242243const ParameterData *pd = playback_parameters.getptr(pi.name);244if (pd && pd->value == K.default_value) {245pi.usage &= ~PROPERTY_USAGE_STORAGE;246}247248p_list->push_back(pi);249}250}251252void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {253if (stream.is_valid()) {254stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));255}256stop_callable.call();257stream = p_stream;258_update_stream_parameters();259if (stream.is_valid()) {260stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));261}262node->notify_property_list_changed();263}264265void AudioStreamPlayerInternal::seek(float p_seconds) {266if (is_playing()) {267stop_callable.call();268play_callable.call(p_seconds);269}270}271272void AudioStreamPlayerInternal::stop_basic() {273for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {274AudioServer::get_singleton()->stop_playback_stream(playback);275}276stream_playbacks.clear();277278active.clear();279_set_process(false);280}281282bool AudioStreamPlayerInternal::is_playing() const {283for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {284if (AudioServer::get_singleton()->is_playback_active(playback)) {285return true;286}287}288return false;289}290291float AudioStreamPlayerInternal::get_playback_position() {292// Return the playback position of the most recently started playback stream.293if (!stream_playbacks.is_empty()) {294return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);295}296return 0;297}298299void AudioStreamPlayerInternal::set_playing(bool p_enable) {300if (p_enable) {301play_callable.call(0.0);302} else {303stop_callable.call();304}305}306307bool AudioStreamPlayerInternal::is_active() const {308return active.is_set();309}310311void AudioStreamPlayerInternal::set_pitch_scale(float p_pitch_scale) {312ERR_FAIL_COND(p_pitch_scale <= 0.0);313pitch_scale = p_pitch_scale;314315for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {316AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);317}318}319320void AudioStreamPlayerInternal::set_max_polyphony(int p_max_polyphony) {321if (p_max_polyphony > 0) {322max_polyphony = p_max_polyphony;323}324}325326bool AudioStreamPlayerInternal::has_stream_playback() {327return !stream_playbacks.is_empty();328}329330Ref<AudioStreamPlayback> AudioStreamPlayerInternal::get_stream_playback() {331ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");332return stream_playbacks[stream_playbacks.size() - 1];333}334335void AudioStreamPlayerInternal::set_playback_type(AudioServer::PlaybackType p_playback_type) {336playback_type = p_playback_type;337}338339AudioServer::PlaybackType AudioStreamPlayerInternal::get_playback_type() const {340return playback_type;341}342343StringName AudioStreamPlayerInternal::get_bus() const {344const String bus_name = bus;345for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {346if (AudioServer::get_singleton()->get_bus_name(i) == bus_name) {347return bus;348}349}350return SceneStringName(Master);351}352353AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {354node = p_node;355play_callable = p_play_callable;356stop_callable = p_stop_callable;357physical = p_physical;358bus = SceneStringName(Master);359360AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp((Object *)node, &Object::notify_property_list_changed));361AudioServer::get_singleton()->connect("bus_renamed", callable_mp((Object *)node, &Object::notify_property_list_changed).unbind(3));362}363364365