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
33#include "core/config/engine.h"
34#include "core/math/audio_frame.h"
35#include "servers/audio_server.h"
36
37void AudioStreamPlayer::_notification(int p_what) {
38 switch (p_what) {
39 case NOTIFICATION_ENTER_TREE: {
40 if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
41 play();
42 }
43 set_stream_paused(false);
44 } break;
45
46 case NOTIFICATION_INTERNAL_PROCESS: {
47 Vector<Ref<AudioStreamPlayback>> playbacks_to_remove;
48 for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
49 if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {
50 playbacks_to_remove.push_back(playback);
51 }
52 }
53 // Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
54 for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) {
55 stream_playbacks.erase(playback);
56 }
57 if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {
58 // This node is no longer actively playing audio.
59 active.clear();
60 set_process_internal(false);
61 }
62 if (!playbacks_to_remove.is_empty()) {
63 emit_signal(SNAME("finished"));
64 }
65 } break;
66
67 case NOTIFICATION_EXIT_TREE: {
68 set_stream_paused(true);
69 } break;
70
71 case NOTIFICATION_PREDELETE: {
72 for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
73 AudioServer::get_singleton()->stop_playback_stream(playback);
74 }
75 stream_playbacks.clear();
76 } break;
77
78 case NOTIFICATION_PAUSED: {
79 if (!can_process()) {
80 // Node can't process so we start fading out to silence
81 set_stream_paused(true);
82 }
83 } break;
84
85 case NOTIFICATION_UNPAUSED: {
86 set_stream_paused(false);
87 } break;
88 }
89}
90
91void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {
92 stop();
93 stream = p_stream;
94}
95
96Ref<AudioStream> AudioStreamPlayer::get_stream() const {
97 return stream;
98}
99
100void AudioStreamPlayer::set_volume_db(float p_volume) {
101 volume_db = p_volume;
102
103 Vector<AudioFrame> volume_vector = _get_volume_vector();
104 for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
105 AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);
106 }
107}
108
109float AudioStreamPlayer::get_volume_db() const {
110 return volume_db;
111}
112
113void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {
114 ERR_FAIL_COND(!(p_pitch_scale > 0.0));
115 pitch_scale = p_pitch_scale;
116
117 for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
118 AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
119 }
120}
121
122float AudioStreamPlayer::get_pitch_scale() const {
123 return pitch_scale;
124}
125
126void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) {
127 if (p_max_polyphony > 0) {
128 max_polyphony = p_max_polyphony;
129 }
130}
131
132int AudioStreamPlayer::get_max_polyphony() const {
133 return max_polyphony;
134}
135
136void AudioStreamPlayer::play(float p_from_pos) {
137 if (stream.is_null()) {
138 return;
139 }
140 ERR_FAIL_COND_MSG(!is_inside_tree(), "Playback can only happen when a node is inside the scene tree");
141 if (stream->is_monophonic() && is_playing()) {
142 stop();
143 }
144 Ref<AudioStreamPlayback> stream_playback = stream->instantiate_playback();
145 ERR_FAIL_COND_MSG(stream_playback.is_null(), "Failed to instantiate playback.");
146
147 AudioServer::get_singleton()->start_playback_stream(stream_playback, bus, _get_volume_vector(), p_from_pos, pitch_scale);
148 stream_playbacks.push_back(stream_playback);
149 active.set();
150 set_process_internal(true);
151 while (stream_playbacks.size() > max_polyphony) {
152 AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);
153 stream_playbacks.remove_at(0);
154 }
155}
156
157void AudioStreamPlayer::seek(float p_seconds) {
158 if (is_playing()) {
159 stop();
160 play(p_seconds);
161 }
162}
163
164void AudioStreamPlayer::stop() {
165 for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
166 AudioServer::get_singleton()->stop_playback_stream(playback);
167 }
168 stream_playbacks.clear();
169 active.clear();
170 set_process_internal(false);
171}
172
173bool AudioStreamPlayer::is_playing() const {
174 for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
175 if (AudioServer::get_singleton()->is_playback_active(playback)) {
176 return true;
177 }
178 }
179 return false;
180}
181
182float AudioStreamPlayer::get_playback_position() {
183 // Return the playback position of the most recently started playback stream.
184 if (!stream_playbacks.is_empty()) {
185 return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
186 }
187 return 0;
188}
189
190void AudioStreamPlayer::set_bus(const StringName &p_bus) {
191 bus = p_bus;
192 for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
193 AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());
194 }
195}
196
197StringName AudioStreamPlayer::get_bus() const {
198 for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
199 if (AudioServer::get_singleton()->get_bus_name(i) == String(bus)) {
200 return bus;
201 }
202 }
203 return SceneStringNames::get_singleton()->Master;
204}
205
206void AudioStreamPlayer::set_autoplay(bool p_enable) {
207 autoplay = p_enable;
208}
209
210bool AudioStreamPlayer::is_autoplay_enabled() {
211 return autoplay;
212}
213
214void AudioStreamPlayer::set_mix_target(MixTarget p_target) {
215 mix_target = p_target;
216}
217
218AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {
219 return mix_target;
220}
221
222void AudioStreamPlayer::_set_playing(bool p_enable) {
223 if (p_enable) {
224 play();
225 } else {
226 stop();
227 }
228}
229
230bool AudioStreamPlayer::_is_active() const {
231 for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
232 if (AudioServer::get_singleton()->is_playback_active(playback)) {
233 return true;
234 }
235 }
236 return false;
237}
238
239void AudioStreamPlayer::set_stream_paused(bool p_pause) {
240 // TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
241 for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
242 AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
243 }
244}
245
246bool AudioStreamPlayer::get_stream_paused() const {
247 // There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
248 if (!stream_playbacks.is_empty()) {
249 return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
250 }
251 return false;
252}
253
254Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {
255 Vector<AudioFrame> volume_vector;
256 // We need at most four stereo pairs (for 7.1 systems).
257 volume_vector.resize(4);
258
259 // Initialize the volume vector to zero.
260 for (AudioFrame &channel_volume_db : volume_vector) {
261 channel_volume_db = AudioFrame(0, 0);
262 }
263
264 float volume_linear = Math::db_to_linear(volume_db);
265
266 // Set the volume vector up according to the speaker mode and mix target.
267 // TODO do we need to scale the volume down when we output to more channels?
268 if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
269 volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
270 } else {
271 switch (mix_target) {
272 case MIX_TARGET_STEREO: {
273 volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
274 } break;
275 case MIX_TARGET_SURROUND: {
276 // TODO Make sure this is right.
277 volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
278 volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
279 volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);
280 volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);
281 } break;
282 case MIX_TARGET_CENTER: {
283 // TODO Make sure this is right.
284 volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
285 } break;
286 }
287 }
288 return volume_vector;
289}
290
291void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {
292 if (p_property.name == "bus") {
293 String options;
294 for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
295 if (i > 0) {
296 options += ",";
297 }
298 String name = AudioServer::get_singleton()->get_bus_name(i);
299 options += name;
300 }
301
302 p_property.hint_string = options;
303 }
304}
305
306void AudioStreamPlayer::_bus_layout_changed() {
307 notify_property_list_changed();
308}
309
310bool AudioStreamPlayer::has_stream_playback() {
311 return !stream_playbacks.is_empty();
312}
313
314Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {
315 ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");
316 return stream_playbacks[stream_playbacks.size() - 1];
317}
318
319void AudioStreamPlayer::_bind_methods() {
320 ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);
321 ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);
322
323 ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
324 ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
325
326 ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
327 ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
328
329 ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
330 ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
331 ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
332
333 ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);
334 ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);
335
336 ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);
337 ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);
338
339 ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);
340 ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);
341
342 ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);
343 ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);
344
345 ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer::_set_playing);
346 ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer::_is_active);
347
348 ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);
349 ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);
350
351 ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);
352 ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);
353
354 ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);
355 ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);
356
357 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
358 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
359 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
360 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
361 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
362 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
363 ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
364 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
365 ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
366
367 ADD_SIGNAL(MethodInfo("finished"));
368
369 BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
370 BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
371 BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
372}
373
374AudioStreamPlayer::AudioStreamPlayer() {
375 AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayer::_bus_layout_changed));
376}
377
378AudioStreamPlayer::~AudioStreamPlayer() {
379}
380