| 1 | /**************************************************************************/ |
| 2 | /* audio_stream_polyphonic.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_polyphonic.h" |
| 32 | #include "audio_stream_polyphonic.compat.inc" |
| 33 | |
| 34 | #include "servers/audio/audio_server.h" |
| 35 | |
| 36 | constexpr uint64_t ID_MASK = 0xFFFFFFFF; |
| 37 | constexpr uint64_t INDEX_SHIFT = 32; |
| 38 | |
| 39 | Ref<AudioStreamPlayback> AudioStreamPolyphonic::instantiate_playback() { |
| 40 | Ref<AudioStreamPlaybackPolyphonic> playback; |
| 41 | playback.instantiate(); |
| 42 | playback->streams.resize(polyphony); |
| 43 | return playback; |
| 44 | } |
| 45 | |
| 46 | String AudioStreamPolyphonic::get_stream_name() const { |
| 47 | return "AudioStreamPolyphonic"; |
| 48 | } |
| 49 | |
| 50 | bool AudioStreamPolyphonic::is_monophonic() const { |
| 51 | return true; // This avoids stream players to instantiate more than one of these. |
| 52 | } |
| 53 | |
| 54 | void AudioStreamPolyphonic::set_polyphony(int p_voices) { |
| 55 | ERR_FAIL_COND(p_voices < 0 || p_voices > 128); |
| 56 | polyphony = p_voices; |
| 57 | } |
| 58 | int AudioStreamPolyphonic::get_polyphony() const { |
| 59 | return polyphony; |
| 60 | } |
| 61 | |
| 62 | void AudioStreamPolyphonic::_bind_methods() { |
| 63 | ClassDB::bind_method(D_METHOD("set_polyphony", "voices"), &AudioStreamPolyphonic::set_polyphony); |
| 64 | ClassDB::bind_method(D_METHOD("get_polyphony"), &AudioStreamPolyphonic::get_polyphony); |
| 65 | |
| 66 | ADD_PROPERTY(PropertyInfo(Variant::INT, "polyphony", PROPERTY_HINT_RANGE, "1,128,1"), "set_polyphony", "get_polyphony"); |
| 67 | } |
| 68 | |
| 69 | AudioStreamPolyphonic::AudioStreamPolyphonic() { |
| 70 | } |
| 71 | |
| 72 | //////////////////////// |
| 73 | |
| 74 | void AudioStreamPlaybackPolyphonic::start(double p_from_pos) { |
| 75 | if (active) { |
| 76 | stop(); |
| 77 | } |
| 78 | |
| 79 | active = true; |
| 80 | } |
| 81 | |
| 82 | void AudioStreamPlaybackPolyphonic::stop() { |
| 83 | if (!active) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | bool locked = false; |
| 88 | for (Stream &s : streams) { |
| 89 | if (s.active.is_set()) { |
| 90 | // Need locking because something may still be mixing. |
| 91 | locked = true; |
| 92 | AudioServer::get_singleton()->lock(); |
| 93 | } |
| 94 | s.active.clear(); |
| 95 | s.finish_request.clear(); |
| 96 | s.stream_playback.unref(); |
| 97 | s.stream.unref(); |
| 98 | } |
| 99 | if (locked) { |
| 100 | AudioServer::get_singleton()->unlock(); |
| 101 | } |
| 102 | |
| 103 | active = false; |
| 104 | } |
| 105 | |
| 106 | bool AudioStreamPlaybackPolyphonic::is_playing() const { |
| 107 | return active; |
| 108 | } |
| 109 | |
| 110 | int AudioStreamPlaybackPolyphonic::get_loop_count() const { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | double AudioStreamPlaybackPolyphonic::get_playback_position() const { |
| 115 | return 0; |
| 116 | } |
| 117 | void AudioStreamPlaybackPolyphonic::seek(double p_time) { |
| 118 | // Ignored. |
| 119 | } |
| 120 | |
| 121 | void AudioStreamPlaybackPolyphonic::tag_used_streams() { |
| 122 | for (Stream &s : streams) { |
| 123 | if (s.active.is_set()) { |
| 124 | s.stream_playback->tag_used_streams(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | int AudioStreamPlaybackPolyphonic::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { |
| 130 | if (!active) { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | // Pre-clear buffer. |
| 135 | for (int i = 0; i < p_frames; i++) { |
| 136 | p_buffer[i] = AudioFrame(0, 0); |
| 137 | } |
| 138 | |
| 139 | for (Stream &s : streams) { |
| 140 | if (!s.active.is_set()) { |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | if (s.stream_playback->get_is_sample()) { |
| 145 | if (s.finish_request.is_set()) { |
| 146 | s.active.clear(); |
| 147 | AudioServer::get_singleton()->stop_sample_playback(s.stream_playback->get_sample_playback()); |
| 148 | } |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | float volume_db = s.volume_db; // Copy because it can be overridden at any time. |
| 153 | float next_volume = Math::db_to_linear(volume_db); |
| 154 | s.prev_volume_db = volume_db; |
| 155 | |
| 156 | if (s.finish_request.is_set()) { |
| 157 | if (s.pending_play.is_set()) { |
| 158 | // Did not get the chance to play, was finalized too soon. |
| 159 | s.active.clear(); |
| 160 | continue; |
| 161 | } |
| 162 | next_volume = 0; |
| 163 | } |
| 164 | |
| 165 | if (s.pending_play.is_set()) { |
| 166 | s.stream_playback->start(s.play_offset); |
| 167 | s.pending_play.clear(); |
| 168 | } |
| 169 | float prev_volume = Math::db_to_linear(s.prev_volume_db); |
| 170 | |
| 171 | float volume_inc = (next_volume - prev_volume) / float(p_frames); |
| 172 | |
| 173 | int todo = p_frames; |
| 174 | int offset = 0; |
| 175 | float volume = prev_volume; |
| 176 | |
| 177 | bool stream_done = false; |
| 178 | |
| 179 | while (todo) { |
| 180 | int to_mix = MIN(todo, int(INTERNAL_BUFFER_LEN)); |
| 181 | int mixed = s.stream_playback->mix(internal_buffer, p_rate_scale * s.pitch_scale, to_mix); |
| 182 | |
| 183 | for (int i = 0; i < to_mix; i++) { |
| 184 | p_buffer[offset + i] += internal_buffer[i] * volume; |
| 185 | volume += volume_inc; |
| 186 | } |
| 187 | |
| 188 | if (mixed < to_mix) { |
| 189 | // Stream is done. |
| 190 | s.active.clear(); |
| 191 | stream_done = true; |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | todo -= to_mix; |
| 196 | offset += to_mix; |
| 197 | } |
| 198 | |
| 199 | if (stream_done) { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | if (s.finish_request.is_set()) { |
| 204 | s.active.clear(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return p_frames; |
| 209 | } |
| 210 | |
| 211 | AudioStreamPlaybackPolyphonic::ID AudioStreamPlaybackPolyphonic::play_stream(const Ref<AudioStream> &p_stream, float p_from_offset, float p_volume_db, float p_pitch_scale, AudioServer::PlaybackType p_playback_type, const StringName &p_bus) { |
| 212 | ERR_FAIL_COND_V(p_stream.is_null(), INVALID_ID); |
| 213 | |
| 214 | AudioServer::PlaybackType playback_type = p_playback_type == AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT |
| 215 | ? AudioServer::get_singleton()->get_default_playback_type() |
| 216 | : p_playback_type; |
| 217 | |
| 218 | for (uint32_t i = 0; i < streams.size(); i++) { |
| 219 | if (streams[i].active.is_set() && streams[i].stream_playback->get_is_sample()) { |
| 220 | Ref<AudioSamplePlayback> active_sample_playback = streams[i].stream_playback->get_sample_playback(); |
| 221 | if (active_sample_playback.is_null() || !AudioServer::get_singleton()->is_sample_playback_active(active_sample_playback)) { |
| 222 | streams[i].active.clear(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (!streams[i].active.is_set()) { |
| 227 | // Can use this stream, as it's not active. |
| 228 | streams[i].stream = p_stream; |
| 229 | streams[i].stream_playback = streams[i].stream->instantiate_playback(); |
| 230 | streams[i].play_offset = p_from_offset; |
| 231 | streams[i].volume_db = p_volume_db; |
| 232 | streams[i].prev_volume_db = p_volume_db; |
| 233 | streams[i].pitch_scale = p_pitch_scale; |
| 234 | streams[i].id = id_counter++; |
| 235 | streams[i].finish_request.clear(); |
| 236 | streams[i].pending_play.set(); |
| 237 | streams[i].active.set(); |
| 238 | |
| 239 | // Sample playback. |
| 240 | if (playback_type == AudioServer::PlaybackType::PLAYBACK_TYPE_SAMPLE && p_stream->can_be_sampled()) { |
| 241 | streams[i].stream_playback->set_is_sample(true); |
| 242 | if (!AudioServer::get_singleton()->is_stream_registered_as_sample(p_stream)) { |
| 243 | AudioServer::get_singleton()->register_stream_as_sample(p_stream); |
| 244 | } |
| 245 | float linear_volume = Math::db_to_linear(p_volume_db); |
| 246 | Ref<AudioSamplePlayback> sp; |
| 247 | sp.instantiate(); |
| 248 | sp->stream = streams[i].stream; |
| 249 | sp->offset = p_from_offset; |
| 250 | sp->volume_vector.resize(4); |
| 251 | sp->volume_vector.write[0] = AudioFrame(linear_volume, linear_volume); |
| 252 | sp->volume_vector.write[1] = AudioFrame(linear_volume, /* LFE= */ 1.0f); |
| 253 | sp->volume_vector.write[2] = AudioFrame(linear_volume, linear_volume); |
| 254 | sp->volume_vector.write[3] = AudioFrame(linear_volume, linear_volume); |
| 255 | sp->bus = p_bus; |
| 256 | |
| 257 | if (streams[i].stream_playback->get_sample_playback().is_valid()) { |
| 258 | AudioServer::get_singleton()->stop_playback_stream(streams[i].stream_playback); |
| 259 | } |
| 260 | |
| 261 | streams[i].stream_playback->set_sample_playback(sp); |
| 262 | AudioServer::get_singleton()->start_sample_playback(sp); |
| 263 | } |
| 264 | |
| 265 | return (ID(i) << INDEX_SHIFT) | ID(streams[i].id); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return INVALID_ID; |
| 270 | } |
| 271 | |
| 272 | AudioStreamPlaybackPolyphonic::Stream *AudioStreamPlaybackPolyphonic::_find_stream(int64_t p_id) { |
| 273 | uint32_t index = static_cast<uint64_t>(p_id) >> INDEX_SHIFT; |
| 274 | if (index >= streams.size()) { |
| 275 | return nullptr; |
| 276 | } |
| 277 | if (!streams[index].active.is_set()) { |
| 278 | return nullptr; // Not active, no longer exists. |
| 279 | } |
| 280 | int64_t id = static_cast<uint64_t>(p_id) & ID_MASK; |
| 281 | if (streams[index].id != id) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | return &streams[index]; |
| 285 | } |
| 286 | |
| 287 | const AudioStreamPlaybackPolyphonic::Stream *AudioStreamPlaybackPolyphonic::_find_stream(int64_t p_id) const { |
| 288 | uint32_t index = static_cast<uint64_t>(p_id) >> INDEX_SHIFT; |
| 289 | if (index >= streams.size()) { |
| 290 | return nullptr; |
| 291 | } |
| 292 | if (!streams[index].active.is_set()) { |
| 293 | return nullptr; // Not active, no longer exists. |
| 294 | } |
| 295 | int64_t id = static_cast<uint64_t>(p_id) & ID_MASK; |
| 296 | if (streams[index].id != id) { |
| 297 | return nullptr; |
| 298 | } |
| 299 | return &streams[index]; |
| 300 | } |
| 301 | |
| 302 | void AudioStreamPlaybackPolyphonic::set_stream_volume(ID p_stream_id, float p_volume_db) { |
| 303 | Stream *s = _find_stream(p_stream_id); |
| 304 | if (!s) { |
| 305 | return; |
| 306 | } |
| 307 | s->volume_db = p_volume_db; |
| 308 | } |
| 309 | |
| 310 | void AudioStreamPlaybackPolyphonic::set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale) { |
| 311 | Stream *s = _find_stream(p_stream_id); |
| 312 | if (!s) { |
| 313 | return; |
| 314 | } |
| 315 | s->pitch_scale = p_pitch_scale; |
| 316 | } |
| 317 | |
| 318 | bool AudioStreamPlaybackPolyphonic::is_stream_playing(ID p_stream_id) const { |
| 319 | return _find_stream(p_stream_id) != nullptr; |
| 320 | } |
| 321 | |
| 322 | void AudioStreamPlaybackPolyphonic::stop_stream(ID p_stream_id) { |
| 323 | Stream *s = _find_stream(p_stream_id); |
| 324 | if (!s) { |
| 325 | return; |
| 326 | } |
| 327 | s->finish_request.set(); |
| 328 | } |
| 329 | |
| 330 | void AudioStreamPlaybackPolyphonic::set_is_sample(bool p_is_sample) { |
| 331 | _is_sample = p_is_sample; |
| 332 | } |
| 333 | |
| 334 | bool AudioStreamPlaybackPolyphonic::get_is_sample() const { |
| 335 | return _is_sample; |
| 336 | } |
| 337 | |
| 338 | Ref<AudioSamplePlayback> AudioStreamPlaybackPolyphonic::get_sample_playback() const { |
| 339 | return sample_playback; |
| 340 | } |
| 341 | |
| 342 | void AudioStreamPlaybackPolyphonic::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { |
| 343 | sample_playback = p_playback; |
| 344 | if (sample_playback.is_valid()) { |
| 345 | sample_playback->stream_playback = Ref<AudioStreamPlayback>(this); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void AudioStreamPlaybackPolyphonic::_bind_methods() { |
| 350 | ClassDB::bind_method(D_METHOD("play_stream", "stream", "from_offset", "volume_db", "pitch_scale", "playback_type", "bus"), &AudioStreamPlaybackPolyphonic::play_stream, DEFVAL(0), DEFVAL(0), DEFVAL(1.0), DEFVAL(0), DEFVAL(SceneStringName(Master))); |
| 351 | ClassDB::bind_method(D_METHOD("set_stream_volume", "stream", "volume_db"), &AudioStreamPlaybackPolyphonic::set_stream_volume); |
| 352 | ClassDB::bind_method(D_METHOD("set_stream_pitch_scale", "stream", "pitch_scale"), &AudioStreamPlaybackPolyphonic::set_stream_pitch_scale); |
| 353 | ClassDB::bind_method(D_METHOD("is_stream_playing", "stream"), &AudioStreamPlaybackPolyphonic::is_stream_playing); |
| 354 | ClassDB::bind_method(D_METHOD("stop_stream", "stream"), &AudioStreamPlaybackPolyphonic::stop_stream); |
| 355 | |
| 356 | BIND_CONSTANT(INVALID_ID); |
| 357 | } |
| 358 | |
| 359 | AudioStreamPlaybackPolyphonic::AudioStreamPlaybackPolyphonic() { |
| 360 | } |