| 1 | /**************************************************************************/ |
| 2 | /* audio_stream_player.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.h" |
| 32 | #include "audio_stream_player.compat.inc" |
| 33 | |
| 34 | #include "scene/audio/audio_stream_player_internal.h" |
| 35 | #include "servers/audio/audio_stream.h" |
| 36 | #include "servers/display/display_server.h" |
| 37 | |
| 38 | void AudioStreamPlayer::_notification(int p_what) { |
| 39 | if (p_what == NOTIFICATION_ACCESSIBILITY_UPDATE) { |
| 40 | RID ae = get_accessibility_element(); |
| 41 | ERR_FAIL_COND(ae.is_null()); |
| 42 | |
| 43 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_AUDIO); |
| 44 | } else { |
| 45 | internal->notification(p_what); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) { |
| 50 | internal->set_stream(p_stream); |
| 51 | } |
| 52 | |
| 53 | bool AudioStreamPlayer::_set(const StringName &p_name, const Variant &p_value) { |
| 54 | return internal->set(p_name, p_value); |
| 55 | } |
| 56 | |
| 57 | bool AudioStreamPlayer::_get(const StringName &p_name, Variant &r_ret) const { |
| 58 | return internal->get(p_name, r_ret); |
| 59 | } |
| 60 | |
| 61 | void AudioStreamPlayer::_get_property_list(List<PropertyInfo> *p_list) const { |
| 62 | internal->get_property_list(p_list); |
| 63 | } |
| 64 | |
| 65 | Ref<AudioStream> AudioStreamPlayer::get_stream() const { |
| 66 | return internal->stream; |
| 67 | } |
| 68 | |
| 69 | void AudioStreamPlayer::set_volume_db(float p_volume) { |
| 70 | ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN."); |
| 71 | internal->volume_db = p_volume; |
| 72 | |
| 73 | Vector<AudioFrame> volume_vector = _get_volume_vector(); |
| 74 | for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) { |
| 75 | AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | float AudioStreamPlayer::get_volume_db() const { |
| 80 | return internal->volume_db; |
| 81 | } |
| 82 | |
| 83 | void AudioStreamPlayer::set_volume_linear(float p_volume) { |
| 84 | set_volume_db(Math::linear_to_db(p_volume)); |
| 85 | } |
| 86 | |
| 87 | float AudioStreamPlayer::get_volume_linear() const { |
| 88 | return Math::db_to_linear(get_volume_db()); |
| 89 | } |
| 90 | |
| 91 | void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) { |
| 92 | internal->set_pitch_scale(p_pitch_scale); |
| 93 | } |
| 94 | |
| 95 | float AudioStreamPlayer::get_pitch_scale() const { |
| 96 | return internal->pitch_scale; |
| 97 | } |
| 98 | |
| 99 | void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) { |
| 100 | internal->set_max_polyphony(p_max_polyphony); |
| 101 | } |
| 102 | |
| 103 | int AudioStreamPlayer::get_max_polyphony() const { |
| 104 | return internal->max_polyphony; |
| 105 | } |
| 106 | |
| 107 | void AudioStreamPlayer::play(float p_from_pos) { |
| 108 | Ref<AudioStreamPlayback> stream_playback = internal->play_basic(); |
| 109 | if (stream_playback.is_null()) { |
| 110 | return; |
| 111 | } |
| 112 | AudioServer::get_singleton()->start_playback_stream(stream_playback, internal->bus, _get_volume_vector(), p_from_pos, internal->pitch_scale); |
| 113 | internal->ensure_playback_limit(); |
| 114 | |
| 115 | // Sample handling. |
| 116 | if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) { |
| 117 | Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback(); |
| 118 | sample_playback->offset = p_from_pos; |
| 119 | sample_playback->volume_vector = _get_volume_vector(); |
| 120 | sample_playback->bus = get_bus(); |
| 121 | |
| 122 | AudioServer::get_singleton()->start_sample_playback(sample_playback); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void AudioStreamPlayer::seek(float p_seconds) { |
| 127 | internal->seek(p_seconds); |
| 128 | } |
| 129 | |
| 130 | void AudioStreamPlayer::stop() { |
| 131 | internal->stop_basic(); |
| 132 | } |
| 133 | |
| 134 | bool AudioStreamPlayer::is_playing() const { |
| 135 | return internal->is_playing(); |
| 136 | } |
| 137 | |
| 138 | float AudioStreamPlayer::get_playback_position() { |
| 139 | return internal->get_playback_position(); |
| 140 | } |
| 141 | |
| 142 | void AudioStreamPlayer::set_bus(const StringName &p_bus) { |
| 143 | internal->bus = p_bus; |
| 144 | for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) { |
| 145 | AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | StringName AudioStreamPlayer::get_bus() const { |
| 150 | return internal->get_bus(); |
| 151 | } |
| 152 | |
| 153 | void AudioStreamPlayer::set_autoplay(bool p_enable) { |
| 154 | internal->autoplay = p_enable; |
| 155 | } |
| 156 | |
| 157 | bool AudioStreamPlayer::is_autoplay_enabled() const { |
| 158 | return internal->autoplay; |
| 159 | } |
| 160 | |
| 161 | void AudioStreamPlayer::set_mix_target(MixTarget p_target) { |
| 162 | mix_target = p_target; |
| 163 | } |
| 164 | |
| 165 | AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const { |
| 166 | return mix_target; |
| 167 | } |
| 168 | |
| 169 | void AudioStreamPlayer::_set_playing(bool p_enable) { |
| 170 | internal->set_playing(p_enable); |
| 171 | } |
| 172 | |
| 173 | void AudioStreamPlayer::set_stream_paused(bool p_pause) { |
| 174 | internal->set_stream_paused(p_pause); |
| 175 | } |
| 176 | |
| 177 | bool AudioStreamPlayer::get_stream_paused() const { |
| 178 | return internal->get_stream_paused(); |
| 179 | } |
| 180 | |
| 181 | Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() { |
| 182 | Vector<AudioFrame> volume_vector; |
| 183 | // We need at most four stereo pairs (for 7.1 systems). |
| 184 | volume_vector.resize(4); |
| 185 | |
| 186 | // Initialize the volume vector to zero. |
| 187 | for (AudioFrame &channel_volume_db : volume_vector) { |
| 188 | channel_volume_db = AudioFrame(0, 0); |
| 189 | } |
| 190 | |
| 191 | float volume_linear = Math::db_to_linear(internal->volume_db); |
| 192 | |
| 193 | // Set the volume vector up according to the speaker mode and mix target. |
| 194 | // TODO do we need to scale the volume down when we output to more channels? |
| 195 | if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) { |
| 196 | volume_vector.write[0] = AudioFrame(volume_linear, volume_linear); |
| 197 | } else { |
| 198 | switch (mix_target) { |
| 199 | case MIX_TARGET_STEREO: { |
| 200 | volume_vector.write[0] = AudioFrame(volume_linear, volume_linear); |
| 201 | } break; |
| 202 | case MIX_TARGET_SURROUND: { |
| 203 | // TODO Make sure this is right. |
| 204 | volume_vector.write[0] = AudioFrame(volume_linear, volume_linear); |
| 205 | volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f); |
| 206 | volume_vector.write[2] = AudioFrame(volume_linear, volume_linear); |
| 207 | volume_vector.write[3] = AudioFrame(volume_linear, volume_linear); |
| 208 | } break; |
| 209 | case MIX_TARGET_CENTER: { |
| 210 | // TODO Make sure this is right. |
| 211 | volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f); |
| 212 | } break; |
| 213 | } |
| 214 | } |
| 215 | return volume_vector; |
| 216 | } |
| 217 | |
| 218 | void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const { |
| 219 | internal->validate_property(p_property); |
| 220 | } |
| 221 | |
| 222 | bool AudioStreamPlayer::has_stream_playback() { |
| 223 | return internal->has_stream_playback(); |
| 224 | } |
| 225 | |
| 226 | Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() { |
| 227 | return internal->get_stream_playback(); |
| 228 | } |
| 229 | |
| 230 | AudioServer::PlaybackType AudioStreamPlayer::get_playback_type() const { |
| 231 | return internal->get_playback_type(); |
| 232 | } |
| 233 | |
| 234 | void AudioStreamPlayer::set_playback_type(AudioServer::PlaybackType p_playback_type) { |
| 235 | internal->set_playback_type(p_playback_type); |
| 236 | } |
| 237 | |
| 238 | void AudioStreamPlayer::_bind_methods() { |
| 239 | ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream); |
| 240 | ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream); |
| 241 | |
| 242 | ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db); |
| 243 | ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db); |
| 244 | |
| 245 | ClassDB::bind_method(D_METHOD("set_volume_linear", "volume_linear"), &AudioStreamPlayer::set_volume_linear); |
| 246 | ClassDB::bind_method(D_METHOD("get_volume_linear"), &AudioStreamPlayer::get_volume_linear); |
| 247 | |
| 248 | ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale); |
| 249 | ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale); |
| 250 | |
| 251 | ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0)); |
| 252 | ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek); |
| 253 | ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop); |
| 254 | |
| 255 | ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing); |
| 256 | ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position); |
| 257 | |
| 258 | ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus); |
| 259 | ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus); |
| 260 | |
| 261 | ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay); |
| 262 | ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled); |
| 263 | |
| 264 | ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target); |
| 265 | ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target); |
| 266 | |
| 267 | ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer::_set_playing); |
| 268 | |
| 269 | ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused); |
| 270 | ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused); |
| 271 | |
| 272 | ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony); |
| 273 | ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony); |
| 274 | |
| 275 | ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback); |
| 276 | ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback); |
| 277 | |
| 278 | ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer::set_playback_type); |
| 279 | ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer::get_playback_type); |
| 280 | |
| 281 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream"); |
| 282 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db"); |
| 283 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_linear", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_volume_linear", "get_volume_linear"); |
| 284 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale"); |
| 285 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing"); |
| 286 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled"); |
| 287 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused"); |
| 288 | ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target"); |
| 289 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony"); |
| 290 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus"); |
| 291 | ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type"); |
| 292 | |
| 293 | ADD_SIGNAL(MethodInfo("finished")); |
| 294 | |
| 295 | BIND_ENUM_CONSTANT(MIX_TARGET_STEREO); |
| 296 | BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND); |
| 297 | BIND_ENUM_CONSTANT(MIX_TARGET_CENTER); |
| 298 | } |
| 299 | |
| 300 | AudioStreamPlayer::AudioStreamPlayer() { |
| 301 | internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false)); |
| 302 | } |
| 303 | |
| 304 | AudioStreamPlayer::~AudioStreamPlayer() { |
| 305 | memdelete(internal); |
| 306 | } |