diff options
author | Max Hilbrunner <mhilbrunner@users.noreply.github.com> | 2018-07-03 17:23:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-03 17:23:53 +0200 |
commit | 6d0ade54c0f4216067cd421cae91ec1e4ac6725e (patch) | |
tree | ef16c0bdea8f4854f9ee0d8f1aaeef3fa12e8e99 /scene/2d | |
parent | 24ce1ace1309f7379169f7f17929bd59f85485b6 (diff) | |
parent | c409fe05972b39fb1509e0b5c106378a0c30450c (diff) |
Merge pull request #19205 from marcelofg55/audio_stream_pause
Pause AudioStreamPlayer when SceneTree pauses
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/audio_stream_player_2d.cpp | 50 | ||||
-rw-r--r-- | scene/2d/audio_stream_player_2d.h | 5 |
2 files changed, 48 insertions, 7 deletions
diff --git a/scene/2d/audio_stream_player_2d.cpp b/scene/2d/audio_stream_player_2d.cpp index 54541293fd..6e77369d65 100644 --- a/scene/2d/audio_stream_player_2d.cpp +++ b/scene/2d/audio_stream_player_2d.cpp @@ -36,11 +36,8 @@ void AudioStreamPlayer2D::_mix_audio() { - if (!stream_playback.is_valid()) { - return; - } - - if (!active) { + if (!stream_playback.is_valid() || !active || + (stream_paused && stream_paused_fade <= 0.f)) { return; } @@ -53,8 +50,10 @@ void AudioStreamPlayer2D::_mix_audio() { AudioFrame *buffer = mix_buffer.ptrw(); int buffer_size = mix_buffer.size(); - //mix - stream_playback->mix(buffer, pitch_scale, buffer_size); + // Mix if we're not paused or we're fading out + if (!stream_paused || stream_paused_fade > 0.f) { + stream_playback->mix(buffer, pitch_scale, buffer_size); + } //write all outputs for (int i = 0; i < output_count; i++) { @@ -86,6 +85,13 @@ void AudioStreamPlayer2D::_mix_audio() { AudioFrame vol_inc = (current.vol - prev_outputs[i].vol) / float(buffer_size); AudioFrame vol = current.vol; + if (stream_paused) { + vol = vol * stream_paused_fade; + if (stream_paused_fade > 0.f) { + stream_paused_fade -= 0.1f; + } + } + int cc = AudioServer::get_singleton()->get_channel_count(); if (cc == 1) { @@ -142,6 +148,17 @@ void AudioStreamPlayer2D::_notification(int p_what) { AudioServer::get_singleton()->remove_callback(_mix_audios, this); } + if (p_what == NOTIFICATION_PAUSED) { + if (!can_process()) { + // Node can't process so we start fading out to silence + set_stream_paused(true); + } + } + + if (p_what == NOTIFICATION_UNPAUSED) { + set_stream_paused(false); + } + if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) { //update anything related to position first, if possible of course @@ -418,6 +435,19 @@ uint32_t AudioStreamPlayer2D::get_area_mask() const { return area_mask; } +void AudioStreamPlayer2D::set_stream_paused(bool p_pause) { + + if (p_pause != stream_paused) { + stream_paused = p_pause; + stream_paused_fade = stream_paused ? 1.f : 0.f; + } +} + +bool AudioStreamPlayer2D::get_stream_paused() const { + + return stream_paused; +} + void AudioStreamPlayer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer2D::set_stream); @@ -454,6 +484,9 @@ void AudioStreamPlayer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer2D::set_area_mask); ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer2D::get_area_mask); + ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer2D::set_stream_paused); + ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer2D::get_stream_paused); + ClassDB::bind_method(D_METHOD("_bus_layout_changed"), &AudioStreamPlayer2D::_bus_layout_changed); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream"); @@ -461,6 +494,7 @@ void AudioStreamPlayer2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,32,0.01"), "set_pitch_scale", "get_pitch_scale"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", 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::REAL, "max_distance", PROPERTY_HINT_EXP_RANGE, "1,4096,1,or_greater"), "set_max_distance", "get_max_distance"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "attenuation", PROPERTY_HINT_EXP_EASING, "attenuation"), "set_attenuation", "get_attenuation"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus"); @@ -483,6 +517,8 @@ AudioStreamPlayer2D::AudioStreamPlayer2D() { setplay = -1; output_ready = false; area_mask = 1; + stream_paused = false; + stream_paused_fade = 0.f; AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed"); } diff --git a/scene/2d/audio_stream_player_2d.h b/scene/2d/audio_stream_player_2d.h index 9ae8e3a518..b508de3171 100644 --- a/scene/2d/audio_stream_player_2d.h +++ b/scene/2d/audio_stream_player_2d.h @@ -71,7 +71,9 @@ private: float volume_db; float pitch_scale; + float stream_paused_fade; bool autoplay; + bool stream_paused; StringName bus; void _mix_audio(); @@ -123,6 +125,9 @@ public: void set_area_mask(uint32_t p_mask); uint32_t get_area_mask() const; + void set_stream_paused(bool p_pause); + bool get_stream_paused() const; + AudioStreamPlayer2D(); ~AudioStreamPlayer2D(); }; |