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/file_access.h"
37#include "core/io/resource_loader.h"
38#include "core/math/audio_frame.h"
39#include "core/os/os.h"
40#include "core/string/string_name.h"
41#include "core/templates/pair.h"
42#include "scene/resources/audio_stream_wav.h"
43#include "scene/scene_string_names.h"
44#include "servers/audio/audio_driver_dummy.h"
45#include "servers/audio/effects/audio_effect_compressor.h"
46
47#include <cstring>
48
49#ifdef TOOLS_ENABLED
50#define MARK_EDITED set_edited(true);
51#else
52#define MARK_EDITED
53#endif
54
55AudioDriver *AudioDriver::singleton = nullptr;
56AudioDriver *AudioDriver::get_singleton() {
57 return singleton;
58}
59
60void AudioDriver::set_singleton() {
61 singleton = this;
62}
63
64void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) {
65 if (p_update_mix_time) {
66 update_mix_time(p_frames);
67 }
68
69 if (AudioServer::get_singleton()) {
70 AudioServer::get_singleton()->_driver_process(p_frames, p_buffer);
71 }
72}
73
74void AudioDriver::update_mix_time(int p_frames) {
75 _last_mix_frames = p_frames;
76 if (OS::get_singleton()) {
77 _last_mix_time = OS::get_singleton()->get_ticks_usec();
78 }
79}
80
81double AudioDriver::get_time_since_last_mix() {
82 lock();
83 uint64_t last_mix_time = _last_mix_time;
84 unlock();
85 return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
86}
87
88double AudioDriver::get_time_to_next_mix() {
89 lock();
90 uint64_t last_mix_time = _last_mix_time;
91 uint64_t last_mix_frames = _last_mix_frames;
92 unlock();
93 double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
94 double mix_buffer = last_mix_frames / (double)get_mix_rate();
95 return mix_buffer - total;
96}
97
98void AudioDriver::input_buffer_init(int driver_buffer_frames) {
99 const int input_buffer_channels = 2;
100 input_buffer.resize(driver_buffer_frames * input_buffer_channels * 4);
101 input_position = 0;
102 input_size = 0;
103}
104
105void AudioDriver::input_buffer_write(int32_t sample) {
106 if ((int)input_position < input_buffer.size()) {
107 input_buffer.write[input_position++] = sample;
108 if ((int)input_position >= input_buffer.size()) {
109 input_position = 0;
110 }
111 if ((int)input_size < input_buffer.size()) {
112 input_size++;
113 }
114 } else {
115 WARN_PRINT("input_buffer_write: Invalid input_position=" + itos(input_position) + " input_buffer.size()=" + itos(input_buffer.size()));
116 }
117}
118
119int 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 // In the case of invalid mix rate, let's default to a sensible value..
124 if (mix_rate <= 0) {
125 WARN_PRINT(vformat("Invalid mix rate of %d, consider reassigning setting \'%s\'. \nDefaulting mix rate to value %d.",
126 mix_rate, audio_driver_setting, AudioDriverManager::DEFAULT_MIX_RATE));
127 mix_rate = AudioDriverManager::DEFAULT_MIX_RATE;
128 }
129
130 return mix_rate;
131}
132
133AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const {
134 switch (p_channels) {
135 case 4:
136 return SPEAKER_SURROUND_31;
137 case 6:
138 return SPEAKER_SURROUND_51;
139 case 8:
140 return SPEAKER_SURROUND_71;
141 }
142
143 // Default to STEREO
144 return SPEAKER_MODE_STEREO;
145}
146
147int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_mode) const {
148 switch (p_mode) {
149 case SPEAKER_MODE_STEREO:
150 return 2;
151 case SPEAKER_SURROUND_31:
152 return 4;
153 case SPEAKER_SURROUND_51:
154 return 6;
155 case SPEAKER_SURROUND_71:
156 return 8;
157 }
158
159 ERR_FAIL_V(2);
160}
161
162PackedStringArray AudioDriver::get_output_device_list() {
163 PackedStringArray list;
164
165 list.push_back("Default");
166
167 return list;
168}
169
170String AudioDriver::get_output_device() {
171 return "Default";
172}
173
174PackedStringArray AudioDriver::get_input_device_list() {
175 PackedStringArray list;
176
177 list.push_back("Default");
178
179 return list;
180}
181
182AudioDriverDummy AudioDriverManager::dummy_driver;
183AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS] = {
184 &AudioDriverManager::dummy_driver,
185};
186int AudioDriverManager::driver_count = 1;
187
188void AudioDriverManager::add_driver(AudioDriver *p_driver) {
189 ERR_FAIL_COND(driver_count >= MAX_DRIVERS);
190 drivers[driver_count - 1] = p_driver;
191
192 // Last driver is always our dummy driver
193 drivers[driver_count++] = &AudioDriverManager::dummy_driver;
194}
195
196int AudioDriverManager::get_driver_count() {
197 return driver_count;
198}
199
200void AudioDriverManager::initialize(int p_driver) {
201 GLOBAL_DEF_RST("audio/driver/enable_input", false);
202 GLOBAL_DEF_RST("audio/driver/mix_rate", DEFAULT_MIX_RATE);
203 GLOBAL_DEF_RST("audio/driver/mix_rate.web", 0); // Safer default output_latency for web (use browser default).
204
205 int failed_driver = -1;
206
207 // Check if there is a selected driver
208 if (p_driver >= 0 && p_driver < driver_count) {
209 if (drivers[p_driver]->init() == OK) {
210 drivers[p_driver]->set_singleton();
211 return;
212 } else {
213 failed_driver = p_driver;
214 }
215 }
216
217 // No selected driver, try them all in order
218 for (int i = 0; i < driver_count; i++) {
219 // Don't re-init the driver if it failed above
220 if (i == failed_driver) {
221 continue;
222 }
223
224 if (drivers[i]->init() == OK) {
225 drivers[i]->set_singleton();
226 break;
227 }
228 }
229
230 if (driver_count > 1 && String(AudioDriver::get_singleton()->get_name()) == "Dummy") {
231 WARN_PRINT("All audio drivers failed, falling back to the dummy driver.");
232 }
233}
234
235AudioDriver *AudioDriverManager::get_driver(int p_driver) {
236 ERR_FAIL_INDEX_V(p_driver, driver_count, nullptr);
237 return drivers[p_driver];
238}
239
240//////////////////////////////////////////////
241//////////////////////////////////////////////
242//////////////////////////////////////////////
243//////////////////////////////////////////////
244
245void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
246 mix_count++;
247 int todo = p_frames;
248
249#ifdef DEBUG_ENABLED
250 uint64_t prof_ticks = OS::get_singleton()->get_ticks_usec();
251#endif
252
253 if (channel_count != get_channel_count()) {
254 // Amount of channels changed due to a output_device change
255 // reinitialize the buses channels and buffers
256 init_channels_and_buffers();
257 }
258
259 ERR_FAIL_COND_MSG(buses.is_empty() && todo, "AudioServer bus count is less than 1.");
260 while (todo) {
261 if (to_mix == 0) {
262 _mix_step();
263 }
264
265 int to_copy = MIN(to_mix, todo);
266
267 Bus *master = buses[0];
268
269 int from = buffer_size - to_mix;
270 int from_buf = p_frames - todo;
271
272 //master master, send to output
273 int cs = master->channels.size();
274 for (int k = 0; k < cs; k++) {
275 if (master->channels[k].active) {
276 const AudioFrame *buf = master->channels[k].buffer.ptr();
277
278 for (int j = 0; j < to_copy; j++) {
279 float l = CLAMP(buf[from + j].l, -1.0, 1.0);
280 int32_t vl = l * ((1 << 20) - 1);
281 int32_t vl2 = (vl < 0 ? -1 : 1) * (ABS(vl) << 11);
282 p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 0] = vl2;
283
284 float r = CLAMP(buf[from + j].r, -1.0, 1.0);
285 int32_t vr = r * ((1 << 20) - 1);
286 int32_t vr2 = (vr < 0 ? -1 : 1) * (ABS(vr) << 11);
287 p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 1] = vr2;
288 }
289
290 } else {
291 for (int j = 0; j < to_copy; j++) {
292 p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 0] = 0;
293 p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 1] = 0;
294 }
295 }
296 }
297
298 todo -= to_copy;
299 to_mix -= to_copy;
300 }
301
302#ifdef DEBUG_ENABLED
303 prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks;
304#endif
305}
306
307void AudioServer::_mix_step() {
308 bool solo_mode = false;
309
310 for (int i = 0; i < buses.size(); i++) {
311 Bus *bus = buses[i];
312 bus->index_cache = i; //might be moved around by editor, so..
313 for (int k = 0; k < bus->channels.size(); k++) {
314 bus->channels.write[k].used = false;
315 }
316
317 if (bus->solo) {
318 //solo chain
319 solo_mode = true;
320 bus->soloed = true;
321 do {
322 if (bus != buses[0]) {
323 //everything has a send save for master bus
324 if (!bus_map.has(bus->send)) {
325 bus = buses[0]; //send to master
326 } else {
327 int prev_index_cache = bus->index_cache;
328 bus = bus_map[bus->send];
329 if (prev_index_cache >= bus->index_cache) { //invalid, send to master
330 bus = buses[0];
331 }
332 }
333
334 bus->soloed = true;
335 } else {
336 bus = nullptr;
337 }
338
339 } while (bus);
340 } else {
341 bus->soloed = false;
342 }
343 }
344 for (CallbackItem *ci : mix_callback_list) {
345 ci->callback(ci->userdata);
346 }
347
348 for (AudioStreamPlaybackListNode *playback : playback_list) {
349 // Paused streams are no-ops. Don't even mix audio from the stream playback.
350 if (playback->state.load() == AudioStreamPlaybackListNode::PAUSED) {
351 continue;
352 }
353
354 bool fading_out = playback->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION || playback->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE;
355
356 AudioFrame *buf = mix_buffer.ptrw();
357
358 // Copy the lookeahead buffer into the mix buffer.
359 for (int i = 0; i < LOOKAHEAD_BUFFER_SIZE; i++) {
360 buf[i] = playback->lookahead[i];
361 }
362
363 // Mix the audio stream
364 unsigned int mixed_frames = playback->stream_playback->mix(&buf[LOOKAHEAD_BUFFER_SIZE], playback->pitch_scale.get(), buffer_size);
365
366 if (tag_used_audio_streams && playback->stream_playback->is_playing()) {
367 playback->stream_playback->tag_used_streams();
368 }
369
370 if (mixed_frames != buffer_size) {
371 // We know we have at least the size of our lookahead buffer for fade-out purposes.
372
373 float fadeout_base = 0.94;
374 float fadeout_coefficient = 1;
375 static_assert(LOOKAHEAD_BUFFER_SIZE == 64, "Update fadeout_base and comment here if you change LOOKAHEAD_BUFFER_SIZE.");
376 // 0.94 ^ 64 = 0.01906. There might still be a pop but it'll be way better than if we didn't do this.
377 for (unsigned int idx = mixed_frames; idx < buffer_size; idx++) {
378 fadeout_coefficient *= fadeout_base;
379 buf[idx] *= fadeout_coefficient;
380 }
381 AudioStreamPlaybackListNode::PlaybackState new_state;
382 new_state = AudioStreamPlaybackListNode::AWAITING_DELETION;
383 playback->state.store(new_state);
384 } else {
385 // Move the last little bit of what we just mixed into our lookahead buffer.
386 for (int i = 0; i < LOOKAHEAD_BUFFER_SIZE; i++) {
387 playback->lookahead[i] = buf[buffer_size + i];
388 }
389 }
390
391 AudioStreamPlaybackBusDetails *ptr = playback->bus_details.load();
392 ERR_FAIL_COND(ptr == nullptr);
393 // By putting null into the bus details pointers, we're taking ownership of their memory for the duration of this mix.
394 AudioStreamPlaybackBusDetails bus_details = *ptr;
395
396 // Mix to any active buses.
397 for (int idx = 0; idx < MAX_BUSES_PER_PLAYBACK; idx++) {
398 if (!bus_details.bus_active[idx]) {
399 continue;
400 }
401 int bus_idx = thread_find_bus_index(bus_details.bus[idx]);
402
403 int prev_bus_idx = -1;
404 for (int search_idx = 0; search_idx < MAX_BUSES_PER_PLAYBACK; search_idx++) {
405 if (!playback->prev_bus_details->bus_active[search_idx]) {
406 continue;
407 }
408 if (playback->prev_bus_details->bus[search_idx].hash() == bus_details.bus[idx].hash()) {
409 prev_bus_idx = search_idx;
410 }
411 }
412
413 for (int channel_idx = 0; channel_idx < channel_count; channel_idx++) {
414 AudioFrame *channel_buf = thread_get_channel_mix_buffer(bus_idx, channel_idx);
415 if (fading_out) {
416 bus_details.volume[idx][channel_idx] = AudioFrame(0, 0);
417 }
418 AudioFrame channel_vol = bus_details.volume[idx][channel_idx];
419
420 AudioFrame prev_channel_vol = AudioFrame(0, 0);
421 if (prev_bus_idx != -1) {
422 prev_channel_vol = playback->prev_bus_details->volume[prev_bus_idx][channel_idx];
423 }
424 _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]);
425 }
426 }
427
428 // Now go through and fade-out any buses that were being played to previously that we missed by going through current data.
429 for (int idx = 0; idx < MAX_BUSES_PER_PLAYBACK; idx++) {
430 if (!playback->prev_bus_details->bus_active[idx]) {
431 continue;
432 }
433 int bus_idx = thread_find_bus_index(playback->prev_bus_details->bus[idx]);
434
435 int current_bus_idx = -1;
436 for (int search_idx = 0; search_idx < MAX_BUSES_PER_PLAYBACK; search_idx++) {
437 if (bus_details.bus[search_idx] == playback->prev_bus_details->bus[idx]) {
438 current_bus_idx = search_idx;
439 }
440 }
441 if (current_bus_idx != -1) {
442 // If we found a corresponding bus in the current bus assignments, we've already mixed to this bus.
443 continue;
444 }
445
446 for (int channel_idx = 0; channel_idx < channel_count; channel_idx++) {
447 AudioFrame *channel_buf = thread_get_channel_mix_buffer(bus_idx, channel_idx);
448 AudioFrame prev_channel_vol = playback->prev_bus_details->volume[idx][channel_idx];
449 // Fade out to silence
450 _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]);
451 }
452 }
453
454 // Copy the bus details we mixed with to the previous bus details to maintain volume ramps.
455 std::copy(std::begin(bus_details.bus_active), std::end(bus_details.bus_active), std::begin(playback->prev_bus_details->bus_active));
456 std::copy(std::begin(bus_details.bus), std::end(bus_details.bus), std::begin(playback->prev_bus_details->bus));
457 for (int bus_idx = 0; bus_idx < MAX_BUSES_PER_PLAYBACK; bus_idx++) {
458 std::copy(std::begin(bus_details.volume[bus_idx]), std::end(bus_details.volume[bus_idx]), std::begin(playback->prev_bus_details->volume[bus_idx]));
459 }
460
461 switch (playback->state.load()) {
462 case AudioStreamPlaybackListNode::AWAITING_DELETION:
463 case AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION:
464 playback_list.erase(playback, [](AudioStreamPlaybackListNode *p) {
465 delete p->prev_bus_details;
466 delete p->bus_details;
467 p->stream_playback.unref();
468 delete p;
469 });
470 break;
471 case AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE: {
472 // Pause the stream.
473 AudioStreamPlaybackListNode::PlaybackState old_state, new_state;
474 do {
475 old_state = playback->state.load();
476 new_state = AudioStreamPlaybackListNode::PAUSED;
477 } while (!playback->state.compare_exchange_strong(/* expected= */ old_state, new_state));
478 } break;
479 case AudioStreamPlaybackListNode::PLAYING:
480 case AudioStreamPlaybackListNode::PAUSED:
481 // No-op!
482 break;
483 }
484 }
485
486 for (int i = buses.size() - 1; i >= 0; i--) {
487 //go bus by bus
488 Bus *bus = buses[i];
489
490 for (int k = 0; k < bus->channels.size(); k++) {
491 if (bus->channels[k].active && !bus->channels[k].used) {
492 //buffer was not used, but it's still active, so it must be cleaned
493 AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
494
495 for (uint32_t j = 0; j < buffer_size; j++) {
496 buf[j] = AudioFrame(0, 0);
497 }
498 }
499 }
500
501 //process effects
502 if (!bus->bypass) {
503 for (int j = 0; j < bus->effects.size(); j++) {
504 if (!bus->effects[j].enabled) {
505 continue;
506 }
507
508#ifdef DEBUG_ENABLED
509 uint64_t ticks = OS::get_singleton()->get_ticks_usec();
510#endif
511
512 for (int k = 0; k < bus->channels.size(); k++) {
513 if (!(bus->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
514 continue;
515 }
516 bus->channels.write[k].effect_instances.write[j]->process(bus->channels[k].buffer.ptr(), temp_buffer.write[k].ptrw(), buffer_size);
517 }
518
519 //swap buffers, so internal buffer always has the right data
520 for (int k = 0; k < bus->channels.size(); k++) {
521 if (!(buses[i]->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
522 continue;
523 }
524 SWAP(bus->channels.write[k].buffer, temp_buffer.write[k]);
525 }
526
527#ifdef DEBUG_ENABLED
528 bus->effects.write[j].prof_time += OS::get_singleton()->get_ticks_usec() - ticks;
529#endif
530 }
531 }
532
533 //process send
534
535 Bus *send = nullptr;
536
537 if (i > 0) {
538 //everything has a send save for master bus
539 if (!bus_map.has(bus->send)) {
540 send = buses[0];
541 } else {
542 send = bus_map[bus->send];
543 if (send->index_cache >= bus->index_cache) { //invalid, send to master
544 send = buses[0];
545 }
546 }
547 }
548
549 for (int k = 0; k < bus->channels.size(); k++) {
550 if (!bus->channels[k].active) {
551 bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
552 continue;
553 }
554
555 AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
556
557 AudioFrame peak = AudioFrame(0, 0);
558
559 float volume = Math::db_to_linear(bus->volume_db);
560
561 if (solo_mode) {
562 if (!bus->soloed) {
563 volume = 0.0;
564 }
565 } else {
566 if (bus->mute) {
567 volume = 0.0;
568 }
569 }
570
571 //apply volume and compute peak
572 for (uint32_t j = 0; j < buffer_size; j++) {
573 buf[j] *= volume;
574
575 float l = ABS(buf[j].l);
576 if (l > peak.l) {
577 peak.l = l;
578 }
579 float r = ABS(buf[j].r);
580 if (r > peak.r) {
581 peak.r = r;
582 }
583 }
584
585 bus->channels.write[k].peak_volume = AudioFrame(Math::linear_to_db(peak.l + AUDIO_PEAK_OFFSET), Math::linear_to_db(peak.r + AUDIO_PEAK_OFFSET));
586
587 if (!bus->channels[k].used) {
588 //see if any audio is contained, because channel was not used
589
590 if (MAX(peak.r, peak.l) > Math::db_to_linear(channel_disable_threshold_db)) {
591 bus->channels.write[k].last_mix_with_audio = mix_frames;
592 } else if (mix_frames - bus->channels[k].last_mix_with_audio > channel_disable_frames) {
593 bus->channels.write[k].active = false;
594 continue; //went inactive, don't mix.
595 }
596 }
597
598 if (send) {
599 //if not master bus, send
600 AudioFrame *target_buf = thread_get_channel_mix_buffer(send->index_cache, k);
601
602 for (uint32_t j = 0; j < buffer_size; j++) {
603 target_buf[j] += buf[j];
604 }
605 }
606 }
607 }
608
609 mix_frames += buffer_size;
610 to_mix = buffer_size;
611}
612
613void 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) {
614 if (p_highshelf_gain != 0) {
615 AudioFilterSW filter;
616 filter.set_mode(AudioFilterSW::HIGHSHELF);
617 filter.set_sampling_rate(AudioServer::get_singleton()->get_mix_rate());
618 filter.set_cutoff(p_attenuation_filter_cutoff_hz);
619 filter.set_resonance(1);
620 filter.set_stages(1);
621 filter.set_gain(p_highshelf_gain);
622
623 ERR_FAIL_COND(p_processor_l == nullptr);
624 ERR_FAIL_COND(p_processor_r == nullptr);
625
626 bool is_just_started = p_vol_start.l == 0 && p_vol_start.r == 0;
627 p_processor_l->set_filter(&filter, /* clear_history= */ is_just_started);
628 p_processor_l->update_coeffs(buffer_size);
629 p_processor_r->set_filter(&filter, /* clear_history= */ is_just_started);
630 p_processor_r->update_coeffs(buffer_size);
631
632 for (unsigned int frame_idx = 0; frame_idx < buffer_size; frame_idx++) {
633 // Make this buffer size invariant if buffer_size ever becomes a project setting.
634 float lerp_param = (float)frame_idx / buffer_size;
635 AudioFrame vol = p_vol_final * lerp_param + (1 - lerp_param) * p_vol_start;
636 AudioFrame mixed = vol * p_source_buf[frame_idx];
637 p_processor_l->process_one_interp(mixed.l);
638 p_processor_r->process_one_interp(mixed.r);
639 p_out_buf[frame_idx] += mixed;
640 }
641
642 } else {
643 for (unsigned int frame_idx = 0; frame_idx < buffer_size; frame_idx++) {
644 // Make this buffer size invariant if buffer_size ever becomes a project setting.
645 float lerp_param = (float)frame_idx / buffer_size;
646 p_out_buf[frame_idx] += (p_vol_final * lerp_param + (1 - lerp_param) * p_vol_start) * p_source_buf[frame_idx];
647 }
648 }
649}
650
651AudioServer::AudioStreamPlaybackListNode *AudioServer::_find_playback_list_node(Ref<AudioStreamPlayback> p_playback) {
652 for (AudioStreamPlaybackListNode *playback_list_node : playback_list) {
653 if (playback_list_node->stream_playback == p_playback) {
654 return playback_list_node;
655 }
656 }
657 return nullptr;
658}
659
660bool AudioServer::thread_has_channel_mix_buffer(int p_bus, int p_buffer) const {
661 if (p_bus < 0 || p_bus >= buses.size()) {
662 return false;
663 }
664 if (p_buffer < 0 || p_buffer >= buses[p_bus]->channels.size()) {
665 return false;
666 }
667 return true;
668}
669
670AudioFrame *AudioServer::thread_get_channel_mix_buffer(int p_bus, int p_buffer) {
671 ERR_FAIL_INDEX_V(p_bus, buses.size(), nullptr);
672 ERR_FAIL_INDEX_V(p_buffer, buses[p_bus]->channels.size(), nullptr);
673
674 AudioFrame *data = buses.write[p_bus]->channels.write[p_buffer].buffer.ptrw();
675
676 if (!buses[p_bus]->channels[p_buffer].used) {
677 buses.write[p_bus]->channels.write[p_buffer].used = true;
678 buses.write[p_bus]->channels.write[p_buffer].active = true;
679 buses.write[p_bus]->channels.write[p_buffer].last_mix_with_audio = mix_frames;
680 for (uint32_t i = 0; i < buffer_size; i++) {
681 data[i] = AudioFrame(0, 0);
682 }
683 }
684
685 return data;
686}
687
688int AudioServer::thread_get_mix_buffer_size() const {
689 return buffer_size;
690}
691
692int AudioServer::thread_find_bus_index(const StringName &p_name) {
693 if (bus_map.has(p_name)) {
694 return bus_map[p_name]->index_cache;
695 } else {
696 return 0;
697 }
698}
699
700void AudioServer::set_bus_count(int p_count) {
701 ERR_FAIL_COND(p_count < 1);
702 ERR_FAIL_INDEX(p_count, 256);
703
704 MARK_EDITED
705
706 lock();
707 int cb = buses.size();
708
709 if (p_count < buses.size()) {
710 for (int i = p_count; i < buses.size(); i++) {
711 bus_map.erase(buses[i]->name);
712 memdelete(buses[i]);
713 }
714 }
715
716 buses.resize(p_count);
717
718 for (int i = cb; i < buses.size(); i++) {
719 String attempt = "New Bus";
720 int attempts = 1;
721 while (true) {
722 bool name_free = true;
723 for (int j = 0; j < i; j++) {
724 if (buses[j]->name == attempt) {
725 name_free = false;
726 break;
727 }
728 }
729
730 if (!name_free) {
731 attempts++;
732 attempt = "New Bus " + itos(attempts);
733 } else {
734 break;
735 }
736 }
737
738 buses.write[i] = memnew(Bus);
739 buses.write[i]->channels.resize(channel_count);
740 for (int j = 0; j < channel_count; j++) {
741 buses.write[i]->channels.write[j].buffer.resize(buffer_size);
742 }
743 buses[i]->name = attempt;
744 buses[i]->solo = false;
745 buses[i]->mute = false;
746 buses[i]->bypass = false;
747 buses[i]->volume_db = 0;
748 if (i > 0) {
749 buses[i]->send = SceneStringNames::get_singleton()->Master;
750 }
751
752 bus_map[attempt] = buses[i];
753 }
754
755 unlock();
756
757 emit_signal(SNAME("bus_layout_changed"));
758}
759
760void AudioServer::remove_bus(int p_index) {
761 ERR_FAIL_INDEX(p_index, buses.size());
762 ERR_FAIL_COND(p_index == 0);
763
764 MARK_EDITED
765
766 lock();
767 bus_map.erase(buses[p_index]->name);
768 memdelete(buses[p_index]);
769 buses.remove_at(p_index);
770 unlock();
771
772 emit_signal(SNAME("bus_layout_changed"));
773}
774
775void AudioServer::add_bus(int p_at_pos) {
776 MARK_EDITED
777
778 if (p_at_pos >= buses.size()) {
779 p_at_pos = -1;
780 } else if (p_at_pos == 0) {
781 if (buses.size() > 1) {
782 p_at_pos = 1;
783 } else {
784 p_at_pos = -1;
785 }
786 }
787
788 String attempt = "New Bus";
789 int attempts = 1;
790 while (true) {
791 bool name_free = true;
792 for (int j = 0; j < buses.size(); j++) {
793 if (buses[j]->name == attempt) {
794 name_free = false;
795 break;
796 }
797 }
798
799 if (!name_free) {
800 attempts++;
801 attempt = "New Bus " + itos(attempts);
802 } else {
803 break;
804 }
805 }
806
807 Bus *bus = memnew(Bus);
808 bus->channels.resize(channel_count);
809 for (int j = 0; j < channel_count; j++) {
810 bus->channels.write[j].buffer.resize(buffer_size);
811 }
812 bus->name = attempt;
813 bus->solo = false;
814 bus->mute = false;
815 bus->bypass = false;
816 bus->volume_db = 0;
817
818 bus_map[attempt] = bus;
819
820 if (p_at_pos == -1) {
821 buses.push_back(bus);
822 } else {
823 buses.insert(p_at_pos, bus);
824 }
825
826 emit_signal(SNAME("bus_layout_changed"));
827}
828
829void AudioServer::move_bus(int p_bus, int p_to_pos) {
830 ERR_FAIL_COND(p_bus < 1 || p_bus >= buses.size());
831 ERR_FAIL_COND(p_to_pos != -1 && (p_to_pos < 1 || p_to_pos > buses.size()));
832
833 MARK_EDITED
834
835 if (p_bus == p_to_pos) {
836 return;
837 }
838
839 Bus *bus = buses[p_bus];
840 buses.remove_at(p_bus);
841
842 if (p_to_pos == -1) {
843 buses.push_back(bus);
844 } else if (p_to_pos < p_bus) {
845 buses.insert(p_to_pos, bus);
846 } else {
847 buses.insert(p_to_pos - 1, bus);
848 }
849
850 emit_signal(SNAME("bus_layout_changed"));
851}
852
853int AudioServer::get_bus_count() const {
854 return buses.size();
855}
856
857void AudioServer::set_bus_name(int p_bus, const String &p_name) {
858 ERR_FAIL_INDEX(p_bus, buses.size());
859 if (p_bus == 0 && p_name != "Master") {
860 return; //bus 0 is always master
861 }
862
863 MARK_EDITED
864
865 lock();
866
867 if (buses[p_bus]->name == p_name) {
868 unlock();
869 return;
870 }
871
872 String attempt = p_name;
873 int attempts = 1;
874
875 while (true) {
876 bool name_free = true;
877 for (int i = 0; i < buses.size(); i++) {
878 if (buses[i]->name == attempt) {
879 name_free = false;
880 break;
881 }
882 }
883
884 if (name_free) {
885 break;
886 }
887
888 attempts++;
889 attempt = p_name + " " + itos(attempts);
890 }
891 bus_map.erase(buses[p_bus]->name);
892 buses[p_bus]->name = attempt;
893 bus_map[attempt] = buses[p_bus];
894 unlock();
895
896 emit_signal(SNAME("bus_layout_changed"));
897}
898
899String AudioServer::get_bus_name(int p_bus) const {
900 ERR_FAIL_INDEX_V(p_bus, buses.size(), String());
901 return buses[p_bus]->name;
902}
903
904int AudioServer::get_bus_index(const StringName &p_bus_name) const {
905 for (int i = 0; i < buses.size(); ++i) {
906 if (buses[i]->name == p_bus_name) {
907 return i;
908 }
909 }
910 return -1;
911}
912
913void AudioServer::set_bus_volume_db(int p_bus, float p_volume_db) {
914 ERR_FAIL_INDEX(p_bus, buses.size());
915
916 MARK_EDITED
917
918 buses[p_bus]->volume_db = p_volume_db;
919}
920
921float AudioServer::get_bus_volume_db(int p_bus) const {
922 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
923 return buses[p_bus]->volume_db;
924}
925
926int AudioServer::get_bus_channels(int p_bus) const {
927 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
928 return buses[p_bus]->channels.size();
929}
930
931void AudioServer::set_bus_send(int p_bus, const StringName &p_send) {
932 ERR_FAIL_INDEX(p_bus, buses.size());
933
934 MARK_EDITED
935
936 buses[p_bus]->send = p_send;
937}
938
939StringName AudioServer::get_bus_send(int p_bus) const {
940 ERR_FAIL_INDEX_V(p_bus, buses.size(), StringName());
941 return buses[p_bus]->send;
942}
943
944void AudioServer::set_bus_solo(int p_bus, bool p_enable) {
945 ERR_FAIL_INDEX(p_bus, buses.size());
946
947 MARK_EDITED
948
949 buses[p_bus]->solo = p_enable;
950}
951
952bool AudioServer::is_bus_solo(int p_bus) const {
953 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
954
955 return buses[p_bus]->solo;
956}
957
958void AudioServer::set_bus_mute(int p_bus, bool p_enable) {
959 ERR_FAIL_INDEX(p_bus, buses.size());
960
961 MARK_EDITED
962
963 buses[p_bus]->mute = p_enable;
964}
965
966bool AudioServer::is_bus_mute(int p_bus) const {
967 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
968
969 return buses[p_bus]->mute;
970}
971
972void AudioServer::set_bus_bypass_effects(int p_bus, bool p_enable) {
973 ERR_FAIL_INDEX(p_bus, buses.size());
974
975 MARK_EDITED
976
977 buses[p_bus]->bypass = p_enable;
978}
979
980bool AudioServer::is_bus_bypassing_effects(int p_bus) const {
981 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
982
983 return buses[p_bus]->bypass;
984}
985
986void AudioServer::_update_bus_effects(int p_bus) {
987 for (int i = 0; i < buses[p_bus]->channels.size(); i++) {
988 buses.write[p_bus]->channels.write[i].effect_instances.resize(buses[p_bus]->effects.size());
989 for (int j = 0; j < buses[p_bus]->effects.size(); j++) {
990 Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instantiate();
991 if (Object::cast_to<AudioEffectCompressorInstance>(*fx)) {
992 Object::cast_to<AudioEffectCompressorInstance>(*fx)->set_current_channel(i);
993 }
994 buses.write[p_bus]->channels.write[i].effect_instances.write[j] = fx;
995 }
996 }
997}
998
999void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos) {
1000 ERR_FAIL_COND(p_effect.is_null());
1001 ERR_FAIL_INDEX(p_bus, buses.size());
1002
1003 MARK_EDITED
1004
1005 lock();
1006
1007 Bus::Effect fx;
1008 fx.effect = p_effect;
1009 //fx.instance=p_effect->instantiate();
1010 fx.enabled = true;
1011#ifdef DEBUG_ENABLED
1012 fx.prof_time = 0;
1013#endif
1014
1015 if (p_at_pos >= buses[p_bus]->effects.size() || p_at_pos < 0) {
1016 buses[p_bus]->effects.push_back(fx);
1017 } else {
1018 buses[p_bus]->effects.insert(p_at_pos, fx);
1019 }
1020
1021 _update_bus_effects(p_bus);
1022
1023 unlock();
1024}
1025
1026void AudioServer::remove_bus_effect(int p_bus, int p_effect) {
1027 ERR_FAIL_INDEX(p_bus, buses.size());
1028
1029 MARK_EDITED
1030
1031 lock();
1032
1033 buses[p_bus]->effects.remove_at(p_effect);
1034 _update_bus_effects(p_bus);
1035
1036 unlock();
1037}
1038
1039int AudioServer::get_bus_effect_count(int p_bus) {
1040 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1041
1042 return buses[p_bus]->effects.size();
1043}
1044
1045Ref<AudioEffectInstance> AudioServer::get_bus_effect_instance(int p_bus, int p_effect, int p_channel) {
1046 ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffectInstance>());
1047 ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffectInstance>());
1048 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), Ref<AudioEffectInstance>());
1049
1050 return buses[p_bus]->channels[p_channel].effect_instances[p_effect];
1051}
1052
1053Ref<AudioEffect> AudioServer::get_bus_effect(int p_bus, int p_effect) {
1054 ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffect>());
1055 ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffect>());
1056
1057 return buses[p_bus]->effects[p_effect].effect;
1058}
1059
1060void AudioServer::swap_bus_effects(int p_bus, int p_effect, int p_by_effect) {
1061 ERR_FAIL_INDEX(p_bus, buses.size());
1062 ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
1063 ERR_FAIL_INDEX(p_by_effect, buses[p_bus]->effects.size());
1064
1065 MARK_EDITED
1066
1067 lock();
1068 SWAP(buses.write[p_bus]->effects.write[p_effect], buses.write[p_bus]->effects.write[p_by_effect]);
1069 _update_bus_effects(p_bus);
1070 unlock();
1071}
1072
1073void AudioServer::set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled) {
1074 ERR_FAIL_INDEX(p_bus, buses.size());
1075 ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
1076
1077 MARK_EDITED
1078
1079 buses.write[p_bus]->effects.write[p_effect].enabled = p_enabled;
1080}
1081
1082bool AudioServer::is_bus_effect_enabled(int p_bus, int p_effect) const {
1083 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1084 ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), false);
1085 return buses[p_bus]->effects[p_effect].enabled;
1086}
1087
1088float AudioServer::get_bus_peak_volume_left_db(int p_bus, int p_channel) const {
1089 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1090 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
1091
1092 return buses[p_bus]->channels[p_channel].peak_volume.l;
1093}
1094
1095float AudioServer::get_bus_peak_volume_right_db(int p_bus, int p_channel) const {
1096 ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
1097 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
1098
1099 return buses[p_bus]->channels[p_channel].peak_volume.r;
1100}
1101
1102bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
1103 ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
1104 ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), false);
1105
1106 return buses[p_bus]->channels[p_channel].active;
1107}
1108
1109void AudioServer::set_playback_speed_scale(float p_scale) {
1110 ERR_FAIL_COND(p_scale <= 0);
1111
1112 playback_speed_scale = p_scale;
1113}
1114
1115float AudioServer::get_playback_speed_scale() const {
1116 return playback_speed_scale;
1117}
1118
1119void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, StringName p_bus, Vector<AudioFrame> p_volume_db_vector, float p_start_time, float p_pitch_scale) {
1120 ERR_FAIL_COND(p_playback.is_null());
1121
1122 HashMap<StringName, Vector<AudioFrame>> map;
1123 map[p_bus] = p_volume_db_vector;
1124
1125 start_playback_stream(p_playback, map, p_start_time, p_pitch_scale);
1126}
1127
1128void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, HashMap<StringName, Vector<AudioFrame>> p_bus_volumes, float p_start_time, float p_pitch_scale, float p_highshelf_gain, float p_attenuation_cutoff_hz) {
1129 ERR_FAIL_COND(p_playback.is_null());
1130
1131 AudioStreamPlaybackListNode *playback_node = new AudioStreamPlaybackListNode();
1132 playback_node->stream_playback = p_playback;
1133 playback_node->stream_playback->start(p_start_time);
1134
1135 AudioStreamPlaybackBusDetails *new_bus_details = new AudioStreamPlaybackBusDetails();
1136 int idx = 0;
1137 for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
1138 if (pair.value.size() < channel_count || pair.value.size() != MAX_CHANNELS_PER_BUS) {
1139 delete new_bus_details;
1140 ERR_FAIL();
1141 }
1142
1143 new_bus_details->bus_active[idx] = true;
1144 new_bus_details->bus[idx] = pair.key;
1145 for (int channel_idx = 0; channel_idx < MAX_CHANNELS_PER_BUS; channel_idx++) {
1146 new_bus_details->volume[idx][channel_idx] = pair.value[channel_idx];
1147 }
1148 }
1149 playback_node->bus_details = new_bus_details;
1150 playback_node->prev_bus_details = new AudioStreamPlaybackBusDetails();
1151
1152 playback_node->pitch_scale.set(p_pitch_scale);
1153 playback_node->highshelf_gain.set(p_highshelf_gain);
1154 playback_node->attenuation_filter_cutoff_hz.set(p_attenuation_cutoff_hz);
1155
1156 memset(playback_node->prev_bus_details->volume, 0, sizeof(playback_node->prev_bus_details->volume));
1157
1158 for (AudioFrame &frame : playback_node->lookahead) {
1159 frame = AudioFrame(0, 0);
1160 }
1161
1162 playback_node->state.store(AudioStreamPlaybackListNode::PLAYING);
1163
1164 playback_list.insert(playback_node);
1165}
1166
1167void AudioServer::stop_playback_stream(Ref<AudioStreamPlayback> p_playback) {
1168 ERR_FAIL_COND(p_playback.is_null());
1169
1170 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1171 if (!playback_node) {
1172 return;
1173 }
1174
1175 AudioStreamPlaybackListNode::PlaybackState new_state, old_state;
1176 do {
1177 old_state = playback_node->state.load();
1178 if (old_state == AudioStreamPlaybackListNode::AWAITING_DELETION) {
1179 break; // Don't fade out again.
1180 }
1181 new_state = AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION;
1182
1183 } while (!playback_node->state.compare_exchange_strong(old_state, new_state));
1184}
1185
1186void AudioServer::set_playback_bus_exclusive(Ref<AudioStreamPlayback> p_playback, StringName p_bus, Vector<AudioFrame> p_volumes) {
1187 ERR_FAIL_COND(p_volumes.size() != MAX_CHANNELS_PER_BUS);
1188
1189 HashMap<StringName, Vector<AudioFrame>> map;
1190 map[p_bus] = p_volumes;
1191
1192 set_playback_bus_volumes_linear(p_playback, map);
1193}
1194
1195void AudioServer::set_playback_bus_volumes_linear(Ref<AudioStreamPlayback> p_playback, HashMap<StringName, Vector<AudioFrame>> p_bus_volumes) {
1196 ERR_FAIL_COND(p_bus_volumes.size() > MAX_BUSES_PER_PLAYBACK);
1197
1198 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1199 if (!playback_node) {
1200 return;
1201 }
1202 AudioStreamPlaybackBusDetails *old_bus_details, *new_bus_details = new AudioStreamPlaybackBusDetails();
1203
1204 int idx = 0;
1205 for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
1206 if (idx >= MAX_BUSES_PER_PLAYBACK) {
1207 break;
1208 }
1209 ERR_FAIL_COND(pair.value.size() < channel_count);
1210 ERR_FAIL_COND(pair.value.size() != MAX_CHANNELS_PER_BUS);
1211
1212 new_bus_details->bus_active[idx] = true;
1213 new_bus_details->bus[idx] = pair.key;
1214 for (int channel_idx = 0; channel_idx < MAX_CHANNELS_PER_BUS; channel_idx++) {
1215 new_bus_details->volume[idx][channel_idx] = pair.value[channel_idx];
1216 }
1217 idx++;
1218 }
1219
1220 do {
1221 old_bus_details = playback_node->bus_details.load();
1222 } while (!playback_node->bus_details.compare_exchange_strong(old_bus_details, new_bus_details));
1223
1224 bus_details_graveyard.insert(old_bus_details);
1225}
1226
1227void AudioServer::set_playback_all_bus_volumes_linear(Ref<AudioStreamPlayback> p_playback, Vector<AudioFrame> p_volumes) {
1228 ERR_FAIL_COND(p_playback.is_null());
1229 ERR_FAIL_COND(p_volumes.size() != MAX_CHANNELS_PER_BUS);
1230
1231 HashMap<StringName, Vector<AudioFrame>> map;
1232
1233 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1234 if (!playback_node) {
1235 return;
1236 }
1237 for (int bus_idx = 0; bus_idx < MAX_BUSES_PER_PLAYBACK; bus_idx++) {
1238 if (playback_node->bus_details.load()->bus_active[bus_idx]) {
1239 map[playback_node->bus_details.load()->bus[bus_idx]] = p_volumes;
1240 }
1241 }
1242
1243 set_playback_bus_volumes_linear(p_playback, map);
1244}
1245
1246void AudioServer::set_playback_pitch_scale(Ref<AudioStreamPlayback> p_playback, float p_pitch_scale) {
1247 ERR_FAIL_COND(p_playback.is_null());
1248
1249 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1250 if (!playback_node) {
1251 return;
1252 }
1253
1254 playback_node->pitch_scale.set(p_pitch_scale);
1255}
1256
1257void AudioServer::set_playback_paused(Ref<AudioStreamPlayback> p_playback, bool p_paused) {
1258 ERR_FAIL_COND(p_playback.is_null());
1259
1260 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1261 if (!playback_node) {
1262 return;
1263 }
1264
1265 AudioStreamPlaybackListNode::PlaybackState new_state, old_state;
1266 do {
1267 old_state = playback_node->state.load();
1268 new_state = p_paused ? AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE : AudioStreamPlaybackListNode::PLAYING;
1269 if (!p_paused && old_state == AudioStreamPlaybackListNode::PLAYING) {
1270 return; // No-op.
1271 }
1272 if (p_paused && (old_state == AudioStreamPlaybackListNode::PAUSED || old_state == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE)) {
1273 return; // No-op.
1274 }
1275
1276 } while (!playback_node->state.compare_exchange_strong(old_state, new_state));
1277}
1278
1279void AudioServer::set_playback_highshelf_params(Ref<AudioStreamPlayback> p_playback, float p_gain, float p_attenuation_cutoff_hz) {
1280 ERR_FAIL_COND(p_playback.is_null());
1281
1282 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1283 if (!playback_node) {
1284 return;
1285 }
1286
1287 playback_node->attenuation_filter_cutoff_hz.set(p_attenuation_cutoff_hz);
1288 playback_node->highshelf_gain.set(p_gain);
1289}
1290
1291bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> p_playback) {
1292 ERR_FAIL_COND_V(p_playback.is_null(), false);
1293
1294 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1295 if (!playback_node) {
1296 return false;
1297 }
1298
1299 return playback_node->state.load() == AudioStreamPlaybackListNode::PLAYING;
1300}
1301
1302float AudioServer::get_playback_position(Ref<AudioStreamPlayback> p_playback) {
1303 ERR_FAIL_COND_V(p_playback.is_null(), 0);
1304
1305 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1306 if (!playback_node) {
1307 return 0;
1308 }
1309
1310 return playback_node->stream_playback->get_playback_position();
1311}
1312
1313bool AudioServer::is_playback_paused(Ref<AudioStreamPlayback> p_playback) {
1314 ERR_FAIL_COND_V(p_playback.is_null(), false);
1315
1316 AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
1317 if (!playback_node) {
1318 return false;
1319 }
1320
1321 return playback_node->state.load() == AudioStreamPlaybackListNode::PAUSED || playback_node->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE;
1322}
1323
1324uint64_t AudioServer::get_mix_count() const {
1325 return mix_count;
1326}
1327
1328uint64_t AudioServer::get_mixed_frames() const {
1329 return mix_frames;
1330}
1331
1332void AudioServer::notify_listener_changed() {
1333 for (CallbackItem *ci : listener_changed_callback_list) {
1334 ci->callback(ci->userdata);
1335 }
1336}
1337
1338void AudioServer::init_channels_and_buffers() {
1339 channel_count = get_channel_count();
1340 temp_buffer.resize(channel_count);
1341 mix_buffer.resize(buffer_size + LOOKAHEAD_BUFFER_SIZE);
1342
1343 for (int i = 0; i < temp_buffer.size(); i++) {
1344 temp_buffer.write[i].resize(buffer_size);
1345 }
1346
1347 for (int i = 0; i < buses.size(); i++) {
1348 buses[i]->channels.resize(channel_count);
1349 for (int j = 0; j < channel_count; j++) {
1350 buses.write[i]->channels.write[j].buffer.resize(buffer_size);
1351 }
1352 _update_bus_effects(i);
1353 }
1354}
1355
1356void AudioServer::init() {
1357 channel_disable_threshold_db = GLOBAL_DEF_RST("audio/buses/channel_disable_threshold_db", -60.0);
1358 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();
1359 buffer_size = 512; //hardcoded for now
1360
1361 init_channels_and_buffers();
1362
1363 mix_count = 0;
1364 set_bus_count(1);
1365 set_bus_name(0, "Master");
1366
1367 if (AudioDriver::get_singleton()) {
1368 AudioDriver::get_singleton()->start();
1369 }
1370
1371#ifdef TOOLS_ENABLED
1372 set_edited(false); //avoid editors from thinking this was edited
1373#endif
1374
1375 GLOBAL_DEF_RST("audio/video/video_delay_compensation_ms", 0);
1376}
1377
1378void AudioServer::update() {
1379#ifdef DEBUG_ENABLED
1380 if (EngineDebugger::is_profiling("servers")) {
1381 // Driver time includes server time + effects times
1382 // Server time includes effects times
1383 uint64_t driver_time = AudioDriver::get_singleton()->get_profiling_time();
1384 uint64_t server_time = prof_time;
1385
1386 // Subtract the server time from the driver time
1387 if (driver_time > server_time) {
1388 driver_time -= server_time;
1389 }
1390
1391 Array values;
1392
1393 for (int i = buses.size() - 1; i >= 0; i--) {
1394 Bus *bus = buses[i];
1395 if (bus->bypass) {
1396 continue;
1397 }
1398
1399 for (int j = 0; j < bus->effects.size(); j++) {
1400 if (!bus->effects[j].enabled) {
1401 continue;
1402 }
1403
1404 values.push_back(String(bus->name) + bus->effects[j].effect->get_name());
1405 values.push_back(USEC_TO_SEC(bus->effects[j].prof_time));
1406
1407 // Subtract the effect time from the driver and server times
1408 if (driver_time > bus->effects[j].prof_time) {
1409 driver_time -= bus->effects[j].prof_time;
1410 }
1411 if (server_time > bus->effects[j].prof_time) {
1412 server_time -= bus->effects[j].prof_time;
1413 }
1414 }
1415 }
1416
1417 values.push_back("audio_server");
1418 values.push_back(USEC_TO_SEC(server_time));
1419 values.push_back("audio_driver");
1420 values.push_back(USEC_TO_SEC(driver_time));
1421
1422 values.push_front("audio_thread");
1423 EngineDebugger::profiler_add_frame_data("servers", values);
1424 }
1425
1426 // Reset profiling times
1427 for (int i = buses.size() - 1; i >= 0; i--) {
1428 Bus *bus = buses[i];
1429 if (bus->bypass) {
1430 continue;
1431 }
1432
1433 for (int j = 0; j < bus->effects.size(); j++) {
1434 if (!bus->effects[j].enabled) {
1435 continue;
1436 }
1437
1438 bus->effects.write[j].prof_time = 0;
1439 }
1440 }
1441
1442 AudioDriver::get_singleton()->reset_profiling_time();
1443 prof_time = 0;
1444#endif
1445
1446 for (CallbackItem *ci : update_callback_list) {
1447 ci->callback(ci->userdata);
1448 }
1449 mix_callback_list.maybe_cleanup();
1450 update_callback_list.maybe_cleanup();
1451 listener_changed_callback_list.maybe_cleanup();
1452 playback_list.maybe_cleanup();
1453 for (AudioStreamPlaybackBusDetails *bus_details : bus_details_graveyard_frame_old) {
1454 bus_details_graveyard_frame_old.erase(bus_details, [](AudioStreamPlaybackBusDetails *d) { delete d; });
1455 }
1456 for (AudioStreamPlaybackBusDetails *bus_details : bus_details_graveyard) {
1457 bus_details_graveyard_frame_old.insert(bus_details);
1458 bus_details_graveyard.erase(bus_details);
1459 }
1460 bus_details_graveyard.maybe_cleanup();
1461 bus_details_graveyard_frame_old.maybe_cleanup();
1462}
1463
1464void AudioServer::load_default_bus_layout() {
1465 String layout_path = GLOBAL_GET("audio/buses/default_bus_layout");
1466
1467 if (ResourceLoader::exists(layout_path)) {
1468 Ref<AudioBusLayout> default_layout = ResourceLoader::load(layout_path);
1469 if (default_layout.is_valid()) {
1470 set_bus_layout(default_layout);
1471 }
1472 }
1473}
1474
1475void AudioServer::finish() {
1476 for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
1477 AudioDriverManager::get_driver(i)->finish();
1478 }
1479
1480 for (int i = 0; i < buses.size(); i++) {
1481 memdelete(buses[i]);
1482 }
1483
1484 buses.clear();
1485}
1486
1487/* MISC config */
1488
1489void AudioServer::lock() {
1490 AudioDriver::get_singleton()->lock();
1491}
1492
1493void AudioServer::unlock() {
1494 AudioDriver::get_singleton()->unlock();
1495}
1496
1497AudioServer::SpeakerMode AudioServer::get_speaker_mode() const {
1498 return (AudioServer::SpeakerMode)AudioDriver::get_singleton()->get_speaker_mode();
1499}
1500
1501float AudioServer::get_mix_rate() const {
1502 return AudioDriver::get_singleton()->get_mix_rate();
1503}
1504
1505float AudioServer::read_output_peak_db() const {
1506 return 0;
1507}
1508
1509AudioServer *AudioServer::get_singleton() {
1510 return singleton;
1511}
1512
1513double AudioServer::get_output_latency() const {
1514 return AudioDriver::get_singleton()->get_latency();
1515}
1516
1517double AudioServer::get_time_to_next_mix() const {
1518 return AudioDriver::get_singleton()->get_time_to_next_mix();
1519}
1520
1521double AudioServer::get_time_since_last_mix() const {
1522 return AudioDriver::get_singleton()->get_time_since_last_mix();
1523}
1524
1525AudioServer *AudioServer::singleton = nullptr;
1526
1527void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) {
1528 CallbackItem *ci = new CallbackItem();
1529 ci->callback = p_callback;
1530 ci->userdata = p_userdata;
1531 update_callback_list.insert(ci);
1532}
1533
1534void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) {
1535 for (CallbackItem *ci : update_callback_list) {
1536 if (ci->callback == p_callback && ci->userdata == p_userdata) {
1537 update_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
1538 }
1539 }
1540}
1541
1542void AudioServer::add_mix_callback(AudioCallback p_callback, void *p_userdata) {
1543 CallbackItem *ci = new CallbackItem();
1544 ci->callback = p_callback;
1545 ci->userdata = p_userdata;
1546 mix_callback_list.insert(ci);
1547}
1548
1549void AudioServer::remove_mix_callback(AudioCallback p_callback, void *p_userdata) {
1550 for (CallbackItem *ci : mix_callback_list) {
1551 if (ci->callback == p_callback && ci->userdata == p_userdata) {
1552 mix_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
1553 }
1554 }
1555}
1556
1557void AudioServer::add_listener_changed_callback(AudioCallback p_callback, void *p_userdata) {
1558 CallbackItem *ci = new CallbackItem();
1559 ci->callback = p_callback;
1560 ci->userdata = p_userdata;
1561 listener_changed_callback_list.insert(ci);
1562}
1563
1564void AudioServer::remove_listener_changed_callback(AudioCallback p_callback, void *p_userdata) {
1565 for (CallbackItem *ci : listener_changed_callback_list) {
1566 if (ci->callback == p_callback && ci->userdata == p_userdata) {
1567 listener_changed_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
1568 }
1569 }
1570}
1571
1572void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
1573 ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.size() == 0);
1574
1575 lock();
1576 for (int i = 0; i < buses.size(); i++) {
1577 memdelete(buses[i]);
1578 }
1579 buses.resize(p_bus_layout->buses.size());
1580 bus_map.clear();
1581 for (int i = 0; i < p_bus_layout->buses.size(); i++) {
1582 Bus *bus = memnew(Bus);
1583 if (i == 0) {
1584 bus->name = SceneStringNames::get_singleton()->Master;
1585 } else {
1586 bus->name = p_bus_layout->buses[i].name;
1587 bus->send = p_bus_layout->buses[i].send;
1588 }
1589
1590 bus->solo = p_bus_layout->buses[i].solo;
1591 bus->mute = p_bus_layout->buses[i].mute;
1592 bus->bypass = p_bus_layout->buses[i].bypass;
1593 bus->volume_db = p_bus_layout->buses[i].volume_db;
1594
1595 for (int j = 0; j < p_bus_layout->buses[i].effects.size(); j++) {
1596 Ref<AudioEffect> fx = p_bus_layout->buses[i].effects[j].effect;
1597
1598 if (fx.is_valid()) {
1599 Bus::Effect bfx;
1600 bfx.effect = fx;
1601 bfx.enabled = p_bus_layout->buses[i].effects[j].enabled;
1602#ifdef DEBUG_ENABLED
1603 bfx.prof_time = 0;
1604#endif
1605 bus->effects.push_back(bfx);
1606 }
1607 }
1608
1609 bus_map[bus->name] = bus;
1610 buses.write[i] = bus;
1611
1612 buses[i]->channels.resize(channel_count);
1613 for (int j = 0; j < channel_count; j++) {
1614 buses.write[i]->channels.write[j].buffer.resize(buffer_size);
1615 }
1616 _update_bus_effects(i);
1617 }
1618#ifdef TOOLS_ENABLED
1619 set_edited(false);
1620#endif
1621 unlock();
1622}
1623
1624Ref<AudioBusLayout> AudioServer::generate_bus_layout() const {
1625 Ref<AudioBusLayout> state;
1626 state.instantiate();
1627
1628 state->buses.resize(buses.size());
1629
1630 for (int i = 0; i < buses.size(); i++) {
1631 state->buses.write[i].name = buses[i]->name;
1632 state->buses.write[i].send = buses[i]->send;
1633 state->buses.write[i].mute = buses[i]->mute;
1634 state->buses.write[i].solo = buses[i]->solo;
1635 state->buses.write[i].bypass = buses[i]->bypass;
1636 state->buses.write[i].volume_db = buses[i]->volume_db;
1637 for (int j = 0; j < buses[i]->effects.size(); j++) {
1638 AudioBusLayout::Bus::Effect fx;
1639 fx.effect = buses[i]->effects[j].effect;
1640 fx.enabled = buses[i]->effects[j].enabled;
1641 state->buses.write[i].effects.push_back(fx);
1642 }
1643 }
1644
1645 return state;
1646}
1647
1648PackedStringArray AudioServer::get_output_device_list() {
1649 return AudioDriver::get_singleton()->get_output_device_list();
1650}
1651
1652String AudioServer::get_output_device() {
1653 return AudioDriver::get_singleton()->get_output_device();
1654}
1655
1656void AudioServer::set_output_device(const String &p_name) {
1657 AudioDriver::get_singleton()->set_output_device(p_name);
1658}
1659
1660PackedStringArray AudioServer::get_input_device_list() {
1661 return AudioDriver::get_singleton()->get_input_device_list();
1662}
1663
1664String AudioServer::get_input_device() {
1665 return AudioDriver::get_singleton()->get_input_device();
1666}
1667
1668void AudioServer::set_input_device(const String &p_name) {
1669 AudioDriver::get_singleton()->set_input_device(p_name);
1670}
1671
1672void AudioServer::set_enable_tagging_used_audio_streams(bool p_enable) {
1673 tag_used_audio_streams = p_enable;
1674}
1675
1676void AudioServer::_bind_methods() {
1677 ClassDB::bind_method(D_METHOD("set_bus_count", "amount"), &AudioServer::set_bus_count);
1678 ClassDB::bind_method(D_METHOD("get_bus_count"), &AudioServer::get_bus_count);
1679
1680 ClassDB::bind_method(D_METHOD("remove_bus", "index"), &AudioServer::remove_bus);
1681 ClassDB::bind_method(D_METHOD("add_bus", "at_position"), &AudioServer::add_bus, DEFVAL(-1));
1682 ClassDB::bind_method(D_METHOD("move_bus", "index", "to_index"), &AudioServer::move_bus);
1683
1684 ClassDB::bind_method(D_METHOD("set_bus_name", "bus_idx", "name"), &AudioServer::set_bus_name);
1685 ClassDB::bind_method(D_METHOD("get_bus_name", "bus_idx"), &AudioServer::get_bus_name);
1686 ClassDB::bind_method(D_METHOD("get_bus_index", "bus_name"), &AudioServer::get_bus_index);
1687
1688 ClassDB::bind_method(D_METHOD("get_bus_channels", "bus_idx"), &AudioServer::get_bus_channels);
1689
1690 ClassDB::bind_method(D_METHOD("set_bus_volume_db", "bus_idx", "volume_db"), &AudioServer::set_bus_volume_db);
1691 ClassDB::bind_method(D_METHOD("get_bus_volume_db", "bus_idx"), &AudioServer::get_bus_volume_db);
1692
1693 ClassDB::bind_method(D_METHOD("set_bus_send", "bus_idx", "send"), &AudioServer::set_bus_send);
1694 ClassDB::bind_method(D_METHOD("get_bus_send", "bus_idx"), &AudioServer::get_bus_send);
1695
1696 ClassDB::bind_method(D_METHOD("set_bus_solo", "bus_idx", "enable"), &AudioServer::set_bus_solo);
1697 ClassDB::bind_method(D_METHOD("is_bus_solo", "bus_idx"), &AudioServer::is_bus_solo);
1698
1699 ClassDB::bind_method(D_METHOD("set_bus_mute", "bus_idx", "enable"), &AudioServer::set_bus_mute);
1700 ClassDB::bind_method(D_METHOD("is_bus_mute", "bus_idx"), &AudioServer::is_bus_mute);
1701
1702 ClassDB::bind_method(D_METHOD("set_bus_bypass_effects", "bus_idx", "enable"), &AudioServer::set_bus_bypass_effects);
1703 ClassDB::bind_method(D_METHOD("is_bus_bypassing_effects", "bus_idx"), &AudioServer::is_bus_bypassing_effects);
1704
1705 ClassDB::bind_method(D_METHOD("add_bus_effect", "bus_idx", "effect", "at_position"), &AudioServer::add_bus_effect, DEFVAL(-1));
1706 ClassDB::bind_method(D_METHOD("remove_bus_effect", "bus_idx", "effect_idx"), &AudioServer::remove_bus_effect);
1707
1708 ClassDB::bind_method(D_METHOD("get_bus_effect_count", "bus_idx"), &AudioServer::get_bus_effect_count);
1709 ClassDB::bind_method(D_METHOD("get_bus_effect", "bus_idx", "effect_idx"), &AudioServer::get_bus_effect);
1710 ClassDB::bind_method(D_METHOD("get_bus_effect_instance", "bus_idx", "effect_idx", "channel"), &AudioServer::get_bus_effect_instance, DEFVAL(0));
1711 ClassDB::bind_method(D_METHOD("swap_bus_effects", "bus_idx", "effect_idx", "by_effect_idx"), &AudioServer::swap_bus_effects);
1712
1713 ClassDB::bind_method(D_METHOD("set_bus_effect_enabled", "bus_idx", "effect_idx", "enabled"), &AudioServer::set_bus_effect_enabled);
1714 ClassDB::bind_method(D_METHOD("is_bus_effect_enabled", "bus_idx", "effect_idx"), &AudioServer::is_bus_effect_enabled);
1715
1716 ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
1717 ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
1718
1719 ClassDB::bind_method(D_METHOD("set_playback_speed_scale", "scale"), &AudioServer::set_playback_speed_scale);
1720 ClassDB::bind_method(D_METHOD("get_playback_speed_scale"), &AudioServer::get_playback_speed_scale);
1721
1722 ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
1723 ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
1724
1725 ClassDB::bind_method(D_METHOD("get_speaker_mode"), &AudioServer::get_speaker_mode);
1726 ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioServer::get_mix_rate);
1727
1728 ClassDB::bind_method(D_METHOD("get_output_device_list"), &AudioServer::get_output_device_list);
1729 ClassDB::bind_method(D_METHOD("get_output_device"), &AudioServer::get_output_device);
1730 ClassDB::bind_method(D_METHOD("set_output_device", "name"), &AudioServer::set_output_device);
1731
1732 ClassDB::bind_method(D_METHOD("get_time_to_next_mix"), &AudioServer::get_time_to_next_mix);
1733 ClassDB::bind_method(D_METHOD("get_time_since_last_mix"), &AudioServer::get_time_since_last_mix);
1734 ClassDB::bind_method(D_METHOD("get_output_latency"), &AudioServer::get_output_latency);
1735
1736 ClassDB::bind_method(D_METHOD("get_input_device_list"), &AudioServer::get_input_device_list);
1737 ClassDB::bind_method(D_METHOD("get_input_device"), &AudioServer::get_input_device);
1738 ClassDB::bind_method(D_METHOD("set_input_device", "name"), &AudioServer::set_input_device);
1739
1740 ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout);
1741 ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout);
1742
1743 ClassDB::bind_method(D_METHOD("set_enable_tagging_used_audio_streams", "enable"), &AudioServer::set_enable_tagging_used_audio_streams);
1744
1745 ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
1746 ADD_PROPERTY(PropertyInfo(Variant::STRING, "output_device"), "set_output_device", "get_output_device");
1747 ADD_PROPERTY(PropertyInfo(Variant::STRING, "input_device"), "set_input_device", "get_input_device");
1748 // The default value may be set to an empty string by the platform-specific audio driver.
1749 // Override for class reference generation purposes.
1750 ADD_PROPERTY_DEFAULT("input_device", "Default");
1751 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed_scale"), "set_playback_speed_scale", "get_playback_speed_scale");
1752
1753 ADD_SIGNAL(MethodInfo("bus_layout_changed"));
1754
1755 BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
1756 BIND_ENUM_CONSTANT(SPEAKER_SURROUND_31);
1757 BIND_ENUM_CONSTANT(SPEAKER_SURROUND_51);
1758 BIND_ENUM_CONSTANT(SPEAKER_SURROUND_71);
1759}
1760
1761AudioServer::AudioServer() {
1762 singleton = this;
1763}
1764
1765AudioServer::~AudioServer() {
1766 singleton = nullptr;
1767}
1768
1769/////////////////////////////////
1770
1771bool AudioBusLayout::_set(const StringName &p_name, const Variant &p_value) {
1772 String s = p_name;
1773 if (s.begins_with("bus/")) {
1774 int index = s.get_slice("/", 1).to_int();
1775 if (buses.size() <= index) {
1776 buses.resize(index + 1);
1777 }
1778
1779 Bus &bus = buses.write[index];
1780
1781 String what = s.get_slice("/", 2);
1782
1783 if (what == "name") {
1784 bus.name = p_value;
1785 } else if (what == "solo") {
1786 bus.solo = p_value;
1787 } else if (what == "mute") {
1788 bus.mute = p_value;
1789 } else if (what == "bypass_fx") {
1790 bus.bypass = p_value;
1791 } else if (what == "volume_db") {
1792 bus.volume_db = p_value;
1793 } else if (what == "send") {
1794 bus.send = p_value;
1795 } else if (what == "effect") {
1796 int which = s.get_slice("/", 3).to_int();
1797 if (bus.effects.size() <= which) {
1798 bus.effects.resize(which + 1);
1799 }
1800
1801 Bus::Effect &fx = bus.effects.write[which];
1802
1803 String fxwhat = s.get_slice("/", 4);
1804 if (fxwhat == "effect") {
1805 fx.effect = p_value;
1806 } else if (fxwhat == "enabled") {
1807 fx.enabled = p_value;
1808 } else {
1809 return false;
1810 }
1811
1812 return true;
1813 } else {
1814 return false;
1815 }
1816
1817 return true;
1818 }
1819
1820 return false;
1821}
1822
1823bool AudioBusLayout::_get(const StringName &p_name, Variant &r_ret) const {
1824 String s = p_name;
1825 if (s.begins_with("bus/")) {
1826 int index = s.get_slice("/", 1).to_int();
1827 if (index < 0 || index >= buses.size()) {
1828 return false;
1829 }
1830
1831 const Bus &bus = buses[index];
1832
1833 String what = s.get_slice("/", 2);
1834
1835 if (what == "name") {
1836 r_ret = bus.name;
1837 } else if (what == "solo") {
1838 r_ret = bus.solo;
1839 } else if (what == "mute") {
1840 r_ret = bus.mute;
1841 } else if (what == "bypass_fx") {
1842 r_ret = bus.bypass;
1843 } else if (what == "volume_db") {
1844 r_ret = bus.volume_db;
1845 } else if (what == "send") {
1846 r_ret = bus.send;
1847 } else if (what == "effect") {
1848 int which = s.get_slice("/", 3).to_int();
1849 if (which < 0 || which >= bus.effects.size()) {
1850 return false;
1851 }
1852
1853 const Bus::Effect &fx = bus.effects[which];
1854
1855 String fxwhat = s.get_slice("/", 4);
1856 if (fxwhat == "effect") {
1857 r_ret = fx.effect;
1858 } else if (fxwhat == "enabled") {
1859 r_ret = fx.enabled;
1860 } else {
1861 return false;
1862 }
1863
1864 return true;
1865 } else {
1866 return false;
1867 }
1868
1869 return true;
1870 }
1871
1872 return false;
1873}
1874
1875void AudioBusLayout::_get_property_list(List<PropertyInfo> *p_list) const {
1876 for (int i = 0; i < buses.size(); i++) {
1877 p_list->push_back(PropertyInfo(Variant::STRING, "bus/" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1878 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/solo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1879 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/mute", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1880 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/bypass_fx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1881 p_list->push_back(PropertyInfo(Variant::FLOAT, "bus/" + itos(i) + "/volume_db", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1882 p_list->push_back(PropertyInfo(Variant::FLOAT, "bus/" + itos(i) + "/send", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1883
1884 for (int j = 0; j < buses[i].effects.size(); j++) {
1885 p_list->push_back(PropertyInfo(Variant::OBJECT, "bus/" + itos(i) + "/effect/" + itos(j) + "/effect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1886 p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/effect/" + itos(j) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
1887 }
1888 }
1889}
1890
1891AudioBusLayout::AudioBusLayout() {
1892 buses.resize(1);
1893 buses.write[0].name = SceneStringNames::get_singleton()->Master;
1894}
1895