| 1 | /**************************************************************************/ |
| 2 | /* audio_stream_player_internal.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "audio_stream_player_internal.h" |
| 32 | |
| 33 | #include "scene/main/node.h" |
| 34 | #include "servers/audio/audio_stream.h" |
| 35 | |
| 36 | void AudioStreamPlayerInternal::_set_process(bool p_enabled) { |
| 37 | if (physical) { |
| 38 | node->set_physics_process_internal(p_enabled); |
| 39 | } else { |
| 40 | node->set_process_internal(p_enabled); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void AudioStreamPlayerInternal::_update_stream_parameters() { |
| 45 | if (stream.is_null()) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | List<AudioStream::Parameter> parameters; |
| 50 | stream->get_parameter_list(¶meters); |
| 51 | for (const AudioStream::Parameter &K : parameters) { |
| 52 | const PropertyInfo &pi = K.property; |
| 53 | StringName key = PARAM_PREFIX + pi.name; |
| 54 | if (!playback_parameters.has(key)) { |
| 55 | ParameterData pd; |
| 56 | pd.path = pi.name; |
| 57 | pd.value = K.default_value; |
| 58 | playback_parameters.insert(key, pd); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void AudioStreamPlayerInternal::process() { |
| 64 | Vector<Ref<AudioStreamPlayback>> playbacks_to_remove; |
| 65 | for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 66 | if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) { |
| 67 | playbacks_to_remove.push_back(playback); |
| 68 | } |
| 69 | } |
| 70 | // Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble. |
| 71 | for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) { |
| 72 | stream_playbacks.erase(playback); |
| 73 | } |
| 74 | if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) { |
| 75 | // This node is no longer actively playing audio. |
| 76 | active.clear(); |
| 77 | _set_process(false); |
| 78 | } |
| 79 | if (!playbacks_to_remove.is_empty()) { |
| 80 | node->emit_signal(SceneStringName(finished)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void AudioStreamPlayerInternal::ensure_playback_limit() { |
| 85 | while (stream_playbacks.size() > max_polyphony) { |
| 86 | AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]); |
| 87 | stream_playbacks.remove_at(0); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void AudioStreamPlayerInternal::notification(int p_what) { |
| 92 | switch (p_what) { |
| 93 | case Node::NOTIFICATION_ENTER_TREE: { |
| 94 | if (autoplay && !Engine::get_singleton()->is_editor_hint()) { |
| 95 | play_callable.call(0.0); |
| 96 | } |
| 97 | set_stream_paused(!node->can_process()); |
| 98 | } break; |
| 99 | |
| 100 | case Node::NOTIFICATION_EXIT_TREE: { |
| 101 | set_stream_paused(true); |
| 102 | } break; |
| 103 | |
| 104 | case Node::NOTIFICATION_INTERNAL_PROCESS: { |
| 105 | process(); |
| 106 | } break; |
| 107 | |
| 108 | case Node::NOTIFICATION_PREDELETE: { |
| 109 | for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 110 | AudioServer::get_singleton()->stop_playback_stream(playback); |
| 111 | } |
| 112 | stream_playbacks.clear(); |
| 113 | } break; |
| 114 | |
| 115 | case Node::NOTIFICATION_SUSPENDED: |
| 116 | case Node::NOTIFICATION_PAUSED: { |
| 117 | bool can_process = node->is_inside_tree() && node->can_process(); |
| 118 | if (!can_process) { |
| 119 | // Node can't process so we start fading out to silence |
| 120 | set_stream_paused(true); |
| 121 | } |
| 122 | } break; |
| 123 | |
| 124 | case Node::NOTIFICATION_UNSUSPENDED: { |
| 125 | if (node->get_tree()->is_paused()) { |
| 126 | break; |
| 127 | } |
| 128 | [[fallthrough]]; |
| 129 | } |
| 130 | |
| 131 | case Node::NOTIFICATION_UNPAUSED: { |
| 132 | set_stream_paused(false); |
| 133 | } break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() { |
| 138 | Ref<AudioStreamPlayback> stream_playback; |
| 139 | if (stream.is_null()) { |
| 140 | return stream_playback; |
| 141 | } |
| 142 | ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree"); |
| 143 | if (stream->is_monophonic() && is_playing()) { |
| 144 | stop_callable.call(); |
| 145 | } |
| 146 | stream_playback = stream->instantiate_playback(); |
| 147 | ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback."); |
| 148 | |
| 149 | for (const KeyValue<StringName, ParameterData> &K : playback_parameters) { |
| 150 | stream_playback->set_parameter(K.value.path, K.value.value); |
| 151 | } |
| 152 | |
| 153 | // Sample handling. |
| 154 | if (_is_sample()) { |
| 155 | if (stream->can_be_sampled()) { |
| 156 | stream_playback->set_is_sample(true); |
| 157 | if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_null()) { |
| 158 | if (!AudioServer::get_singleton()->is_stream_registered_as_sample(stream)) { |
| 159 | AudioServer::get_singleton()->register_stream_as_sample(stream); |
| 160 | } |
| 161 | Ref<AudioSamplePlayback> sample_playback; |
| 162 | sample_playback.instantiate(); |
| 163 | sample_playback->stream = stream; |
| 164 | sample_playback->pitch_scale = pitch_scale; |
| 165 | stream_playback->set_sample_playback(sample_playback); |
| 166 | } |
| 167 | } else if (!stream->is_meta_stream()) { |
| 168 | WARN_PRINT(vformat(R"(%s is trying to play a sample from a stream that cannot be sampled.)", node->get_path())); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | stream_playbacks.push_back(stream_playback); |
| 173 | active.set(); |
| 174 | _set_process(true); |
| 175 | return stream_playback; |
| 176 | } |
| 177 | |
| 178 | void AudioStreamPlayerInternal::set_stream_paused(bool p_pause) { |
| 179 | // TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted. |
| 180 | for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 181 | AudioServer::get_singleton()->set_playback_paused(playback, p_pause); |
| 182 | if (_is_sample() && playback->get_sample_playback().is_valid()) { |
| 183 | AudioServer::get_singleton()->set_sample_playback_pause(playback->get_sample_playback(), p_pause); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool AudioStreamPlayerInternal::get_stream_paused() const { |
| 189 | // There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest. |
| 190 | if (!stream_playbacks.is_empty()) { |
| 191 | return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]); |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | void AudioStreamPlayerInternal::validate_property(PropertyInfo &p_property) const { |
| 197 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 198 | return; |
| 199 | } |
| 200 | if (p_property.name == "bus") { |
| 201 | String options; |
| 202 | for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { |
| 203 | if (i > 0) { |
| 204 | options += ","; |
| 205 | } |
| 206 | String name = AudioServer::get_singleton()->get_bus_name(i); |
| 207 | options += name; |
| 208 | } |
| 209 | |
| 210 | p_property.hint_string = options; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | bool AudioStreamPlayerInternal::set(const StringName &p_name, const Variant &p_value) { |
| 215 | ParameterData *pd = playback_parameters.getptr(p_name); |
| 216 | if (!pd) { |
| 217 | return false; |
| 218 | } |
| 219 | pd->value = p_value; |
| 220 | for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 221 | playback->set_parameter(pd->path, pd->value); |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | bool AudioStreamPlayerInternal::get(const StringName &p_name, Variant &r_ret) const { |
| 227 | const ParameterData *pd = playback_parameters.getptr(p_name); |
| 228 | if (!pd) { |
| 229 | return false; |
| 230 | } |
| 231 | r_ret = pd->value; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | void AudioStreamPlayerInternal::get_property_list(List<PropertyInfo> *p_list) const { |
| 236 | if (stream.is_null()) { |
| 237 | return; |
| 238 | } |
| 239 | List<AudioStream::Parameter> parameters; |
| 240 | stream->get_parameter_list(¶meters); |
| 241 | for (const AudioStream::Parameter &K : parameters) { |
| 242 | PropertyInfo pi = K.property; |
| 243 | pi.name = PARAM_PREFIX + pi.name; |
| 244 | |
| 245 | const ParameterData *pd = playback_parameters.getptr(pi.name); |
| 246 | if (pd && pd->value == K.default_value) { |
| 247 | pi.usage &= ~PROPERTY_USAGE_STORAGE; |
| 248 | } |
| 249 | |
| 250 | p_list->push_back(pi); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) { |
| 255 | if (stream.is_valid()) { |
| 256 | stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters)); |
| 257 | } |
| 258 | stop_callable.call(); |
| 259 | stream = p_stream; |
| 260 | _update_stream_parameters(); |
| 261 | if (stream.is_valid()) { |
| 262 | stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters)); |
| 263 | } |
| 264 | node->notify_property_list_changed(); |
| 265 | } |
| 266 | |
| 267 | void AudioStreamPlayerInternal::seek(float p_seconds) { |
| 268 | if (is_playing()) { |
| 269 | stop_callable.call(); |
| 270 | play_callable.call(p_seconds); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void AudioStreamPlayerInternal::stop_basic() { |
| 275 | for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 276 | AudioServer::get_singleton()->stop_playback_stream(playback); |
| 277 | } |
| 278 | stream_playbacks.clear(); |
| 279 | |
| 280 | active.clear(); |
| 281 | _set_process(false); |
| 282 | } |
| 283 | |
| 284 | bool AudioStreamPlayerInternal::is_playing() const { |
| 285 | for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 286 | if (AudioServer::get_singleton()->is_playback_active(playback)) { |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | float AudioStreamPlayerInternal::get_playback_position() { |
| 294 | // Return the playback position of the most recently started playback stream. |
| 295 | if (!stream_playbacks.is_empty()) { |
| 296 | return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | void AudioStreamPlayerInternal::set_playing(bool p_enable) { |
| 302 | if (p_enable) { |
| 303 | play_callable.call(0.0); |
| 304 | } else { |
| 305 | stop_callable.call(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | bool AudioStreamPlayerInternal::is_active() const { |
| 310 | return active.is_set(); |
| 311 | } |
| 312 | |
| 313 | void AudioStreamPlayerInternal::set_pitch_scale(float p_pitch_scale) { |
| 314 | ERR_FAIL_COND(p_pitch_scale <= 0.0); |
| 315 | pitch_scale = p_pitch_scale; |
| 316 | |
| 317 | for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { |
| 318 | AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void AudioStreamPlayerInternal::set_max_polyphony(int p_max_polyphony) { |
| 323 | if (p_max_polyphony > 0) { |
| 324 | max_polyphony = p_max_polyphony; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | bool AudioStreamPlayerInternal::has_stream_playback() { |
| 329 | return !stream_playbacks.is_empty(); |
| 330 | } |
| 331 | |
| 332 | Ref<AudioStreamPlayback> AudioStreamPlayerInternal::get_stream_playback() { |
| 333 | ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback()."); |
| 334 | return stream_playbacks[stream_playbacks.size() - 1]; |
| 335 | } |
| 336 | |
| 337 | void AudioStreamPlayerInternal::set_playback_type(AudioServer::PlaybackType p_playback_type) { |
| 338 | playback_type = p_playback_type; |
| 339 | } |
| 340 | |
| 341 | AudioServer::PlaybackType AudioStreamPlayerInternal::get_playback_type() const { |
| 342 | return playback_type; |
| 343 | } |
| 344 | |
| 345 | StringName AudioStreamPlayerInternal::get_bus() const { |
| 346 | const String bus_name = bus; |
| 347 | for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { |
| 348 | if (AudioServer::get_singleton()->get_bus_name(i) == bus_name) { |
| 349 | return bus; |
| 350 | } |
| 351 | } |
| 352 | return SceneStringName(Master); |
| 353 | } |
| 354 | |
| 355 | AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) { |
| 356 | node = p_node; |
| 357 | play_callable = p_play_callable; |
| 358 | stop_callable = p_stop_callable; |
| 359 | physical = p_physical; |
| 360 | bus = SceneStringName(Master); |
| 361 | |
| 362 | AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp((Object *)node, &Object::notify_property_list_changed)); |
| 363 | AudioServer::get_singleton()->connect("bus_renamed", callable_mp((Object *)node, &Object::notify_property_list_changed).unbind(3)); |
| 364 | } |