Browse Source

Merge pull request #25974 from marcelofg55/stream_stop_fade

Fix AudioStreams::stop possibly causing a small noise
Rémi Verschelde 6 years ago
parent
commit
006561a081

+ 21 - 14
scene/2d/audio_stream_player_2d.cpp

@@ -37,7 +37,7 @@
 void AudioStreamPlayer2D::_mix_audio() {
 
 	if (!stream_playback.is_valid() || !active ||
-			(stream_paused && !stream_paused_fade_out)) {
+			(stream_paused && !stream_fade_out)) {
 		return;
 	}
 
@@ -50,7 +50,7 @@ void AudioStreamPlayer2D::_mix_audio() {
 	AudioFrame *buffer = mix_buffer.ptrw();
 	int buffer_size = mix_buffer.size();
 
-	if (stream_paused_fade_out) {
+	if (stream_fade_out) {
 		// Short fadeout ramp
 		buffer_size = MIN(buffer_size, 128);
 	}
@@ -84,10 +84,10 @@ void AudioStreamPlayer2D::_mix_audio() {
 		}
 
 		//mix!
-		AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol;
-		AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol;
+		AudioFrame target_volume = stream_fade_out ? AudioFrame(0.f, 0.f) : current.vol;
+		AudioFrame vol_prev = stream_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol;
 		AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
-		AudioFrame vol = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : current.vol;
+		AudioFrame vol = stream_fade_in ? AudioFrame(0.f, 0.f) : current.vol;
 
 		int cc = AudioServer::get_singleton()->get_channel_count();
 
@@ -139,9 +139,15 @@ void AudioStreamPlayer2D::_mix_audio() {
 		active = false;
 	}
 
+	if (stream_stop) {
+		active = false;
+		set_physics_process_internal(false);
+		setplay = -1;
+	}
+
 	output_ready = false;
-	stream_paused_fade_in = false;
-	stream_paused_fade_out = false;
+	stream_fade_in = false;
+	stream_fade_out = false;
 }
 
 void AudioStreamPlayer2D::_notification(int p_what) {
@@ -323,6 +329,7 @@ void AudioStreamPlayer2D::play(float p_from_pos) {
 	}
 
 	if (stream_playback.is_valid()) {
+		stream_stop = false;
 		active = true;
 		setplay = p_from_pos;
 		output_ready = false;
@@ -340,9 +347,8 @@ void AudioStreamPlayer2D::seek(float p_seconds) {
 void AudioStreamPlayer2D::stop() {
 
 	if (stream_playback.is_valid()) {
-		active = false;
-		set_physics_process_internal(false);
-		setplay = -1;
+		stream_stop = true;
+		stream_fade_out = true;
 	}
 }
 
@@ -457,8 +463,8 @@ void AudioStreamPlayer2D::set_stream_paused(bool p_pause) {
 
 	if (p_pause != stream_paused) {
 		stream_paused = p_pause;
-		stream_paused_fade_in = p_pause ? false : true;
-		stream_paused_fade_out = p_pause ? true : false;
+		stream_fade_in = p_pause ? false : true;
+		stream_fade_out = p_pause ? true : false;
 	}
 }
 
@@ -537,8 +543,9 @@ AudioStreamPlayer2D::AudioStreamPlayer2D() {
 	output_ready = false;
 	area_mask = 1;
 	stream_paused = false;
-	stream_paused_fade_in = false;
-	stream_paused_fade_out = false;
+	stream_fade_in = false;
+	stream_fade_out = false;
+	stream_stop = false;
 	AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
 }
 

+ 3 - 2
scene/2d/audio_stream_player_2d.h

@@ -73,8 +73,9 @@ private:
 	float pitch_scale;
 	bool autoplay;
 	bool stream_paused;
-	bool stream_paused_fade_in;
-	bool stream_paused_fade_out;
+	bool stream_fade_in;
+	bool stream_fade_out;
+	bool stream_stop;
 	StringName bus;
 
 	void _mix_audio();

+ 21 - 14
scene/3d/audio_stream_player_3d.cpp

@@ -38,7 +38,7 @@
 void AudioStreamPlayer3D::_mix_audio() {
 
 	if (!stream_playback.is_valid() || !active ||
-			(stream_paused && !stream_paused_fade_out)) {
+			(stream_paused && !stream_fade_out)) {
 		return;
 	}
 
@@ -53,7 +53,7 @@ void AudioStreamPlayer3D::_mix_audio() {
 	AudioFrame *buffer = mix_buffer.ptrw();
 	int buffer_size = mix_buffer.size();
 
-	if (stream_paused_fade_out) {
+	if (stream_fade_out) {
 		// Short fadeout ramp
 		buffer_size = MIN(buffer_size, 128);
 	}
@@ -109,10 +109,10 @@ void AudioStreamPlayer3D::_mix_audio() {
 		int buffers = AudioServer::get_singleton()->get_channel_count();
 
 		for (int k = 0; k < buffers; k++) {
-			AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol[k];
-			AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol[k];
+			AudioFrame target_volume = stream_fade_out ? AudioFrame(0.f, 0.f) : current.vol[k];
+			AudioFrame vol_prev = stream_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol[k];
 			AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
-			AudioFrame vol = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : current.vol[k];
+			AudioFrame vol = stream_fade_in ? AudioFrame(0.f, 0.f) : current.vol[k];
 
 			if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, k))
 				continue; //may have been deleted, will be updated on process
@@ -198,9 +198,15 @@ void AudioStreamPlayer3D::_mix_audio() {
 		active = false;
 	}
 
+	if (stream_stop) {
+		active = false;
+		set_physics_process_internal(false);
+		setplay = -1;
+	}
+
 	output_ready = false;
-	stream_paused_fade_in = false;
-	stream_paused_fade_out = false;
+	stream_fade_in = false;
+	stream_fade_out = false;
 }
 
 float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
@@ -656,6 +662,7 @@ float AudioStreamPlayer3D::get_pitch_scale() const {
 void AudioStreamPlayer3D::play(float p_from_pos) {
 
 	if (stream_playback.is_valid()) {
+		stream_stop = false;
 		active = true;
 		setplay = p_from_pos;
 		output_ready = false;
@@ -673,9 +680,8 @@ void AudioStreamPlayer3D::seek(float p_seconds) {
 void AudioStreamPlayer3D::stop() {
 
 	if (stream_playback.is_valid()) {
-		active = false;
-		set_physics_process_internal(false);
-		setplay = -1;
+		stream_stop = true;
+		stream_fade_out = true;
 	}
 }
 
@@ -869,8 +875,8 @@ void AudioStreamPlayer3D::set_stream_paused(bool p_pause) {
 
 	if (p_pause != stream_paused) {
 		stream_paused = p_pause;
-		stream_paused_fade_in = stream_paused ? false : true;
-		stream_paused_fade_out = stream_paused ? true : false;
+		stream_fade_in = stream_paused ? false : true;
+		stream_fade_out = stream_paused ? true : false;
 	}
 }
 
@@ -1008,8 +1014,9 @@ AudioStreamPlayer3D::AudioStreamPlayer3D() {
 	out_of_range_mode = OUT_OF_RANGE_MIX;
 	doppler_tracking = DOPPLER_TRACKING_DISABLED;
 	stream_paused = false;
-	stream_paused_fade_in = false;
-	stream_paused_fade_out = false;
+	stream_fade_in = false;
+	stream_fade_out = false;
+	stream_stop = false;
 
 	velocity_tracker.instance();
 	AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");

+ 3 - 2
scene/3d/audio_stream_player_3d.h

@@ -109,8 +109,9 @@ private:
 	float pitch_scale;
 	bool autoplay;
 	bool stream_paused;
-	bool stream_paused_fade_in;
-	bool stream_paused_fade_out;
+	bool stream_fade_in;
+	bool stream_fade_out;
+	bool stream_stop;
 	StringName bus;
 
 	void _mix_audio();

+ 14 - 9
scene/audio/audio_stream_player.cpp

@@ -94,10 +94,14 @@ void AudioStreamPlayer::_mix_audio() {
 	if (!stream_playback.is_valid() || !active)
 		return;
 
-	if (stream_paused) {
-		if (stream_paused_fade) {
-			_mix_internal(true);
-			stream_paused_fade = false;
+	if (stream_fade) {
+		_mix_internal(true);
+		stream_fade = false;
+
+		if (stream_stop) {
+			stream_playback->stop();
+			active = false;
+			set_process_internal(false);
 		}
 		return;
 	}
@@ -203,6 +207,7 @@ void AudioStreamPlayer::play(float p_from_pos) {
 
 	if (stream_playback.is_valid()) {
 		//mix_volume_db = volume_db; do not reset volume ramp here, can cause clicks
+		stream_stop = false;
 		setseek = p_from_pos;
 		active = true;
 		set_process_internal(true);
@@ -219,9 +224,8 @@ void AudioStreamPlayer::seek(float p_seconds) {
 void AudioStreamPlayer::stop() {
 
 	if (stream_playback.is_valid()) {
-		stream_playback->stop();
-		active = false;
-		set_process_internal(false);
+		stream_stop = true;
+		stream_fade = true;
 	}
 }
 
@@ -295,7 +299,7 @@ void AudioStreamPlayer::set_stream_paused(bool p_pause) {
 
 	if (p_pause != stream_paused) {
 		stream_paused = p_pause;
-		stream_paused_fade = p_pause ? true : false;
+		stream_fade = p_pause ? true : false;
 	}
 }
 
@@ -385,7 +389,8 @@ AudioStreamPlayer::AudioStreamPlayer() {
 	setseek = -1;
 	active = false;
 	stream_paused = false;
-	stream_paused_fade = false;
+	stream_fade = false;
+	stream_stop = false;
 	mix_target = MIX_TARGET_STEREO;
 
 	AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");

+ 2 - 1
scene/audio/audio_stream_player.h

@@ -58,7 +58,8 @@ private:
 	float volume_db;
 	bool autoplay;
 	bool stream_paused;
-	bool stream_paused_fade;
+	bool stream_fade;
+	bool stream_stop;
 	StringName bus;
 
 	MixTarget mix_target;