audio_server.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. #include "audio_server.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #include "scene/resources/audio_stream_sample.h"
  36. #include "servers/audio/audio_driver_dummy.h"
  37. #include "servers/audio/effects/audio_effect_compressor.h"
  38. #ifdef TOOLS_ENABLED
  39. #define MARK_EDITED set_edited(true);
  40. #else
  41. #define MARK_EDITED
  42. #endif
  43. AudioDriver *AudioDriver::singleton = nullptr;
  44. AudioDriver *AudioDriver::get_singleton() {
  45. return singleton;
  46. }
  47. void AudioDriver::set_singleton() {
  48. singleton = this;
  49. }
  50. void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) {
  51. if (p_update_mix_time) {
  52. update_mix_time(p_frames);
  53. }
  54. if (AudioServer::get_singleton()) {
  55. AudioServer::get_singleton()->_driver_process(p_frames, p_buffer);
  56. }
  57. }
  58. void AudioDriver::update_mix_time(int p_frames) {
  59. _last_mix_frames = p_frames;
  60. if (OS::get_singleton()) {
  61. _last_mix_time = OS::get_singleton()->get_ticks_usec();
  62. }
  63. }
  64. double AudioDriver::get_time_since_last_mix() {
  65. lock();
  66. uint64_t last_mix_time = _last_mix_time;
  67. unlock();
  68. return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
  69. }
  70. double AudioDriver::get_time_to_next_mix() {
  71. lock();
  72. uint64_t last_mix_time = _last_mix_time;
  73. uint64_t last_mix_frames = _last_mix_frames;
  74. unlock();
  75. double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
  76. double mix_buffer = last_mix_frames / (double)get_mix_rate();
  77. return mix_buffer - total;
  78. }
  79. void AudioDriver::input_buffer_init(int driver_buffer_frames) {
  80. const int input_buffer_channels = 2;
  81. input_buffer.resize(driver_buffer_frames * input_buffer_channels * 4);
  82. input_position = 0;
  83. input_size = 0;
  84. }
  85. void AudioDriver::input_buffer_write(int32_t sample) {
  86. if ((int)input_position < input_buffer.size()) {
  87. input_buffer.write[input_position++] = sample;
  88. if ((int)input_position >= input_buffer.size()) {
  89. input_position = 0;
  90. }
  91. if ((int)input_size < input_buffer.size()) {
  92. input_size++;
  93. }
  94. } else {
  95. WARN_PRINT("input_buffer_write: Invalid input_position=" + itos(input_position) + " input_buffer.size()=" + itos(input_buffer.size()));
  96. }
  97. }
  98. AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const {
  99. switch (p_channels) {
  100. case 4:
  101. return SPEAKER_SURROUND_31;
  102. case 6:
  103. return SPEAKER_SURROUND_51;
  104. case 8:
  105. return SPEAKER_SURROUND_71;
  106. }
  107. // Default to STEREO
  108. return SPEAKER_MODE_STEREO;
  109. }
  110. int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_mode) const {
  111. switch (p_mode) {
  112. case SPEAKER_MODE_STEREO:
  113. return 2;
  114. case SPEAKER_SURROUND_31:
  115. return 4;
  116. case SPEAKER_SURROUND_51:
  117. return 6;
  118. case SPEAKER_SURROUND_71:
  119. return 8;
  120. }
  121. ERR_FAIL_V(2);
  122. }
  123. Array AudioDriver::get_device_list() {
  124. Array list;
  125. list.push_back("Default");
  126. return list;
  127. }
  128. String AudioDriver::get_device() {
  129. return "Default";
  130. }
  131. Array AudioDriver::capture_get_device_list() {
  132. Array list;
  133. list.push_back("Default");
  134. return list;
  135. }
  136. AudioDriver::AudioDriver() {
  137. _last_mix_time = 0;
  138. _last_mix_frames = 0;
  139. input_position = 0;
  140. input_size = 0;
  141. #ifdef DEBUG_ENABLED
  142. prof_time = 0;
  143. #endif
  144. }
  145. AudioDriverDummy AudioDriverManager::dummy_driver;
  146. AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS] = {
  147. &AudioDriverManager::dummy_driver,
  148. };
  149. int AudioDriverManager::driver_count = 1;
  150. void AudioDriverManager::add_driver(AudioDriver *p_driver) {
  151. ERR_FAIL_COND(driver_count >= MAX_DRIVERS);
  152. drivers[driver_count - 1] = p_driver;
  153. // Last driver is always our dummy driver
  154. drivers[driver_count++] = &AudioDriverManager::dummy_driver;
  155. }
  156. int AudioDriverManager::get_driver_count() {
  157. return driver_count;
  158. }
  159. void AudioDriverManager::initialize(int p_driver) {
  160. GLOBAL_DEF_RST("audio/enable_audio_input", false);
  161. GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
  162. GLOBAL_DEF_RST("audio/mix_rate.web", 0); // Safer default output_latency for web (use browser default).
  163. GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
  164. GLOBAL_DEF_RST("audio/output_latency.web", 50); // Safer default output_latency for web.
  165. int failed_driver = -1;
  166. // Check if there is a selected driver
  167. if (p_driver >= 0 && p_driver < driver_count) {
  168. if (drivers[p_driver]->init() == OK) {
  169. drivers[p_driver]->set_singleton();
  170. return;
  171. } else {
  172. failed_driver = p_driver;
  173. }
  174. }
  175. // No selected driver, try them all in order
  176. for (int i = 0; i < driver_count; i++) {
  177. // Don't re-init the driver if it failed above
  178. if (i == failed_driver) {
  179. continue;
  180. }
  181. if (drivers[i]->init() == OK) {
  182. drivers[i]->set_singleton();
  183. break;
  184. }
  185. }
  186. if (driver_count > 1 && String(AudioDriver::get_singleton()->get_name()) == "Dummy") {
  187. WARN_PRINT("All audio drivers failed, falling back to the dummy driver.");
  188. }
  189. }
  190. AudioDriver *AudioDriverManager::get_driver(int p_driver) {
  191. ERR_FAIL_INDEX_V(p_driver, driver_count, nullptr);
  192. return drivers[p_driver];
  193. }
  194. //////////////////////////////////////////////
  195. //////////////////////////////////////////////
  196. //////////////////////////////////////////////
  197. //////////////////////////////////////////////
  198. void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
  199. int todo = p_frames;
  200. #ifdef DEBUG_ENABLED
  201. uint64_t prof_ticks = OS::get_singleton()->get_ticks_usec();
  202. #endif
  203. if (channel_count != get_channel_count()) {
  204. // Amount of channels changed due to a device change
  205. // reinitialize the buses channels and buffers
  206. init_channels_and_buffers();
  207. }
  208. ERR_FAIL_COND_MSG(buses.empty() && todo, "AudioServer bus count is less than 1.");
  209. while (todo) {
  210. if (to_mix == 0) {
  211. _mix_step();
  212. }
  213. int to_copy = MIN(to_mix, todo);
  214. Bus *master = buses[0];
  215. int from = buffer_size - to_mix;
  216. int from_buf = p_frames - todo;
  217. //master master, send to output
  218. int cs = master->channels.size();
  219. // take away 1 from the stride as we are manually incrementing one for stereo
  220. uintptr_t stride_minus_one = (cs * 2) - 1;
  221. for (int k = 0; k < cs; k++) {
  222. // destination start for data will be the same in all cases
  223. int32_t *dest = &p_buffer[(from_buf * (cs * 2)) + (k * 2)];
  224. if (master->channels[k].active) {
  225. const AudioFrame *buf = master->channels[k].buffer.ptr();
  226. for (int j = 0; j < to_copy; j++) {
  227. float l = CLAMP(buf[from + j].l, -1.0, 1.0);
  228. int32_t vl = l * ((1 << 20) - 1);
  229. int32_t vl2 = (vl < 0 ? -1 : 1) * (ABS(vl) << 11);
  230. *dest = vl2;
  231. dest++;
  232. float r = CLAMP(buf[from + j].r, -1.0, 1.0);
  233. int32_t vr = r * ((1 << 20) - 1);
  234. int32_t vr2 = (vr < 0 ? -1 : 1) * (ABS(vr) << 11);
  235. *dest = vr2;
  236. dest += stride_minus_one;
  237. }
  238. } else {
  239. // Bizarrely, profiling indicates that detecting the common case of cs == 1
  240. // and k == 0, and using memset is SLOWER than setting individually.
  241. // (Perhaps it gets optimized to a faster instruction than memset).
  242. for (int j = 0; j < to_copy; j++) {
  243. *dest = 0;
  244. dest++;
  245. *dest = 0;
  246. dest += stride_minus_one;
  247. }
  248. }
  249. }
  250. todo -= to_copy;
  251. to_mix -= to_copy;
  252. }
  253. #ifdef DEBUG_ENABLED
  254. prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks;
  255. #endif
  256. }
  257. void AudioServer::_mix_step() {
  258. bool solo_mode = false;
  259. for (int i = 0; i < buses.size(); i++) {
  260. Bus *bus = buses[i];
  261. bus->index_cache = i; //might be moved around by editor, so..
  262. for (int k = 0; k < bus->channels.size(); k++) {
  263. bus->channels.write[k].used = false;
  264. }
  265. if (bus->solo) {
  266. //solo chain
  267. solo_mode = true;
  268. bus->soloed = true;
  269. do {
  270. if (bus != buses[0]) {
  271. //everything has a send save for master bus
  272. if (!bus_map.has(bus->send)) {
  273. bus = buses[0]; //send to master
  274. } else {
  275. int prev_index_cache = bus->index_cache;
  276. bus = bus_map[bus->send];
  277. if (prev_index_cache >= bus->index_cache) { //invalid, send to master
  278. bus = buses[0];
  279. }
  280. }
  281. bus->soloed = true;
  282. } else {
  283. bus = nullptr;
  284. }
  285. } while (bus);
  286. } else {
  287. bus->soloed = false;
  288. }
  289. }
  290. //make callbacks for mixing the audio
  291. for (Set<CallbackItem>::Element *E = callbacks.front(); E; E = E->next()) {
  292. E->get().callback(E->get().userdata);
  293. }
  294. for (int i = buses.size() - 1; i >= 0; i--) {
  295. //go bus by bus
  296. Bus *bus = buses[i];
  297. for (int k = 0; k < bus->channels.size(); k++) {
  298. if (bus->channels[k].active && !bus->channels[k].used) {
  299. //buffer was not used, but it's still active, so it must be cleaned
  300. AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
  301. for (uint32_t j = 0; j < buffer_size; j++) {
  302. buf[j] = AudioFrame(0, 0);
  303. }
  304. }
  305. }
  306. //process effects
  307. if (!bus->bypass) {
  308. for (int j = 0; j < bus->effects.size(); j++) {
  309. if (!bus->effects[j].enabled) {
  310. continue;
  311. }
  312. #ifdef DEBUG_ENABLED
  313. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  314. #endif
  315. for (int k = 0; k < bus->channels.size(); k++) {
  316. if (!(bus->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
  317. continue;
  318. }
  319. bus->channels.write[k].effect_instances.write[j]->process(bus->channels[k].buffer.ptr(), temp_buffer.write[k].ptrw(), buffer_size);
  320. }
  321. //swap buffers, so internal buffer always has the right data
  322. for (int k = 0; k < bus->channels.size(); k++) {
  323. if (!(buses[i]->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
  324. continue;
  325. }
  326. SWAP(bus->channels.write[k].buffer, temp_buffer.write[k]);
  327. }
  328. #ifdef DEBUG_ENABLED
  329. bus->effects.write[j].prof_time += OS::get_singleton()->get_ticks_usec() - ticks;
  330. #endif
  331. }
  332. }
  333. //process send
  334. Bus *send = nullptr;
  335. if (i > 0) {
  336. //everything has a send save for master bus
  337. if (!bus_map.has(bus->send)) {
  338. send = buses[0];
  339. } else {
  340. send = bus_map[bus->send];
  341. if (send->index_cache >= bus->index_cache) { //invalid, send to master
  342. send = buses[0];
  343. }
  344. }
  345. }
  346. for (int k = 0; k < bus->channels.size(); k++) {
  347. if (!bus->channels[k].active) {
  348. bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
  349. continue;
  350. }
  351. AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
  352. AudioFrame peak = AudioFrame(0, 0);
  353. float volume = Math::db2linear(bus->volume_db);
  354. if (solo_mode) {
  355. if (!bus->soloed) {
  356. volume = 0.0;
  357. }
  358. } else {
  359. if (bus->mute) {
  360. volume = 0.0;
  361. }
  362. }
  363. //apply volume and compute peak
  364. for (uint32_t j = 0; j < buffer_size; j++) {
  365. buf[j] *= volume;
  366. float l = ABS(buf[j].l);
  367. if (l > peak.l) {
  368. peak.l = l;
  369. }
  370. float r = ABS(buf[j].r);
  371. if (r > peak.r) {
  372. peak.r = r;
  373. }
  374. }
  375. bus->channels.write[k].peak_volume = AudioFrame(Math::linear2db(peak.l + AUDIO_PEAK_OFFSET), Math::linear2db(peak.r + AUDIO_PEAK_OFFSET));
  376. if (!bus->channels[k].used) {
  377. //see if any audio is contained, because channel was not used
  378. if (MAX(peak.r, peak.l) > Math::db2linear(channel_disable_threshold_db)) {
  379. bus->channels.write[k].last_mix_with_audio = mix_frames;
  380. } else if (mix_frames - bus->channels[k].last_mix_with_audio > channel_disable_frames) {
  381. bus->channels.write[k].active = false;
  382. continue; //went inactive, don't mix.
  383. }
  384. }
  385. if (send) {
  386. //if not master bus, send
  387. AudioFrame *target_buf = thread_get_channel_mix_buffer(send->index_cache, k);
  388. for (uint32_t j = 0; j < buffer_size; j++) {
  389. target_buf[j] += buf[j];
  390. }
  391. }
  392. }
  393. }
  394. mix_frames += buffer_size;
  395. to_mix = buffer_size;
  396. }
  397. bool AudioServer::thread_has_channel_mix_buffer(int p_bus, int p_buffer) const {
  398. if (p_bus < 0 || p_bus >= buses.size()) {
  399. return false;
  400. }
  401. if (p_buffer < 0 || p_buffer >= buses[p_bus]->channels.size()) {
  402. return false;
  403. }
  404. return true;
  405. }
  406. AudioFrame *AudioServer::thread_get_channel_mix_buffer(int p_bus, int p_buffer) {
  407. ERR_FAIL_INDEX_V(p_bus, buses.size(), nullptr);
  408. ERR_FAIL_INDEX_V(p_buffer, buses[p_bus]->channels.size(), nullptr);
  409. AudioFrame *data = buses.write[p_bus]->channels.write[p_buffer].buffer.ptrw();
  410. if (!buses[p_bus]->channels[p_buffer].used) {
  411. buses.write[p_bus]->channels.write[p_buffer].used = true;
  412. buses.write[p_bus]->channels.write[p_buffer].active = true;
  413. buses.write[p_bus]->channels.write[p_buffer].last_mix_with_audio = mix_frames;
  414. for (uint32_t i = 0; i < buffer_size; i++) {
  415. data[i] = AudioFrame(0, 0);
  416. }
  417. }
  418. return data;
  419. }
  420. int AudioServer::thread_get_mix_buffer_size() const {
  421. return buffer_size;
  422. }
  423. int AudioServer::thread_find_bus_index(const StringName &p_name) {
  424. if (bus_map.has(p_name)) {
  425. return bus_map[p_name]->index_cache;
  426. } else {
  427. return 0;
  428. }
  429. }
  430. void AudioServer::set_bus_count(int p_count) {
  431. ERR_FAIL_COND(p_count < 1);
  432. ERR_FAIL_INDEX(p_count, 256);
  433. MARK_EDITED
  434. lock();
  435. int cb = buses.size();
  436. if (p_count < buses.size()) {
  437. for (int i = p_count; i < buses.size(); i++) {
  438. bus_map.erase(buses[i]->name);
  439. memdelete(buses[i]);
  440. }
  441. }
  442. buses.resize(p_count);
  443. for (int i = cb; i < buses.size(); i++) {
  444. String attempt = "New Bus";
  445. int attempts = 1;
  446. while (true) {
  447. bool name_free = true;
  448. for (int j = 0; j < i; j++) {
  449. if (buses[j]->name == attempt) {
  450. name_free = false;
  451. break;
  452. }
  453. }
  454. if (!name_free) {
  455. attempts++;
  456. attempt = "New Bus " + itos(attempts);
  457. } else {
  458. break;
  459. }
  460. }
  461. buses.write[i] = memnew(Bus);
  462. buses.write[i]->channels.resize(channel_count);
  463. for (int j = 0; j < channel_count; j++) {
  464. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  465. }
  466. buses[i]->name = attempt;
  467. buses[i]->solo = false;
  468. buses[i]->mute = false;
  469. buses[i]->bypass = false;
  470. buses[i]->volume_db = 0;
  471. if (i > 0) {
  472. buses[i]->send = "Master";
  473. }
  474. bus_map[attempt] = buses[i];
  475. }
  476. unlock();
  477. emit_signal("bus_layout_changed");
  478. }
  479. void AudioServer::remove_bus(int p_index) {
  480. ERR_FAIL_INDEX(p_index, buses.size());
  481. ERR_FAIL_COND(p_index == 0);
  482. MARK_EDITED
  483. lock();
  484. bus_map.erase(buses[p_index]->name);
  485. memdelete(buses[p_index]);
  486. buses.remove(p_index);
  487. unlock();
  488. emit_signal("bus_layout_changed");
  489. }
  490. void AudioServer::add_bus(int p_at_pos) {
  491. MARK_EDITED
  492. if (p_at_pos >= buses.size()) {
  493. p_at_pos = -1;
  494. } else if (p_at_pos == 0) {
  495. if (buses.size() > 1) {
  496. p_at_pos = 1;
  497. } else {
  498. p_at_pos = -1;
  499. }
  500. }
  501. String attempt = "New Bus";
  502. int attempts = 1;
  503. while (true) {
  504. bool name_free = true;
  505. for (int j = 0; j < buses.size(); j++) {
  506. if (buses[j]->name == attempt) {
  507. name_free = false;
  508. break;
  509. }
  510. }
  511. if (!name_free) {
  512. attempts++;
  513. attempt = "New Bus " + itos(attempts);
  514. } else {
  515. break;
  516. }
  517. }
  518. Bus *bus = memnew(Bus);
  519. bus->channels.resize(channel_count);
  520. for (int j = 0; j < channel_count; j++) {
  521. bus->channels.write[j].buffer.resize(buffer_size);
  522. }
  523. bus->name = attempt;
  524. bus->solo = false;
  525. bus->mute = false;
  526. bus->bypass = false;
  527. bus->volume_db = 0;
  528. bus_map[attempt] = bus;
  529. if (p_at_pos == -1) {
  530. buses.push_back(bus);
  531. } else {
  532. buses.insert(p_at_pos, bus);
  533. }
  534. emit_signal("bus_layout_changed");
  535. }
  536. void AudioServer::move_bus(int p_bus, int p_to_pos) {
  537. ERR_FAIL_COND(p_bus < 1 || p_bus >= buses.size());
  538. ERR_FAIL_COND(p_to_pos != -1 && (p_to_pos < 1 || p_to_pos > buses.size()));
  539. MARK_EDITED
  540. if (p_bus == p_to_pos) {
  541. return;
  542. }
  543. Bus *bus = buses[p_bus];
  544. buses.remove(p_bus);
  545. if (p_to_pos == -1) {
  546. buses.push_back(bus);
  547. } else if (p_to_pos < p_bus) {
  548. buses.insert(p_to_pos, bus);
  549. } else {
  550. buses.insert(p_to_pos - 1, bus);
  551. }
  552. emit_signal("bus_layout_changed");
  553. }
  554. int AudioServer::get_bus_count() const {
  555. return buses.size();
  556. }
  557. void AudioServer::set_bus_name(int p_bus, const String &p_name) {
  558. ERR_FAIL_INDEX(p_bus, buses.size());
  559. if (p_bus == 0 && p_name != "Master") {
  560. return; //bus 0 is always master
  561. }
  562. MARK_EDITED
  563. lock();
  564. if (buses[p_bus]->name == p_name) {
  565. unlock();
  566. return;
  567. }
  568. String attempt = p_name;
  569. int attempts = 1;
  570. while (true) {
  571. bool name_free = true;
  572. for (int i = 0; i < buses.size(); i++) {
  573. if (buses[i]->name == attempt) {
  574. name_free = false;
  575. break;
  576. }
  577. }
  578. if (name_free) {
  579. break;
  580. }
  581. attempts++;
  582. attempt = p_name + " " + itos(attempts);
  583. }
  584. bus_map.erase(buses[p_bus]->name);
  585. buses[p_bus]->name = attempt;
  586. bus_map[attempt] = buses[p_bus];
  587. unlock();
  588. emit_signal("bus_layout_changed");
  589. }
  590. String AudioServer::get_bus_name(int p_bus) const {
  591. ERR_FAIL_INDEX_V(p_bus, buses.size(), String());
  592. return buses[p_bus]->name;
  593. }
  594. int AudioServer::get_bus_index(const StringName &p_bus_name) const {
  595. for (int i = 0; i < buses.size(); ++i) {
  596. if (buses[i]->name == p_bus_name) {
  597. return i;
  598. }
  599. }
  600. return -1;
  601. }
  602. void AudioServer::set_bus_volume_db(int p_bus, float p_volume_db) {
  603. ERR_FAIL_INDEX(p_bus, buses.size());
  604. MARK_EDITED
  605. buses[p_bus]->volume_db = p_volume_db;
  606. }
  607. float AudioServer::get_bus_volume_db(int p_bus) const {
  608. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  609. return buses[p_bus]->volume_db;
  610. }
  611. int AudioServer::get_bus_channels(int p_bus) const {
  612. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  613. return buses[p_bus]->channels.size();
  614. }
  615. void AudioServer::set_bus_send(int p_bus, const StringName &p_send) {
  616. ERR_FAIL_INDEX(p_bus, buses.size());
  617. MARK_EDITED
  618. buses[p_bus]->send = p_send;
  619. }
  620. StringName AudioServer::get_bus_send(int p_bus) const {
  621. ERR_FAIL_INDEX_V(p_bus, buses.size(), StringName());
  622. return buses[p_bus]->send;
  623. }
  624. void AudioServer::set_bus_solo(int p_bus, bool p_enable) {
  625. ERR_FAIL_INDEX(p_bus, buses.size());
  626. MARK_EDITED
  627. buses[p_bus]->solo = p_enable;
  628. }
  629. bool AudioServer::is_bus_solo(int p_bus) const {
  630. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  631. return buses[p_bus]->solo;
  632. }
  633. void AudioServer::set_bus_mute(int p_bus, bool p_enable) {
  634. ERR_FAIL_INDEX(p_bus, buses.size());
  635. MARK_EDITED
  636. buses[p_bus]->mute = p_enable;
  637. }
  638. bool AudioServer::is_bus_mute(int p_bus) const {
  639. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  640. return buses[p_bus]->mute;
  641. }
  642. void AudioServer::set_bus_bypass_effects(int p_bus, bool p_enable) {
  643. ERR_FAIL_INDEX(p_bus, buses.size());
  644. MARK_EDITED
  645. buses[p_bus]->bypass = p_enable;
  646. }
  647. bool AudioServer::is_bus_bypassing_effects(int p_bus) const {
  648. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  649. return buses[p_bus]->bypass;
  650. }
  651. void AudioServer::_update_bus_effects(int p_bus) {
  652. for (int i = 0; i < buses[p_bus]->channels.size(); i++) {
  653. buses.write[p_bus]->channels.write[i].effect_instances.resize(buses[p_bus]->effects.size());
  654. for (int j = 0; j < buses[p_bus]->effects.size(); j++) {
  655. Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instance();
  656. if (Object::cast_to<AudioEffectCompressorInstance>(*fx)) {
  657. Object::cast_to<AudioEffectCompressorInstance>(*fx)->set_current_channel(i);
  658. }
  659. buses.write[p_bus]->channels.write[i].effect_instances.write[j] = fx;
  660. }
  661. }
  662. }
  663. void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos) {
  664. ERR_FAIL_COND(p_effect.is_null());
  665. ERR_FAIL_INDEX(p_bus, buses.size());
  666. MARK_EDITED
  667. lock();
  668. Bus::Effect fx;
  669. fx.effect = p_effect;
  670. //fx.instance=p_effect->instance();
  671. fx.enabled = true;
  672. #ifdef DEBUG_ENABLED
  673. fx.prof_time = 0;
  674. #endif
  675. if (p_at_pos >= buses[p_bus]->effects.size() || p_at_pos < 0) {
  676. buses[p_bus]->effects.push_back(fx);
  677. } else {
  678. buses[p_bus]->effects.insert(p_at_pos, fx);
  679. }
  680. _update_bus_effects(p_bus);
  681. unlock();
  682. }
  683. void AudioServer::remove_bus_effect(int p_bus, int p_effect) {
  684. ERR_FAIL_INDEX(p_bus, buses.size());
  685. MARK_EDITED
  686. lock();
  687. buses[p_bus]->effects.remove(p_effect);
  688. _update_bus_effects(p_bus);
  689. unlock();
  690. }
  691. int AudioServer::get_bus_effect_count(int p_bus) {
  692. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  693. return buses[p_bus]->effects.size();
  694. }
  695. Ref<AudioEffectInstance> AudioServer::get_bus_effect_instance(int p_bus, int p_effect, int p_channel) {
  696. ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffectInstance>());
  697. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffectInstance>());
  698. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), Ref<AudioEffectInstance>());
  699. return buses[p_bus]->channels[p_channel].effect_instances[p_effect];
  700. }
  701. Ref<AudioEffect> AudioServer::get_bus_effect(int p_bus, int p_effect) {
  702. ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffect>());
  703. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffect>());
  704. return buses[p_bus]->effects[p_effect].effect;
  705. }
  706. void AudioServer::swap_bus_effects(int p_bus, int p_effect, int p_by_effect) {
  707. ERR_FAIL_INDEX(p_bus, buses.size());
  708. ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
  709. ERR_FAIL_INDEX(p_by_effect, buses[p_bus]->effects.size());
  710. MARK_EDITED
  711. lock();
  712. SWAP(buses.write[p_bus]->effects.write[p_effect], buses.write[p_bus]->effects.write[p_by_effect]);
  713. _update_bus_effects(p_bus);
  714. unlock();
  715. }
  716. void AudioServer::set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled) {
  717. ERR_FAIL_INDEX(p_bus, buses.size());
  718. ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
  719. MARK_EDITED
  720. buses.write[p_bus]->effects.write[p_effect].enabled = p_enabled;
  721. }
  722. bool AudioServer::is_bus_effect_enabled(int p_bus, int p_effect) const {
  723. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  724. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), false);
  725. return buses[p_bus]->effects[p_effect].enabled;
  726. }
  727. float AudioServer::get_bus_peak_volume_left_db(int p_bus, int p_channel) const {
  728. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  729. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
  730. return buses[p_bus]->channels[p_channel].peak_volume.l;
  731. }
  732. float AudioServer::get_bus_peak_volume_right_db(int p_bus, int p_channel) const {
  733. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  734. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
  735. return buses[p_bus]->channels[p_channel].peak_volume.r;
  736. }
  737. bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
  738. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  739. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), false);
  740. return buses[p_bus]->channels[p_channel].active;
  741. }
  742. void AudioServer::set_global_rate_scale(float p_scale) {
  743. ERR_FAIL_COND(p_scale <= 0);
  744. global_rate_scale = p_scale;
  745. }
  746. float AudioServer::get_global_rate_scale() const {
  747. return global_rate_scale;
  748. }
  749. void AudioServer::init_channels_and_buffers() {
  750. channel_count = get_channel_count();
  751. temp_buffer.resize(channel_count);
  752. for (int i = 0; i < temp_buffer.size(); i++) {
  753. temp_buffer.write[i].resize(buffer_size);
  754. }
  755. for (int i = 0; i < buses.size(); i++) {
  756. buses[i]->channels.resize(channel_count);
  757. for (int j = 0; j < channel_count; j++) {
  758. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  759. }
  760. _update_bus_effects(i);
  761. }
  762. }
  763. void AudioServer::init() {
  764. channel_disable_threshold_db = GLOBAL_DEF_RST("audio/channel_disable_threshold_db", -60.0);
  765. channel_disable_frames = float(GLOBAL_DEF_RST("audio/channel_disable_time", 2.0)) * get_mix_rate();
  766. ProjectSettings::get_singleton()->set_custom_property_info("audio/channel_disable_time", PropertyInfo(Variant::REAL, "audio/channel_disable_time", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  767. buffer_size = 1024; //hardcoded for now
  768. init_channels_and_buffers();
  769. mix_count = 0;
  770. set_bus_count(1);
  771. set_bus_name(0, "Master");
  772. if (AudioDriver::get_singleton()) {
  773. AudioDriver::get_singleton()->start();
  774. }
  775. #ifdef TOOLS_ENABLED
  776. set_edited(false); //avoid editors from thinking this was edited
  777. #endif
  778. GLOBAL_DEF_RST("audio/video_delay_compensation_ms", 0);
  779. }
  780. void AudioServer::update() {
  781. #ifdef DEBUG_ENABLED
  782. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  783. // Driver time includes server time + effects times
  784. // Server time includes effects times
  785. uint64_t driver_time = AudioDriver::get_singleton()->get_profiling_time();
  786. uint64_t server_time = prof_time;
  787. // Subtract the server time from the driver time
  788. if (driver_time > server_time) {
  789. driver_time -= server_time;
  790. }
  791. Array values;
  792. for (int i = buses.size() - 1; i >= 0; i--) {
  793. Bus *bus = buses[i];
  794. if (bus->bypass) {
  795. continue;
  796. }
  797. for (int j = 0; j < bus->effects.size(); j++) {
  798. if (!bus->effects[j].enabled) {
  799. continue;
  800. }
  801. values.push_back(String(bus->name) + bus->effects[j].effect->get_name());
  802. values.push_back(USEC_TO_SEC(bus->effects[j].prof_time));
  803. // Subtract the effect time from the driver and server times
  804. if (driver_time > bus->effects[j].prof_time) {
  805. driver_time -= bus->effects[j].prof_time;
  806. }
  807. if (server_time > bus->effects[j].prof_time) {
  808. server_time -= bus->effects[j].prof_time;
  809. }
  810. }
  811. }
  812. values.push_back("audio_server");
  813. values.push_back(USEC_TO_SEC(server_time));
  814. values.push_back("audio_driver");
  815. values.push_back(USEC_TO_SEC(driver_time));
  816. ScriptDebugger::get_singleton()->add_profiling_frame_data("audio_thread", values);
  817. }
  818. // Reset profiling times
  819. for (int i = buses.size() - 1; i >= 0; i--) {
  820. Bus *bus = buses[i];
  821. if (bus->bypass) {
  822. continue;
  823. }
  824. for (int j = 0; j < bus->effects.size(); j++) {
  825. if (!bus->effects[j].enabled) {
  826. continue;
  827. }
  828. bus->effects.write[j].prof_time = 0;
  829. }
  830. }
  831. AudioDriver::get_singleton()->reset_profiling_time();
  832. prof_time = 0;
  833. #endif
  834. for (Set<CallbackItem>::Element *E = update_callbacks.front(); E; E = E->next()) {
  835. E->get().callback(E->get().userdata);
  836. }
  837. }
  838. void AudioServer::load_default_bus_layout() {
  839. String layout_path = ProjectSettings::get_singleton()->get("audio/default_bus_layout");
  840. if (ResourceLoader::exists(layout_path)) {
  841. Ref<AudioBusLayout> default_layout = ResourceLoader::load(layout_path);
  842. if (default_layout.is_valid()) {
  843. set_bus_layout(default_layout);
  844. }
  845. }
  846. }
  847. void AudioServer::finish() {
  848. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  849. AudioDriverManager::get_driver(i)->finish();
  850. }
  851. for (int i = 0; i < buses.size(); i++) {
  852. memdelete(buses[i]);
  853. }
  854. buses.clear();
  855. }
  856. /* MISC config */
  857. void AudioServer::lock() {
  858. AudioDriver::get_singleton()->lock();
  859. }
  860. void AudioServer::unlock() {
  861. AudioDriver::get_singleton()->unlock();
  862. }
  863. AudioServer::SpeakerMode AudioServer::get_speaker_mode() const {
  864. return (AudioServer::SpeakerMode)AudioDriver::get_singleton()->get_speaker_mode();
  865. }
  866. float AudioServer::get_mix_rate() const {
  867. return AudioDriver::get_singleton()->get_mix_rate();
  868. }
  869. float AudioServer::read_output_peak_db() const {
  870. return 0;
  871. }
  872. AudioServer *AudioServer::get_singleton() {
  873. return singleton;
  874. }
  875. double AudioServer::get_output_latency() const {
  876. return AudioDriver::get_singleton()->get_latency();
  877. }
  878. double AudioServer::get_time_to_next_mix() const {
  879. return AudioDriver::get_singleton()->get_time_to_next_mix();
  880. }
  881. double AudioServer::get_time_since_last_mix() const {
  882. return AudioDriver::get_singleton()->get_time_since_last_mix();
  883. }
  884. AudioServer *AudioServer::singleton = nullptr;
  885. void *AudioServer::audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data) {
  886. void *ad = memalloc(p_data_len);
  887. ERR_FAIL_COND_V(!ad, nullptr);
  888. if (p_from_data) {
  889. memcpy(ad, p_from_data, p_data_len);
  890. }
  891. audio_data_lock.lock();
  892. audio_data[ad] = p_data_len;
  893. audio_data_total_mem += p_data_len;
  894. audio_data_max_mem = MAX(audio_data_total_mem, audio_data_max_mem);
  895. audio_data_lock.unlock();
  896. return ad;
  897. }
  898. void AudioServer::audio_data_free(void *p_data) {
  899. audio_data_lock.lock();
  900. if (!audio_data.has(p_data)) {
  901. audio_data_lock.unlock();
  902. ERR_FAIL();
  903. }
  904. audio_data_total_mem -= audio_data[p_data];
  905. audio_data.erase(p_data);
  906. memfree(p_data);
  907. audio_data_lock.unlock();
  908. }
  909. size_t AudioServer::audio_data_get_total_memory_usage() const {
  910. return audio_data_total_mem;
  911. }
  912. size_t AudioServer::audio_data_get_max_memory_usage() const {
  913. return audio_data_max_mem;
  914. }
  915. void AudioServer::add_callback(AudioCallback p_callback, void *p_userdata) {
  916. lock();
  917. CallbackItem ci;
  918. ci.callback = p_callback;
  919. ci.userdata = p_userdata;
  920. callbacks.insert(ci);
  921. unlock();
  922. }
  923. void AudioServer::remove_callback(AudioCallback p_callback, void *p_userdata) {
  924. lock();
  925. CallbackItem ci;
  926. ci.callback = p_callback;
  927. ci.userdata = p_userdata;
  928. callbacks.erase(ci);
  929. unlock();
  930. }
  931. void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) {
  932. lock();
  933. CallbackItem ci;
  934. ci.callback = p_callback;
  935. ci.userdata = p_userdata;
  936. update_callbacks.insert(ci);
  937. unlock();
  938. }
  939. void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) {
  940. lock();
  941. CallbackItem ci;
  942. ci.callback = p_callback;
  943. ci.userdata = p_userdata;
  944. update_callbacks.erase(ci);
  945. unlock();
  946. }
  947. void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
  948. ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.size() == 0);
  949. lock();
  950. for (int i = 0; i < buses.size(); i++) {
  951. memdelete(buses[i]);
  952. }
  953. buses.resize(p_bus_layout->buses.size());
  954. bus_map.clear();
  955. for (int i = 0; i < p_bus_layout->buses.size(); i++) {
  956. Bus *bus = memnew(Bus);
  957. if (i == 0) {
  958. bus->name = "Master";
  959. } else {
  960. bus->name = p_bus_layout->buses[i].name;
  961. bus->send = p_bus_layout->buses[i].send;
  962. }
  963. bus->solo = p_bus_layout->buses[i].solo;
  964. bus->mute = p_bus_layout->buses[i].mute;
  965. bus->bypass = p_bus_layout->buses[i].bypass;
  966. bus->volume_db = p_bus_layout->buses[i].volume_db;
  967. for (int j = 0; j < p_bus_layout->buses[i].effects.size(); j++) {
  968. Ref<AudioEffect> fx = p_bus_layout->buses[i].effects[j].effect;
  969. if (fx.is_valid()) {
  970. Bus::Effect bfx;
  971. bfx.effect = fx;
  972. bfx.enabled = p_bus_layout->buses[i].effects[j].enabled;
  973. bus->effects.push_back(bfx);
  974. }
  975. }
  976. bus_map[bus->name] = bus;
  977. buses.write[i] = bus;
  978. buses[i]->channels.resize(channel_count);
  979. for (int j = 0; j < channel_count; j++) {
  980. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  981. }
  982. _update_bus_effects(i);
  983. }
  984. #ifdef TOOLS_ENABLED
  985. set_edited(false);
  986. #endif
  987. unlock();
  988. }
  989. Ref<AudioBusLayout> AudioServer::generate_bus_layout() const {
  990. Ref<AudioBusLayout> state;
  991. state.instance();
  992. state->buses.resize(buses.size());
  993. for (int i = 0; i < buses.size(); i++) {
  994. state->buses.write[i].name = buses[i]->name;
  995. state->buses.write[i].send = buses[i]->send;
  996. state->buses.write[i].mute = buses[i]->mute;
  997. state->buses.write[i].solo = buses[i]->solo;
  998. state->buses.write[i].bypass = buses[i]->bypass;
  999. state->buses.write[i].volume_db = buses[i]->volume_db;
  1000. for (int j = 0; j < buses[i]->effects.size(); j++) {
  1001. AudioBusLayout::Bus::Effect fx;
  1002. fx.effect = buses[i]->effects[j].effect;
  1003. fx.enabled = buses[i]->effects[j].enabled;
  1004. state->buses.write[i].effects.push_back(fx);
  1005. }
  1006. }
  1007. return state;
  1008. }
  1009. Array AudioServer::get_device_list() {
  1010. return AudioDriver::get_singleton()->get_device_list();
  1011. }
  1012. String AudioServer::get_device() {
  1013. return AudioDriver::get_singleton()->get_device();
  1014. }
  1015. void AudioServer::set_device(String device) {
  1016. AudioDriver::get_singleton()->set_device(device);
  1017. }
  1018. Array AudioServer::capture_get_device_list() {
  1019. return AudioDriver::get_singleton()->capture_get_device_list();
  1020. }
  1021. String AudioServer::capture_get_device() {
  1022. return AudioDriver::get_singleton()->capture_get_device();
  1023. }
  1024. void AudioServer::capture_set_device(const String &p_name) {
  1025. AudioDriver::get_singleton()->capture_set_device(p_name);
  1026. }
  1027. void AudioServer::_bind_methods() {
  1028. ClassDB::bind_method(D_METHOD("set_bus_count", "amount"), &AudioServer::set_bus_count);
  1029. ClassDB::bind_method(D_METHOD("get_bus_count"), &AudioServer::get_bus_count);
  1030. ClassDB::bind_method(D_METHOD("remove_bus", "index"), &AudioServer::remove_bus);
  1031. ClassDB::bind_method(D_METHOD("add_bus", "at_position"), &AudioServer::add_bus, DEFVAL(-1));
  1032. ClassDB::bind_method(D_METHOD("move_bus", "index", "to_index"), &AudioServer::move_bus);
  1033. ClassDB::bind_method(D_METHOD("set_bus_name", "bus_idx", "name"), &AudioServer::set_bus_name);
  1034. ClassDB::bind_method(D_METHOD("get_bus_name", "bus_idx"), &AudioServer::get_bus_name);
  1035. ClassDB::bind_method(D_METHOD("get_bus_index", "bus_name"), &AudioServer::get_bus_index);
  1036. ClassDB::bind_method(D_METHOD("get_bus_channels", "bus_idx"), &AudioServer::get_bus_channels);
  1037. ClassDB::bind_method(D_METHOD("set_bus_volume_db", "bus_idx", "volume_db"), &AudioServer::set_bus_volume_db);
  1038. ClassDB::bind_method(D_METHOD("get_bus_volume_db", "bus_idx"), &AudioServer::get_bus_volume_db);
  1039. ClassDB::bind_method(D_METHOD("set_bus_send", "bus_idx", "send"), &AudioServer::set_bus_send);
  1040. ClassDB::bind_method(D_METHOD("get_bus_send", "bus_idx"), &AudioServer::get_bus_send);
  1041. ClassDB::bind_method(D_METHOD("set_bus_solo", "bus_idx", "enable"), &AudioServer::set_bus_solo);
  1042. ClassDB::bind_method(D_METHOD("is_bus_solo", "bus_idx"), &AudioServer::is_bus_solo);
  1043. ClassDB::bind_method(D_METHOD("set_bus_mute", "bus_idx", "enable"), &AudioServer::set_bus_mute);
  1044. ClassDB::bind_method(D_METHOD("is_bus_mute", "bus_idx"), &AudioServer::is_bus_mute);
  1045. ClassDB::bind_method(D_METHOD("set_bus_bypass_effects", "bus_idx", "enable"), &AudioServer::set_bus_bypass_effects);
  1046. ClassDB::bind_method(D_METHOD("is_bus_bypassing_effects", "bus_idx"), &AudioServer::is_bus_bypassing_effects);
  1047. ClassDB::bind_method(D_METHOD("add_bus_effect", "bus_idx", "effect", "at_position"), &AudioServer::add_bus_effect, DEFVAL(-1));
  1048. ClassDB::bind_method(D_METHOD("remove_bus_effect", "bus_idx", "effect_idx"), &AudioServer::remove_bus_effect);
  1049. ClassDB::bind_method(D_METHOD("get_bus_effect_count", "bus_idx"), &AudioServer::get_bus_effect_count);
  1050. ClassDB::bind_method(D_METHOD("get_bus_effect", "bus_idx", "effect_idx"), &AudioServer::get_bus_effect);
  1051. ClassDB::bind_method(D_METHOD("get_bus_effect_instance", "bus_idx", "effect_idx", "channel"), &AudioServer::get_bus_effect_instance, DEFVAL(0));
  1052. ClassDB::bind_method(D_METHOD("swap_bus_effects", "bus_idx", "effect_idx", "by_effect_idx"), &AudioServer::swap_bus_effects);
  1053. ClassDB::bind_method(D_METHOD("set_bus_effect_enabled", "bus_idx", "effect_idx", "enabled"), &AudioServer::set_bus_effect_enabled);
  1054. ClassDB::bind_method(D_METHOD("is_bus_effect_enabled", "bus_idx", "effect_idx"), &AudioServer::is_bus_effect_enabled);
  1055. ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
  1056. ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
  1057. ClassDB::bind_method(D_METHOD("set_global_rate_scale", "scale"), &AudioServer::set_global_rate_scale);
  1058. ClassDB::bind_method(D_METHOD("get_global_rate_scale"), &AudioServer::get_global_rate_scale);
  1059. ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
  1060. ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
  1061. ClassDB::bind_method(D_METHOD("get_speaker_mode"), &AudioServer::get_speaker_mode);
  1062. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioServer::get_mix_rate);
  1063. ClassDB::bind_method(D_METHOD("get_device_list"), &AudioServer::get_device_list);
  1064. ClassDB::bind_method(D_METHOD("get_device"), &AudioServer::get_device);
  1065. ClassDB::bind_method(D_METHOD("set_device", "device"), &AudioServer::set_device);
  1066. ClassDB::bind_method(D_METHOD("get_time_to_next_mix"), &AudioServer::get_time_to_next_mix);
  1067. ClassDB::bind_method(D_METHOD("get_time_since_last_mix"), &AudioServer::get_time_since_last_mix);
  1068. ClassDB::bind_method(D_METHOD("get_output_latency"), &AudioServer::get_output_latency);
  1069. ClassDB::bind_method(D_METHOD("capture_get_device_list"), &AudioServer::capture_get_device_list);
  1070. ClassDB::bind_method(D_METHOD("capture_get_device"), &AudioServer::capture_get_device);
  1071. ClassDB::bind_method(D_METHOD("capture_set_device", "name"), &AudioServer::capture_set_device);
  1072. ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout);
  1073. ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout);
  1074. ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
  1075. ADD_PROPERTY(PropertyInfo(Variant::STRING, "device"), "set_device", "get_device");
  1076. ADD_PROPERTY(PropertyInfo(Variant::STRING, "capture_device"), "capture_set_device", "capture_get_device");
  1077. // The default value may be set to an empty string by the platform-specific audio driver.
  1078. // Override for class reference generation purposes.
  1079. ADD_PROPERTY_DEFAULT("capture_device", "Default");
  1080. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rate_scale"), "set_global_rate_scale", "get_global_rate_scale");
  1081. ADD_SIGNAL(MethodInfo("bus_layout_changed"));
  1082. BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
  1083. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_31);
  1084. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_51);
  1085. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_71);
  1086. }
  1087. AudioServer::AudioServer() {
  1088. singleton = this;
  1089. audio_data_total_mem = 0;
  1090. audio_data_max_mem = 0;
  1091. mix_frames = 0;
  1092. channel_count = 0;
  1093. to_mix = 0;
  1094. #ifdef DEBUG_ENABLED
  1095. prof_time = 0;
  1096. #endif
  1097. mix_time = 0;
  1098. mix_size = 0;
  1099. global_rate_scale = 1;
  1100. }
  1101. AudioServer::~AudioServer() {
  1102. singleton = nullptr;
  1103. }
  1104. /////////////////////////////////
  1105. bool AudioBusLayout::_set(const StringName &p_name, const Variant &p_value) {
  1106. String s = p_name;
  1107. if (s.begins_with("bus/")) {
  1108. int index = s.get_slice("/", 1).to_int();
  1109. if (buses.size() <= index) {
  1110. buses.resize(index + 1);
  1111. }
  1112. Bus &bus = buses.write[index];
  1113. String what = s.get_slice("/", 2);
  1114. if (what == "name") {
  1115. bus.name = p_value;
  1116. } else if (what == "solo") {
  1117. bus.solo = p_value;
  1118. } else if (what == "mute") {
  1119. bus.mute = p_value;
  1120. } else if (what == "bypass_fx") {
  1121. bus.bypass = p_value;
  1122. } else if (what == "volume_db") {
  1123. bus.volume_db = p_value;
  1124. } else if (what == "send") {
  1125. bus.send = p_value;
  1126. } else if (what == "effect") {
  1127. int which = s.get_slice("/", 3).to_int();
  1128. if (bus.effects.size() <= which) {
  1129. bus.effects.resize(which + 1);
  1130. }
  1131. Bus::Effect &fx = bus.effects.write[which];
  1132. String fxwhat = s.get_slice("/", 4);
  1133. if (fxwhat == "effect") {
  1134. fx.effect = p_value;
  1135. } else if (fxwhat == "enabled") {
  1136. fx.enabled = p_value;
  1137. } else {
  1138. return false;
  1139. }
  1140. return true;
  1141. } else {
  1142. return false;
  1143. }
  1144. return true;
  1145. }
  1146. return false;
  1147. }
  1148. bool AudioBusLayout::_get(const StringName &p_name, Variant &r_ret) const {
  1149. String s = p_name;
  1150. if (s.begins_with("bus/")) {
  1151. int index = s.get_slice("/", 1).to_int();
  1152. if (index < 0 || index >= buses.size()) {
  1153. return false;
  1154. }
  1155. const Bus &bus = buses[index];
  1156. String what = s.get_slice("/", 2);
  1157. if (what == "name") {
  1158. r_ret = bus.name;
  1159. } else if (what == "solo") {
  1160. r_ret = bus.solo;
  1161. } else if (what == "mute") {
  1162. r_ret = bus.mute;
  1163. } else if (what == "bypass_fx") {
  1164. r_ret = bus.bypass;
  1165. } else if (what == "volume_db") {
  1166. r_ret = bus.volume_db;
  1167. } else if (what == "send") {
  1168. r_ret = bus.send;
  1169. } else if (what == "effect") {
  1170. int which = s.get_slice("/", 3).to_int();
  1171. if (which < 0 || which >= bus.effects.size()) {
  1172. return false;
  1173. }
  1174. const Bus::Effect &fx = bus.effects[which];
  1175. String fxwhat = s.get_slice("/", 4);
  1176. if (fxwhat == "effect") {
  1177. r_ret = fx.effect;
  1178. } else if (fxwhat == "enabled") {
  1179. r_ret = fx.enabled;
  1180. } else {
  1181. return false;
  1182. }
  1183. return true;
  1184. } else {
  1185. return false;
  1186. }
  1187. return true;
  1188. }
  1189. return false;
  1190. }
  1191. void AudioBusLayout::_get_property_list(List<PropertyInfo> *p_list) const {
  1192. for (int i = 0; i < buses.size(); i++) {
  1193. p_list->push_back(PropertyInfo(Variant::STRING, "bus/" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1194. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/solo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1195. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/mute", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1196. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/bypass_fx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1197. p_list->push_back(PropertyInfo(Variant::REAL, "bus/" + itos(i) + "/volume_db", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1198. p_list->push_back(PropertyInfo(Variant::REAL, "bus/" + itos(i) + "/send", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1199. for (int j = 0; j < buses[i].effects.size(); j++) {
  1200. p_list->push_back(PropertyInfo(Variant::OBJECT, "bus/" + itos(i) + "/effect/" + itos(j) + "/effect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1201. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/effect/" + itos(j) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1202. }
  1203. }
  1204. }
  1205. AudioBusLayout::AudioBusLayout() {
  1206. buses.resize(1);
  1207. buses.write[0].name = "Master";
  1208. }