audio_server.cpp 41 KB

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