This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
audio_stream_player_steamaudio.cpp
392 lines (330 loc) · 15.6 KB
/
audio_stream_player_steamaudio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**************************************************************************/
/* audio_stream_player_steamaudio.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
//Above notice retained as this is largely based on AudioStreamPlayer
#include "audio_stream_player_steamaudio.h"
#include "core/config/engine.h"
#include "core/math/audio_frame.h"
#include "servers/audio_server.h"
void AudioStreamPlayerSteamAudio::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
play();
}
set_stream_paused(false);
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
Vector<Ref<AudioStreamPlaybackSteamAudio>> playbacks_to_remove;
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {
playbacks_to_remove.push_back(playback);
}
}
// Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
for (Ref<AudioStreamPlaybackSteamAudio> &playback : playbacks_to_remove) {
stream_playbacks.erase(playback);
}
if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {
// This node is no longer actively playing audio.
active.clear();
set_process_internal(false);
}
if (!playbacks_to_remove.is_empty()) {
emit_signal(SNAME("finished"));
}
} break;
case NOTIFICATION_EXIT_TREE: {
set_stream_paused(true);
} break;
case NOTIFICATION_PREDELETE: {
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback);
}
stream_playbacks.clear();
} break;
case NOTIFICATION_PAUSED: {
if (!can_process()) {
// Node can't process so we start fading out to silence
set_stream_paused(true);
}
} break;
case NOTIFICATION_UNPAUSED: {
set_stream_paused(false);
} break;
}
}
void AudioStreamPlayerSteamAudio::set_stream(Ref<AudioStreamSteamAudio> p_stream) {
stop();
stream = p_stream;
}
Ref<AudioStreamSteamAudio> AudioStreamPlayerSteamAudio::get_stream() const {
return stream;
}
void AudioStreamPlayerSteamAudio::set_volume_db(float p_volume) {
volume_db = p_volume;
Vector<AudioFrame> volume_vector = _get_volume_vector();
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);
}
}
float AudioStreamPlayerSteamAudio::get_volume_db() const {
return volume_db;
}
void AudioStreamPlayerSteamAudio::set_pitch_scale(float p_pitch_scale) {
ERR_FAIL_COND(!(p_pitch_scale > 0.0));
pitch_scale = p_pitch_scale;
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
}
}
float AudioStreamPlayerSteamAudio::get_pitch_scale() const {
return pitch_scale;
}
void AudioStreamPlayerSteamAudio::set_max_polyphony(int p_max_polyphony) {
if (p_max_polyphony > 0) {
max_polyphony = p_max_polyphony;
}
}
int AudioStreamPlayerSteamAudio::get_max_polyphony() const {
return max_polyphony;
}
void AudioStreamPlayerSteamAudio::play(float p_from_pos) {
if (stream.is_null()) {
return;
}
ERR_FAIL_COND_MSG(!is_inside_tree(), "Playback can only happen when a node is inside the scene tree");
if (stream->is_monophonic() && is_playing()) {
stop();
}
Ref<AudioStreamPlaybackSteamAudio> stream_playback = stream->instantiate_playback();
ERR_FAIL_COND_MSG(stream_playback.is_null(), "Failed to instantiate playback.");
AudioServer::get_singleton()->start_playback_stream(stream_playback, bus, _get_volume_vector(), p_from_pos, pitch_scale);
stream_playbacks.push_back(stream_playback);
active.set();
set_process_internal(true);
while (stream_playbacks.size() > max_polyphony) {
AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);
stream_playbacks.remove_at(0);
}
}
void AudioStreamPlayerSteamAudio::seek(float p_seconds) {
if (is_playing()) {
stop();
play(p_seconds);
}
}
void AudioStreamPlayerSteamAudio::stop() {
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback);
}
stream_playbacks.clear();
active.clear();
set_process_internal(false);
}
bool AudioStreamPlayerSteamAudio::is_playing() const {
for (const Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
if (AudioServer::get_singleton()->is_playback_active(playback)) {
return true;
}
}
return false;
}
float AudioStreamPlayerSteamAudio::get_playback_position() {
// Return the playback position of the most recently started playback stream.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
}
return 0;
}
void AudioStreamPlayerSteamAudio::set_bus(const StringName &p_bus) {
bus = p_bus;
for (const Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());
}
}
StringName AudioStreamPlayerSteamAudio::get_bus() const {
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
if (AudioServer::get_singleton()->get_bus_name(i) == String(bus)) {
return bus;
}
}
return SNAME("Master");
}
void AudioStreamPlayerSteamAudio::set_autoplay(bool p_enable) {
autoplay = p_enable;
}
bool AudioStreamPlayerSteamAudio::is_autoplay_enabled() {
return autoplay;
}
void AudioStreamPlayerSteamAudio::set_mix_target(MixTarget p_target) {
mix_target = p_target;
}
AudioStreamPlayerSteamAudio::MixTarget AudioStreamPlayerSteamAudio::get_mix_target() const {
return mix_target;
}
void AudioStreamPlayerSteamAudio::_set_playing(bool p_enable) {
if (p_enable) {
play();
} else {
stop();
}
}
bool AudioStreamPlayerSteamAudio::_is_active() const {
for (const Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
if (AudioServer::get_singleton()->is_playback_active(playback)) {
return true;
}
}
return false;
}
void AudioStreamPlayerSteamAudio::set_stream_paused(bool p_pause) {
// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
}
}
bool AudioStreamPlayerSteamAudio::get_stream_paused() const {
// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
}
return false;
}
Vector<AudioFrame> AudioStreamPlayerSteamAudio::_get_volume_vector() {
Vector<AudioFrame> volume_vector;
// We need at most four stereo pairs (for 7.1 systems).
volume_vector.resize(4);
// Initialize the volume vector to zero.
for (AudioFrame &channel_volume_db : volume_vector) {
channel_volume_db = AudioFrame(0, 0);
}
float volume_linear = Math::db_to_linear(volume_db);
// Set the volume vector up according to the speaker mode and mix target.
// TODO do we need to scale the volume down when we output to more channels?
if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
} else {
switch (mix_target) {
case MIX_TARGET_STEREO: {
volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
} break;
case MIX_TARGET_SURROUND: {
// TODO Make sure this is right.
volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);
volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);
} break;
case MIX_TARGET_CENTER: {
// TODO Make sure this is right.
volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
} break;
}
}
return volume_vector;
}
void AudioStreamPlayerSteamAudio::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "bus") {
String options;
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
if (i > 0) {
options += ",";
}
String name = AudioServer::get_singleton()->get_bus_name(i);
options += name;
}
p_property.hint_string = options;
}
}
void AudioStreamPlayerSteamAudio::_bus_layout_changed() {
notify_property_list_changed();
}
bool AudioStreamPlayerSteamAudio::has_stream_playback() {
return !stream_playbacks.is_empty();
}
Ref<AudioStreamPlaybackSteamAudio> AudioStreamPlayerSteamAudio::get_stream_playback() {
ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlaybackSteamAudio>(), "Player is inactive. Call play() before requesting get_stream_playback().");
return stream_playbacks[stream_playbacks.size() - 1];
}
bool AudioStreamPlayerSteamAudio::init_source_steamaudio() {
for (Ref<AudioStreamPlaybackSteamAudio> &playback : stream_playbacks) {
bool success = playback->init_source_steamaudio(this);
if (!success)
return false;
}
return true;
}
void AudioStreamPlayerSteamAudio::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayerSteamAudio::set_stream);
ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayerSteamAudio::get_stream);
ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayerSteamAudio::set_volume_db);
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayerSteamAudio::get_volume_db);
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayerSteamAudio::set_pitch_scale);
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayerSteamAudio::get_pitch_scale);
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayerSteamAudio::play, DEFVAL(0.0));
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayerSteamAudio::seek);
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayerSteamAudio::stop);
ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayerSteamAudio::is_playing);
ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayerSteamAudio::get_playback_position);
ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayerSteamAudio::set_bus);
ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayerSteamAudio::get_bus);
ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayerSteamAudio::set_autoplay);
ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayerSteamAudio::is_autoplay_enabled);
ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayerSteamAudio::set_mix_target);
ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayerSteamAudio::get_mix_target);
ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayerSteamAudio::_set_playing);
ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayerSteamAudio::_is_active);
ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayerSteamAudio::set_stream_paused);
ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayerSteamAudio::get_stream_paused);
ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayerSteamAudio::set_max_polyphony);
ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayerSteamAudio::get_max_polyphony);
ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayerSteamAudio::has_stream_playback);
ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayerSteamAudio::get_stream_playback);
ClassDB::bind_method(D_METHOD("init_source_steamaudio"), &AudioStreamPlayerSteamAudio::init_source_steamaudio);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStreamSteamAudio"), "set_stream", "get_stream");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
ADD_SIGNAL(MethodInfo("finished"));
BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
}
AudioStreamPlayerSteamAudio::AudioStreamPlayerSteamAudio() {
AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayerSteamAudio::_bus_layout_changed));
set_disable_scale(true);
}
AudioStreamPlayerSteamAudio::~AudioStreamPlayerSteamAudio() {
}