audio_server.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /**************************************************************************/
  2. /* audio_server.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "audio_server.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #include "scene/resources/audio_stream_sample.h"
  36. #include "servers/audio/audio_driver_dummy.h"
  37. #include "servers/audio/effects/audio_effect_compressor.h"
  38. #ifdef TOOLS_ENABLED
  39. #define MARK_EDITED set_edited(true);
  40. #else
  41. #define MARK_EDITED
  42. #endif
  43. AudioDriver *AudioDriver::singleton = nullptr;
  44. AudioDriver *AudioDriver::get_singleton() {
  45. return singleton;
  46. }
  47. void AudioDriver::set_singleton() {
  48. singleton = this;
  49. }
  50. void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) {
  51. if (p_update_mix_time) {
  52. update_mix_time(p_frames);
  53. }
  54. if (AudioServer::get_singleton()) {
  55. AudioServer::get_singleton()->_driver_process(p_frames, p_buffer);
  56. }
  57. }
  58. void AudioDriver::update_mix_time(int p_frames) {
  59. _last_mix_frames = p_frames;
  60. if (OS::get_singleton()) {
  61. _last_mix_time = OS::get_singleton()->get_ticks_usec();
  62. }
  63. }
  64. double AudioDriver::get_time_since_last_mix() {
  65. lock();
  66. uint64_t last_mix_time = _last_mix_time;
  67. unlock();
  68. return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
  69. }
  70. double AudioDriver::get_time_to_next_mix() {
  71. lock();
  72. uint64_t last_mix_time = _last_mix_time;
  73. uint64_t last_mix_frames = _last_mix_frames;
  74. unlock();
  75. double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
  76. double mix_buffer = last_mix_frames / (double)get_mix_rate();
  77. return mix_buffer - total;
  78. }
  79. void AudioDriver::input_buffer_init(int driver_buffer_frames) {
  80. const int input_buffer_channels = 2;
  81. input_buffer.resize(driver_buffer_frames * input_buffer_channels * 4);
  82. input_position = 0;
  83. input_size = 0;
  84. }
  85. void AudioDriver::input_buffer_write(int32_t sample) {
  86. if ((int)input_position < input_buffer.size()) {
  87. input_buffer.write[input_position++] = sample;
  88. if ((int)input_position >= input_buffer.size()) {
  89. input_position = 0;
  90. }
  91. if ((int)input_size < input_buffer.size()) {
  92. input_size++;
  93. }
  94. } else {
  95. WARN_PRINT("input_buffer_write: Invalid input_position=" + itos(input_position) + " input_buffer.size()=" + itos(input_buffer.size()));
  96. }
  97. }
  98. AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const {
  99. switch (p_channels) {
  100. case 4:
  101. return SPEAKER_SURROUND_31;
  102. case 6:
  103. return SPEAKER_SURROUND_51;
  104. case 8:
  105. return SPEAKER_SURROUND_71;
  106. }
  107. // Default to STEREO
  108. return SPEAKER_MODE_STEREO;
  109. }
  110. int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_mode) const {
  111. switch (p_mode) {
  112. case SPEAKER_MODE_STEREO:
  113. return 2;
  114. case SPEAKER_SURROUND_31:
  115. return 4;
  116. case SPEAKER_SURROUND_51:
  117. return 6;
  118. case SPEAKER_SURROUND_71:
  119. return 8;
  120. }
  121. ERR_FAIL_V(2);
  122. }
  123. Array AudioDriver::get_device_list() {
  124. Array list;
  125. list.push_back("Default");
  126. return list;
  127. }
  128. String AudioDriver::get_device() {
  129. return "Default";
  130. }
  131. Array AudioDriver::capture_get_device_list() {
  132. Array list;
  133. list.push_back("Default");
  134. return list;
  135. }
  136. AudioDriver::AudioDriver() {
  137. _last_mix_time = 0;
  138. _last_mix_frames = 0;
  139. input_position = 0;
  140. input_size = 0;
  141. #ifdef DEBUG_ENABLED
  142. prof_time = 0;
  143. #endif
  144. }
  145. AudioDriverDummy AudioDriverManager::dummy_driver;
  146. AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS] = {
  147. &AudioDriverManager::dummy_driver,
  148. };
  149. int AudioDriverManager::driver_count = 1;
  150. void AudioDriverManager::add_driver(AudioDriver *p_driver) {
  151. ERR_FAIL_COND(driver_count >= MAX_DRIVERS);
  152. drivers[driver_count - 1] = p_driver;
  153. // Last driver is always our dummy driver
  154. drivers[driver_count++] = &AudioDriverManager::dummy_driver;
  155. }
  156. int AudioDriverManager::get_driver_count() {
  157. return driver_count;
  158. }
  159. void AudioDriverManager::initialize(int p_driver) {
  160. GLOBAL_DEF_RST("audio/enable_audio_input", false);
  161. GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
  162. GLOBAL_DEF_RST("audio/mix_rate.web", 0); // Safer default output_latency for web (use browser default).
  163. GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
  164. GLOBAL_DEF_RST("audio/output_latency.web", 50); // Safer default output_latency for web.
  165. int failed_driver = -1;
  166. // Check if there is a selected driver
  167. if (p_driver >= 0 && p_driver < driver_count) {
  168. if (drivers[p_driver]->init() == OK) {
  169. drivers[p_driver]->set_singleton();
  170. return;
  171. } else {
  172. failed_driver = p_driver;
  173. }
  174. }
  175. // No selected driver, try them all in order
  176. for (int i = 0; i < driver_count; i++) {
  177. // Don't re-init the driver if it failed above
  178. if (i == failed_driver) {
  179. continue;
  180. }
  181. if (drivers[i]->init() == OK) {
  182. drivers[i]->set_singleton();
  183. break;
  184. }
  185. }
  186. if (driver_count > 1 && String(AudioDriver::get_singleton()->get_name()) == "Dummy") {
  187. WARN_PRINT("All audio drivers failed, falling back to the dummy driver.");
  188. }
  189. }
  190. AudioDriver *AudioDriverManager::get_driver(int p_driver) {
  191. ERR_FAIL_INDEX_V(p_driver, driver_count, nullptr);
  192. return drivers[p_driver];
  193. }
  194. //////////////////////////////////////////////
  195. //////////////////////////////////////////////
  196. //////////////////////////////////////////////
  197. //////////////////////////////////////////////
  198. void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
  199. int todo = p_frames;
  200. #ifdef DEBUG_ENABLED
  201. uint64_t prof_ticks = OS::get_singleton()->get_ticks_usec();
  202. #endif
  203. if (channel_count != get_channel_count()) {
  204. // Amount of channels changed due to a device change
  205. // reinitialize the buses channels and buffers
  206. init_channels_and_buffers();
  207. }
  208. ERR_FAIL_COND_MSG(buses.empty() && todo, "AudioServer bus count is less than 1.");
  209. while (todo) {
  210. if (to_mix == 0) {
  211. _mix_step();
  212. }
  213. int to_copy = MIN(to_mix, todo);
  214. Bus *master = buses[0];
  215. int from = buffer_size - to_mix;
  216. int from_buf = p_frames - todo;
  217. //master master, send to output
  218. int cs = master->channels.size();
  219. 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::STRING, "capture_device"), "capture_set_device", "capture_get_device");
  1066. // The default value may be set to an empty string by the platform-specific audio driver.
  1067. // Override for class reference generation purposes.
  1068. ADD_PROPERTY_DEFAULT("capture_device", "Default");
  1069. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rate_scale"), "set_global_rate_scale", "get_global_rate_scale");
  1070. ADD_SIGNAL(MethodInfo("bus_layout_changed"));
  1071. BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
  1072. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_31);
  1073. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_51);
  1074. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_71);
  1075. }
  1076. AudioServer::AudioServer() {
  1077. singleton = this;
  1078. audio_data_total_mem = 0;
  1079. audio_data_max_mem = 0;
  1080. mix_frames = 0;
  1081. channel_count = 0;
  1082. to_mix = 0;
  1083. #ifdef DEBUG_ENABLED
  1084. prof_time = 0;
  1085. #endif
  1086. mix_time = 0;
  1087. mix_size = 0;
  1088. global_rate_scale = 1;
  1089. }
  1090. AudioServer::~AudioServer() {
  1091. singleton = nullptr;
  1092. }
  1093. /////////////////////////////////
  1094. bool AudioBusLayout::_set(const StringName &p_name, const Variant &p_value) {
  1095. String s = p_name;
  1096. if (s.begins_with("bus/")) {
  1097. int index = s.get_slice("/", 1).to_int();
  1098. if (buses.size() <= index) {
  1099. buses.resize(index + 1);
  1100. }
  1101. Bus &bus = buses.write[index];
  1102. String what = s.get_slice("/", 2);
  1103. if (what == "name") {
  1104. bus.name = p_value;
  1105. } else if (what == "solo") {
  1106. bus.solo = p_value;
  1107. } else if (what == "mute") {
  1108. bus.mute = p_value;
  1109. } else if (what == "bypass_fx") {
  1110. bus.bypass = p_value;
  1111. } else if (what == "volume_db") {
  1112. bus.volume_db = p_value;
  1113. } else if (what == "send") {
  1114. bus.send = p_value;
  1115. } else if (what == "effect") {
  1116. int which = s.get_slice("/", 3).to_int();
  1117. if (bus.effects.size() <= which) {
  1118. bus.effects.resize(which + 1);
  1119. }
  1120. Bus::Effect &fx = bus.effects.write[which];
  1121. String fxwhat = s.get_slice("/", 4);
  1122. if (fxwhat == "effect") {
  1123. fx.effect = p_value;
  1124. } else if (fxwhat == "enabled") {
  1125. fx.enabled = p_value;
  1126. } else {
  1127. return false;
  1128. }
  1129. return true;
  1130. } else {
  1131. return false;
  1132. }
  1133. return true;
  1134. }
  1135. return false;
  1136. }
  1137. bool AudioBusLayout::_get(const StringName &p_name, Variant &r_ret) const {
  1138. String s = p_name;
  1139. if (s.begins_with("bus/")) {
  1140. int index = s.get_slice("/", 1).to_int();
  1141. if (index < 0 || index >= buses.size()) {
  1142. return false;
  1143. }
  1144. const Bus &bus = buses[index];
  1145. String what = s.get_slice("/", 2);
  1146. if (what == "name") {
  1147. r_ret = bus.name;
  1148. } else if (what == "solo") {
  1149. r_ret = bus.solo;
  1150. } else if (what == "mute") {
  1151. r_ret = bus.mute;
  1152. } else if (what == "bypass_fx") {
  1153. r_ret = bus.bypass;
  1154. } else if (what == "volume_db") {
  1155. r_ret = bus.volume_db;
  1156. } else if (what == "send") {
  1157. r_ret = bus.send;
  1158. } else if (what == "effect") {
  1159. int which = s.get_slice("/", 3).to_int();
  1160. if (which < 0 || which >= bus.effects.size()) {
  1161. return false;
  1162. }
  1163. const Bus::Effect &fx = bus.effects[which];
  1164. String fxwhat = s.get_slice("/", 4);
  1165. if (fxwhat == "effect") {
  1166. r_ret = fx.effect;
  1167. } else if (fxwhat == "enabled") {
  1168. r_ret = fx.enabled;
  1169. } else {
  1170. return false;
  1171. }
  1172. return true;
  1173. } else {
  1174. return false;
  1175. }
  1176. return true;
  1177. }
  1178. return false;
  1179. }
  1180. void AudioBusLayout::_get_property_list(List<PropertyInfo> *p_list) const {
  1181. for (int i = 0; i < buses.size(); i++) {
  1182. p_list->push_back(PropertyInfo(Variant::STRING, "bus/" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1183. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/solo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1184. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/mute", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1185. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/bypass_fx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1186. p_list->push_back(PropertyInfo(Variant::REAL, "bus/" + itos(i) + "/volume_db", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1187. p_list->push_back(PropertyInfo(Variant::REAL, "bus/" + itos(i) + "/send", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1188. for (int j = 0; j < buses[i].effects.size(); j++) {
  1189. p_list->push_back(PropertyInfo(Variant::OBJECT, "bus/" + itos(i) + "/effect/" + itos(j) + "/effect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1190. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/effect/" + itos(j) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  1191. }
  1192. }
  1193. }
  1194. AudioBusLayout::AudioBusLayout() {
  1195. buses.resize(1);
  1196. buses.write[0].name = "Master";
  1197. }