audio_server.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. /*************************************************************************/
  2. /* audio_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. for (int k = 0; k < cs; k++) {
  220. if (master->channels[k].active) {
  221. const AudioFrame *buf = master->channels[k].buffer.ptr();
  222. for (int j = 0; j < to_copy; j++) {
  223. float l = CLAMP(buf[from + j].l, -1.0, 1.0);
  224. int32_t vl = l * ((1 << 20) - 1);
  225. int32_t vl2 = (vl < 0 ? -1 : 1) * (ABS(vl) << 11);
  226. p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 0] = vl2;
  227. float r = CLAMP(buf[from + j].r, -1.0, 1.0);
  228. int32_t vr = r * ((1 << 20) - 1);
  229. int32_t vr2 = (vr < 0 ? -1 : 1) * (ABS(vr) << 11);
  230. p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 1] = vr2;
  231. }
  232. } else {
  233. for (int j = 0; j < to_copy; j++) {
  234. p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 0] = 0;
  235. p_buffer[(from_buf + j) * (cs * 2) + k * 2 + 1] = 0;
  236. }
  237. }
  238. }
  239. todo -= to_copy;
  240. to_mix -= to_copy;
  241. }
  242. #ifdef DEBUG_ENABLED
  243. prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks;
  244. #endif
  245. }
  246. void AudioServer::_mix_step() {
  247. bool solo_mode = false;
  248. for (int i = 0; i < buses.size(); i++) {
  249. Bus *bus = buses[i];
  250. bus->index_cache = i; //might be moved around by editor, so..
  251. for (int k = 0; k < bus->channels.size(); k++) {
  252. bus->channels.write[k].used = false;
  253. }
  254. if (bus->solo) {
  255. //solo chain
  256. solo_mode = true;
  257. bus->soloed = true;
  258. do {
  259. if (bus != buses[0]) {
  260. //everything has a send save for master bus
  261. if (!bus_map.has(bus->send)) {
  262. bus = buses[0]; //send to master
  263. } else {
  264. int prev_index_cache = bus->index_cache;
  265. bus = bus_map[bus->send];
  266. if (prev_index_cache >= bus->index_cache) { //invalid, send to master
  267. bus = buses[0];
  268. }
  269. }
  270. bus->soloed = true;
  271. } else {
  272. bus = nullptr;
  273. }
  274. } while (bus);
  275. } else {
  276. bus->soloed = false;
  277. }
  278. }
  279. //make callbacks for mixing the audio
  280. for (Set<CallbackItem>::Element *E = callbacks.front(); E; E = E->next()) {
  281. E->get().callback(E->get().userdata);
  282. }
  283. for (int i = buses.size() - 1; i >= 0; i--) {
  284. //go bus by bus
  285. Bus *bus = buses[i];
  286. for (int k = 0; k < bus->channels.size(); k++) {
  287. if (bus->channels[k].active && !bus->channels[k].used) {
  288. //buffer was not used, but it's still active, so it must be cleaned
  289. AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
  290. for (uint32_t j = 0; j < buffer_size; j++) {
  291. buf[j] = AudioFrame(0, 0);
  292. }
  293. }
  294. }
  295. //process effects
  296. if (!bus->bypass) {
  297. for (int j = 0; j < bus->effects.size(); j++) {
  298. if (!bus->effects[j].enabled) {
  299. continue;
  300. }
  301. #ifdef DEBUG_ENABLED
  302. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  303. #endif
  304. for (int k = 0; k < bus->channels.size(); k++) {
  305. if (!(bus->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
  306. continue;
  307. }
  308. bus->channels.write[k].effect_instances.write[j]->process(bus->channels[k].buffer.ptr(), temp_buffer.write[k].ptrw(), buffer_size);
  309. }
  310. //swap buffers, so internal buffer always has the right data
  311. for (int k = 0; k < bus->channels.size(); k++) {
  312. if (!(buses[i]->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
  313. continue;
  314. }
  315. SWAP(bus->channels.write[k].buffer, temp_buffer.write[k]);
  316. }
  317. #ifdef DEBUG_ENABLED
  318. bus->effects.write[j].prof_time += OS::get_singleton()->get_ticks_usec() - ticks;
  319. #endif
  320. }
  321. }
  322. //process send
  323. Bus *send = nullptr;
  324. if (i > 0) {
  325. //everything has a send save for master bus
  326. if (!bus_map.has(bus->send)) {
  327. send = buses[0];
  328. } else {
  329. send = bus_map[bus->send];
  330. if (send->index_cache >= bus->index_cache) { //invalid, send to master
  331. send = buses[0];
  332. }
  333. }
  334. }
  335. for (int k = 0; k < bus->channels.size(); k++) {
  336. if (!bus->channels[k].active) {
  337. bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
  338. continue;
  339. }
  340. AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
  341. AudioFrame peak = AudioFrame(0, 0);
  342. float volume = Math::db2linear(bus->volume_db);
  343. if (solo_mode) {
  344. if (!bus->soloed) {
  345. volume = 0.0;
  346. }
  347. } else {
  348. if (bus->mute) {
  349. volume = 0.0;
  350. }
  351. }
  352. //apply volume and compute peak
  353. for (uint32_t j = 0; j < buffer_size; j++) {
  354. buf[j] *= volume;
  355. float l = ABS(buf[j].l);
  356. if (l > peak.l) {
  357. peak.l = l;
  358. }
  359. float r = ABS(buf[j].r);
  360. if (r > peak.r) {
  361. peak.r = r;
  362. }
  363. }
  364. bus->channels.write[k].peak_volume = AudioFrame(Math::linear2db(peak.l + AUDIO_PEAK_OFFSET), Math::linear2db(peak.r + AUDIO_PEAK_OFFSET));
  365. if (!bus->channels[k].used) {
  366. //see if any audio is contained, because channel was not used
  367. if (MAX(peak.r, peak.l) > Math::db2linear(channel_disable_threshold_db)) {
  368. bus->channels.write[k].last_mix_with_audio = mix_frames;
  369. } else if (mix_frames - bus->channels[k].last_mix_with_audio > channel_disable_frames) {
  370. bus->channels.write[k].active = false;
  371. continue; //went inactive, don't mix.
  372. }
  373. }
  374. if (send) {
  375. //if not master bus, send
  376. AudioFrame *target_buf = thread_get_channel_mix_buffer(send->index_cache, k);
  377. for (uint32_t j = 0; j < buffer_size; j++) {
  378. target_buf[j] += buf[j];
  379. }
  380. }
  381. }
  382. }
  383. mix_frames += buffer_size;
  384. to_mix = buffer_size;
  385. }
  386. bool AudioServer::thread_has_channel_mix_buffer(int p_bus, int p_buffer) const {
  387. if (p_bus < 0 || p_bus >= buses.size()) {
  388. return false;
  389. }
  390. if (p_buffer < 0 || p_buffer >= buses[p_bus]->channels.size()) {
  391. return false;
  392. }
  393. return true;
  394. }
  395. AudioFrame *AudioServer::thread_get_channel_mix_buffer(int p_bus, int p_buffer) {
  396. ERR_FAIL_INDEX_V(p_bus, buses.size(), nullptr);
  397. ERR_FAIL_INDEX_V(p_buffer, buses[p_bus]->channels.size(), nullptr);
  398. AudioFrame *data = buses.write[p_bus]->channels.write[p_buffer].buffer.ptrw();
  399. if (!buses[p_bus]->channels[p_buffer].used) {
  400. buses.write[p_bus]->channels.write[p_buffer].used = true;
  401. buses.write[p_bus]->channels.write[p_buffer].active = true;
  402. buses.write[p_bus]->channels.write[p_buffer].last_mix_with_audio = mix_frames;
  403. for (uint32_t i = 0; i < buffer_size; i++) {
  404. data[i] = AudioFrame(0, 0);
  405. }
  406. }
  407. return data;
  408. }
  409. int AudioServer::thread_get_mix_buffer_size() const {
  410. return buffer_size;
  411. }
  412. int AudioServer::thread_find_bus_index(const StringName &p_name) {
  413. if (bus_map.has(p_name)) {
  414. return bus_map[p_name]->index_cache;
  415. } else {
  416. return 0;
  417. }
  418. }
  419. void AudioServer::set_bus_count(int p_count) {
  420. ERR_FAIL_COND(p_count < 1);
  421. ERR_FAIL_INDEX(p_count, 256);
  422. MARK_EDITED
  423. lock();
  424. int cb = buses.size();
  425. if (p_count < buses.size()) {
  426. for (int i = p_count; i < buses.size(); i++) {
  427. bus_map.erase(buses[i]->name);
  428. memdelete(buses[i]);
  429. }
  430. }
  431. buses.resize(p_count);
  432. for (int i = cb; i < buses.size(); i++) {
  433. String attempt = "New Bus";
  434. int attempts = 1;
  435. while (true) {
  436. bool name_free = true;
  437. for (int j = 0; j < i; j++) {
  438. if (buses[j]->name == attempt) {
  439. name_free = false;
  440. break;
  441. }
  442. }
  443. if (!name_free) {
  444. attempts++;
  445. attempt = "New Bus " + itos(attempts);
  446. } else {
  447. break;
  448. }
  449. }
  450. buses.write[i] = memnew(Bus);
  451. buses.write[i]->channels.resize(channel_count);
  452. for (int j = 0; j < channel_count; j++) {
  453. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  454. }
  455. buses[i]->name = attempt;
  456. buses[i]->solo = false;
  457. buses[i]->mute = false;
  458. buses[i]->bypass = false;
  459. buses[i]->volume_db = 0;
  460. if (i > 0) {
  461. buses[i]->send = "Master";
  462. }
  463. bus_map[attempt] = buses[i];
  464. }
  465. unlock();
  466. emit_signal("bus_layout_changed");
  467. }
  468. void AudioServer::remove_bus(int p_index) {
  469. ERR_FAIL_INDEX(p_index, buses.size());
  470. ERR_FAIL_COND(p_index == 0);
  471. MARK_EDITED
  472. lock();
  473. bus_map.erase(buses[p_index]->name);
  474. memdelete(buses[p_index]);
  475. buses.remove(p_index);
  476. unlock();
  477. emit_signal("bus_layout_changed");
  478. }
  479. void AudioServer::add_bus(int p_at_pos) {
  480. MARK_EDITED
  481. if (p_at_pos >= buses.size()) {
  482. p_at_pos = -1;
  483. } else if (p_at_pos == 0) {
  484. if (buses.size() > 1) {
  485. p_at_pos = 1;
  486. } else {
  487. p_at_pos = -1;
  488. }
  489. }
  490. String attempt = "New Bus";
  491. int attempts = 1;
  492. while (true) {
  493. bool name_free = true;
  494. for (int j = 0; j < buses.size(); j++) {
  495. if (buses[j]->name == attempt) {
  496. name_free = false;
  497. break;
  498. }
  499. }
  500. if (!name_free) {
  501. attempts++;
  502. attempt = "New Bus " + itos(attempts);
  503. } else {
  504. break;
  505. }
  506. }
  507. Bus *bus = memnew(Bus);
  508. bus->channels.resize(channel_count);
  509. for (int j = 0; j < channel_count; j++) {
  510. bus->channels.write[j].buffer.resize(buffer_size);
  511. }
  512. bus->name = attempt;
  513. bus->solo = false;
  514. bus->mute = false;
  515. bus->bypass = false;
  516. bus->volume_db = 0;
  517. bus_map[attempt] = bus;
  518. if (p_at_pos == -1) {
  519. buses.push_back(bus);
  520. } else {
  521. buses.insert(p_at_pos, bus);
  522. }
  523. emit_signal("bus_layout_changed");
  524. }
  525. void AudioServer::move_bus(int p_bus, int p_to_pos) {
  526. ERR_FAIL_COND(p_bus < 1 || p_bus >= buses.size());
  527. ERR_FAIL_COND(p_to_pos != -1 && (p_to_pos < 1 || p_to_pos > buses.size()));
  528. MARK_EDITED
  529. if (p_bus == p_to_pos) {
  530. return;
  531. }
  532. Bus *bus = buses[p_bus];
  533. buses.remove(p_bus);
  534. if (p_to_pos == -1) {
  535. buses.push_back(bus);
  536. } else if (p_to_pos < p_bus) {
  537. buses.insert(p_to_pos, bus);
  538. } else {
  539. buses.insert(p_to_pos - 1, bus);
  540. }
  541. emit_signal("bus_layout_changed");
  542. }
  543. int AudioServer::get_bus_count() const {
  544. return buses.size();
  545. }
  546. void AudioServer::set_bus_name(int p_bus, const String &p_name) {
  547. ERR_FAIL_INDEX(p_bus, buses.size());
  548. if (p_bus == 0 && p_name != "Master") {
  549. return; //bus 0 is always master
  550. }
  551. MARK_EDITED
  552. lock();
  553. if (buses[p_bus]->name == p_name) {
  554. unlock();
  555. return;
  556. }
  557. String attempt = p_name;
  558. int attempts = 1;
  559. while (true) {
  560. bool name_free = true;
  561. for (int i = 0; i < buses.size(); i++) {
  562. if (buses[i]->name == attempt) {
  563. name_free = false;
  564. break;
  565. }
  566. }
  567. if (name_free) {
  568. break;
  569. }
  570. attempts++;
  571. attempt = p_name + " " + itos(attempts);
  572. }
  573. bus_map.erase(buses[p_bus]->name);
  574. buses[p_bus]->name = attempt;
  575. bus_map[attempt] = buses[p_bus];
  576. unlock();
  577. emit_signal("bus_layout_changed");
  578. }
  579. String AudioServer::get_bus_name(int p_bus) const {
  580. ERR_FAIL_INDEX_V(p_bus, buses.size(), String());
  581. return buses[p_bus]->name;
  582. }
  583. int AudioServer::get_bus_index(const StringName &p_bus_name) const {
  584. for (int i = 0; i < buses.size(); ++i) {
  585. if (buses[i]->name == p_bus_name) {
  586. return i;
  587. }
  588. }
  589. return -1;
  590. }
  591. void AudioServer::set_bus_volume_db(int p_bus, float p_volume_db) {
  592. ERR_FAIL_INDEX(p_bus, buses.size());
  593. MARK_EDITED
  594. buses[p_bus]->volume_db = p_volume_db;
  595. }
  596. float AudioServer::get_bus_volume_db(int p_bus) const {
  597. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  598. return buses[p_bus]->volume_db;
  599. }
  600. int AudioServer::get_bus_channels(int p_bus) const {
  601. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  602. return buses[p_bus]->channels.size();
  603. }
  604. void AudioServer::set_bus_send(int p_bus, const StringName &p_send) {
  605. ERR_FAIL_INDEX(p_bus, buses.size());
  606. MARK_EDITED
  607. buses[p_bus]->send = p_send;
  608. }
  609. StringName AudioServer::get_bus_send(int p_bus) const {
  610. ERR_FAIL_INDEX_V(p_bus, buses.size(), StringName());
  611. return buses[p_bus]->send;
  612. }
  613. void AudioServer::set_bus_solo(int p_bus, bool p_enable) {
  614. ERR_FAIL_INDEX(p_bus, buses.size());
  615. MARK_EDITED
  616. buses[p_bus]->solo = p_enable;
  617. }
  618. bool AudioServer::is_bus_solo(int p_bus) const {
  619. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  620. return buses[p_bus]->solo;
  621. }
  622. void AudioServer::set_bus_mute(int p_bus, bool p_enable) {
  623. ERR_FAIL_INDEX(p_bus, buses.size());
  624. MARK_EDITED
  625. buses[p_bus]->mute = p_enable;
  626. }
  627. bool AudioServer::is_bus_mute(int p_bus) const {
  628. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  629. return buses[p_bus]->mute;
  630. }
  631. void AudioServer::set_bus_bypass_effects(int p_bus, bool p_enable) {
  632. ERR_FAIL_INDEX(p_bus, buses.size());
  633. MARK_EDITED
  634. buses[p_bus]->bypass = p_enable;
  635. }
  636. bool AudioServer::is_bus_bypassing_effects(int p_bus) const {
  637. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  638. return buses[p_bus]->bypass;
  639. }
  640. void AudioServer::_update_bus_effects(int p_bus) {
  641. for (int i = 0; i < buses[p_bus]->channels.size(); i++) {
  642. buses.write[p_bus]->channels.write[i].effect_instances.resize(buses[p_bus]->effects.size());
  643. for (int j = 0; j < buses[p_bus]->effects.size(); j++) {
  644. Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instance();
  645. if (Object::cast_to<AudioEffectCompressorInstance>(*fx)) {
  646. Object::cast_to<AudioEffectCompressorInstance>(*fx)->set_current_channel(i);
  647. }
  648. buses.write[p_bus]->channels.write[i].effect_instances.write[j] = fx;
  649. }
  650. }
  651. }
  652. void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos) {
  653. ERR_FAIL_COND(p_effect.is_null());
  654. ERR_FAIL_INDEX(p_bus, buses.size());
  655. MARK_EDITED
  656. lock();
  657. Bus::Effect fx;
  658. fx.effect = p_effect;
  659. //fx.instance=p_effect->instance();
  660. fx.enabled = true;
  661. #ifdef DEBUG_ENABLED
  662. fx.prof_time = 0;
  663. #endif
  664. if (p_at_pos >= buses[p_bus]->effects.size() || p_at_pos < 0) {
  665. buses[p_bus]->effects.push_back(fx);
  666. } else {
  667. buses[p_bus]->effects.insert(p_at_pos, fx);
  668. }
  669. _update_bus_effects(p_bus);
  670. unlock();
  671. }
  672. void AudioServer::remove_bus_effect(int p_bus, int p_effect) {
  673. ERR_FAIL_INDEX(p_bus, buses.size());
  674. MARK_EDITED
  675. lock();
  676. buses[p_bus]->effects.remove(p_effect);
  677. _update_bus_effects(p_bus);
  678. unlock();
  679. }
  680. int AudioServer::get_bus_effect_count(int p_bus) {
  681. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  682. return buses[p_bus]->effects.size();
  683. }
  684. Ref<AudioEffectInstance> AudioServer::get_bus_effect_instance(int p_bus, int p_effect, int p_channel) {
  685. ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffectInstance>());
  686. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffectInstance>());
  687. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), Ref<AudioEffectInstance>());
  688. return buses[p_bus]->channels[p_channel].effect_instances[p_effect];
  689. }
  690. Ref<AudioEffect> AudioServer::get_bus_effect(int p_bus, int p_effect) {
  691. ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffect>());
  692. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffect>());
  693. return buses[p_bus]->effects[p_effect].effect;
  694. }
  695. void AudioServer::swap_bus_effects(int p_bus, int p_effect, int p_by_effect) {
  696. ERR_FAIL_INDEX(p_bus, buses.size());
  697. ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
  698. ERR_FAIL_INDEX(p_by_effect, buses[p_bus]->effects.size());
  699. MARK_EDITED
  700. lock();
  701. SWAP(buses.write[p_bus]->effects.write[p_effect], buses.write[p_bus]->effects.write[p_by_effect]);
  702. _update_bus_effects(p_bus);
  703. unlock();
  704. }
  705. void AudioServer::set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled) {
  706. ERR_FAIL_INDEX(p_bus, buses.size());
  707. ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
  708. MARK_EDITED
  709. buses.write[p_bus]->effects.write[p_effect].enabled = p_enabled;
  710. }
  711. bool AudioServer::is_bus_effect_enabled(int p_bus, int p_effect) const {
  712. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  713. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), false);
  714. return buses[p_bus]->effects[p_effect].enabled;
  715. }
  716. float AudioServer::get_bus_peak_volume_left_db(int p_bus, int p_channel) const {
  717. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  718. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
  719. return buses[p_bus]->channels[p_channel].peak_volume.l;
  720. }
  721. float AudioServer::get_bus_peak_volume_right_db(int p_bus, int p_channel) const {
  722. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  723. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
  724. return buses[p_bus]->channels[p_channel].peak_volume.r;
  725. }
  726. bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
  727. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  728. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), false);
  729. return buses[p_bus]->channels[p_channel].active;
  730. }
  731. void AudioServer::set_global_rate_scale(float p_scale) {
  732. ERR_FAIL_COND(p_scale <= 0);
  733. global_rate_scale = p_scale;
  734. }
  735. float AudioServer::get_global_rate_scale() const {
  736. return global_rate_scale;
  737. }
  738. void AudioServer::init_channels_and_buffers() {
  739. channel_count = get_channel_count();
  740. temp_buffer.resize(channel_count);
  741. for (int i = 0; i < temp_buffer.size(); i++) {
  742. temp_buffer.write[i].resize(buffer_size);
  743. }
  744. for (int i = 0; i < buses.size(); i++) {
  745. buses[i]->channels.resize(channel_count);
  746. for (int j = 0; j < channel_count; j++) {
  747. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  748. }
  749. _update_bus_effects(i);
  750. }
  751. }
  752. void AudioServer::init() {
  753. channel_disable_threshold_db = GLOBAL_DEF_RST("audio/channel_disable_threshold_db", -60.0);
  754. channel_disable_frames = float(GLOBAL_DEF_RST("audio/channel_disable_time", 2.0)) * get_mix_rate();
  755. 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"));
  756. buffer_size = 1024; //hardcoded for now
  757. init_channels_and_buffers();
  758. mix_count = 0;
  759. set_bus_count(1);
  760. set_bus_name(0, "Master");
  761. if (AudioDriver::get_singleton()) {
  762. AudioDriver::get_singleton()->start();
  763. }
  764. #ifdef TOOLS_ENABLED
  765. set_edited(false); //avoid editors from thinking this was edited
  766. #endif
  767. GLOBAL_DEF_RST("audio/video_delay_compensation_ms", 0);
  768. }
  769. void AudioServer::update() {
  770. #ifdef DEBUG_ENABLED
  771. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  772. // Driver time includes server time + effects times
  773. // Server time includes effects times
  774. uint64_t driver_time = AudioDriver::get_singleton()->get_profiling_time();
  775. uint64_t server_time = prof_time;
  776. // Subtract the server time from the driver time
  777. if (driver_time > server_time) {
  778. driver_time -= server_time;
  779. }
  780. Array values;
  781. for (int i = buses.size() - 1; i >= 0; i--) {
  782. Bus *bus = buses[i];
  783. if (bus->bypass) {
  784. continue;
  785. }
  786. for (int j = 0; j < bus->effects.size(); j++) {
  787. if (!bus->effects[j].enabled) {
  788. continue;
  789. }
  790. values.push_back(String(bus->name) + bus->effects[j].effect->get_name());
  791. values.push_back(USEC_TO_SEC(bus->effects[j].prof_time));
  792. // Subtract the effect time from the driver and server times
  793. if (driver_time > bus->effects[j].prof_time) {
  794. driver_time -= bus->effects[j].prof_time;
  795. }
  796. if (server_time > bus->effects[j].prof_time) {
  797. server_time -= bus->effects[j].prof_time;
  798. }
  799. }
  800. }
  801. values.push_back("audio_server");
  802. values.push_back(USEC_TO_SEC(server_time));
  803. values.push_back("audio_driver");
  804. values.push_back(USEC_TO_SEC(driver_time));
  805. ScriptDebugger::get_singleton()->add_profiling_frame_data("audio_thread", values);
  806. }
  807. // Reset profiling times
  808. for (int i = buses.size() - 1; i >= 0; i--) {
  809. Bus *bus = buses[i];
  810. if (bus->bypass) {
  811. continue;
  812. }
  813. for (int j = 0; j < bus->effects.size(); j++) {
  814. if (!bus->effects[j].enabled) {
  815. continue;
  816. }
  817. bus->effects.write[j].prof_time = 0;
  818. }
  819. }
  820. AudioDriver::get_singleton()->reset_profiling_time();
  821. prof_time = 0;
  822. #endif
  823. for (Set<CallbackItem>::Element *E = update_callbacks.front(); E; E = E->next()) {
  824. E->get().callback(E->get().userdata);
  825. }
  826. }
  827. void AudioServer::load_default_bus_layout() {
  828. String layout_path = ProjectSettings::get_singleton()->get("audio/default_bus_layout");
  829. if (ResourceLoader::exists(layout_path)) {
  830. Ref<AudioBusLayout> default_layout = ResourceLoader::load(layout_path);
  831. if (default_layout.is_valid()) {
  832. set_bus_layout(default_layout);
  833. }
  834. }
  835. }
  836. void AudioServer::finish() {
  837. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  838. AudioDriverManager::get_driver(i)->finish();
  839. }
  840. for (int i = 0; i < buses.size(); i++) {
  841. memdelete(buses[i]);
  842. }
  843. buses.clear();
  844. }
  845. /* MISC config */
  846. void AudioServer::lock() {
  847. AudioDriver::get_singleton()->lock();
  848. }
  849. void AudioServer::unlock() {
  850. AudioDriver::get_singleton()->unlock();
  851. }
  852. AudioServer::SpeakerMode AudioServer::get_speaker_mode() const {
  853. return (AudioServer::SpeakerMode)AudioDriver::get_singleton()->get_speaker_mode();
  854. }
  855. float AudioServer::get_mix_rate() const {
  856. return AudioDriver::get_singleton()->get_mix_rate();
  857. }
  858. float AudioServer::read_output_peak_db() const {
  859. return 0;
  860. }
  861. AudioServer *AudioServer::get_singleton() {
  862. return singleton;
  863. }
  864. double AudioServer::get_output_latency() const {
  865. return AudioDriver::get_singleton()->get_latency();
  866. }
  867. double AudioServer::get_time_to_next_mix() const {
  868. return AudioDriver::get_singleton()->get_time_to_next_mix();
  869. }
  870. double AudioServer::get_time_since_last_mix() const {
  871. return AudioDriver::get_singleton()->get_time_since_last_mix();
  872. }
  873. AudioServer *AudioServer::singleton = nullptr;
  874. void *AudioServer::audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data) {
  875. void *ad = memalloc(p_data_len);
  876. ERR_FAIL_COND_V(!ad, nullptr);
  877. if (p_from_data) {
  878. memcpy(ad, p_from_data, p_data_len);
  879. }
  880. audio_data_lock.lock();
  881. audio_data[ad] = p_data_len;
  882. audio_data_total_mem += p_data_len;
  883. audio_data_max_mem = MAX(audio_data_total_mem, audio_data_max_mem);
  884. audio_data_lock.unlock();
  885. return ad;
  886. }
  887. void AudioServer::audio_data_free(void *p_data) {
  888. audio_data_lock.lock();
  889. if (!audio_data.has(p_data)) {
  890. audio_data_lock.unlock();
  891. ERR_FAIL();
  892. }
  893. audio_data_total_mem -= audio_data[p_data];
  894. audio_data.erase(p_data);
  895. memfree(p_data);
  896. audio_data_lock.unlock();
  897. }
  898. size_t AudioServer::audio_data_get_total_memory_usage() const {
  899. return audio_data_total_mem;
  900. }
  901. size_t AudioServer::audio_data_get_max_memory_usage() const {
  902. return audio_data_max_mem;
  903. }
  904. void AudioServer::add_callback(AudioCallback p_callback, void *p_userdata) {
  905. lock();
  906. CallbackItem ci;
  907. ci.callback = p_callback;
  908. ci.userdata = p_userdata;
  909. callbacks.insert(ci);
  910. unlock();
  911. }
  912. void AudioServer::remove_callback(AudioCallback p_callback, void *p_userdata) {
  913. lock();
  914. CallbackItem ci;
  915. ci.callback = p_callback;
  916. ci.userdata = p_userdata;
  917. callbacks.erase(ci);
  918. unlock();
  919. }
  920. void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) {
  921. lock();
  922. CallbackItem ci;
  923. ci.callback = p_callback;
  924. ci.userdata = p_userdata;
  925. update_callbacks.insert(ci);
  926. unlock();
  927. }
  928. void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) {
  929. lock();
  930. CallbackItem ci;
  931. ci.callback = p_callback;
  932. ci.userdata = p_userdata;
  933. update_callbacks.erase(ci);
  934. unlock();
  935. }
  936. void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
  937. ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.size() == 0);
  938. lock();
  939. for (int i = 0; i < buses.size(); i++) {
  940. memdelete(buses[i]);
  941. }
  942. buses.resize(p_bus_layout->buses.size());
  943. bus_map.clear();
  944. for (int i = 0; i < p_bus_layout->buses.size(); i++) {
  945. Bus *bus = memnew(Bus);
  946. if (i == 0) {
  947. bus->name = "Master";
  948. } else {
  949. bus->name = p_bus_layout->buses[i].name;
  950. bus->send = p_bus_layout->buses[i].send;
  951. }
  952. bus->solo = p_bus_layout->buses[i].solo;
  953. bus->mute = p_bus_layout->buses[i].mute;
  954. bus->bypass = p_bus_layout->buses[i].bypass;
  955. bus->volume_db = p_bus_layout->buses[i].volume_db;
  956. for (int j = 0; j < p_bus_layout->buses[i].effects.size(); j++) {
  957. Ref<AudioEffect> fx = p_bus_layout->buses[i].effects[j].effect;
  958. if (fx.is_valid()) {
  959. Bus::Effect bfx;
  960. bfx.effect = fx;
  961. bfx.enabled = p_bus_layout->buses[i].effects[j].enabled;
  962. bus->effects.push_back(bfx);
  963. }
  964. }
  965. bus_map[bus->name] = bus;
  966. buses.write[i] = bus;
  967. buses[i]->channels.resize(channel_count);
  968. for (int j = 0; j < channel_count; j++) {
  969. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  970. }
  971. _update_bus_effects(i);
  972. }
  973. #ifdef TOOLS_ENABLED
  974. set_edited(false);
  975. #endif
  976. unlock();
  977. }
  978. Ref<AudioBusLayout> AudioServer::generate_bus_layout() const {
  979. Ref<AudioBusLayout> state;
  980. state.instance();
  981. state->buses.resize(buses.size());
  982. for (int i = 0; i < buses.size(); i++) {
  983. state->buses.write[i].name = buses[i]->name;
  984. state->buses.write[i].send = buses[i]->send;
  985. state->buses.write[i].mute = buses[i]->mute;
  986. state->buses.write[i].solo = buses[i]->solo;
  987. state->buses.write[i].bypass = buses[i]->bypass;
  988. state->buses.write[i].volume_db = buses[i]->volume_db;
  989. for (int j = 0; j < buses[i]->effects.size(); j++) {
  990. AudioBusLayout::Bus::Effect fx;
  991. fx.effect = buses[i]->effects[j].effect;
  992. fx.enabled = buses[i]->effects[j].enabled;
  993. state->buses.write[i].effects.push_back(fx);
  994. }
  995. }
  996. return state;
  997. }
  998. Array AudioServer::get_device_list() {
  999. return AudioDriver::get_singleton()->get_device_list();
  1000. }
  1001. String AudioServer::get_device() {
  1002. return AudioDriver::get_singleton()->get_device();
  1003. }
  1004. void AudioServer::set_device(String device) {
  1005. AudioDriver::get_singleton()->set_device(device);
  1006. }
  1007. Array AudioServer::capture_get_device_list() {
  1008. return AudioDriver::get_singleton()->capture_get_device_list();
  1009. }
  1010. String AudioServer::capture_get_device() {
  1011. return AudioDriver::get_singleton()->capture_get_device();
  1012. }
  1013. void AudioServer::capture_set_device(const String &p_name) {
  1014. AudioDriver::get_singleton()->capture_set_device(p_name);
  1015. }
  1016. void AudioServer::_bind_methods() {
  1017. ClassDB::bind_method(D_METHOD("set_bus_count", "amount"), &AudioServer::set_bus_count);
  1018. ClassDB::bind_method(D_METHOD("get_bus_count"), &AudioServer::get_bus_count);
  1019. ClassDB::bind_method(D_METHOD("remove_bus", "index"), &AudioServer::remove_bus);
  1020. ClassDB::bind_method(D_METHOD("add_bus", "at_position"), &AudioServer::add_bus, DEFVAL(-1));
  1021. ClassDB::bind_method(D_METHOD("move_bus", "index", "to_index"), &AudioServer::move_bus);
  1022. ClassDB::bind_method(D_METHOD("set_bus_name", "bus_idx", "name"), &AudioServer::set_bus_name);
  1023. ClassDB::bind_method(D_METHOD("get_bus_name", "bus_idx"), &AudioServer::get_bus_name);
  1024. ClassDB::bind_method(D_METHOD("get_bus_index", "bus_name"), &AudioServer::get_bus_index);
  1025. ClassDB::bind_method(D_METHOD("get_bus_channels", "bus_idx"), &AudioServer::get_bus_channels);
  1026. ClassDB::bind_method(D_METHOD("set_bus_volume_db", "bus_idx", "volume_db"), &AudioServer::set_bus_volume_db);
  1027. ClassDB::bind_method(D_METHOD("get_bus_volume_db", "bus_idx"), &AudioServer::get_bus_volume_db);
  1028. ClassDB::bind_method(D_METHOD("set_bus_send", "bus_idx", "send"), &AudioServer::set_bus_send);
  1029. ClassDB::bind_method(D_METHOD("get_bus_send", "bus_idx"), &AudioServer::get_bus_send);
  1030. ClassDB::bind_method(D_METHOD("set_bus_solo", "bus_idx", "enable"), &AudioServer::set_bus_solo);
  1031. ClassDB::bind_method(D_METHOD("is_bus_solo", "bus_idx"), &AudioServer::is_bus_solo);
  1032. ClassDB::bind_method(D_METHOD("set_bus_mute", "bus_idx", "enable"), &AudioServer::set_bus_mute);
  1033. ClassDB::bind_method(D_METHOD("is_bus_mute", "bus_idx"), &AudioServer::is_bus_mute);
  1034. ClassDB::bind_method(D_METHOD("set_bus_bypass_effects", "bus_idx", "enable"), &AudioServer::set_bus_bypass_effects);
  1035. ClassDB::bind_method(D_METHOD("is_bus_bypassing_effects", "bus_idx"), &AudioServer::is_bus_bypassing_effects);
  1036. ClassDB::bind_method(D_METHOD("add_bus_effect", "bus_idx", "effect", "at_position"), &AudioServer::add_bus_effect, DEFVAL(-1));
  1037. ClassDB::bind_method(D_METHOD("remove_bus_effect", "bus_idx", "effect_idx"), &AudioServer::remove_bus_effect);
  1038. ClassDB::bind_method(D_METHOD("get_bus_effect_count", "bus_idx"), &AudioServer::get_bus_effect_count);
  1039. ClassDB::bind_method(D_METHOD("get_bus_effect", "bus_idx", "effect_idx"), &AudioServer::get_bus_effect);
  1040. ClassDB::bind_method(D_METHOD("get_bus_effect_instance", "bus_idx", "effect_idx", "channel"), &AudioServer::get_bus_effect_instance, DEFVAL(0));
  1041. ClassDB::bind_method(D_METHOD("swap_bus_effects", "bus_idx", "effect_idx", "by_effect_idx"), &AudioServer::swap_bus_effects);
  1042. ClassDB::bind_method(D_METHOD("set_bus_effect_enabled", "bus_idx", "effect_idx", "enabled"), &AudioServer::set_bus_effect_enabled);
  1043. ClassDB::bind_method(D_METHOD("is_bus_effect_enabled", "bus_idx", "effect_idx"), &AudioServer::is_bus_effect_enabled);
  1044. ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
  1045. ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
  1046. ClassDB::bind_method(D_METHOD("set_global_rate_scale", "scale"), &AudioServer::set_global_rate_scale);
  1047. ClassDB::bind_method(D_METHOD("get_global_rate_scale"), &AudioServer::get_global_rate_scale);
  1048. ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
  1049. ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
  1050. ClassDB::bind_method(D_METHOD("get_speaker_mode"), &AudioServer::get_speaker_mode);
  1051. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioServer::get_mix_rate);
  1052. ClassDB::bind_method(D_METHOD("get_device_list"), &AudioServer::get_device_list);
  1053. ClassDB::bind_method(D_METHOD("get_device"), &AudioServer::get_device);
  1054. ClassDB::bind_method(D_METHOD("set_device", "device"), &AudioServer::set_device);
  1055. ClassDB::bind_method(D_METHOD("get_time_to_next_mix"), &AudioServer::get_time_to_next_mix);
  1056. ClassDB::bind_method(D_METHOD("get_time_since_last_mix"), &AudioServer::get_time_since_last_mix);
  1057. ClassDB::bind_method(D_METHOD("get_output_latency"), &AudioServer::get_output_latency);
  1058. ClassDB::bind_method(D_METHOD("capture_get_device_list"), &AudioServer::capture_get_device_list);
  1059. ClassDB::bind_method(D_METHOD("capture_get_device"), &AudioServer::capture_get_device);
  1060. ClassDB::bind_method(D_METHOD("capture_set_device", "name"), &AudioServer::capture_set_device);
  1061. ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout);
  1062. ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout);
  1063. ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
  1064. ADD_PROPERTY(PropertyInfo(Variant::STRING, "device"), "set_device", "get_device");
  1065. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rate_scale"), "set_global_rate_scale", "get_global_rate_scale");
  1066. ADD_SIGNAL(MethodInfo("bus_layout_changed"));
  1067. BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
  1068. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_31);
  1069. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_51);
  1070. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_71);
  1071. }
  1072. AudioServer::AudioServer() {
  1073. singleton = this;
  1074. audio_data_total_mem = 0;
  1075. audio_data_max_mem = 0;
  1076. mix_frames = 0;
  1077. channel_count = 0;
  1078. to_mix = 0;
  1079. #ifdef DEBUG_ENABLED
  1080. prof_time = 0;
  1081. #endif
  1082. mix_time = 0;
  1083. mix_size = 0;
  1084. global_rate_scale = 1;
  1085. }
  1086. AudioServer::~AudioServer() {
  1087. singleton = nullptr;
  1088. }
  1089. /////////////////////////////////
  1090. bool AudioBusLayout::_set(const StringName &p_name, const Variant &p_value) {
  1091. String s = p_name;
  1092. if (s.begins_with("bus/")) {
  1093. int index = s.get_slice("/", 1).to_int();
  1094. if (buses.size() <= index) {
  1095. buses.resize(index + 1);
  1096. }
  1097. Bus &bus = buses.write[index];
  1098. String what = s.get_slice("/", 2);
  1099. if (what == "name") {
  1100. bus.name = p_value;
  1101. } else if (what == "solo") {
  1102. bus.solo = p_value;
  1103. } else if (what == "mute") {
  1104. bus.mute = p_value;
  1105. } else if (what == "bypass_fx") {
  1106. bus.bypass = p_value;
  1107. } else if (what == "volume_db") {
  1108. bus.volume_db = p_value;
  1109. } else if (what == "send") {
  1110. bus.send = p_value;
  1111. } else if (what == "effect") {
  1112. int which = s.get_slice("/", 3).to_int();
  1113. if (bus.effects.size() <= which) {
  1114. bus.effects.resize(which + 1);
  1115. }
  1116. Bus::Effect &fx = bus.effects.write[which];
  1117. String fxwhat = s.get_slice("/", 4);
  1118. if (fxwhat == "effect") {
  1119. fx.effect = p_value;
  1120. } else if (fxwhat == "enabled") {
  1121. fx.enabled = p_value;
  1122. } else {
  1123. return false;
  1124. }
  1125. return true;
  1126. } else {
  1127. return false;
  1128. }
  1129. return true;
  1130. }
  1131. return false;
  1132. }
  1133. bool AudioBusLayout::_get(const StringName &p_name, Variant &r_ret) const {
  1134. String s = p_name;
  1135. if (s.begins_with("bus/")) {
  1136. int index = s.get_slice("/", 1).to_int();
  1137. if (index < 0 || index >= buses.size()) {
  1138. return false;
  1139. }
  1140. const Bus &bus = buses[index];
  1141. String what = s.get_slice("/", 2);
  1142. if (what == "name") {
  1143. r_ret = bus.name;
  1144. } else if (what == "solo") {
  1145. r_ret = bus.solo;
  1146. } else if (what == "mute") {
  1147. r_ret = bus.mute;
  1148. } else if (what == "bypass_fx") {
  1149. r_ret = bus.bypass;
  1150. } else if (what == "volume_db") {
  1151. r_ret = bus.volume_db;
  1152. } else if (what == "send") {
  1153. r_ret = bus.send;
  1154. } else if (what == "effect") {
  1155. int which = s.get_slice("/", 3).to_int();
  1156. if (which < 0 || which >= bus.effects.size()) {
  1157. return false;
  1158. }
  1159. const Bus::Effect &fx = bus.effects[which];
  1160. String fxwhat = s.get_slice("/", 4);
  1161. if (fxwhat == "effect") {
  1162. r_ret = fx.effect;
  1163. } else if (fxwhat == "enabled") {
  1164. r_ret = fx.enabled;
  1165. } else {
  1166. return false;
  1167. }
  1168. return true;
  1169. } else {
  1170. return false;
  1171. }
  1172. return true;
  1173. }
  1174. return false;
  1175. }
  1176. void AudioBusLayout::_get_property_list(List<PropertyInfo> *p_list) const {
  1177. for (int i = 0; i < buses.size(); i++) {
  1178. p_list->push_back(PropertyInfo(Variant::STRING, "bus/" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1179. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/solo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1180. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/mute", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1181. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/bypass_fx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1182. p_list->push_back(PropertyInfo(Variant::REAL, "bus/" + itos(i) + "/volume_db", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1183. p_list->push_back(PropertyInfo(Variant::REAL, "bus/" + itos(i) + "/send", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1184. for (int j = 0; j < buses[i].effects.size(); j++) {
  1185. p_list->push_back(PropertyInfo(Variant::OBJECT, "bus/" + itos(i) + "/effect/" + itos(j) + "/effect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1186. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/effect/" + itos(j) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1187. }
  1188. }
  1189. }
  1190. AudioBusLayout::AudioBusLayout() {
  1191. buses.resize(1);
  1192. buses.write[0].name = "Master";
  1193. }