| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-02-12 17:18:13 +01:00
										 |  |  | /*  audio_stream_player.cpp                                              */ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 17:18:13 +01:00
										 |  |  | #include "audio_stream_player.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/config/engine.h"
 | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | #include "core/math/audio_frame.h"
 | 
					
						
							|  |  |  | #include "servers/audio_server.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:11:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 	switch (p_what) { | 
					
						
							|  |  |  | 		case NOTIFICATION_ENTER_TREE: { | 
					
						
							|  |  |  | 			if (autoplay && !Engine::get_singleton()->is_editor_hint()) { | 
					
						
							|  |  |  | 				play(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 		case NOTIFICATION_INTERNAL_PROCESS: { | 
					
						
							|  |  |  | 			Vector<Ref<AudioStreamPlayback>> playbacks_to_remove; | 
					
						
							|  |  |  | 			for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 				if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) { | 
					
						
							|  |  |  | 					playbacks_to_remove.push_back(playback); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 			// Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
 | 
					
						
							|  |  |  | 			for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) { | 
					
						
							|  |  |  | 				stream_playbacks.erase(playback); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) { | 
					
						
							|  |  |  | 				// This node is no longer actively playing audio.
 | 
					
						
							|  |  |  | 				active.clear(); | 
					
						
							|  |  |  | 				set_process_internal(false); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!playbacks_to_remove.is_empty()) { | 
					
						
							|  |  |  | 				emit_signal(SNAME("finished")); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2017-08-25 11:58:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 		case NOTIFICATION_EXIT_TREE: { | 
					
						
							|  |  |  | 			for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 				AudioServer::get_singleton()->stop_playback_stream(playback); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			stream_playbacks.clear(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 		case NOTIFICATION_PAUSED: { | 
					
						
							|  |  |  | 			if (!can_process()) { | 
					
						
							|  |  |  | 				// Node can't process so we start fading out to silence
 | 
					
						
							|  |  |  | 				set_stream_paused(true); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 		case NOTIFICATION_UNPAUSED: { | 
					
						
							|  |  |  | 			set_stream_paused(false); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	stop(); | 
					
						
							|  |  |  | 	stream = p_stream; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | Ref<AudioStream> AudioStreamPlayer::get_stream() const { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	return stream; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::set_volume_db(float p_volume) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	volume_db = p_volume; | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	Vector<AudioFrame> volume_vector = _get_volume_vector(); | 
					
						
							|  |  |  | 	for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | float AudioStreamPlayer::get_volume_db() const { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	return volume_db; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 22:23:16 +01:00
										 |  |  | void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) { | 
					
						
							| 
									
										
										
										
											2018-08-20 11:25:48 +03:00
										 |  |  | 	ERR_FAIL_COND(p_pitch_scale <= 0.0); | 
					
						
							| 
									
										
										
										
											2018-01-01 22:23:16 +01:00
										 |  |  | 	pitch_scale = p_pitch_scale; | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-01 22:23:16 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 22:23:16 +01:00
										 |  |  | float AudioStreamPlayer::get_pitch_scale() const { | 
					
						
							|  |  |  | 	return pitch_scale; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) { | 
					
						
							|  |  |  | 	if (p_max_polyphony > 0) { | 
					
						
							|  |  |  | 		max_polyphony = p_max_polyphony; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioStreamPlayer::get_max_polyphony() const { | 
					
						
							|  |  |  | 	return max_polyphony; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::play(float p_from_pos) { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	if (stream.is_null()) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ERR_FAIL_COND_MSG(!is_inside_tree(), "Playback can only happen when a node is inside the scene tree"); | 
					
						
							|  |  |  | 	if (stream->is_monophonic() && is_playing()) { | 
					
						
							|  |  |  | 		stop(); | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-21 01:00:58 +02:00
										 |  |  | 	Ref<AudioStreamPlayback> stream_playback = stream->instantiate_playback(); | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	ERR_FAIL_COND_MSG(stream_playback.is_null(), "Failed to instantiate playback."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-10 13:20:56 -03:00
										 |  |  | 	AudioServer::get_singleton()->start_playback_stream(stream_playback, bus, _get_volume_vector(), p_from_pos, pitch_scale); | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	stream_playbacks.push_back(stream_playback); | 
					
						
							|  |  |  | 	active.set(); | 
					
						
							|  |  |  | 	set_process_internal(true); | 
					
						
							|  |  |  | 	while (stream_playbacks.size() > max_polyphony) { | 
					
						
							|  |  |  | 		AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]); | 
					
						
							| 
									
										
										
										
											2021-07-03 16:17:03 -06:00
										 |  |  | 		stream_playbacks.remove_at(0); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::seek(float p_seconds) { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	if (is_playing()) { | 
					
						
							|  |  |  | 		stop(); | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 		play(p_seconds); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::stop() { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		AudioServer::get_singleton()->stop_playback_stream(playback); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	stream_playbacks.clear(); | 
					
						
							|  |  |  | 	active.clear(); | 
					
						
							|  |  |  | 	set_process_internal(false); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | bool AudioStreamPlayer::is_playing() const { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		if (AudioServer::get_singleton()->is_playback_active(playback)) { | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 00:31:36 -03:00
										 |  |  | float AudioStreamPlayer::get_playback_position() { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	// Return the playback position of the most recently started playback stream.
 | 
					
						
							|  |  |  | 	if (!stream_playbacks.is_empty()) { | 
					
						
							|  |  |  | 		return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::set_bus(const StringName &p_bus) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	bus = p_bus; | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector()); | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | StringName AudioStreamPlayer::get_bus() const { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 		if (AudioServer::get_singleton()->get_bus_name(i) == String(bus)) { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 			return bus; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	return SNAME("Master"); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::set_autoplay(bool p_enable) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	autoplay = p_enable; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | bool AudioStreamPlayer::is_autoplay_enabled() { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	return autoplay; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::set_mix_target(MixTarget p_target) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	mix_target = p_target; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	return mix_target; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::_set_playing(bool p_enable) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_enable) { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 		play(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 		stop(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | bool AudioStreamPlayer::_is_active() const { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		if (AudioServer::get_singleton()->is_playback_active(playback)) { | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | void AudioStreamPlayer::set_stream_paused(bool p_pause) { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
 | 
					
						
							|  |  |  | 	for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { | 
					
						
							|  |  |  | 		AudioServer::get_singleton()->set_playback_paused(playback, p_pause); | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AudioStreamPlayer::get_stream_paused() const { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
 | 
					
						
							|  |  |  | 	if (!stream_playbacks.is_empty()) { | 
					
						
							|  |  |  | 		return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]); | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() { | 
					
						
							|  |  |  | 	Vector<AudioFrame> volume_vector; | 
					
						
							|  |  |  | 	// We need at most four stereo pairs (for 7.1 systems).
 | 
					
						
							|  |  |  | 	volume_vector.resize(4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize the volume vector to zero.
 | 
					
						
							|  |  |  | 	for (AudioFrame &channel_volume_db : volume_vector) { | 
					
						
							|  |  |  | 		channel_volume_db = AudioFrame(0, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-13 17:45:42 +02:00
										 |  |  | 	float volume_linear = Math::db_to_linear(volume_db); | 
					
						
							| 
									
										
										
										
											2021-08-27 10:28:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Set the volume vector up according to the speaker mode and mix target.
 | 
					
						
							|  |  |  | 	// TODO do we need to scale the volume down when we output to more channels?
 | 
					
						
							|  |  |  | 	if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) { | 
					
						
							|  |  |  | 		volume_vector.write[0] = AudioFrame(volume_linear, volume_linear); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		switch (mix_target) { | 
					
						
							|  |  |  | 			case MIX_TARGET_STEREO: { | 
					
						
							|  |  |  | 				volume_vector.write[0] = AudioFrame(volume_linear, volume_linear); | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 			case MIX_TARGET_SURROUND: { | 
					
						
							|  |  |  | 				// TODO Make sure this is right.
 | 
					
						
							|  |  |  | 				volume_vector.write[0] = AudioFrame(volume_linear, volume_linear); | 
					
						
							|  |  |  | 				volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f); | 
					
						
							|  |  |  | 				volume_vector.write[2] = AudioFrame(volume_linear, volume_linear); | 
					
						
							|  |  |  | 				volume_vector.write[3] = AudioFrame(volume_linear, volume_linear); | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 			case MIX_TARGET_CENTER: { | 
					
						
							|  |  |  | 				// TODO Make sure this is right.
 | 
					
						
							|  |  |  | 				volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f); | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return volume_vector; | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const { | 
					
						
							|  |  |  | 	if (p_property.name == "bus") { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 		String options; | 
					
						
							| 
									
										
										
										
											2019-05-20 13:51:51 +02:00
										 |  |  | 		for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			if (i > 0) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				options += ","; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 			String name = AudioServer::get_singleton()->get_bus_name(i); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			options += name; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 		p_property.hint_string = options; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::_bus_layout_changed() { | 
					
						
							| 
									
										
										
										
											2021-02-10 17:18:45 -03:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 12:57:03 -03:00
										 |  |  | Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() { | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	if (!stream_playbacks.is_empty()) { | 
					
						
							|  |  |  | 		return stream_playbacks[stream_playbacks.size() - 1]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2019-04-10 12:57:03 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | void AudioStreamPlayer::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 22:23:16 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0)); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek); | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing); | 
					
						
							| 
									
										
										
										
											2017-09-21 00:31:36 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer::_set_playing); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer::_is_active); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 12:57:03 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream"); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:09:19 -06:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db"); | 
					
						
							| 
									
										
										
											
												Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
											
										 
											2020-02-24 15:20:53 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale"); | 
					
						
							| 
									
										
										
										
											2017-09-13 08:40:41 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing"); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled"); | 
					
						
							| 
									
										
										
										
											2018-05-27 15:29:10 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused"); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target"); | 
					
						
							| 
									
										
										
										
											2021-08-27 21:51:03 -07:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony"); | 
					
						
							| 
									
										
										
										
											2020-02-20 18:58:05 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus"); | 
					
						
							| 
									
										
										
										
											2017-08-25 11:58:21 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("finished")); | 
					
						
							| 
									
										
										
										
											2017-09-12 22:09:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(MIX_TARGET_STEREO); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(MIX_TARGET_CENTER); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | AudioStreamPlayer::AudioStreamPlayer() { | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayer::_bus_layout_changed)); | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 21:07:32 -03:00
										 |  |  | AudioStreamPlayer::~AudioStreamPlayer() { | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | } |