Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/interactive_music/audio_stream_synchronized.cpp
10277 views
1
/**************************************************************************/
2
/* audio_stream_synchronized.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_synchronized.h"
32
33
#include "core/math/math_funcs.h"
34
35
AudioStreamSynchronized::AudioStreamSynchronized() {
36
}
37
38
Ref<AudioStreamPlayback> AudioStreamSynchronized::instantiate_playback() {
39
Ref<AudioStreamPlaybackSynchronized> playback_playlist;
40
playback_playlist.instantiate();
41
playback_playlist->stream = Ref<AudioStreamSynchronized>(this);
42
playback_playlist->_update_playback_instances();
43
playbacks.insert(playback_playlist.operator->());
44
return playback_playlist;
45
}
46
47
String AudioStreamSynchronized::get_stream_name() const {
48
return "Synchronized";
49
}
50
51
void AudioStreamSynchronized::set_sync_stream(int p_stream_index, Ref<AudioStream> p_stream) {
52
ERR_FAIL_COND(p_stream == this);
53
ERR_FAIL_INDEX(p_stream_index, MAX_STREAMS);
54
55
AudioServer::get_singleton()->lock();
56
audio_streams[p_stream_index] = p_stream;
57
for (AudioStreamPlaybackSynchronized *E : playbacks) {
58
E->_update_playback_instances();
59
}
60
AudioServer::get_singleton()->unlock();
61
}
62
63
Ref<AudioStream> AudioStreamSynchronized::get_sync_stream(int p_stream_index) const {
64
ERR_FAIL_INDEX_V(p_stream_index, MAX_STREAMS, Ref<AudioStream>());
65
66
return audio_streams[p_stream_index];
67
}
68
69
void AudioStreamSynchronized::set_sync_stream_volume(int p_stream_index, float p_db) {
70
ERR_FAIL_INDEX(p_stream_index, MAX_STREAMS);
71
audio_stream_volume_db[p_stream_index] = p_db;
72
}
73
74
float AudioStreamSynchronized::get_sync_stream_volume(int p_stream_index) const {
75
ERR_FAIL_INDEX_V(p_stream_index, MAX_STREAMS, 0);
76
return audio_stream_volume_db[p_stream_index];
77
}
78
79
double AudioStreamSynchronized::get_bpm() const {
80
for (int i = 0; i < stream_count; i++) {
81
if (audio_streams[i].is_valid()) {
82
double bpm = audio_streams[i]->get_bpm();
83
if (bpm != 0.0) {
84
return bpm;
85
}
86
}
87
}
88
return 0.0;
89
}
90
91
int AudioStreamSynchronized::get_beat_count() const {
92
int max_beats = 0;
93
for (int i = 0; i < stream_count; i++) {
94
if (audio_streams[i].is_valid()) {
95
max_beats = MAX(max_beats, audio_streams[i]->get_beat_count());
96
}
97
}
98
return max_beats;
99
}
100
101
int AudioStreamSynchronized::get_bar_beats() const {
102
for (int i = 0; i < stream_count; i++) {
103
if (audio_streams[i].is_valid()) {
104
int bar_beats = audio_streams[i]->get_bar_beats();
105
if (bar_beats != 0) {
106
return bar_beats;
107
}
108
}
109
}
110
return 0;
111
}
112
113
bool AudioStreamSynchronized::has_loop() const {
114
for (int i = 0; i < stream_count; i++) {
115
if (audio_streams[i].is_valid()) {
116
if (audio_streams[i]->has_loop()) {
117
return true;
118
}
119
}
120
}
121
return false;
122
}
123
124
double AudioStreamSynchronized::get_length() const {
125
double max_length = 0.0;
126
for (int i = 0; i < stream_count; i++) {
127
if (audio_streams[i].is_valid()) {
128
max_length = MAX(max_length, audio_streams[i]->get_length());
129
}
130
}
131
return max_length;
132
}
133
134
void AudioStreamSynchronized::set_stream_count(int p_count) {
135
ERR_FAIL_COND(p_count < 0 || p_count > MAX_STREAMS);
136
AudioServer::get_singleton()->lock();
137
stream_count = p_count;
138
AudioServer::get_singleton()->unlock();
139
notify_property_list_changed();
140
}
141
142
int AudioStreamSynchronized::get_stream_count() const {
143
return stream_count;
144
}
145
146
void AudioStreamSynchronized::_validate_property(PropertyInfo &property) const {
147
String prop = property.name;
148
if (prop != "stream_count" && prop.begins_with("stream_")) {
149
int stream = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
150
if (stream >= stream_count) {
151
property.usage = PROPERTY_USAGE_INTERNAL;
152
}
153
}
154
}
155
156
void AudioStreamSynchronized::_bind_methods() {
157
ClassDB::bind_method(D_METHOD("set_stream_count", "stream_count"), &AudioStreamSynchronized::set_stream_count);
158
ClassDB::bind_method(D_METHOD("get_stream_count"), &AudioStreamSynchronized::get_stream_count);
159
160
ClassDB::bind_method(D_METHOD("set_sync_stream", "stream_index", "audio_stream"), &AudioStreamSynchronized::set_sync_stream);
161
ClassDB::bind_method(D_METHOD("get_sync_stream", "stream_index"), &AudioStreamSynchronized::get_sync_stream);
162
ClassDB::bind_method(D_METHOD("set_sync_stream_volume", "stream_index", "volume_db"), &AudioStreamSynchronized::set_sync_stream_volume);
163
ClassDB::bind_method(D_METHOD("get_sync_stream_volume", "stream_index"), &AudioStreamSynchronized::get_sync_stream_volume);
164
165
ADD_PROPERTY(PropertyInfo(Variant::INT, "stream_count", PROPERTY_HINT_RANGE, "0," + itos(MAX_STREAMS), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Streams,stream_,unfoldable,page_size=999,add_button_text=" + String(TTRC("Add Stream"))), "set_stream_count", "get_stream_count");
166
167
for (int i = 0; i < MAX_STREAMS; i++) {
168
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "stream_" + itos(i) + "/stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_sync_stream", "get_sync_stream", i);
169
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "stream_" + itos(i) + "/volume", PROPERTY_HINT_RANGE, "-60,12,0.01,suffix:db", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_sync_stream_volume", "get_sync_stream_volume", i);
170
}
171
172
BIND_CONSTANT(MAX_STREAMS);
173
}
174
175
//////////////////////
176
//////////////////////
177
178
AudioStreamPlaybackSynchronized::AudioStreamPlaybackSynchronized() {
179
}
180
181
AudioStreamPlaybackSynchronized::~AudioStreamPlaybackSynchronized() {
182
if (stream.is_valid()) {
183
stream->playbacks.erase(this);
184
}
185
}
186
187
void AudioStreamPlaybackSynchronized::stop() {
188
active = false;
189
for (int i = 0; i < stream->stream_count; i++) {
190
if (playback[i].is_valid()) {
191
playback[i]->stop();
192
}
193
}
194
}
195
196
void AudioStreamPlaybackSynchronized::start(double p_from_pos) {
197
if (active) {
198
stop();
199
}
200
201
for (int i = 0; i < stream->stream_count; i++) {
202
if (playback[i].is_valid()) {
203
playback[i]->start(p_from_pos);
204
active = true;
205
}
206
}
207
}
208
209
void AudioStreamPlaybackSynchronized::seek(double p_time) {
210
for (int i = 0; i < stream->stream_count; i++) {
211
if (playback[i].is_valid()) {
212
playback[i]->seek(p_time);
213
}
214
}
215
}
216
217
int AudioStreamPlaybackSynchronized::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
218
if (!active) {
219
return 0;
220
}
221
222
int todo = p_frames;
223
224
bool any_active = false;
225
while (todo) {
226
int to_mix = MIN(todo, MIX_BUFFER_SIZE);
227
228
bool first = true;
229
for (int i = 0; i < stream->stream_count; i++) {
230
if (playback[i].is_valid() && playback[i]->is_playing()) {
231
float volume = Math::db_to_linear(stream->audio_stream_volume_db[i]);
232
if (first) {
233
playback[i]->mix(p_buffer, p_rate_scale, to_mix);
234
for (int j = 0; j < to_mix; j++) {
235
p_buffer[j] *= volume;
236
}
237
first = false;
238
any_active = true;
239
} else {
240
playback[i]->mix(mix_buffer, p_rate_scale, to_mix);
241
for (int j = 0; j < to_mix; j++) {
242
p_buffer[j] += mix_buffer[j] * volume;
243
}
244
}
245
}
246
}
247
248
if (first) {
249
// Nothing mixed, put zeroes.
250
for (int j = 0; j < to_mix; j++) {
251
p_buffer[j] = AudioFrame(0, 0);
252
}
253
}
254
255
p_buffer += to_mix;
256
todo -= to_mix;
257
}
258
259
if (!any_active) {
260
active = false;
261
}
262
return p_frames;
263
}
264
265
void AudioStreamPlaybackSynchronized::tag_used_streams() {
266
if (active) {
267
for (int i = 0; i < stream->stream_count; i++) {
268
if (playback[i].is_valid() && playback[i]->is_playing()) {
269
stream->audio_streams[i]->tag_used(playback[i]->get_playback_position());
270
}
271
}
272
stream->tag_used(0);
273
}
274
}
275
276
int AudioStreamPlaybackSynchronized::get_loop_count() const {
277
int min_loops = 0;
278
bool min_loops_found = false;
279
for (int i = 0; i < stream->stream_count; i++) {
280
if (playback[i].is_valid() && playback[i]->is_playing()) {
281
int loops = playback[i]->get_loop_count();
282
if (!min_loops_found || loops < min_loops) {
283
min_loops = loops;
284
min_loops_found = true;
285
}
286
}
287
}
288
return min_loops;
289
}
290
291
double AudioStreamPlaybackSynchronized::get_playback_position() const {
292
float max_pos = 0;
293
bool pos_found = false;
294
for (int i = 0; i < stream->stream_count; i++) {
295
if (playback[i].is_valid() && playback[i]->is_playing()) {
296
float pos = playback[i]->get_playback_position();
297
if (!pos_found || pos > max_pos) {
298
max_pos = pos;
299
pos_found = true;
300
}
301
}
302
}
303
return max_pos;
304
}
305
306
bool AudioStreamPlaybackSynchronized::is_playing() const {
307
return active;
308
}
309
310
void AudioStreamPlaybackSynchronized::_update_playback_instances() {
311
stop();
312
313
for (int i = 0; i < stream->stream_count; i++) {
314
if (stream->audio_streams[i].is_valid()) {
315
playback[i] = stream->audio_streams[i]->instantiate_playback();
316
} else {
317
playback[i].unref();
318
}
319
}
320
}
321
322