Debian Codegraph @

servers/audio/audio_server.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* audio_server.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_server.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/debugger/engine_debugger.h"
35 #include "core/error/error_macros.h"
36 #include "core/io/resource_loader.h"
37 #include "core/math/audio_frame.h"
38 #include "core/os/os.h"
39 #include "core/string/string_name.h"
40 #include "core/templates/pair.h"
41 #include "scene/scene_string_names.h"
42 #include "servers/audio/audio_driver_dummy.h"
43 #include "servers/audio/audio_stream.h"
44 #include "servers/audio/effects/audio_effect_compressor.h"
45
46 #ifdef TOOLS_ENABLED
47 #define MARK_EDITED set_edited(true);
48 #else
49 #define MARK_EDITED
50 #endif
51
52 AudioDriver *AudioDriver::singleton = nullptr;
53 AudioDriver *AudioDriver::get_singleton() {
54 return singleton;
55 }
56
57 void AudioDriver::set_singleton() {
58 singleton = this;
59 }
60
61 void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) {
62 if (p_update_mix_time) {
63 update_mix_time(p_frames);
64 }
65
66 if (AudioServer::get_singleton()) {
67 AudioServer::get_singleton()->_driver_process(p_frames, p_buffer);
68 }
69 }
70
71 void AudioDriver::update_mix_time(int p_frames) {
72 _last_mix_frames = p_frames;
73 if (OS::get_singleton()) {
74 _last_mix_time = OS::get_singleton()->get_ticks_usec();
75 }
76 }
77
78 double AudioDriver::get_time_since_last_mix() {
79 lock();
80 uint64_t last_mix_time = _last_mix_time;
81 unlock();
82 return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
83 }
84
85 double AudioDriver::get_time_to_next_mix() {
86 lock();
87 uint64_t last_mix_time = _last_mix_time;
88 uint64_t last_mix_frames = _last_mix_frames;
89 unlock();
90 double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
91 double mix_buffer = last_mix_frames / (double)get_mix_rate();
92 return mix_buffer - total;
93 }
94
95 void AudioDriver::input_buffer_init(int driver_buffer_frames) {
96 const int input_buffer_channels = 2;
97 input_buffer.resize(driver_buffer_frames * input_buffer_channels * 4);
98 input_position = 0;
99 input_size = 0;
100 }
101
102 void AudioDriver::input_buffer_write(int32_t sample) {
103 if ((int)input_position < input_buffer.size()) {
104 input_buffer.write[input_position++] = sample;
105 if ((int)input_position >= input_buffer.size()) {
106 input_position = 0;
107 }
108 if ((int)input_size < input_buffer.size()) {
109 input_size++;
110 }
111 } else {
112 // This protection was added in GH-26505 due to a "possible crash".
113 // This cannot have happened unless two non-locked threads entered function simultaneously, which was possible when multiple calls to
114 // `AudioDriver::input_start()` did not raise an error condition.
115 WARN_PRINT("input_buffer_write: Invalid input_position=" + itos(input_position) + " input_buffer.size()=" + itos(input_buffer.size()));
116 }
117 }
118
119 int AudioDriver::_get_configured_mix_rate() {
120 StringName audio_driver_setting = "audio/driver/mix_rate";
121 int mix_rate = GLOBAL_GET(audio_driver_setting);
122
123 #ifdef WEB_ENABLED
124 // `0` is an acceptable value (resorts to the browser's default).
125 return MAX(0, mix_rate);
126 #else // !WEB_ENABLED
127 // In the case of invalid mix rate, let's default to a sensible value..
128 if (mix_rate <= 0) {
129 WARN_PRINT(vformat("Invalid mix rate of %d, consider reassigning setting \'%s\'. \nDefaulting mix rate to value %d.",
130 mix_rate, audio_driver_setting, AudioDriverManager::DEFAULT_MIX_RATE));
131 mix_rate = AudioDriverManager::DEFAULT_MIX_RATE;
132 }
133 return mix_rate;
134 #endif
135 }
136
137 AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const {
138 switch (p_channels) {
139 case 4:
140 return SPEAKER_SURROUND_31;
141 case 6:
142 return SPEAKER_SURROUND_51;
143 case 8:
144 return SPEAKER_SURROUND_71;
145 }
146
147 // Default to STEREO
148 return SPEAKER_MODE_STEREO;
149 }
150
151 int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_mode) const {
152 switch (p_mode) {
153 case SPEAKER_MODE_STEREO:
154 return 2;
155 case SPEAKER_SURROUND_31:
156 return 4;
157 case SPEAKER_SURROUND_51:
158 return 6;
159 case SPEAKER_SURROUND_71:
160 return 8;
161 }
162
163 ERR_FAIL_V(2);
164 }
165
166 PackedStringArray AudioDriver::get_output_device_list() {
167 PackedStringArray list;
168
169 list.push_back("Default");
170
171 return list;
172 }
173
174 String AudioDriver::get_output_device() {
175 return "Default";
176 }
177
178 PackedStringArray AudioDriver::get_input_device_list() {
179 PackedStringArray list;
180
181 list.push_back("Default");
182
183 return list;
184 }
185
186 void AudioDriver::start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
187 if (p_playback.is_valid()) {
188 if (p_playback->stream.is_valid()) {
189 WARN_PRINT_ED(vformat(R"(Trying to play stream (%s) as a sample (%s), but the driver doesn't support sample playback.)", p_playback->get_instance_id(), p_playback->stream->get_instance_id()));
190 } else {
191 WARN_PRINT_ED(vformat(R"(Trying to play stream (%s) as a null sample, but the driver doesn't support sample playback.)", p_playback->get_instance_id()));
192 }
193 } else {
194 WARN_PRINT_ED("Trying to play a null sample playback from a driver that don't support sample playback.");
195 }
196 }
197
198 AudioDriverDummy AudioDriverManager::dummy_driver;
199 AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS] = {
200 &AudioDriverManager::dummy_driver,
201 };
202 int AudioDriverManager::driver_count = 1;
203
204 void AudioDriverManager::add_driver(AudioDriver *p_driver) {
205 ERR_FAIL_COND(driver_count >= MAX_DRIVERS);
206 drivers[driver_count - 1] = p_driver;
207
208 // Last driver is always our dummy driver
209 drivers[driver_count++] = &AudioDriverManager::dummy_driver;
210 }
211
212 int AudioDriverManager::get_driver_count() {
213 return driver_count;
214 }
215
216 void AudioDriverManager::initialize(int p_driver) {
217 GLOBAL_DEF_RST("audio/driver/enable_input", false);
218 GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/driver/mix_rate", PROPERTY_HINT_RANGE, "11025,192000,1,or_greater,suffix:Hz"), DEFAULT_MIX_RATE);
219 GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/driver/mix_rate.web", PROPERTY_HINT_RANGE, "0,192000,1,or_greater,suffix:Hz"), 0); // Safer default output_latency for web (use browser default).
220
221 int failed_driver = -1;
222
223 // Check if there is a selected driver
224 if (p_driver >= 0 && p_driver < driver_count) {
225 if (drivers[p_driver]->init() == OK) {
226 drivers[p_driver]->set_singleton();
227 return;
228 } else {
229 failed_driver = p_driver;
230 }
231 }
232
233 // No selected driver, try them all in order
234 for (int i = 0; i < driver_count; i++) {
235 // Don't re-init the driver if it failed above
236 if (i == failed_driver) {
237 continue;
238 }
239
240 if (drivers[i]->init() == OK) {
241 drivers[i]->set_singleton();
242 break;
243 }
244 }
245
246 if (driver_count > 1 && String(AudioDriver::get_singleton()->get_name()) == "Dummy") {
247 WARN_PRINT("All audio drivers failed, falling back to the dummy driver.");
248 }
249 }
250
251 AudioDriver *AudioDriverManager::get_driver(int p_driver) {
252 ERR_FAIL_INDEX_V(p_driver, driver_count, nullptr);
253 return drivers[p_driver];
254 }
255
256 //////////////////////////////////////////////
257 //////////////////////////////////////////////
258 //////////////////////////////////////////////
259 //////////////////////////////////////////////
260
261 void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
262 mix_count++;
263 int todo = p_frames;
264
265 #ifdef DEBUG_ENABLED
266 uint64_t prof_ticks = OS::get_singleton()->get_ticks_usec();
267 #endif
268
269 if (channel_count != get_channel_count()) {
270 // Amount of channels changed due to a output_device change
271 // reinitialize the buses channels and buffers
272 init_channels_and_buffers();
273 }
274
275 ERR_FAIL_COND_MSG(buses.is_empty() && todo, "AudioServer bus count is less than 1.");
276 while (todo) {
277 if (to_mix == 0) {
278 _mix_step();
279 }
280
281 int to_copy = MIN(to_mix, todo);
282
283 Bus *master = buses[0];
284
285 int from = buffer_size - to_mix;
286 int from_buf = p_frames - todo;
287
288 //master master, send to output
289 int cs = master->channels.size();
290
291 // Take away 1 from the stride, as we are manually incrementing by 1 for stereo.
292 uintptr_t stride_minus_one = (cs * 2) - 1;
293
294 for (int k = 0; k < cs; k++) {
295 // The destination start for data will be the same in all cases.
296 int32_t *dest = &p_buffer[from_buf * (cs * 2) + (k * 2)];
297
298 #ifdef DEBUG_ENABLED
299 if (!debug_mute && master->channels[k].active) {
300 #else
301 if (master->channels[k].active) {
302 #endif // DEBUG_ENABLED
303 const AudioFrame *buf = master->channels[k].buffer.ptr();
304
305 for (int j = 0; j < to_copy; j++) {
306 float l = CLAMP(buf[from + j].left, -1.0, 1.0);
307 int32_t vl = l * ((1 << 20) - 1);
308 int32_t vl2 = (vl < 0 ? -1 : 1) * (Math::abs(vl) << 11);
309 *dest = vl2;
310 dest++;
311
312 float r = CLAMP(buf[from + j].right, -1.0, 1.0);
313 int32_t vr = r * ((1 << 20) - 1);
314 int32_t vr2 = (vr < 0 ? -1 : 1) * (Math::abs(vr) << 11);
315 *dest = vr2;
316 dest += stride_minus_one;
317 }
318
319 } else {
320 // Bizarrely, profiling indicates that detecting the common case of cs == 1,
321 // k == 0, and using memset is SLOWER than setting them individually.
322 // Perhaps it gets optimized to a faster instruction than memset.
323 for (int j = 0; j < to_copy; j++) {
324 *dest = 0;
325 dest++;
326 *dest = 0;
327 dest += stride_minus_one;
328 }
329 }
330 }
331
332 todo -= to_copy;
333 to_mix -= to_copy;
334 }
335
336 #ifdef DEBUG_ENABLED
337 prof_time.add(OS::get_singleton()->get_ticks_usec() - prof_ticks);
338 #endif
339 }
340
341 void AudioServer::_mix_step() {
342 bool solo_mode = false;
343
344 for (int i = 0; i < buses.size(); i++) {
345 Bus *bus = buses[i];
346 bus->index_cache = i; //might be moved around by editor, so..
347 for (int k = 0; k < bus->channels.size(); k++) {
348 bus->channels.write[k].used = false;
349 }
350
351 if (bus->solo) {
352 //solo chain
353 solo_mode = true;
354 bus->soloed = true;
355 do {
356 if (bus != buses[0]) {
357 //everything has a send save for master bus
358 if (!bus_map.has(bus->send)) {
359 bus = buses[0]; //send to master
360 } else {
361 int prev_index_cache = bus->index_cache;
362 bus = bus_map[bus->send];
363 if (prev_index_cache >= bus->index_cache) { //invalid, send to master
364 bus = buses[0];
365 }
366 }
367
368 bus->soloed = true;
369 } else {
370 bus = nullptr;
371 }
372
373 } while (bus);
374 } else {
375 bus->soloed = false;
376 }
377 }
378 // This is legacy code from 3.x that allows video players and other audio sources that do not implement AudioStreamPlayback to output audio.
379 for (CallbackItem *ci : mix_callback_list) {
380 ci->callback(ci->userdata);
381 }
382
383 // Main mixing loop for audio streams.
384 // The basic idea here is to copy the samples returned by the AudioStreamPlayback's mix function into the audio buffers,
385 // while always maintaining a lookahead buffer of size LOOKAHEAD_BUFFER_SIZE to allow fade-outs for sudden stoppages.
386 for (AudioStreamPlaybackListNode *playback : playback_list) {
387 // Paused streams are no-ops. Don't even mix audio from the stream playback.
388 if (playback->state.load() == AudioStreamPlaybackListNode::PAUSED) {
389 continue;
390 }
391
392 if (playback->stream_playback->get_is_sample()) {
393 continue;
394 }
395
396 // If `fading_out` is true, we're in the process of fading out the stream playback.
397 // TODO: Currently this sets the volume of the stream to 0 which creates a linear interpolation between its previous volume and silence.
398 // A more punchy option for fading out could be to just use the lookahead buffer.
399 bool fading_out = playback->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION || playback->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE;
400
401 AudioFrame *buf = mix_buffer.ptrw();
402
403 // Copy the old contents of the lookahead buffer into the beginning of the mix buffer.
404 for (int i = 0; i < LOOKAHEAD_BUFFER_SIZE; i++) {
405 buf[i] = playback->lookahead[i];
406 }
407
408 // Mix the audio stream.
409 unsigned int mixed_frames = playback->stream_playback->mix(&buf[LOOKAHEAD_BUFFER_SIZE], playback->pitch_scale.get(), buffer_size);
410
411 if (tag_used_audio_streams && playback->stream_playback->is_playing()) {
412 playback->stream_playback->tag_used_streams();
413 }
414
415 // Check to see if the stream has run out of samples.
416 if (mixed_frames != buffer_size) {
417 // We know we have at least the size of our lookahead buffer for fade-out purposes.
418
419 float fadeout_base = 0.94;
420 float fadeout_coefficient = 1;
421 static_assert(LOOKAHEAD_BUFFER_SIZE == 64, "Update fadeout_base and comment here if you change LOOKAHEAD_BUFFER_SIZE.");
422 // 0.94 ^ 64 = 0.01906. There might still be a pop but it'll be way better than if we didn't do this.
423 for (unsigned int idx = mixed_frames; idx < buffer_size; idx++) {
424 fadeout_coefficient *= fadeout_base;
425 buf[idx] *= fadeout_coefficient;
426 }
427 AudioStreamPlaybackListNode::PlaybackState new_state;
428 new_state = AudioStreamPlaybackListNode::AWAITING_DELETION;
429 playback->state.store(new_state);
430 } else {
431 // Move the last little bit of what we just mixed into our lookahead buffer for the next call to _mix_step.
432 for (int i = 0; i < LOOKAHEAD_BUFFER_SIZE; i++) {
433 playback->lookahead[i] = buf[buffer_size + i];
434 }
435 }
436
437 // Get the bus details for this playback. This contains information about which buses the playback is assigned to and the volume of the playback on each bus.
438 AudioStreamPlaybackBusDetails *bus_details_ptr = playback->bus_details.load();
439 ERR_FAIL_NULL(bus_details_ptr);
440 // Make a copy of the bus details so we can modify it without worrying about other threads.
441 AudioStreamPlaybackBusDetails bus_details = *bus_details_ptr;
442
443 // Mix to any active buses.
444 for (int idx = 0; idx < MAX_BUSES_PER_PLAYBACK; idx++) {
445 if (!bus_details.bus_active[idx]) {
446 continue;
447 }
448 // This is the AudioServer-internal index of the bus we're mixing to in this step of the loop. Not to be confused with `idx` which is an index into `AudioStreamPlaybackBusDetails` member var arrays.
449 int bus_idx = thread_find_bus_index(bus_details.bus[idx]);
450
451 // It's important to know whether or not this bus was active in the previous mix step of this stream. If it was, we need to perform volume interpolation to avoid pops.
452 int prev_bus_idx = -1;
453 for (int search_idx = 0; search_idx < MAX_BUSES_PER_PLAYBACK; search_idx++) {
454 if (!playback->prev_bus_details->bus_active[search_idx]) {
455 continue;
456 }
457 // If the StringNames of the buses match, we've found the previous bus index. This indicates that this playback mixed to `prev_bus_details->bus[prev_bus_index]` in the previous mix step, which gives us a way to look up the playback's previous volume.
458 if (playback->prev_bus_details->bus[search_idx].hash() == bus_details.bus[idx].hash()) {
459 prev_bus_idx = search_idx;
460 break;
461 }
462 }
463
464 // It's now time to mix to the bus. We do this by going through each channel of the bus and mixing to it.
465 // The channels correspond to output channels of the audio device, e.g. stereo or 5.1. To reduce needless nesting, this is done with a helper method named `_mix_step_for_channel`.
466 for (int channel_idx = 0; channel_idx < channel_count; channel_idx++) {
467 AudioFrame *channel_buf = thread_get_channel_mix_buffer(bus_idx, channel_idx);
468 // TODO: This `fading_out` check could be replaced with with an exponential fadeout of the samples from the lookahead buffer for more punchy results.
469 if (fading_out) {
470 bus_details.volume[idx][channel_idx] = AudioFrame(0, 0);
471 }
472 AudioFrame channel_vol = bus_details.volume[idx][channel_idx];
473
474 // If this bus was not active in the previous mix step, we want to start playback at the full volume to avoid crushing transients.
475 AudioFrame prev_channel_vol = channel_vol;
476 // If this bus was active in the previous mix step, we need to interpolate between the previous volume and the current volume to avoid pops. Set `prev_channel_volume` accordingly.
477 if (prev_bus_idx != -1) {
478 prev_channel_vol = playback->prev_bus_details->volume[prev_bus_idx][channel_idx];
479 }
480 _mix_step_for_channel(channel_buf, buf, prev_channel_vol, channel_vol, playback->attenuation_filter_cutoff_hz.get(), playback->highshelf_gain.get(), &playback->filter_process[channel_idx * 2], &playback->filter_process[channel_idx * 2 + 1]);
481 }
482 }
483
484 // Now go through and fade-out any buses that were being played to previously that we missed by going through current data.
485 for (int idx = 0; idx < MAX_BUSES_PER_PLAYBACK; idx++) {
486 if (!playback->prev_bus_details->bus_active[idx]) {
487 continue;
488 }
489 int bus_idx = thread_find_bus_index(playback->prev_bus_details->bus[idx]);
490
491 int current_bus_idx = -1;
492 for (int search_idx = 0; search_idx < MAX_BUSES_PER_PLAYBACK; search_idx++) {
493 if (bus_details.bus[search_idx] == playback->prev_bus_details->bus[idx]) {
494 current_bus_idx = search_idx;
495 }
496 }
497 if (current_bus_idx != -1) {
498 // If we found a corresponding bus in the current bus assignments, we've already mixed to this bus.
499 continue;
500 }
501
502 for (int channel_idx = 0; channel_idx < channel_count; channel_idx++) {
503 AudioFrame *channel_buf = thread_get_channel_mix_buffer(bus_idx, channel_idx);
504 AudioFrame prev_channel_vol = playback->prev_bus_details->volume[idx][channel_idx];
505 // Fade out to silence. This could be replaced with an exponential fadeout of the samples from the lookahead buffer for more punchy results.
506 _mix_step_for_channel(channel_buf, buf, prev_channel_vol, AudioFrame(0, 0), playback->attenuation_filter_cutoff_hz.get(), playback->highshelf_gain.get(), &playback->filter_process[channel_idx * 2], &playback->filter_process[channel_idx * 2 + 1]);
507 }
508 }
509
510 // Copy the bus details we mixed with to the previous bus details to maintain volume ramps.
511 for (int i = 0; i < MAX_BUSES_PER_PLAYBACK; i++) {
512 playback->prev_bus_details->bus_active[i] = bus_details.bus_active[i];
513 }
514 for (int i = 0; i < MAX_BUSES_PER_PLAYBACK; i++) {
515 playback->prev_bus_details->bus[i] = bus_details.bus[i];
516 }
517 for (int i = 0; i < MAX_BUSES_PER_PLAYBACK; i++) {
518 for (int j = 0; j < MAX_CHANNELS_PER_BUS; j++) {
519 playback->prev_bus_details->volume[i][j] = bus_details.volume[i][j];
520 }
521 }
522
523 switch (playback->state.load()) {
524 case AudioStreamPlaybackListNode::AWAITING_DELETION:
525 case AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION:
526 // Remove the playback from the list.
527 _delete_stream_playback_list_node(playback);
528 break;
529 case AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE: {
530 // Pause the stream.
531 playback->state.store(AudioStreamPlaybackListNode::PAUSED);
532 } break;
533 case AudioStreamPlaybackListNode::PLAYING:
534 case AudioStreamPlaybackListNode::PAUSED:
535 // No-op!
536 break;
537 }
538 }
539
540 // Now that all of the buses have their audio sources mixed into them, we can process the effects and bus sends.
541 for (int i = buses.size() - 1; i >= 0; i--) {
542 Bus *bus = buses[i];
543
544 for (int k = 0; k < bus->channels.size(); k++) {
545 if (bus->channels[k].active && !bus->channels[k].used) {
546 // Buffer was not used, but it's still active, so it must be cleaned.
547 AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
548
549 for (uint32_t j = 0; j < buffer_size; j++) {
550 buf[j] = AudioFrame(0, 0);
551 }
552 }
553 }
554
555 // Process effects.
556 if (!bus->bypass) {
557 for (int j = 0; j < bus->effects.size(); j++) {
558 if (!bus->effects[j].enabled) {
559 continue;
560 }
561
562 #ifdef DEBUG_ENABLED
563 uint64_t ticks = OS::get_singleton()->get_ticks_usec();
564 #endif
565
566 for (int k = 0; k < bus->channels.size(); k++) {
567 if (!(bus->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
568 continue;
569 }
570 bus->channels.write[k].effect_instances.write[j]->process(bus->channels[k].buffer.ptr(), temp_buffer.write[k].ptrw(), buffer_size);
571 }
572
573 // Swap buffers, so internal buffer always has the right data.
574 for (int k = 0; k < bus->channels.size(); k++) {
575 if (!(buses[i]->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
576 continue;
577 }
578 SWAP(bus->channels.write[k].buffer, temp_buffer.write[k]);
579 }
580
581 #ifdef DEBUG_ENABLED
582 bus->effects.write[j].prof_time += OS::get_singleton()->get_ticks_usec() - ticks;
583 #endif
584 }
585 }
586
587 // Process send.
588
589 Bus *send = nullptr;
590
591 if (i > 0) {
592 // Everything has a send except for the master bus.
593 if (!bus_map.has(bus->send)) {
594 send = buses[0];
595 } else {
596 send = bus_map[bus->send];
597 if (send->index_cache >= bus->index_cache) { // Invalid, send to master.
598 send = buses[0];
599 }
600 }
601 }
602
603 for (int k = 0; k < bus->channels.size(); k++) {
604 if (!bus->channels[k].active) {
605 bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
606 continue;
607 }
608
609 AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
610
611 AudioFrame peak = AudioFrame(0, 0);
612
613 float volume = Math::db_to_linear(bus->volume_db);
614
615 if (solo_mode) {
616 if (!bus->soloed) {
617 volume = 0.0;
618 }
619 } else {
620 if (bus->mute) {
621 volume = 0.0;
622 }
623 }
624
625 // Apply volume and compute peak.
626 for (uint32_t j = 0; j < buffer_size; j++) {
627 buf[j] *= volume;
628
629 float l = Math::abs(buf[j].left);
630 if (l > peak.left) {
631 peak.left = l;
632 }
633 float r = Math::abs(buf[j].right);
634 if (r > peak.right) {
635 peak.right = r;
636 }
637 }
638
639 bus->channels.write[k].peak_volume = AudioFrame(Math::linear_to_db(peak.left + AUDIO_PEAK_OFFSET), Math::linear_to_db(peak.right + AUDIO_PEAK_OFFSET));
640
641 if (!bus->channels[k].used) {
642 // See if any audio is contained, because channel was not used.
643
644 if (MAX(peak.right, peak.left) > Math::db_to_linear(channel_disable_threshold_db)) {
645 bus->channels.write[k].last_mix_with_audio = mix_frames;
646 } else if (mix_frames - bus->channels[k].last_mix_with_audio > channel_disable_frames) {
647 bus->channels.write[k].active = false;
648 continue; //went inactive, don't mix.
649 }
650 }
651
652 if (send) {
653 // If not master bus, send.
654 AudioFrame *target_buf = thread_get_channel_mix_buffer(send->index_cache, k);
655
656 for (uint32_t j = 0; j < buffer_size; j++) {
657 target_buf[j] += buf[j];
658 }
659 }
660 }
661 }
662
663 mix_frames += buffer_size;
664 to_mix = buffer_size;
665 }
666
667 void AudioServer::_mix_step_for_channel(AudioFrame *p_out_buf, AudioFrame *p_source_buf, AudioFrame p_vol_start, AudioFrame p_vol_final, float p_attenuation_filter_cutoff_hz, float p_highshelf_gain, AudioFilterSW::Processor *p_processor_l, AudioFilterSW::Processor *p_processor_r) {
668 // TODO: In the future it could be nice to replace all of these hardcoded effects with something a bit cleaner and more flexible, but for now this is what we do to support 3D audio players.
669 if (p_highshelf_gain != 0) {
670 AudioFilterSW filter;
671 filter.set_mode(AudioFilterSW::HIGHSHELF);
672 filter.set_sampling_rate(AudioServer::get_singleton()->get_mix_rate());
673 filter.set_cutoff(p_attenuation_filter_cutoff_hz);
674 filter.set_resonance(1);
675 filter.set_stages(1);
676 filter.set_gain(p_highshelf_gain);
677
678 ERR_FAIL_NULL(p_processor_l);
679 ERR_FAIL_NULL(p_processor_r);
680
681 bool is_just_started = p_vol_start.left == 0 && p_vol_start.right == 0;
682 p_processor_l->set_filter(&filter, /* clear_history= */ is_just_started);
683 p_processor_l->update_coeffs(buffer_size);
684 p_processor_r->set_filter(&filter, /* clear_history= */ is_just_started);
685 p_processor_r->update_coeffs(buffer_size);
686
687 for (unsigned int frame_idx = 0; frame_idx < buffer_size; frame_idx++) {
688 // TODO: Make lerp speed buffer-size-invariant if buffer_size ever becomes a project setting to avoid very small buffer sizes causing pops due to too-fast lerps.
689 float lerp_param = (float)frame_idx / buffer_size;
690 AudioFrame vol = p_vol_final * lerp_param + (1 - lerp_param) * p_vol_start;
691 AudioFrame mixed = vol * p_source_buf[frame_idx];
692 p_processor_l->process_one_interp(mixed.left);
693 p_processor_r->process_one_interp(mixed.right);
694 p_out_buf[frame_idx] += mixed;
695 }
696
697 } else {
698 for (unsigned int frame_idx = 0; frame_idx < buffer_size; frame_idx++) {
699 // TODO: Make lerp speed buffer-size-invariant if buffer_size ever becomes a project setting to avoid very small buffer sizes causing pops due to too-fast lerps.
700 float lerp_param = (float)frame_idx / buffer_size;
701 p_out_buf[frame_idx] += (p_vol_final * lerp_param + (1 - lerp_param) * p_vol_start) * p_source_buf[frame_idx];
702 }
703 }
704 }
705
706 AudioServer::AudioStreamPlaybackListNode *AudioServer::_find_playback_list_node(Ref<AudioStreamPlayback> p_playback) {
707 for (AudioStreamPlaybackListNode *playback_list_node : playback_list) {
708 if (playback_list_node->stream_playback == p_playback) {
709 return playback_list_node;
710 }
711 }
712 return nullptr;
713 }
714
715 void AudioServer::_delete_stream_playback(Ref<AudioStreamPlayback> p_playback) {
716 ERR_FAIL_COND(p_playback.is_null());
717 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
718 if (playback_node) {
719 _delete_stream_playback_list_node(playback_node);
720 }
721 }
722
723 void AudioServer::_delete_stream_playback_list_node(AudioStreamPlaybackListNode *p_playback_node) {
724 // Remove the playback from the list, registering a destructor to be run on the main thread.
725 playback_list.erase(p_playback_node, [](AudioStreamPlaybackListNode *p) {
726 delete p->prev_bus_details;
727 delete p->bus_details.load();
728 p->stream_playback.unref();
729 delete p;
730 });
731 }
732
733 bool AudioServer::thread_has_channel_mix_buffer(int p_bus, int p_buffer) const {
734 if (p_bus < 0 || p_bus >= buses.size()) {
735 return false;
736 }
737 if (p_buffer < 0 || p_buffer >= buses[p_bus]->channels.size()) {
738 return false;
739 }
740 return true;
741 }
742
743 AudioFrame *AudioServer::thread_get_channel_mix_buffer(int p_bus, int p_buffer) {
744 ERR_FAIL_INDEX_V(p_bus, buses.size(), nullptr);
745 ERR_FAIL_INDEX_V(p_buffer, buses[p_bus]->channels.size(), nullptr);
746
747 AudioFrame *data = buses.write[p_bus]->channels.write[p_buffer].buffer.ptrw();
748
749 if (!buses[p_bus]->channels[p_buffer].used) {
750 buses.write[p_bus]->channels.write[p_buffer].used = true;
751 buses.write[p_bus]->channels.write[p_buffer].active = true;
752 buses.write[p_bus]->channels.write[p_buffer].last_mix_with_audio = mix_frames;
753 for (uint32_t i = 0; i < buffer_size; i++) {
754 data[i] = AudioFrame(0, 0);
755 }
756 }
757
758 return data;
759 }
760
761 int AudioServer::thread_get_mix_buffer_size() const {
762 return buffer_size;
763 }
764
765 int AudioServer::thread_find_bus_index(const StringName &p_name) {
766 if (bus_map.has(p_name)) {
767 return bus_map[p_name]->index_cache;
768 } else {
769 return 0;
770 }
771 }
772
773 #ifdef DEBUG_ENABLED
774 void AudioServer::set_debug_mute(bool p_mute) {
775 debug_mute = p_mute;
776 }
777
778 bool AudioServer::get_debug_mute() const {
779 return debug_mute;
780 }
781 #endif // DEBUG_ENABLED
782
783 void AudioServer::set_bus_count(int p_count) {
784 ERR_FAIL_COND(p_count < 1);
785 ERR_FAIL_INDEX(p_count, 256);
786
787 MARK_EDITED
788
789 lock();
790 int cb = buses.size();
791
792 if (p_count < buses.size()) {
793 for (int i = p_count; i < buses.size(); i++) {
794 bus_map.erase(buses[i]->name);
795 memdelete(buses[i]);
796 }
797 }
798
799 buses.resize(p_count);
800
801 for (int i = cb; i < buses.size(); i++) {
802 String attempt = "New Bus";
803 int attempts = 1;
804 while (true) {
805 bool name_free = true;
806 for (int j = 0; j < i; j++) {
807 if (buses[j]->name == attempt) {
808 name_free = false;
809 break;
810 }
811 }
812
813 if (!name_free) {
814 attempts++;
815 attempt = "New Bus " + itos(attempts);
816 } else {
817 break;
818 }
819 }
820
821 buses.write[i] = memnew(Bus);
822 buses.write[i]->channels.resize(channel_count);
823 for (int j = 0; j < channel_count; j++) {
824 buses.write[i]->channels.write[j].buffer.resize(buffer_size);
825 }
826 buses[i]->name = attempt;
827 buses[i]->solo = false;
828 buses[i]->mute = false;
829 buses[i]->bypass = false;
830 buses[i]->volume_db = 0;
831 if (i > 0) {
832 buses[i]->send = SceneStringName(Master);
833 }
834
835 bus_map[attempt] = buses[i];
836 }
837
838 unlock();
839
840 AudioDriver::get_singleton()->set_sample_bus_count(p_count);
841
842 emit_signal(SNAME("bus_layout_changed"));
843 }
844
845 void AudioServer::remove_bus(int p_index) {
846 ERR_FAIL_INDEX(p_index, buses.size());
847 ERR_FAIL_COND(p_index == 0);
848
849 MARK_EDITED
850
851 lock();
852 bus_map.erase(buses[p_index]->name);
853 memdelete(buses[p_index]);
854 buses.remove_at(p_index);
855 unlock();
856
857 AudioDriver::get_singleton()->remove_sample_bus(p_index);
858
859 emit_signal(SNAME("bus_layout_changed"));
860 }
861
862 void AudioServer::add_bus(int p_at_pos) {
863 MARK_EDITED
864
865 if (p_at_pos >= buses.size()) {
866 p_at_pos = -1;
867 } else if (p_at_pos == 0) {
868 if (buses.size() > 1) {
869 p_at_pos = 1;
870 } else {
871 p_at_pos = -1;
872 }
873 }
874
875 String attempt = "New Bus";
876 int attempts = 1;
877 while (true) {
878 bool name_free = true;
879 for (int j = 0; j < buses.size(); j++) {
880 if (buses[j]->name == attempt) {
881 name_free = false;
882 break;
883 }
884 }
885
886 if (!name_free) {
887 attempts++;
888 attempt = "New Bus " + itos(attempts);
889 } else {
890 break;
891 }
892 }
893
894 Bus *bus = memnew(Bus);
895 bus->channels.resize(channel_count);
896 for (int j = 0; j < channel_count; j++) {
897 bus->channels.write[j].buffer.resize(buffer_size);
898 }
899 bus->name = attempt;
900 bus->solo = false;
901 bus->mute = false;
902 bus->bypass = false;
903 bus->volume_db = 0;
904
905 bus_map[attempt] = bus;
906
907 if (p_at_pos == -1) {
908 buses.push_back(bus);
909 } else {
910 buses.insert(p_at_pos, bus);
911 }
912
913 AudioDriver::get_singleton()->add_sample_bus(p_at_pos);
914
915 emit_signal(SNAME("bus_layout_changed"));
916 }
917
918 void AudioServer::move_bus(int p_bus, int p_to_pos) {
919 ERR_FAIL_COND(p_bus < 1 || p_bus >= buses.size());
920 ERR_FAIL_COND(p_to_pos != -1 && (p_to_pos < 1 || p_to_pos > buses.size()));
921
922 MARK_EDITED
923
924 if (p_bus == p_to_pos) {
925 return;
926 }
927
928 Bus *bus = buses[p_bus];
929 buses.remove_at(p_bus);
930
931 if (p_to_pos == -1) {
932 buses.push_back(bus);
933 } else if (p_to_pos < p_bus) {
934 buses.insert(p_to_pos, bus);
935 } else {
936 buses.insert(p_to_pos - 1, bus);
937 }
938
939 AudioDriver::get_singleton()->move_sample_bus(p_bus, p_to_pos);
940
941 emit_signal(SNAME("bus_layout_changed"));
942 }
943
944 int AudioServer::get_bus_count() const {
945 return buses.size();
946 }
947
948 void AudioServer::set_bus_name(int p_bus, const String &p_name) {
949 ERR_FAIL_INDEX(p_bus, buses.size());
950 if (p_bus == 0 && p_name != "Master") {
951 return; // Bus 0 is always "Master".
952 }
953
954 MARK_EDITED
955
956 lock();
957
958 StringName old_name = buses[p_bus]->name;
959
960 if (old_name == p_name) {
961 unlock();
962 return;
963 }
964
965 String attempt = p_name;
966 int attempts = 1;
967
968 while (true) {
969 bool name_free = true;
970 for (int i = 0; i < buses.size(); i++) {
971 if (buses[i]->name == attempt) {
972 name_free = false;
973 break;
974 }
975 }
976
977 if (name_free) {
978 break;
979 }
980
981 attempts++;
982 attempt = p_name + " " + itos(attempts);
983 }
984 bus_map.erase(old_name);
985 buses[p_bus]->name = attempt;
986 bus_map[attempt] = buses[p_bus];
987 unlock();
988
989 emit_signal(SNAME("bus_renamed"), p_bus, old_name, attempt);
990 }
991
992 String AudioServer::get_bus_name(int p_bus) const {
993 ERR_FAIL_INDEX_V(p_bus, buses.size(), String());
994 return buses[p_bus]->name;
995 }
996
997 int AudioServer::get_bus_index(const StringName &p_bus_name) const {
998 for (int i = 0; i < buses.size(); ++i) {
999 if (buses[i]->name == p_bus_name) {
1000 return i;
1001 }
1002 }
1003 return -1;
1004 }
1005
1006 void AudioServer::set_bus_volume_db(int p_bus, float p_volume_db) {
1007 ERR_FAIL_INDEX(p_bus, buses.size());
1008
1009 MARK_EDITED
1010
1011 buses[p_bus]->volume_db = p_volume_db;
1012
1013 AudioDriver::get_singleton()->set_sample_bus_volume_db(p_bus, p_volume_db);
1014 }
1015
1016 float AudioServer::get_bus_volume_db(int p_bus) const {
1017 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1018 return buses[p_bus]->volume_db;
1019 }
1020
1021 void AudioServer::set_bus_volume_linear(int p_bus, float p_volume_linear) {
1022 set_bus_volume_db(p_bus, Math::linear_to_db(p_volume_linear));
1023 }
1024
1025 float AudioServer::get_bus_volume_linear(int p_bus) const {
1026 return Math::db_to_linear(get_bus_volume_db(p_bus));
1027 }
1028
1029 int AudioServer::get_bus_channels(int p_bus) const {
1030 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1031 return buses[p_bus]->channels.size();
1032 }
1033
1034 void AudioServer::set_bus_send(int p_bus, const StringName &p_send) {
1035 ERR_FAIL_INDEX(p_bus, buses.size());
1036
1037 MARK_EDITED
1038
1039 buses[p_bus]->send = p_send;
1040
1041 AudioDriver::get_singleton()->set_sample_bus_send(p_bus, p_send);
1042 }
1043
1044 StringName AudioServer::get_bus_send(int p_bus) const {
1045 ERR_FAIL_INDEX_V(p_bus, buses.size(), StringName());
1046 return buses[p_bus]->send;
1047 }
1048
1049 void AudioServer::set_bus_solo(int p_bus, bool p_enable) {
1050 ERR_FAIL_INDEX(p_bus, buses.size());
1051
1052 MARK_EDITED
1053
1054 buses[p_bus]->solo = p_enable;
1055
1056 AudioDriver::get_singleton()->set_sample_bus_solo(p_bus, p_enable);
1057 }
1058
1059 bool AudioServer::is_bus_solo(int p_bus) const {
1060 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1061
1062 return buses[p_bus]->solo;
1063 }
1064
1065 void AudioServer::set_bus_mute(int p_bus, bool p_enable) {
1066 ERR_FAIL_INDEX(p_bus, buses.size());
1067
1068 MARK_EDITED
1069
1070 buses[p_bus]->mute = p_enable;
1071
1072 AudioDriver::get_singleton()->set_sample_bus_mute(p_bus, p_enable);
1073 }
1074
1075 bool AudioServer::is_bus_mute(int p_bus) const {
1076 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1077
1078 return buses[p_bus]->mute;
1079 }
1080
1081 void AudioServer::set_bus_bypass_effects(int p_bus, bool p_enable) {
1082 ERR_FAIL_INDEX(p_bus, buses.size());
1083
1084 MARK_EDITED
1085
1086 buses[p_bus]->bypass = p_enable;
1087 }
1088
1089 bool AudioServer::is_bus_bypassing_effects(int p_bus) const {
1090 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1091
1092 return buses[p_bus]->bypass;
1093 }
1094
1095 void AudioServer::_update_bus_effects(int p_bus) {
1096 for (int i = 0; i < buses[p_bus]->channels.size(); i++) {
1097 buses.write[p_bus]->channels.write[i].effect_instances.resize(buses[p_bus]->effects.size());
1098 for (int j = 0; j < buses[p_bus]->effects.size(); j++) {
1099 Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instantiate();
1100 if (Object::cast_to<AudioEffectCompressorInstance>(*fx)) {
1101 Object::cast_to<AudioEffectCompressorInstance>(*fx)->set_current_channel(i);
1102 }
1103 buses.write[p_bus]->channels.write[i].effect_instances.write[j] = fx;
1104 }
1105 }
1106 }
1107
1108 void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos) {
1109 ERR_FAIL_COND(p_effect.is_null());
1110 ERR_FAIL_INDEX(p_bus, buses.size());
1111
1112 MARK_EDITED
1113
1114 lock();
1115
1116 Bus::Effect fx;
1117 fx.effect = p_effect;
1118 //fx.instance=p_effect->instantiate();
1119 fx.enabled = true;
1120 #ifdef DEBUG_ENABLED
1121 fx.prof_time = 0;
1122 #endif
1123
1124 if (p_at_pos >= buses[p_bus]->effects.size() || p_at_pos < 0) {
1125 buses[p_bus]->effects.push_back(fx);
1126 } else {
1127 buses[p_bus]->effects.insert(p_at_pos, fx);
1128 }
1129
1130 _update_bus_effects(p_bus);
1131
1132 unlock();
1133 }
1134
1135 void AudioServer::remove_bus_effect(int p_bus, int p_effect) {
1136 ERR_FAIL_INDEX(p_bus, buses.size());
1137
1138 MARK_EDITED
1139
1140 lock();
1141
1142 buses[p_bus]->effects.remove_at(p_effect);
1143 _update_bus_effects(p_bus);
1144
1145 unlock();
1146 }
1147
1148 int AudioServer::get_bus_effect_count(int p_bus) {
1149 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1150
1151 return buses[p_bus]->effects.size();
1152 }
1153
1154 Ref<AudioEffectInstance> AudioServer::get_bus_effect_instance(int p_bus, int p_effect, int p_channel) {
1155 ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffectInstance>());
1156 ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffectInstance>());
1157 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), Ref<AudioEffectInstance>());
1158
1159 return buses[p_bus]->channels[p_channel].effect_instances[p_effect];
1160 }
1161
1162 Ref<AudioEffect> AudioServer::get_bus_effect(int p_bus, int p_effect) {
1163 ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffect>());
1164 ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffect>());
1165
1166 return buses[p_bus]->effects[p_effect].effect;
1167 }
1168
1169 void AudioServer::swap_bus_effects(int p_bus, int p_effect, int p_by_effect) {
1170 ERR_FAIL_INDEX(p_bus, buses.size());
1171 ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
1172 ERR_FAIL_INDEX(p_by_effect, buses[p_bus]->effects.size());
1173
1174 MARK_EDITED
1175
1176 lock();
1177 SWAP(buses.write[p_bus]->effects.write[p_effect], buses.write[p_bus]->effects.write[p_by_effect]);
1178 _update_bus_effects(p_bus);
1179 unlock();
1180 }
1181
1182 void AudioServer::set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled) {
1183 ERR_FAIL_INDEX(p_bus, buses.size());
1184 ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
1185
1186 MARK_EDITED
1187
1188 buses.write[p_bus]->effects.write[p_effect].enabled = p_enabled;
1189 }
1190
1191 bool AudioServer::is_bus_effect_enabled(int p_bus, int p_effect) const {
1192 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1193 ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), false);
1194 return buses[p_bus]->effects[p_effect].enabled;
1195 }
1196
1197 float AudioServer::get_bus_peak_volume_left_db(int p_bus, int p_channel) const {
1198 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1199 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
1200
1201 return buses[p_bus]->channels[p_channel].peak_volume.left;
1202 }
1203
1204 float AudioServer::get_bus_peak_volume_right_db(int p_bus, int p_channel) const {
1205 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1206 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
1207
1208 return buses[p_bus]->channels[p_channel].peak_volume.right;
1209 }
1210
1211 bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
1212 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1213 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), false);
1214
1215 return buses[p_bus]->channels[p_channel].active;
1216 }
1217
1218 void AudioServer::set_playback_speed_scale(float p_scale) {
1219 ERR_FAIL_COND(p_scale <= 0);
1220
1221 playback_speed_scale = p_scale;
1222 }
1223
1224 float AudioServer::get_playback_speed_scale() const {
1225 return playback_speed_scale;
1226 }
1227
1228 void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, const StringName &p_bus, Vector<AudioFrame> p_volume_db_vector, float p_start_time, float p_pitch_scale) {
1229 ERR_FAIL_COND(p_playback.is_null());
1230
1231 HashMap<StringName, Vector<AudioFrame>> map;
1232 map[p_bus] = p_volume_db_vector;
1233
1234 start_playback_stream(p_playback, map, p_start_time, p_pitch_scale);
1235 }
1236
1237 void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes, float p_start_time, float p_pitch_scale, float p_highshelf_gain, float p_attenuation_cutoff_hz) {
1238 ERR_FAIL_COND(p_playback.is_null());
1239
1240 AudioStreamPlaybackListNode *playback_node = new AudioStreamPlaybackListNode();
1241 playback_node->stream_playback = p_playback;
1242 playback_node->stream_playback->start(p_start_time);
1243
1244 AudioStreamPlaybackBusDetails *new_bus_details = new AudioStreamPlaybackBusDetails();
1245 int idx = 0;
1246 for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
1247 if (pair.value.size() < channel_count || pair.value.size() != MAX_CHANNELS_PER_BUS) {
1248 delete playback_node;
1249 delete new_bus_details;
1250 ERR_FAIL();
1251 }
1252
1253 new_bus_details->bus_active[idx] = true;
1254 new_bus_details->bus[idx] = pair.key;
1255 for (int channel_idx = 0; channel_idx < MAX_CHANNELS_PER_BUS; channel_idx++) {
1256 new_bus_details->volume[idx][channel_idx] = pair.value[channel_idx];
1257 }
1258 idx++;
1259 }
1260 playback_node->bus_details.store(new_bus_details);
1261 playback_node->prev_bus_details = new AudioStreamPlaybackBusDetails();
1262
1263 playback_node->pitch_scale.set(p_pitch_scale);
1264 playback_node->highshelf_gain.set(p_highshelf_gain);
1265 playback_node->attenuation_filter_cutoff_hz.set(p_attenuation_cutoff_hz);
1266
1267 memset(playback_node->prev_bus_details->volume, 0, sizeof(playback_node->prev_bus_details->volume));
1268
1269 for (AudioFrame &frame : playback_node->lookahead) {
1270 frame = AudioFrame(0, 0);
1271 }
1272
1273 playback_node->state.store(AudioStreamPlaybackListNode::PLAYING);
1274
1275 playback_list.insert(playback_node);
1276 }
1277
1278 void AudioServer::stop_playback_stream(Ref<AudioStreamPlayback> p_playback) {
1279 ERR_FAIL_COND(p_playback.is_null());
1280
1281 // Handle sample playback.
1282 if (p_playback->get_is_sample()) {
1283 if (p_playback->get_sample_playback().is_valid()) {
1284 AudioServer::get_singleton()->stop_sample_playback(p_playback->get_sample_playback());
1285 } else {
1286 _delete_stream_playback(p_playback);
1287 }
1288 return;
1289 }
1290
1291 if (!p_playback->is_playing()) {
1292 p_playback->stop();
1293 }
1294
1295 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1296 if (!playback_node) {
1297 return;
1298 }
1299
1300 AudioStreamPlaybackListNode::PlaybackState new_state, old_state;
1301 do {
1302 old_state = playback_node->state.load();
1303 if (old_state == AudioStreamPlaybackListNode::AWAITING_DELETION) {
1304 break; // Don't fade out again.
1305 }
1306 new_state = AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION;
1307
1308 } while (!playback_node->state.compare_exchange_strong(old_state, new_state));
1309 }
1310
1311 void AudioServer::set_playback_bus_exclusive(Ref<AudioStreamPlayback> p_playback, const StringName &p_bus, Vector<AudioFrame> p_volumes) {
1312 ERR_FAIL_COND(p_volumes.size() != MAX_CHANNELS_PER_BUS);
1313
1314 HashMap<StringName, Vector<AudioFrame>> map;
1315 map[p_bus] = p_volumes;
1316
1317 set_playback_bus_volumes_linear(p_playback, map);
1318 }
1319
1320 void AudioServer::set_playback_bus_volumes_linear(Ref<AudioStreamPlayback> p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) {
1321 ERR_FAIL_COND(p_bus_volumes.size() > MAX_BUSES_PER_PLAYBACK);
1322
1323 // Samples.
1324 if (p_playback->get_is_sample() && p_playback->get_sample_playback().is_valid()) {
1325 Ref<AudioSamplePlayback> sample_playback = p_playback->get_sample_playback();
1326 AudioDriver::get_singleton()->set_sample_playback_bus_volumes_linear(sample_playback, p_bus_volumes);
1327 return;
1328 }
1329
1330 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1331 if (!playback_node) {
1332 return;
1333 }
1334 AudioStreamPlaybackBusDetails *old_bus_details, *new_bus_details = new AudioStreamPlaybackBusDetails();
1335
1336 int idx = 0;
1337 for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
1338 if (idx >= MAX_BUSES_PER_PLAYBACK) {
1339 break;
1340 }
1341 if (pair.value.size() < channel_count || pair.value.size() != MAX_CHANNELS_PER_BUS) {
1342 delete new_bus_details;
1343 ERR_FAIL();
1344 }
1345
1346 new_bus_details->bus_active[idx] = true;
1347 new_bus_details->bus[idx] = pair.key;
1348 for (int channel_idx = 0; channel_idx < MAX_CHANNELS_PER_BUS; channel_idx++) {
1349 new_bus_details->volume[idx][channel_idx] = pair.value[channel_idx];
1350 }
1351 idx++;
1352 }
1353
1354 do {
1355 old_bus_details = playback_node->bus_details.load();
1356 } while (!playback_node->bus_details.compare_exchange_strong(old_bus_details, new_bus_details));
1357
1358 bus_details_graveyard.insert(old_bus_details);
1359 }
1360
1361 void AudioServer::set_playback_all_bus_volumes_linear(Ref<AudioStreamPlayback> p_playback, Vector<AudioFrame> p_volumes) {
1362 ERR_FAIL_COND(p_playback.is_null());
1363 ERR_FAIL_COND(p_volumes.size() != MAX_CHANNELS_PER_BUS);
1364
1365 HashMap<StringName, Vector<AudioFrame>> map;
1366
1367 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1368 if (!playback_node) {
1369 return;
1370 }
1371 for (int bus_idx = 0; bus_idx < MAX_BUSES_PER_PLAYBACK; bus_idx++) {
1372 if (playback_node->bus_details.load()->bus_active[bus_idx]) {
1373 map[playback_node->bus_details.load()->bus[bus_idx]] = p_volumes;
1374 }
1375 }
1376
1377 set_playback_bus_volumes_linear(p_playback, map);
1378 }
1379
1380 void AudioServer::set_playback_pitch_scale(Ref<AudioStreamPlayback> p_playback, float p_pitch_scale) {
1381 ERR_FAIL_COND(p_playback.is_null());
1382
1383 // Samples.
1384 if (p_playback->get_is_sample() && p_playback->get_sample_playback().is_valid()) {
1385 Ref<AudioSamplePlayback> sample_playback = p_playback->get_sample_playback();
1386 AudioServer::get_singleton()->update_sample_playback_pitch_scale(sample_playback, p_pitch_scale);
1387 return;
1388 }
1389
1390 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1391 if (!playback_node) {
1392 return;
1393 }
1394
1395 playback_node->pitch_scale.set(p_pitch_scale);
1396 }
1397
1398 void AudioServer::set_playback_paused(Ref<AudioStreamPlayback> p_playback, bool p_paused) {
1399 ERR_FAIL_COND(p_playback.is_null());
1400
1401 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1402 if (!playback_node) {
1403 return;
1404 }
1405
1406 AudioStreamPlaybackListNode::PlaybackState new_state, old_state;
1407 do {
1408 old_state = playback_node->state.load();
1409 new_state = p_paused ? AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE : AudioStreamPlaybackListNode::PLAYING;
1410 if (!p_paused && old_state == AudioStreamPlaybackListNode::PLAYING) {
1411 return; // No-op.
1412 }
1413 if (p_paused && (old_state == AudioStreamPlaybackListNode::PAUSED || old_state == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE)) {
1414 return; // No-op.
1415 }
1416
1417 } while (!playback_node->state.compare_exchange_strong(old_state, new_state));
1418 }
1419
1420 void AudioServer::set_playback_highshelf_params(Ref<AudioStreamPlayback> p_playback, float p_gain, float p_attenuation_cutoff_hz) {
1421 ERR_FAIL_COND(p_playback.is_null());
1422
1423 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1424 if (!playback_node) {
1425 return;
1426 }
1427
1428 playback_node->attenuation_filter_cutoff_hz.set(p_attenuation_cutoff_hz);
1429 playback_node->highshelf_gain.set(p_gain);
1430 }
1431
1432 bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> p_playback) {
1433 ERR_FAIL_COND_V(p_playback.is_null(), false);
1434
1435 if (p_playback->get_is_sample()) {
1436 if (p_playback->get_sample_playback().is_valid()) {
1437 return sample_playback_list.has(p_playback->get_sample_playback());
1438 } else {
1439 return false;
1440 }
1441 }
1442
1443 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1444 if (!playback_node) {
1445 return false;
1446 }
1447
1448 return playback_node->state.load() == AudioStreamPlaybackListNode::PLAYING;
1449 }
1450
1451 float AudioServer::get_playback_position(Ref<AudioStreamPlayback> p_playback) {
1452 ERR_FAIL_COND_V(p_playback.is_null(), 0);
1453
1454 // Samples.
1455 if (p_playback->get_is_sample() && p_playback->get_sample_playback().is_valid()) {
1456 Ref<AudioSamplePlayback> sample_playback = p_playback->get_sample_playback();
1457 return AudioServer::get_singleton()->get_sample_playback_position(sample_playback);
1458 }
1459
1460 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1461 if (!playback_node) {
1462 return 0;
1463 }
1464
1465 return playback_node->stream_playback->get_playback_position();
1466 }
1467
1468 bool AudioServer::is_playback_paused(Ref<AudioStreamPlayback> p_playback) {
1469 ERR_FAIL_COND_V(p_playback.is_null(), false);
1470
1471 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1472 if (!playback_node) {
1473 return false;
1474 }
1475
1476 return playback_node->state.load() == AudioStreamPlaybackListNode::PAUSED || playback_node->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE;
1477 }
1478
1479 uint64_t AudioServer::get_mix_count() const {
1480 return mix_count;
1481 }
1482
1483 uint64_t AudioServer::get_mixed_frames() const {
1484 return mix_frames;
1485 }
1486
1487 String AudioServer::get_driver_name() const {
1488 return AudioDriver::get_singleton()->get_name();
1489 }
1490
1491 void AudioServer::notify_listener_changed() {
1492 for (CallbackItem *ci : listener_changed_callback_list) {
1493 ci->callback(ci->userdata);
1494 }
1495 }
1496
1497 void AudioServer::init_channels_and_buffers() {
1498 channel_count = get_channel_count();
1499 temp_buffer.resize(channel_count);
1500 mix_buffer.resize(buffer_size + LOOKAHEAD_BUFFER_SIZE);
1501
1502 for (int i = 0; i < temp_buffer.size(); i++) {
1503 temp_buffer.write[i].resize(buffer_size);
1504 }
1505
1506 for (int i = 0; i < buses.size(); i++) {
1507 buses[i]->channels.resize(channel_count);
1508 for (int j = 0; j < channel_count; j++) {
1509 buses.write[i]->channels.write[j].buffer.resize(buffer_size);
1510 }
1511 _update_bus_effects(i);
1512 }
1513 }
1514
1515 void AudioServer::init() {
1516 channel_disable_threshold_db = GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_threshold_db", PROPERTY_HINT_RANGE, "-80,0,0.1,suffix:dB"), -60.0);
1517 channel_disable_frames = float(GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_time", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"), 2.0)) * get_mix_rate();
1518 // TODO: Buffer size is hardcoded for now. This would be really nice to have as a project setting because currently it limits audio latency to an absolute minimum of 11ms with default mix rate, but there's some additional work required to make that happen. See TODOs in `_mix_step_for_channel`.
1519 // When this becomes a project setting, it should be specified in milliseconds rather than raw sample count, because 512 samples at 192khz is shorter than it is at 48khz, for example.
1520 buffer_size = 512;
1521
1522 init_channels_and_buffers();
1523
1524 mix_count = 0;
1525 set_bus_count(1);
1526 set_bus_name(0, "Master");
1527
1528 if (AudioDriver::get_singleton()) {
1529 AudioDriver::get_singleton()->start();
1530 AudioDriver::get_singleton()->set_sample_bus_count(1);
1531 }
1532
1533 #ifdef TOOLS_ENABLED
1534 set_edited(false); //avoid editors from thinking this was edited
1535 #endif
1536
1537 GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/video/video_delay_compensation_ms", PROPERTY_HINT_RANGE, "-1000,1000,1,suffix:ms"), 0);
1538 }
1539
1540 void AudioServer::update() {
1541 #ifdef DEBUG_ENABLED
1542 if (EngineDebugger::is_profiling(SNAME("servers"))) {
1543 // Driver time includes server time + effects times
1544 // Server time includes effects times
1545 uint64_t driver_time = AudioDriver::get_singleton()->get_profiling_time();
1546 uint64_t server_time = prof_time.get();
1547
1548 // Subtract the server time from the driver time
1549 if (driver_time > server_time) {
1550 driver_time -= server_time;
1551 }
1552
1553 Array values;
1554
1555 for (int i = buses.size() - 1; i >= 0; i--) {
1556 Bus *bus = buses[i];
1557 if (bus->bypass) {
1558 continue;
1559 }
1560
1561 for (int j = 0; j < bus->effects.size(); j++) {
1562 if (!bus->effects[j].enabled) {
1563 continue;
1564 }
1565
1566 values.push_back(String(bus->name) + bus->effects[j].effect->get_name());
1567 values.push_back(USEC_TO_SEC(bus->effects[j].prof_time));
1568
1569 // Subtract the effect time from the driver and server times
1570 if (driver_time > bus->effects[j].prof_time) {
1571 driver_time -= bus->effects[j].prof_time;
1572 }
1573 if (server_time > bus->effects[j].prof_time) {
1574 server_time -= bus->effects[j].prof_time;
1575 }
1576 }
1577 }
1578
1579 values.push_back("audio_server");
1580 values.push_back(USEC_TO_SEC(server_time));
1581 values.push_back("audio_driver");
1582 values.push_back(USEC_TO_SEC(driver_time));
1583
1584 values.push_front("audio_thread");
1585 EngineDebugger::profiler_add_frame_data("servers", values);
1586 }
1587
1588 // Reset profiling times
1589 for (int i = buses.size() - 1; i >= 0; i--) {
1590 Bus *bus = buses[i];
1591 if (bus->bypass) {
1592 continue;
1593 }
1594
1595 for (int j = 0; j < bus->effects.size(); j++) {
1596 if (!bus->effects[j].enabled) {
1597 continue;
1598 }
1599
1600 bus->effects.write[j].prof_time = 0;
1601 }
1602 }
1603
1604 AudioDriver::get_singleton()->reset_profiling_time();
1605 prof_time.set(0);
1606 #endif
1607
1608 for (CallbackItem *ci : update_callback_list) {
1609 ci->callback(ci->userdata);
1610 }
1611
1612 _cleanup_lists();
1613 }
1614
1615 void AudioServer::_cleanup_lists() {
1616 mix_callback_list.maybe_cleanup();
1617 update_callback_list.maybe_cleanup();
1618 listener_changed_callback_list.maybe_cleanup();
1619 playback_list.maybe_cleanup();
1620 for (AudioStreamPlaybackBusDetails *bus_details : bus_details_graveyard_frame_old) {
1621 bus_details_graveyard_frame_old.erase(bus_details, [](AudioStreamPlaybackBusDetails *d) { delete d; });
1622 }
1623 for (AudioStreamPlaybackBusDetails *bus_details : bus_details_graveyard) {
1624 bus_details_graveyard_frame_old.insert(bus_details);
1625 bus_details_graveyard.erase(bus_details);
1626 }
1627 bus_details_graveyard.maybe_cleanup();
1628 bus_details_graveyard_frame_old.maybe_cleanup();
1629 }
1630
1631 void AudioServer::load_default_bus_layout() {
1632 String layout_path = GLOBAL_GET("audio/buses/default_bus_layout");
1633
1634 if (ResourceLoader::exists(layout_path)) {
1635 Ref<AudioBusLayout> default_layout = ResourceLoader::load(layout_path);
1636 if (default_layout.is_valid()) {
1637 set_bus_layout(default_layout);
1638 }
1639 }
1640 }
1641
1642 void AudioServer::finish() {
1643 for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
1644 AudioDriverManager::get_driver(i)->finish();
1645 }
1646
1647 for (int i = 0; i < buses.size(); i++) {
1648 memdelete(buses[i]);
1649 }
1650
1651 buses.clear();
1652 }
1653
1654 /* MISC config */
1655
1656 void AudioServer::lock() {
1657 AudioDriver::get_singleton()->lock();
1658 }
1659
1660 void AudioServer::unlock() {
1661 AudioDriver::get_singleton()->unlock();
1662 }
1663
1664 AudioServer::SpeakerMode AudioServer::get_speaker_mode() const {
1665 return (AudioServer::SpeakerMode)AudioDriver::get_singleton()->get_speaker_mode();
1666 }
1667
1668 float AudioServer::get_mix_rate() const {
1669 return AudioDriver::get_singleton()->get_mix_rate();
1670 }
1671
1672 float AudioServer::get_input_mix_rate() const {
1673 return AudioDriver::get_singleton()->get_input_mix_rate();
1674 }
1675
1676 float AudioServer::read_output_peak_db() const {
1677 return 0;
1678 }
1679
1680 AudioServer *AudioServer::get_singleton() {
1681 return singleton;
1682 }
1683
1684 double AudioServer::get_output_latency() const {
1685 return AudioDriver::get_singleton()->get_latency();
1686 }
1687
1688 double AudioServer::get_time_to_next_mix() const {
1689 return AudioDriver::get_singleton()->get_time_to_next_mix();
1690 }
1691
1692 double AudioServer::get_time_since_last_mix() const {
1693 return AudioDriver::get_singleton()->get_time_since_last_mix();
1694 }
1695
1696 AudioServer *AudioServer::singleton = nullptr;
1697
1698 void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) {
1699 CallbackItem *ci = new CallbackItem();
1700 ci->callback = p_callback;
1701 ci->userdata = p_userdata;
1702 update_callback_list.insert(ci);
1703 }
1704
1705 void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) {
1706 for (CallbackItem *ci : update_callback_list) {
1707 if (ci->callback == p_callback && ci->userdata == p_userdata) {
1708 update_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
1709 }
1710 }
1711 }
1712
1713 void AudioServer::add_mix_callback(AudioCallback p_callback, void *p_userdata) {
1714 CallbackItem *ci = new CallbackItem();
1715 ci->callback = p_callback;
1716 ci->userdata = p_userdata;
1717 mix_callback_list.insert(ci);
1718 }
1719
1720 void AudioServer::remove_mix_callback(AudioCallback p_callback, void *p_userdata) {
1721 for (CallbackItem *ci : mix_callback_list) {
1722 if (ci->callback == p_callback && ci->userdata == p_userdata) {
1723 mix_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
1724 }
1725 }
1726 }
1727
1728 void AudioServer::add_listener_changed_callback(AudioCallback p_callback, void *p_userdata) {
1729 CallbackItem *ci = new CallbackItem();
1730 ci->callback = p_callback;
1731 ci->userdata = p_userdata;
1732 listener_changed_callback_list.insert(ci);
1733 }
1734
1735 void AudioServer::remove_listener_changed_callback(AudioCallback p_callback, void *p_userdata) {
1736 for (CallbackItem *ci : listener_changed_callback_list) {
1737 if (ci->callback == p_callback && ci->userdata == p_userdata) {
1738 listener_changed_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
1739 }
1740 }
1741 }
1742
1743 void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
1744 ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.is_empty());
1745
1746 lock();
1747 for (int i = 0; i < buses.size(); i++) {
1748 memdelete(buses[i]);
1749 }
1750 buses.resize(p_bus_layout->buses.size());
1751 bus_map.clear();
1752
1753 AudioDriver::get_singleton()->set_sample_bus_count(buses.size());
1754
1755 for (int i = 0; i < p_bus_layout->buses.size(); i++) {
1756 Bus *bus = memnew(Bus);
1757 if (i == 0) {
1758 bus->name = SceneStringName(Master);
1759 } else {
1760 bus->name = p_bus_layout->buses[i].name;
1761 bus->send = p_bus_layout->buses[i].send;
1762 AudioDriver::get_singleton()->set_sample_bus_send(i, bus->send);
1763 }
1764
1765 bus->solo = p_bus_layout->buses[i].solo;
1766 bus->mute = p_bus_layout->buses[i].mute;
1767 bus->bypass = p_bus_layout->buses[i].bypass;
1768 bus->volume_db = p_bus_layout->buses[i].volume_db;
1769
1770 AudioDriver::get_singleton()->set_sample_bus_solo(i, bus->solo);
1771 AudioDriver::get_singleton()->set_sample_bus_mute(i, bus->mute);
1772 AudioDriver::get_singleton()->set_sample_bus_volume_db(i, bus->volume_db);
1773
1774 for (int j = 0; j < p_bus_layout->buses[i].effects.size(); j++) {
1775 Ref<AudioEffect> fx = p_bus_layout->buses[i].effects[j].effect;
1776
1777 if (fx.is_valid()) {
1778 Bus::Effect bfx;
1779 bfx.effect = fx;
1780 bfx.enabled = p_bus_layout->buses[i].effects[j].enabled;
1781 #ifdef DEBUG_ENABLED
1782 bfx.prof_time = 0;
1783 #endif
1784 bus->effects.push_back(bfx);
1785 }
1786 }
1787
1788 bus_map[bus->name] = bus;
1789 buses.write[i] = bus;
1790
1791 buses[i]->channels.resize(channel_count);
1792 for (int j = 0; j < channel_count; j++) {
1793 buses.write[i]->channels.write[j].buffer.resize(buffer_size);
1794 }
1795 _update_bus_effects(i);
1796 }
1797 #ifdef TOOLS_ENABLED
1798 set_edited(false);
1799 #endif
1800 unlock();
1801
1802 // Samples bus sync.
1803 }
1804
1805 Ref<AudioBusLayout> AudioServer::generate_bus_layout() const {
1806 Ref<AudioBusLayout> state;
1807 state.instantiate();
1808
1809 state->buses.resize(buses.size());
1810
1811 for (int i = 0; i < buses.size(); i++) {
1812 state->buses.write[i].name = buses[i]->name;
1813 state->buses.write[i].send = buses[i]->send;
1814 state->buses.write[i].mute = buses[i]->mute;
1815 state->buses.write[i].solo = buses[i]->solo;
1816 state->buses.write[i].bypass = buses[i]->bypass;
1817 state->buses.write[i].volume_db = buses[i]->volume_db;
1818 for (int j = 0; j < buses[i]->effects.size(); j++) {
1819 AudioBusLayout::Bus::Effect fx;
1820 fx.effect = buses[i]->effects[j].effect;
1821 fx.enabled = buses[i]->effects[j].enabled;
1822 state->buses.write[i].effects.push_back(fx);
1823 }
1824 }
1825
1826 return state;
1827 }
1828
1829 PackedStringArray AudioServer::get_output_device_list() {
1830 return AudioDriver::get_singleton()->get_output_device_list();
1831 }
1832
1833 String AudioServer::get_output_device() {
1834 return AudioDriver::get_singleton()->get_output_device();
1835 }
1836
1837 void AudioServer::set_output_device(const String &p_name) {
1838 AudioDriver::get_singleton()->set_output_device(p_name);
1839 }
1840
1841 PackedStringArray AudioServer::get_input_device_list() {
1842 return AudioDriver::get_singleton()->get_input_device_list();
1843 }
1844
1845 String AudioServer::get_input_device() {
1846 return AudioDriver::get_singleton()->get_input_device();
1847 }
1848
1849 void AudioServer::set_input_device(const String &p_name) {
1850 AudioDriver::get_singleton()->set_input_device(p_name);
1851 }
1852
1853 Error AudioServer::set_input_device_active(bool p_is_active) {
1854 if (input_device_active == p_is_active) {
1855 return OK;
1856 }
1857 if (p_is_active) {
1858 if (!GLOBAL_GET("audio/driver/enable_input")) {
1859 WARN_PRINT("You must enable the project setting \"audio/driver/enable_input\" to use audio capture.");
1860 return FAILED;
1861 }
1862
1863 input_buffer_ofs = 0;
1864 input_device_active = true;
1865 return AudioDriver::get_singleton()->input_start();
1866 } else {
1867 input_device_active = false;
1868 return AudioDriver::get_singleton()->input_stop();
1869 }
1870 }
1871
1872 int AudioServer::get_input_frames_available() {
1873 AudioDriver *ad = AudioDriver::get_singleton();
1874 ad->lock();
1875 int64_t input_position = ad->get_input_position();
1876 if (input_position < input_buffer_ofs) {
1877 input_position += ad->get_input_buffer().size();
1878 }
1879 ad->unlock();
1880 return (int)((input_position - input_buffer_ofs) / 2); // Buffer is stereo.
1881 }
1882
1883 int AudioServer::get_input_buffer_length_frames() {
1884 AudioDriver *ad = AudioDriver::get_singleton();
1885 ad->lock();
1886 int buffsize = ad->get_input_buffer().size();
1887 ad->unlock();
1888 return buffsize / 2;
1889 }
1890
1891 PackedVector2Array AudioServer::get_input_frames(int p_frames) {
1892 PackedVector2Array ret;
1893 AudioDriver *ad = AudioDriver::get_singleton();
1894 ad->lock();
1895 int input_position = ad->get_input_position();
1896 Vector<int32_t> buf = ad->get_input_buffer();
1897 if (input_position < input_buffer_ofs) {
1898 input_position += buf.size();
1899 }
1900 if ((input_buffer_ofs + p_frames * 2 <= input_position) && (p_frames >= 0)) {
1901 ret.resize(p_frames);
1902 for (int i = 0; i < p_frames; i++) {
1903 float l = (buf[input_buffer_ofs++] >> 16) / 32768.f;
1904 if (input_buffer_ofs >= buf.size()) {
1905 input_buffer_ofs = 0;
1906 }
1907 float r = (buf[input_buffer_ofs++] >> 16) / 32768.f;
1908 if (input_buffer_ofs >= buf.size()) {
1909 input_buffer_ofs = 0;
1910 }
1911 ret.write[i] = Vector2(l, r);
1912 }
1913 }
1914 ad->unlock();
1915 return ret;
1916 }
1917
1918 void AudioServer::set_enable_tagging_used_audio_streams(bool p_enable) {
1919 tag_used_audio_streams = p_enable;
1920 }
1921
1922 #ifdef TOOLS_ENABLED
1923 void AudioServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
1924 const String pf = p_function;
1925 if ((p_idx == 0 && pf == "get_bus_index") || (p_idx == 1 && pf == "set_bus_send")) {
1926 for (const AudioServer::Bus *E : buses) {
1927 r_options->push_back(String(E->name).quote());
1928 }
1929 }
1930
1931 Object::get_argument_options(p_function, p_idx, r_options);
1932 }
1933 #endif
1934
1935 AudioServer::PlaybackType AudioServer::get_default_playback_type() const {
1936 int playback_type = GLOBAL_GET_CACHED(int, "audio/general/default_playback_type");
1937 ERR_FAIL_COND_V_MSG(
1938 playback_type < 0 || playback_type >= PlaybackType::PLAYBACK_TYPE_MAX,
1939 PlaybackType::PLAYBACK_TYPE_STREAM,
1940 vformat(R"(Project settings value (%s) for "audio/general/default_playback_type" is not supported)", playback_type));
1941
1942 switch (playback_type) {
1943 case 1: {
1944 return PlaybackType::PLAYBACK_TYPE_SAMPLE;
1945 } break;
1946
1947 case 0:
1948 default: {
1949 return PlaybackType::PLAYBACK_TYPE_STREAM;
1950 } break;
1951 }
1952 }
1953
1954 bool AudioServer::is_stream_registered_as_sample(const Ref<AudioStream> &p_stream) {
1955 ERR_FAIL_COND_V_MSG(p_stream.is_null(), false, "Parameter p_stream is null.");
1956 return AudioDriver::get_singleton()->is_stream_registered_as_sample(p_stream);
1957 }
1958
1959 void AudioServer::register_stream_as_sample(const Ref<AudioStream> &p_stream) {
1960 ERR_FAIL_COND_MSG(p_stream.is_null(), "Parameter p_stream is null.");
1961 ERR_FAIL_COND_MSG(!(p_stream->can_be_sampled()), "Parameter p_stream cannot be sampled.");
1962 Ref<AudioSample> sample = p_stream->generate_sample();
1963 register_sample(sample);
1964 }
1965
1966 void AudioServer::unregister_stream_as_sample(const Ref<AudioStream> &p_stream) {
1967 ERR_FAIL_COND_MSG(p_stream.is_null(), "Parameter p_stream is null.");
1968 ERR_FAIL_COND_MSG(!(p_stream->can_be_sampled()), "Parameter p_stream cannot be sampled.");
1969 Ref<AudioSample> sample = p_stream->generate_sample();
1970 unregister_sample(sample);
1971 }
1972
1973 void AudioServer::register_sample(const Ref<AudioSample> &p_sample) {
1974 ERR_FAIL_COND_MSG(p_sample.is_null(), "Parameter p_sample is null.");
1975 ERR_FAIL_COND_MSG(p_sample->stream.is_null(), "Parameter p_sample->stream is null.");
1976 ERR_FAIL_COND_MSG(!(p_sample->stream->can_be_sampled()), "Parameter p_stream cannot be sampled.");
1977 AudioDriver::get_singleton()->register_sample(p_sample);
1978 }
1979
1980 void AudioServer::unregister_sample(const Ref<AudioSample> &p_sample) {
1981 ERR_FAIL_COND_MSG(p_sample.is_null(), "Parameter p_sample is null.");
1982 ERR_FAIL_COND_MSG(p_sample->stream.is_null(), "Parameter p_sample->stream is null.");
1983 AudioDriver::get_singleton()->unregister_sample(p_sample);
1984 }
1985
1986 void AudioServer::start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
1987 ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
1988 AudioDriver::get_singleton()->start_sample_playback(p_playback);
1989 sample_playback_list.ordered_insert(p_playback);
1990 }
1991
1992 void AudioServer::stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
1993 ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
1994 if (!sample_playback_list.has(p_playback)) {
1995 return;
1996 }
1997 sample_playback_list.erase(p_playback);
1998 AudioDriver::get_singleton()->stop_sample_playback(p_playback);
1999 p_playback->stream_playback->set_sample_playback(nullptr);
2000 stop_playback_stream(p_playback->stream_playback);
2001 }
2002
2003 void AudioServer::set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) {
2004 ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
2005 AudioDriver::get_singleton()->set_sample_playback_pause(p_playback, p_paused);
2006 }
2007
2008 bool AudioServer::is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) {
2009 ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null.");
2010 return sample_playback_list.has(p_playback);
2011 }
2012
2013 double AudioServer::get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) {
2014 ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null.");
2015 return AudioDriver::get_singleton()->get_sample_playback_position(p_playback);
2016 }
2017
2018 void AudioServer::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) {
2019 ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
2020 return AudioDriver::get_singleton()->update_sample_playback_pitch_scale(p_playback, p_pitch_scale);
2021 }
2022
2023 void AudioServer::_bind_methods() {
2024 ClassDB::bind_method(D_METHOD("set_bus_count", "amount"), &AudioServer::set_bus_count);
2025 ClassDB::bind_method(D_METHOD("get_bus_count"), &AudioServer::get_bus_count);
2026
2027 ClassDB::bind_method(D_METHOD("remove_bus", "index"), &AudioServer::remove_bus);
2028 ClassDB::bind_method(D_METHOD("add_bus", "at_position"), &AudioServer::add_bus, DEFVAL(-1));
2029 ClassDB::bind_method(D_METHOD("move_bus", "index", "to_index"), &AudioServer::move_bus);
2030
2031 ClassDB::bind_method(D_METHOD("set_bus_name", "bus_idx", "name"), &AudioServer::set_bus_name);
2032 ClassDB::bind_method(D_METHOD("get_bus_name", "bus_idx"), &AudioServer::get_bus_name);
2033 ClassDB::bind_method(D_METHOD("get_bus_index", "bus_name"), &AudioServer::get_bus_index);
2034
2035 ClassDB::bind_method(D_METHOD("get_bus_channels", "bus_idx"), &AudioServer::get_bus_channels);
2036
2037 ClassDB::bind_method(D_METHOD("set_bus_volume_db", "bus_idx", "volume_db"), &AudioServer::set_bus_volume_db);
2038 ClassDB::bind_method(D_METHOD("get_bus_volume_db", "bus_idx"), &AudioServer::get_bus_volume_db);
2039
2040 ClassDB::bind_method(D_METHOD("set_bus_volume_linear", "bus_idx", "volume_linear"), &AudioServer::set_bus_volume_linear);
2041 ClassDB::bind_method(D_METHOD("get_bus_volume_linear", "bus_idx"), &AudioServer::get_bus_volume_linear);
2042
2043 ClassDB::bind_method(D_METHOD("set_bus_send", "bus_idx", "send"), &AudioServer::set_bus_send);
2044 ClassDB::bind_method(D_METHOD("get_bus_send", "bus_idx"), &AudioServer::get_bus_send);
2045
2046 ClassDB::bind_method(D_METHOD("set_bus_solo", "bus_idx", "enable"), &AudioServer::set_bus_solo);
2047 ClassDB::bind_method(D_METHOD("is_bus_solo", "bus_idx"), &AudioServer::is_bus_solo);
2048
2049 ClassDB::bind_method(D_METHOD("set_bus_mute", "bus_idx", "enable"), &AudioServer::set_bus_mute);
2050 ClassDB::bind_method(D_METHOD("is_bus_mute", "bus_idx"), &AudioServer::is_bus_mute);
2051
2052 ClassDB::bind_method(D_METHOD("set_bus_bypass_effects", "bus_idx", "enable"), &AudioServer::set_bus_bypass_effects);
2053 ClassDB::bind_method(D_METHOD("is_bus_bypassing_effects", "bus_idx"), &AudioServer::is_bus_bypassing_effects);
2054
2055 ClassDB::bind_method(D_METHOD("add_bus_effect", "bus_idx", "effect", "at_position"), &AudioServer::add_bus_effect, DEFVAL(-1));
2056 ClassDB::bind_method(D_METHOD("remove_bus_effect", "bus_idx", "effect_idx"), &AudioServer::remove_bus_effect);
2057
2058 ClassDB::bind_method(D_METHOD("get_bus_effect_count", "bus_idx"), &AudioServer::get_bus_effect_count);
2059 ClassDB::bind_method(D_METHOD("get_bus_effect", "bus_idx", "effect_idx"), &AudioServer::get_bus_effect);
2060 ClassDB::bind_method(D_METHOD("get_bus_effect_instance", "bus_idx", "effect_idx", "channel"), &AudioServer::get_bus_effect_instance, DEFVAL(0));
2061 ClassDB::bind_method(D_METHOD("swap_bus_effects", "bus_idx", "effect_idx", "by_effect_idx"), &AudioServer::swap_bus_effects);
2062
2063 ClassDB::bind_method(D_METHOD("set_bus_effect_enabled", "bus_idx", "effect_idx", "enabled"), &AudioServer::set_bus_effect_enabled);
2064 ClassDB::bind_method(D_METHOD("is_bus_effect_enabled", "bus_idx", "effect_idx"), &AudioServer::is_bus_effect_enabled);
2065
2066 ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
2067 ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
2068
2069 ClassDB::bind_method(D_METHOD("set_playback_speed_scale", "scale"), &AudioServer::set_playback_speed_scale);
2070 ClassDB::bind_method(D_METHOD("get_playback_speed_scale"), &AudioServer::get_playback_speed_scale);
2071
2072 ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
2073 ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
2074
2075 ClassDB::bind_method(D_METHOD("get_speaker_mode"), &AudioServer::get_speaker_mode);
2076 ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioServer::get_mix_rate);
2077 ClassDB::bind_method(D_METHOD("get_input_mix_rate"), &AudioServer::get_input_mix_rate);
2078
2079 ClassDB::bind_method(D_METHOD("get_driver_name"), &AudioServer::get_driver_name);
2080
2081 ClassDB::bind_method(D_METHOD("get_output_device_list"), &AudioServer::get_output_device_list);
2082 ClassDB::bind_method(D_METHOD("get_output_device"), &AudioServer::get_output_device);
2083 ClassDB::bind_method(D_METHOD("set_output_device", "name"), &AudioServer::set_output_device);
2084
2085 ClassDB::bind_method(D_METHOD("get_time_to_next_mix"), &AudioServer::get_time_to_next_mix);
2086 ClassDB::bind_method(D_METHOD("get_time_since_last_mix"), &AudioServer::get_time_since_last_mix);
2087 ClassDB::bind_method(D_METHOD("get_output_latency"), &AudioServer::get_output_latency);
2088
2089 ClassDB::bind_method(D_METHOD("get_input_device_list"), &AudioServer::get_input_device_list);
2090 ClassDB::bind_method(D_METHOD("get_input_device"), &AudioServer::get_input_device);
2091 ClassDB::bind_method(D_METHOD("set_input_device", "name"), &AudioServer::set_input_device);
2092 ClassDB::bind_method(D_METHOD("set_input_device_active", "active"), &AudioServer::set_input_device_active);
2093 ClassDB::bind_method(D_METHOD("get_input_frames_available"), &AudioServer::get_input_frames_available);
2094 ClassDB::bind_method(D_METHOD("get_input_buffer_length_frames"), &AudioServer::get_input_buffer_length_frames);
2095 ClassDB::bind_method(D_METHOD("get_input_frames", "frames"), &AudioServer::get_input_frames);
2096
2097 ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout);
2098 ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout);
2099
2100 ClassDB::bind_method(D_METHOD("set_enable_tagging_used_audio_streams", "enable"), &AudioServer::set_enable_tagging_used_audio_streams);
2101
2102 ClassDB::bind_method(D_METHOD("is_stream_registered_as_sample", "stream"), &AudioServer::is_stream_registered_as_sample);
2103 ClassDB::bind_method(D_METHOD("register_stream_as_sample", "stream"), &AudioServer::register_stream_as_sample);
2104
2105 ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
2106 ADD_PROPERTY(PropertyInfo(Variant::STRING, "output_device"), "set_output_device", "get_output_device");
2107 ADD_PROPERTY(PropertyInfo(Variant::STRING, "input_device"), "set_input_device", "get_input_device");
2108 // The default value may be set to an empty string by the platform-specific audio driver.
2109 // Override for class reference generation purposes.
2110 ADD_PROPERTY_DEFAULT("input_device", "Default");
2111 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed_scale"), "set_playback_speed_scale", "get_playback_speed_scale");
2112
2113 ADD_SIGNAL(MethodInfo("bus_layout_changed"));
2114 ADD_SIGNAL(MethodInfo("bus_renamed", PropertyInfo(Variant::INT, "bus_index"), PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
2115
2116 BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
2117 BIND_ENUM_CONSTANT(SPEAKER_SURROUND_31);
2118 BIND_ENUM_CONSTANT(SPEAKER_SURROUND_51);
2119 BIND_ENUM_CONSTANT(SPEAKER_SURROUND_71);
2120
2121 BIND_ENUM_CONSTANT(PLAYBACK_TYPE_DEFAULT);
2122 BIND_ENUM_CONSTANT(PLAYBACK_TYPE_STREAM);
2123 BIND_ENUM_CONSTANT(PLAYBACK_TYPE_SAMPLE);
2124 BIND_ENUM_CONSTANT(PLAYBACK_TYPE_MAX);
2125 }
2126
2127 AudioServer::AudioServer() {
2128 singleton = this;
2129 }
2130
2131 AudioServer::~AudioServer() {
2132 // Cleanup resources while we still have an active AudioServer singleton,
2133 // for resources that depend on the singleton still existing.
2134 _cleanup_lists();
2135
2136 singleton = nullptr;
2137 }
2138
2139 /////////////////////////////////
2140
2141 bool AudioBusLayout::_set(const StringName &p_name, const Variant &p_value) {
2142 String s = p_name;
2143 if (s.begins_with("bus/")) {
2144 int index = s.get_slicec('/', 1).to_int();
2145 if (buses.size() <= index) {
2146 buses.resize(index + 1);
2147 }
2148
2149 Bus &bus = buses.write[index];
2150
2151 String what = s.get_slicec('/', 2);
2152
2153 if (what == "name") {
2154 bus.name = p_value;
2155 } else if (what == "solo") {
2156 bus.solo = p_value;
2157 } else if (what == "mute") {
2158 bus.mute = p_value;
2159 } else if (what == "bypass_fx") {
2160 bus.bypass = p_value;
2161 } else if (what == "volume_db") {
2162 bus.volume_db = p_value;
2163 } else if (what == "send") {
2164 bus.send = p_value;
2165 } else if (what == "effect") {
2166 int which = s.get_slicec('/', 3).to_int();
2167 if (bus.effects.size() <= which) {
2168 bus.effects.resize(which + 1);
2169 }
2170
2171 Bus::Effect &fx = bus.effects.write[which];
2172
2173 String fxwhat = s.get_slicec('/', 4);
2174 if (fxwhat == "effect") {
2175 fx.effect = p_value;
2176 } else if (fxwhat == "enabled") {
2177 fx.enabled = p_value;
2178 } else {
2179 return false;
2180 }
2181
2182 return true;
2183 } else {
2184 return false;
2185 }
2186
2187 return true;
2188 }
2189
2190 return false;
2191 }
2192
2193 bool AudioBusLayout::_get(const StringName &p_name, Variant &r_ret) const {
2194 String s = p_name;
2195 if (s.begins_with("bus/")) {
2196 int index = s.get_slicec('/', 1).to_int();
2197 if (index < 0 || index >= buses.size()) {
2198 return false;
2199 }
2200
2201 const Bus &bus = buses[index];
2202
2203 String what = s.get_slicec('/', 2);
2204
2205 if (what == "name") {
2206 r_ret = bus.name;
2207 } else if (what == "solo") {
2208 r_ret = bus.solo;
2209 } else if (what == "mute") {
2210 r_ret = bus.mute;
2211 } else if (what == "bypass_fx") {
2212 r_ret = bus.bypass;
2213 } else if (what == "volume_db") {
2214 r_ret = bus.volume_db;
2215 } else if (what == "send") {
2216 r_ret = bus.send;
2217 } else if (what == "effect") {
2218 int which = s.get_slicec('/', 3).to_int();
2219 if (which < 0 || which >= bus.effects.size()) {
2220 return false;
2221 }
2222
2223 const Bus::Effect &fx = bus.effects[which];
2224
2225 String fxwhat = s.get_slicec('/', 4);
2226 if (fxwhat == "effect") {
2227 r_ret = fx.effect;
2228 } else if (fxwhat == "enabled") {
2229 r_ret = fx.enabled;
2230 } else {
2231 return false;
2232 }
2233
2234 return true;
2235 } else {
2236 return false;
2237 }
2238
2239 return true;
2240 }
2241
2242 return false;
2243 }
2244
2245 void AudioBusLayout::_get_property_list(List<PropertyInfo> *p_list) const {
2246 for (int i = 0; i < buses.size(); i++) {
2247 p_list->push_back(PropertyInfo(Variant::STRING, "bus/" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2248 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/solo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2249 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/mute", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2250 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/bypass_fx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2251 p_list->push_back(PropertyInfo(Variant::FLOAT, "bus/" + itos(i) + "/volume_db", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2252 p_list->push_back(PropertyInfo(Variant::FLOAT, "bus/" + itos(i) + "/send", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2253
2254 for (int j = 0; j < buses[i].effects.size(); j++) {
2255 p_list->push_back(PropertyInfo(Variant::OBJECT, "bus/" + itos(i) + "/effect/" + itos(j) + "/effect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2256 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/effect/" + itos(j) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
2257 }
2258 }
2259 }
2260
2261 AudioBusLayout::AudioBusLayout() {
2262 buses.resize(1);
2263 buses.write[0].name = SceneStringName(Master);
2264 }