audio_server.cpp 41 KB

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