diff options
author | Marcelo Fernandez <marcelofg55@gmail.com> | 2018-05-27 15:29:10 -0300 |
---|---|---|
committer | Marcelo Fernandez <marcelofg55@gmail.com> | 2018-06-03 14:01:47 -0300 |
commit | c409fe05972b39fb1509e0b5c106378a0c30450c (patch) | |
tree | 9132e9250a24d86b65295abe039ccfd0f1c5cd43 /scene/2d/audio_stream_player_2d.cpp | |
parent | c80ac06253e832284be7ae37515b0ca1ecf11a52 (diff) |
Pause AudioStreamPlayers when SceneTree pauses
Diffstat (limited to 'scene/2d/audio_stream_player_2d.cpp')
-rw-r--r-- | scene/2d/audio_stream_player_2d.cpp | 50 |
1 files changed, 43 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"); } |