Path: blob/master/scene/audio/audio_stream_player.cpp
9896 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"3536void AudioStreamPlayer::_notification(int p_what) {37if (p_what == NOTIFICATION_ACCESSIBILITY_UPDATE) {38RID ae = get_accessibility_element();39ERR_FAIL_COND(ae.is_null());4041DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_AUDIO);42} else {43internal->notification(p_what);44}45}4647void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {48internal->set_stream(p_stream);49}5051bool AudioStreamPlayer::_set(const StringName &p_name, const Variant &p_value) {52return internal->set(p_name, p_value);53}5455bool AudioStreamPlayer::_get(const StringName &p_name, Variant &r_ret) const {56return internal->get(p_name, r_ret);57}5859void AudioStreamPlayer::_get_property_list(List<PropertyInfo> *p_list) const {60internal->get_property_list(p_list);61}6263Ref<AudioStream> AudioStreamPlayer::get_stream() const {64return internal->stream;65}6667void AudioStreamPlayer::set_volume_db(float p_volume) {68ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");69internal->volume_db = p_volume;7071Vector<AudioFrame> volume_vector = _get_volume_vector();72for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {73AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);74}75}7677float AudioStreamPlayer::get_volume_db() const {78return internal->volume_db;79}8081void AudioStreamPlayer::set_volume_linear(float p_volume) {82set_volume_db(Math::linear_to_db(p_volume));83}8485float AudioStreamPlayer::get_volume_linear() const {86return Math::db_to_linear(get_volume_db());87}8889void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {90internal->set_pitch_scale(p_pitch_scale);91}9293float AudioStreamPlayer::get_pitch_scale() const {94return internal->pitch_scale;95}9697void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) {98internal->set_max_polyphony(p_max_polyphony);99}100101int AudioStreamPlayer::get_max_polyphony() const {102return internal->max_polyphony;103}104105void AudioStreamPlayer::play(float p_from_pos) {106Ref<AudioStreamPlayback> stream_playback = internal->play_basic();107if (stream_playback.is_null()) {108return;109}110AudioServer::get_singleton()->start_playback_stream(stream_playback, internal->bus, _get_volume_vector(), p_from_pos, internal->pitch_scale);111internal->ensure_playback_limit();112113// Sample handling.114if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) {115Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();116sample_playback->offset = p_from_pos;117sample_playback->volume_vector = _get_volume_vector();118sample_playback->bus = get_bus();119120AudioServer::get_singleton()->start_sample_playback(sample_playback);121}122}123124void AudioStreamPlayer::seek(float p_seconds) {125internal->seek(p_seconds);126}127128void AudioStreamPlayer::stop() {129internal->stop_basic();130}131132bool AudioStreamPlayer::is_playing() const {133return internal->is_playing();134}135136float AudioStreamPlayer::get_playback_position() {137return internal->get_playback_position();138}139140void AudioStreamPlayer::set_bus(const StringName &p_bus) {141internal->bus = p_bus;142for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {143AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());144}145}146147StringName AudioStreamPlayer::get_bus() const {148return internal->get_bus();149}150151void AudioStreamPlayer::set_autoplay(bool p_enable) {152internal->autoplay = p_enable;153}154155bool AudioStreamPlayer::is_autoplay_enabled() const {156return internal->autoplay;157}158159void AudioStreamPlayer::set_mix_target(MixTarget p_target) {160mix_target = p_target;161}162163AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {164return mix_target;165}166167void AudioStreamPlayer::_set_playing(bool p_enable) {168internal->set_playing(p_enable);169}170171void AudioStreamPlayer::set_stream_paused(bool p_pause) {172internal->set_stream_paused(p_pause);173}174175bool AudioStreamPlayer::get_stream_paused() const {176return internal->get_stream_paused();177}178179Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {180Vector<AudioFrame> volume_vector;181// We need at most four stereo pairs (for 7.1 systems).182volume_vector.resize(4);183184// Initialize the volume vector to zero.185for (AudioFrame &channel_volume_db : volume_vector) {186channel_volume_db = AudioFrame(0, 0);187}188189float volume_linear = Math::db_to_linear(internal->volume_db);190191// Set the volume vector up according to the speaker mode and mix target.192// TODO do we need to scale the volume down when we output to more channels?193if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {194volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);195} else {196switch (mix_target) {197case MIX_TARGET_STEREO: {198volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);199} break;200case MIX_TARGET_SURROUND: {201// TODO Make sure this is right.202volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);203volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);204volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);205volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);206} break;207case MIX_TARGET_CENTER: {208// TODO Make sure this is right.209volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);210} break;211}212}213return volume_vector;214}215216void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {217internal->validate_property(p_property);218}219220bool AudioStreamPlayer::has_stream_playback() {221return internal->has_stream_playback();222}223224Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {225return internal->get_stream_playback();226}227228AudioServer::PlaybackType AudioStreamPlayer::get_playback_type() const {229return internal->get_playback_type();230}231232void AudioStreamPlayer::set_playback_type(AudioServer::PlaybackType p_playback_type) {233internal->set_playback_type(p_playback_type);234}235236void AudioStreamPlayer::_bind_methods() {237ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);238ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);239240ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);241ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);242243ClassDB::bind_method(D_METHOD("set_volume_linear", "volume_linear"), &AudioStreamPlayer::set_volume_linear);244ClassDB::bind_method(D_METHOD("get_volume_linear"), &AudioStreamPlayer::get_volume_linear);245246ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);247ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);248249ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));250ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);251ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);252253ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);254ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);255256ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);257ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);258259ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);260ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);261262ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);263ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);264265ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer::_set_playing);266267ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);268ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);269270ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);271ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);272273ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);274ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);275276ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer::set_playback_type);277ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer::get_playback_type);278279ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");280ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");281ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_linear", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_volume_linear", "get_volume_linear");282ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");283ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");284ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");285ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");286ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");287ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");288ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");289ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");290291ADD_SIGNAL(MethodInfo("finished"));292293BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);294BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);295BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);296}297298AudioStreamPlayer::AudioStreamPlayer() {299internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false));300}301302AudioStreamPlayer::~AudioStreamPlayer() {303memdelete(internal);304}305306307