Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/audio/audio_stream_player_internal.cpp
9903 views
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(&parameters);
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
if (!node->can_process()) {
118
// Node can't process so we start fading out to silence
119
set_stream_paused(true);
120
}
121
} break;
122
123
case Node::NOTIFICATION_UNSUSPENDED: {
124
if (node->get_tree()->is_paused()) {
125
break;
126
}
127
[[fallthrough]];
128
}
129
130
case Node::NOTIFICATION_UNPAUSED: {
131
set_stream_paused(false);
132
} break;
133
}
134
}
135
136
Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
137
Ref<AudioStreamPlayback> stream_playback;
138
if (stream.is_null()) {
139
return stream_playback;
140
}
141
ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");
142
if (stream->is_monophonic() && is_playing()) {
143
stop_callable.call();
144
}
145
stream_playback = stream->instantiate_playback();
146
ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");
147
148
for (const KeyValue<StringName, ParameterData> &K : playback_parameters) {
149
stream_playback->set_parameter(K.value.path, K.value.value);
150
}
151
152
// Sample handling.
153
if (_is_sample()) {
154
if (stream->can_be_sampled()) {
155
stream_playback->set_is_sample(true);
156
if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_null()) {
157
if (!AudioServer::get_singleton()->is_stream_registered_as_sample(stream)) {
158
AudioServer::get_singleton()->register_stream_as_sample(stream);
159
}
160
Ref<AudioSamplePlayback> sample_playback;
161
sample_playback.instantiate();
162
sample_playback->stream = stream;
163
sample_playback->pitch_scale = pitch_scale;
164
stream_playback->set_sample_playback(sample_playback);
165
}
166
} else if (!stream->is_meta_stream()) {
167
WARN_PRINT(vformat(R"(%s is trying to play a sample from a stream that cannot be sampled.)", node->get_path()));
168
}
169
}
170
171
stream_playbacks.push_back(stream_playback);
172
active.set();
173
_set_process(true);
174
return stream_playback;
175
}
176
177
void AudioStreamPlayerInternal::set_stream_paused(bool p_pause) {
178
// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
179
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
180
AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
181
if (_is_sample() && playback->get_sample_playback().is_valid()) {
182
AudioServer::get_singleton()->set_sample_playback_pause(playback->get_sample_playback(), p_pause);
183
}
184
}
185
}
186
187
bool AudioStreamPlayerInternal::get_stream_paused() const {
188
// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
189
if (!stream_playbacks.is_empty()) {
190
return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
191
}
192
return false;
193
}
194
195
void AudioStreamPlayerInternal::validate_property(PropertyInfo &p_property) const {
196
if (!Engine::get_singleton()->is_editor_hint()) {
197
return;
198
}
199
if (p_property.name == "bus") {
200
String options;
201
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
202
if (i > 0) {
203
options += ",";
204
}
205
String name = AudioServer::get_singleton()->get_bus_name(i);
206
options += name;
207
}
208
209
p_property.hint_string = options;
210
}
211
}
212
213
bool AudioStreamPlayerInternal::set(const StringName &p_name, const Variant &p_value) {
214
ParameterData *pd = playback_parameters.getptr(p_name);
215
if (!pd) {
216
return false;
217
}
218
pd->value = p_value;
219
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
220
playback->set_parameter(pd->path, pd->value);
221
}
222
return true;
223
}
224
225
bool AudioStreamPlayerInternal::get(const StringName &p_name, Variant &r_ret) const {
226
const ParameterData *pd = playback_parameters.getptr(p_name);
227
if (!pd) {
228
return false;
229
}
230
r_ret = pd->value;
231
return true;
232
}
233
234
void AudioStreamPlayerInternal::get_property_list(List<PropertyInfo> *p_list) const {
235
if (stream.is_null()) {
236
return;
237
}
238
List<AudioStream::Parameter> parameters;
239
stream->get_parameter_list(&parameters);
240
for (const AudioStream::Parameter &K : parameters) {
241
PropertyInfo pi = K.property;
242
pi.name = PARAM_PREFIX + pi.name;
243
244
const ParameterData *pd = playback_parameters.getptr(pi.name);
245
if (pd && pd->value == K.default_value) {
246
pi.usage &= ~PROPERTY_USAGE_STORAGE;
247
}
248
249
p_list->push_back(pi);
250
}
251
}
252
253
void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {
254
if (stream.is_valid()) {
255
stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
256
}
257
stop_callable.call();
258
stream = p_stream;
259
_update_stream_parameters();
260
if (stream.is_valid()) {
261
stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
262
}
263
node->notify_property_list_changed();
264
}
265
266
void AudioStreamPlayerInternal::seek(float p_seconds) {
267
if (is_playing()) {
268
stop_callable.call();
269
play_callable.call(p_seconds);
270
}
271
}
272
273
void AudioStreamPlayerInternal::stop_basic() {
274
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
275
AudioServer::get_singleton()->stop_playback_stream(playback);
276
}
277
stream_playbacks.clear();
278
279
active.clear();
280
_set_process(false);
281
}
282
283
bool AudioStreamPlayerInternal::is_playing() const {
284
for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
285
if (AudioServer::get_singleton()->is_playback_active(playback)) {
286
return true;
287
}
288
}
289
return false;
290
}
291
292
float AudioStreamPlayerInternal::get_playback_position() {
293
// Return the playback position of the most recently started playback stream.
294
if (!stream_playbacks.is_empty()) {
295
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
296
}
297
return 0;
298
}
299
300
void AudioStreamPlayerInternal::set_playing(bool p_enable) {
301
if (p_enable) {
302
play_callable.call(0.0);
303
} else {
304
stop_callable.call();
305
}
306
}
307
308
bool AudioStreamPlayerInternal::is_active() const {
309
return active.is_set();
310
}
311
312
void AudioStreamPlayerInternal::set_pitch_scale(float p_pitch_scale) {
313
ERR_FAIL_COND(p_pitch_scale <= 0.0);
314
pitch_scale = p_pitch_scale;
315
316
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
317
AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
318
}
319
}
320
321
void AudioStreamPlayerInternal::set_max_polyphony(int p_max_polyphony) {
322
if (p_max_polyphony > 0) {
323
max_polyphony = p_max_polyphony;
324
}
325
}
326
327
bool AudioStreamPlayerInternal::has_stream_playback() {
328
return !stream_playbacks.is_empty();
329
}
330
331
Ref<AudioStreamPlayback> AudioStreamPlayerInternal::get_stream_playback() {
332
ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");
333
return stream_playbacks[stream_playbacks.size() - 1];
334
}
335
336
void AudioStreamPlayerInternal::set_playback_type(AudioServer::PlaybackType p_playback_type) {
337
playback_type = p_playback_type;
338
}
339
340
AudioServer::PlaybackType AudioStreamPlayerInternal::get_playback_type() const {
341
return playback_type;
342
}
343
344
StringName AudioStreamPlayerInternal::get_bus() const {
345
const String bus_name = bus;
346
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
347
if (AudioServer::get_singleton()->get_bus_name(i) == bus_name) {
348
return bus;
349
}
350
}
351
return SceneStringName(Master);
352
}
353
354
AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {
355
node = p_node;
356
play_callable = p_play_callable;
357
stop_callable = p_stop_callable;
358
physical = p_physical;
359
bus = SceneStringName(Master);
360
361
AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp((Object *)node, &Object::notify_property_list_changed));
362
AudioServer::get_singleton()->connect("bus_renamed", callable_mp((Object *)node, &Object::notify_property_list_changed).unbind(3));
363
}
364
365