Path: blob/master/scene/audio/audio_stream_player.cpp
20818 views
/**************************************************************************/1/* audio_stream_player.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.h"31#include "audio_stream_player.compat.inc"3233#include "scene/audio/audio_stream_player_internal.h"34#include "servers/audio/audio_stream.h"35#include "servers/display/display_server.h"3637void AudioStreamPlayer::_notification(int p_what) {38if (p_what == NOTIFICATION_ACCESSIBILITY_UPDATE) {39RID ae = get_accessibility_element();40ERR_FAIL_COND(ae.is_null());4142DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_AUDIO);43} else {44internal->notification(p_what);45}46}4748void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {49internal->set_stream(p_stream);50}5152bool AudioStreamPlayer::_set(const StringName &p_name, const Variant &p_value) {53return internal->set(p_name, p_value);54}5556bool AudioStreamPlayer::_get(const StringName &p_name, Variant &r_ret) const {57return internal->get(p_name, r_ret);58}5960void AudioStreamPlayer::_get_property_list(List<PropertyInfo> *p_list) const {61internal->get_property_list(p_list);62}6364Ref<AudioStream> AudioStreamPlayer::get_stream() const {65return internal->stream;66}6768void AudioStreamPlayer::set_volume_db(float p_volume) {69ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");70internal->volume_db = p_volume;7172Vector<AudioFrame> volume_vector = _get_volume_vector();73for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {74AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);75}76}7778float AudioStreamPlayer::get_volume_db() const {79return internal->volume_db;80}8182void AudioStreamPlayer::set_volume_linear(float p_volume) {83set_volume_db(Math::linear_to_db(p_volume));84}8586float AudioStreamPlayer::get_volume_linear() const {87return Math::db_to_linear(get_volume_db());88}8990void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {91internal->set_pitch_scale(p_pitch_scale);92}9394float AudioStreamPlayer::get_pitch_scale() const {95return internal->pitch_scale;96}9798void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) {99internal->set_max_polyphony(p_max_polyphony);100}101102int AudioStreamPlayer::get_max_polyphony() const {103return internal->max_polyphony;104}105106void AudioStreamPlayer::play(float p_from_pos) {107Ref<AudioStreamPlayback> stream_playback = internal->play_basic();108if (stream_playback.is_null()) {109return;110}111AudioServer::get_singleton()->start_playback_stream(stream_playback, internal->bus, _get_volume_vector(), p_from_pos, internal->pitch_scale);112internal->ensure_playback_limit();113114// Sample handling.115if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) {116Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();117sample_playback->offset = p_from_pos;118sample_playback->volume_vector = _get_volume_vector();119sample_playback->bus = get_bus();120121AudioServer::get_singleton()->start_sample_playback(sample_playback);122}123}124125void AudioStreamPlayer::seek(float p_seconds) {126internal->seek(p_seconds);127}128129void AudioStreamPlayer::stop() {130internal->stop_basic();131}132133bool AudioStreamPlayer::is_playing() const {134return internal->is_playing();135}136137float AudioStreamPlayer::get_playback_position() {138return internal->get_playback_position();139}140141void AudioStreamPlayer::set_bus(const StringName &p_bus) {142internal->bus = p_bus;143for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {144AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());145}146}147148StringName AudioStreamPlayer::get_bus() const {149return internal->get_bus();150}151152void AudioStreamPlayer::set_autoplay(bool p_enable) {153internal->autoplay = p_enable;154}155156bool AudioStreamPlayer::is_autoplay_enabled() const {157return internal->autoplay;158}159160void AudioStreamPlayer::set_mix_target(MixTarget p_target) {161mix_target = p_target;162}163164AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {165return mix_target;166}167168void AudioStreamPlayer::_set_playing(bool p_enable) {169internal->set_playing(p_enable);170}171172void AudioStreamPlayer::set_stream_paused(bool p_pause) {173internal->set_stream_paused(p_pause);174}175176bool AudioStreamPlayer::get_stream_paused() const {177return internal->get_stream_paused();178}179180Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {181Vector<AudioFrame> volume_vector;182// We need at most four stereo pairs (for 7.1 systems).183volume_vector.resize(4);184185// Initialize the volume vector to zero.186for (AudioFrame &channel_volume_db : volume_vector) {187channel_volume_db = AudioFrame(0, 0);188}189190float volume_linear = Math::db_to_linear(internal->volume_db);191192// Set the volume vector up according to the speaker mode and mix target.193// TODO do we need to scale the volume down when we output to more channels?194if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {195volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);196} else {197switch (mix_target) {198case MIX_TARGET_STEREO: {199volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);200} break;201case MIX_TARGET_SURROUND: {202// TODO Make sure this is right.203volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);204volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);205volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);206volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);207} break;208case MIX_TARGET_CENTER: {209// TODO Make sure this is right.210volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);211} break;212}213}214return volume_vector;215}216217void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {218internal->validate_property(p_property);219}220221bool AudioStreamPlayer::has_stream_playback() {222return internal->has_stream_playback();223}224225Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {226return internal->get_stream_playback();227}228229AudioServer::PlaybackType AudioStreamPlayer::get_playback_type() const {230return internal->get_playback_type();231}232233void AudioStreamPlayer::set_playback_type(AudioServer::PlaybackType p_playback_type) {234internal->set_playback_type(p_playback_type);235}236237void AudioStreamPlayer::_bind_methods() {238ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);239ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);240241ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);242ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);243244ClassDB::bind_method(D_METHOD("set_volume_linear", "volume_linear"), &AudioStreamPlayer::set_volume_linear);245ClassDB::bind_method(D_METHOD("get_volume_linear"), &AudioStreamPlayer::get_volume_linear);246247ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);248ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);249250ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));251ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);252ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);253254ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);255ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);256257ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);258ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);259260ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);261ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);262263ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);264ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);265266ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer::_set_playing);267268ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);269ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);270271ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);272ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);273274ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);275ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);276277ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer::set_playback_type);278ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer::get_playback_type);279280ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, AudioStream::get_class_static()), "set_stream", "get_stream");281ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");282ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_linear", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_volume_linear", "get_volume_linear");283ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");284ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");285ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");286ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");287ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");288ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");289ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");290ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");291292ADD_SIGNAL(MethodInfo("finished"));293294BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);295BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);296BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);297}298299AudioStreamPlayer::AudioStreamPlayer() {300internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false));301}302303AudioStreamPlayer::~AudioStreamPlayer() {304memdelete(internal);305}306307308