audio_server.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  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/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/error/error_macros.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/math/audio_frame.h"
  37. #include "core/os/os.h"
  38. #include "core/string/string_name.h"
  39. #include "core/templates/pair.h"
  40. #include "scene/resources/audio_stream_wav.h"
  41. #include "scene/scene_string_names.h"
  42. #include "servers/audio/audio_driver_dummy.h"
  43. #include "servers/audio/effects/audio_effect_compressor.h"
  44. #include <cstring>
  45. #ifdef TOOLS_ENABLED
  46. #define MARK_EDITED set_edited(true);
  47. #else
  48. #define MARK_EDITED
  49. #endif
  50. AudioDriver *AudioDriver::singleton = nullptr;
  51. AudioDriver *AudioDriver::get_singleton() {
  52. return singleton;
  53. }
  54. void AudioDriver::set_singleton() {
  55. singleton = this;
  56. }
  57. void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) {
  58. if (p_update_mix_time) {
  59. update_mix_time(p_frames);
  60. }
  61. if (AudioServer::get_singleton()) {
  62. AudioServer::get_singleton()->_driver_process(p_frames, p_buffer);
  63. }
  64. }
  65. void AudioDriver::update_mix_time(int p_frames) {
  66. _last_mix_frames = p_frames;
  67. if (OS::get_singleton()) {
  68. _last_mix_time = OS::get_singleton()->get_ticks_usec();
  69. }
  70. }
  71. double AudioDriver::get_time_since_last_mix() {
  72. lock();
  73. uint64_t last_mix_time = _last_mix_time;
  74. unlock();
  75. return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
  76. }
  77. double AudioDriver::get_time_to_next_mix() {
  78. lock();
  79. uint64_t last_mix_time = _last_mix_time;
  80. uint64_t last_mix_frames = _last_mix_frames;
  81. unlock();
  82. double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
  83. double mix_buffer = last_mix_frames / (double)get_mix_rate();
  84. return mix_buffer - total;
  85. }
  86. void AudioDriver::input_buffer_init(int driver_buffer_frames) {
  87. const int input_buffer_channels = 2;
  88. input_buffer.resize(driver_buffer_frames * input_buffer_channels * 4);
  89. input_position = 0;
  90. input_size = 0;
  91. }
  92. void AudioDriver::input_buffer_write(int32_t sample) {
  93. if ((int)input_position < input_buffer.size()) {
  94. input_buffer.write[input_position++] = sample;
  95. if ((int)input_position >= input_buffer.size()) {
  96. input_position = 0;
  97. }
  98. if ((int)input_size < input_buffer.size()) {
  99. input_size++;
  100. }
  101. } else {
  102. WARN_PRINT("input_buffer_write: Invalid input_position=" + itos(input_position) + " input_buffer.size()=" + itos(input_buffer.size()));
  103. }
  104. }
  105. int AudioDriver::_get_configured_mix_rate() {
  106. StringName audio_driver_setting = "audio/driver/mix_rate";
  107. int mix_rate = GLOBAL_GET(audio_driver_setting);
  108. // In the case of invalid mix rate, let's default to a sensible value..
  109. if (mix_rate <= 0) {
  110. #ifndef WEB_ENABLED
  111. WARN_PRINT(vformat("Invalid mix rate of %d, consider reassigning setting \'%s\'. \nDefaulting mix rate to value %d.",
  112. mix_rate, audio_driver_setting, AudioDriverManager::DEFAULT_MIX_RATE));
  113. #endif
  114. mix_rate = AudioDriverManager::DEFAULT_MIX_RATE;
  115. }
  116. return mix_rate;
  117. }
  118. AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const {
  119. switch (p_channels) {
  120. case 4:
  121. return SPEAKER_SURROUND_31;
  122. case 6:
  123. return SPEAKER_SURROUND_51;
  124. case 8:
  125. return SPEAKER_SURROUND_71;
  126. }
  127. // Default to STEREO
  128. return SPEAKER_MODE_STEREO;
  129. }
  130. int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_mode) const {
  131. switch (p_mode) {
  132. case SPEAKER_MODE_STEREO:
  133. return 2;
  134. case SPEAKER_SURROUND_31:
  135. return 4;
  136. case SPEAKER_SURROUND_51:
  137. return 6;
  138. case SPEAKER_SURROUND_71:
  139. return 8;
  140. }
  141. ERR_FAIL_V(2);
  142. }
  143. PackedStringArray AudioDriver::get_output_device_list() {
  144. PackedStringArray list;
  145. list.push_back("Default");
  146. return list;
  147. }
  148. String AudioDriver::get_output_device() {
  149. return "Default";
  150. }
  151. PackedStringArray AudioDriver::get_input_device_list() {
  152. PackedStringArray list;
  153. list.push_back("Default");
  154. return list;
  155. }
  156. AudioDriverDummy AudioDriverManager::dummy_driver;
  157. AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS] = {
  158. &AudioDriverManager::dummy_driver,
  159. };
  160. int AudioDriverManager::driver_count = 1;
  161. void AudioDriverManager::add_driver(AudioDriver *p_driver) {
  162. ERR_FAIL_COND(driver_count >= MAX_DRIVERS);
  163. drivers[driver_count - 1] = p_driver;
  164. // Last driver is always our dummy driver
  165. drivers[driver_count++] = &AudioDriverManager::dummy_driver;
  166. }
  167. int AudioDriverManager::get_driver_count() {
  168. return driver_count;
  169. }
  170. void AudioDriverManager::initialize(int p_driver) {
  171. GLOBAL_DEF_RST("audio/driver/enable_input", false);
  172. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/driver/mix_rate", PROPERTY_HINT_RANGE, "11025,192000,1,or_greater,suffix:Hz"), DEFAULT_MIX_RATE);
  173. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/driver/mix_rate.web", PROPERTY_HINT_RANGE, "0,192000,1,or_greater,suffix:Hz"), 0); // Safer default output_latency for web (use browser default).
  174. int failed_driver = -1;
  175. // Check if there is a selected driver
  176. if (p_driver >= 0 && p_driver < driver_count) {
  177. if (drivers[p_driver]->init() == OK) {
  178. drivers[p_driver]->set_singleton();
  179. return;
  180. } else {
  181. failed_driver = p_driver;
  182. }
  183. }
  184. // No selected driver, try them all in order
  185. for (int i = 0; i < driver_count; i++) {
  186. // Don't re-init the driver if it failed above
  187. if (i == failed_driver) {
  188. continue;
  189. }
  190. if (drivers[i]->init() == OK) {
  191. drivers[i]->set_singleton();
  192. break;
  193. }
  194. }
  195. if (driver_count > 1 && String(AudioDriver::get_singleton()->get_name()) == "Dummy") {
  196. WARN_PRINT("All audio drivers failed, falling back to the dummy driver.");
  197. }
  198. }
  199. AudioDriver *AudioDriverManager::get_driver(int p_driver) {
  200. ERR_FAIL_INDEX_V(p_driver, driver_count, nullptr);
  201. return drivers[p_driver];
  202. }
  203. //////////////////////////////////////////////
  204. //////////////////////////////////////////////
  205. //////////////////////////////////////////////
  206. //////////////////////////////////////////////
  207. void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
  208. mix_count++;
  209. int todo = p_frames;
  210. #ifdef DEBUG_ENABLED
  211. uint64_t prof_ticks = OS::get_singleton()->get_ticks_usec();
  212. #endif
  213. if (channel_count != get_channel_count()) {
  214. // Amount of channels changed due to a output_device change
  215. // reinitialize the buses channels and buffers
  216. init_channels_and_buffers();
  217. }
  218. ERR_FAIL_COND_MSG(buses.is_empty() && todo, "AudioServer bus count is less than 1.");
  219. while (todo) {
  220. if (to_mix == 0) {
  221. _mix_step();
  222. }
  223. int to_copy = MIN(to_mix, todo);
  224. Bus *master = buses[0];
  225. int from = buffer_size - to_mix;
  226. int from_buf = p_frames - todo;
  227. //master master, send to output
  228. int cs = master->channels.size();
  229. // Take away 1 from the stride, as we are manually incrementing by 1 for stereo.
  230. uintptr_t stride_minus_one = (cs * 2) - 1;
  231. for (int k = 0; k < cs; k++) {
  232. // The destination start for data will be the same in all cases.
  233. int32_t *dest = &p_buffer[from_buf * (cs * 2) + (k * 2)];
  234. if (master->channels[k].active) {
  235. const AudioFrame *buf = master->channels[k].buffer.ptr();
  236. for (int j = 0; j < to_copy; j++) {
  237. float l = CLAMP(buf[from + j].left, -1.0, 1.0);
  238. int32_t vl = l * ((1 << 20) - 1);
  239. int32_t vl2 = (vl < 0 ? -1 : 1) * (ABS(vl) << 11);
  240. *dest = vl2;
  241. dest++;
  242. float r = CLAMP(buf[from + j].right, -1.0, 1.0);
  243. int32_t vr = r * ((1 << 20) - 1);
  244. int32_t vr2 = (vr < 0 ? -1 : 1) * (ABS(vr) << 11);
  245. *dest = vr2;
  246. dest += stride_minus_one;
  247. }
  248. } else {
  249. // Bizarrely, profiling indicates that detecting the common case of cs == 1,
  250. // k == 0, and using memset is SLOWER than setting them individually.
  251. // Perhaps it gets optimized to a faster instruction than memset.
  252. for (int j = 0; j < to_copy; j++) {
  253. *dest = 0;
  254. dest++;
  255. *dest = 0;
  256. dest += stride_minus_one;
  257. }
  258. }
  259. }
  260. todo -= to_copy;
  261. to_mix -= to_copy;
  262. }
  263. #ifdef DEBUG_ENABLED
  264. prof_time.add(OS::get_singleton()->get_ticks_usec() - prof_ticks);
  265. #endif
  266. }
  267. void AudioServer::_mix_step() {
  268. bool solo_mode = false;
  269. for (int i = 0; i < buses.size(); i++) {
  270. Bus *bus = buses[i];
  271. bus->index_cache = i; //might be moved around by editor, so..
  272. for (int k = 0; k < bus->channels.size(); k++) {
  273. bus->channels.write[k].used = false;
  274. }
  275. if (bus->solo) {
  276. //solo chain
  277. solo_mode = true;
  278. bus->soloed = true;
  279. do {
  280. if (bus != buses[0]) {
  281. //everything has a send save for master bus
  282. if (!bus_map.has(bus->send)) {
  283. bus = buses[0]; //send to master
  284. } else {
  285. int prev_index_cache = bus->index_cache;
  286. bus = bus_map[bus->send];
  287. if (prev_index_cache >= bus->index_cache) { //invalid, send to master
  288. bus = buses[0];
  289. }
  290. }
  291. bus->soloed = true;
  292. } else {
  293. bus = nullptr;
  294. }
  295. } while (bus);
  296. } else {
  297. bus->soloed = false;
  298. }
  299. }
  300. for (CallbackItem *ci : mix_callback_list) {
  301. ci->callback(ci->userdata);
  302. }
  303. for (AudioStreamPlaybackListNode *playback : playback_list) {
  304. // Paused streams are no-ops. Don't even mix audio from the stream playback.
  305. if (playback->state.load() == AudioStreamPlaybackListNode::PAUSED) {
  306. continue;
  307. }
  308. bool fading_out = playback->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION || playback->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE;
  309. AudioFrame *buf = mix_buffer.ptrw();
  310. // Copy the lookeahead buffer into the mix buffer.
  311. for (int i = 0; i < LOOKAHEAD_BUFFER_SIZE; i++) {
  312. buf[i] = playback->lookahead[i];
  313. }
  314. // Mix the audio stream
  315. unsigned int mixed_frames = playback->stream_playback->mix(&buf[LOOKAHEAD_BUFFER_SIZE], playback->pitch_scale.get(), buffer_size);
  316. if (tag_used_audio_streams && playback->stream_playback->is_playing()) {
  317. playback->stream_playback->tag_used_streams();
  318. }
  319. if (mixed_frames != buffer_size) {
  320. // We know we have at least the size of our lookahead buffer for fade-out purposes.
  321. float fadeout_base = 0.94;
  322. float fadeout_coefficient = 1;
  323. static_assert(LOOKAHEAD_BUFFER_SIZE == 64, "Update fadeout_base and comment here if you change LOOKAHEAD_BUFFER_SIZE.");
  324. // 0.94 ^ 64 = 0.01906. There might still be a pop but it'll be way better than if we didn't do this.
  325. for (unsigned int idx = mixed_frames; idx < buffer_size; idx++) {
  326. fadeout_coefficient *= fadeout_base;
  327. buf[idx] *= fadeout_coefficient;
  328. }
  329. AudioStreamPlaybackListNode::PlaybackState new_state;
  330. new_state = AudioStreamPlaybackListNode::AWAITING_DELETION;
  331. playback->state.store(new_state);
  332. } else {
  333. // Move the last little bit of what we just mixed into our lookahead buffer.
  334. for (int i = 0; i < LOOKAHEAD_BUFFER_SIZE; i++) {
  335. playback->lookahead[i] = buf[buffer_size + i];
  336. }
  337. }
  338. AudioStreamPlaybackBusDetails *ptr = playback->bus_details.load();
  339. ERR_FAIL_NULL(ptr);
  340. // By putting null into the bus details pointers, we're taking ownership of their memory for the duration of this mix.
  341. AudioStreamPlaybackBusDetails bus_details = *ptr;
  342. // Mix to any active buses.
  343. for (int idx = 0; idx < MAX_BUSES_PER_PLAYBACK; idx++) {
  344. if (!bus_details.bus_active[idx]) {
  345. continue;
  346. }
  347. int bus_idx = thread_find_bus_index(bus_details.bus[idx]);
  348. int prev_bus_idx = -1;
  349. for (int search_idx = 0; search_idx < MAX_BUSES_PER_PLAYBACK; search_idx++) {
  350. if (!playback->prev_bus_details->bus_active[search_idx]) {
  351. continue;
  352. }
  353. if (playback->prev_bus_details->bus[search_idx].hash() == bus_details.bus[idx].hash()) {
  354. prev_bus_idx = search_idx;
  355. }
  356. }
  357. for (int channel_idx = 0; channel_idx < channel_count; channel_idx++) {
  358. AudioFrame *channel_buf = thread_get_channel_mix_buffer(bus_idx, channel_idx);
  359. if (fading_out) {
  360. bus_details.volume[idx][channel_idx] = AudioFrame(0, 0);
  361. }
  362. AudioFrame channel_vol = bus_details.volume[idx][channel_idx];
  363. AudioFrame prev_channel_vol = AudioFrame(0, 0);
  364. if (prev_bus_idx != -1) {
  365. prev_channel_vol = playback->prev_bus_details->volume[prev_bus_idx][channel_idx];
  366. }
  367. _mix_step_for_channel(channel_buf, buf, prev_channel_vol, channel_vol, playback->attenuation_filter_cutoff_hz.get(), playback->highshelf_gain.get(), &playback->filter_process[channel_idx * 2], &playback->filter_process[channel_idx * 2 + 1]);
  368. }
  369. }
  370. // Now go through and fade-out any buses that were being played to previously that we missed by going through current data.
  371. for (int idx = 0; idx < MAX_BUSES_PER_PLAYBACK; idx++) {
  372. if (!playback->prev_bus_details->bus_active[idx]) {
  373. continue;
  374. }
  375. int bus_idx = thread_find_bus_index(playback->prev_bus_details->bus[idx]);
  376. int current_bus_idx = -1;
  377. for (int search_idx = 0; search_idx < MAX_BUSES_PER_PLAYBACK; search_idx++) {
  378. if (bus_details.bus[search_idx] == playback->prev_bus_details->bus[idx]) {
  379. current_bus_idx = search_idx;
  380. }
  381. }
  382. if (current_bus_idx != -1) {
  383. // If we found a corresponding bus in the current bus assignments, we've already mixed to this bus.
  384. continue;
  385. }
  386. for (int channel_idx = 0; channel_idx < channel_count; channel_idx++) {
  387. AudioFrame *channel_buf = thread_get_channel_mix_buffer(bus_idx, channel_idx);
  388. AudioFrame prev_channel_vol = playback->prev_bus_details->volume[idx][channel_idx];
  389. // Fade out to silence
  390. _mix_step_for_channel(channel_buf, buf, prev_channel_vol, AudioFrame(0, 0), playback->attenuation_filter_cutoff_hz.get(), playback->highshelf_gain.get(), &playback->filter_process[channel_idx * 2], &playback->filter_process[channel_idx * 2 + 1]);
  391. }
  392. }
  393. // Copy the bus details we mixed with to the previous bus details to maintain volume ramps.
  394. std::copy(std::begin(bus_details.bus_active), std::end(bus_details.bus_active), std::begin(playback->prev_bus_details->bus_active));
  395. std::copy(std::begin(bus_details.bus), std::end(bus_details.bus), std::begin(playback->prev_bus_details->bus));
  396. for (int bus_idx = 0; bus_idx < MAX_BUSES_PER_PLAYBACK; bus_idx++) {
  397. std::copy(std::begin(bus_details.volume[bus_idx]), std::end(bus_details.volume[bus_idx]), std::begin(playback->prev_bus_details->volume[bus_idx]));
  398. }
  399. switch (playback->state.load()) {
  400. case AudioStreamPlaybackListNode::AWAITING_DELETION:
  401. case AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION:
  402. playback_list.erase(playback, [](AudioStreamPlaybackListNode *p) {
  403. delete p->prev_bus_details;
  404. delete p->bus_details;
  405. p->stream_playback.unref();
  406. delete p;
  407. });
  408. break;
  409. case AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE: {
  410. // Pause the stream.
  411. AudioStreamPlaybackListNode::PlaybackState old_state, new_state;
  412. do {
  413. old_state = playback->state.load();
  414. new_state = AudioStreamPlaybackListNode::PAUSED;
  415. } while (!playback->state.compare_exchange_strong(/* expected= */ old_state, new_state));
  416. } break;
  417. case AudioStreamPlaybackListNode::PLAYING:
  418. case AudioStreamPlaybackListNode::PAUSED:
  419. // No-op!
  420. break;
  421. }
  422. }
  423. for (int i = buses.size() - 1; i >= 0; i--) {
  424. //go bus by bus
  425. Bus *bus = buses[i];
  426. for (int k = 0; k < bus->channels.size(); k++) {
  427. if (bus->channels[k].active && !bus->channels[k].used) {
  428. //buffer was not used, but it's still active, so it must be cleaned
  429. AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
  430. for (uint32_t j = 0; j < buffer_size; j++) {
  431. buf[j] = AudioFrame(0, 0);
  432. }
  433. }
  434. }
  435. //process effects
  436. if (!bus->bypass) {
  437. for (int j = 0; j < bus->effects.size(); j++) {
  438. if (!bus->effects[j].enabled) {
  439. continue;
  440. }
  441. #ifdef DEBUG_ENABLED
  442. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  443. #endif
  444. for (int k = 0; k < bus->channels.size(); k++) {
  445. if (!(bus->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
  446. continue;
  447. }
  448. bus->channels.write[k].effect_instances.write[j]->process(bus->channels[k].buffer.ptr(), temp_buffer.write[k].ptrw(), buffer_size);
  449. }
  450. //swap buffers, so internal buffer always has the right data
  451. for (int k = 0; k < bus->channels.size(); k++) {
  452. if (!(buses[i]->channels[k].active || bus->channels[k].effect_instances[j]->process_silence())) {
  453. continue;
  454. }
  455. SWAP(bus->channels.write[k].buffer, temp_buffer.write[k]);
  456. }
  457. #ifdef DEBUG_ENABLED
  458. bus->effects.write[j].prof_time += OS::get_singleton()->get_ticks_usec() - ticks;
  459. #endif
  460. }
  461. }
  462. //process send
  463. Bus *send = nullptr;
  464. if (i > 0) {
  465. //everything has a send save for master bus
  466. if (!bus_map.has(bus->send)) {
  467. send = buses[0];
  468. } else {
  469. send = bus_map[bus->send];
  470. if (send->index_cache >= bus->index_cache) { //invalid, send to master
  471. send = buses[0];
  472. }
  473. }
  474. }
  475. for (int k = 0; k < bus->channels.size(); k++) {
  476. if (!bus->channels[k].active) {
  477. bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
  478. continue;
  479. }
  480. AudioFrame *buf = bus->channels.write[k].buffer.ptrw();
  481. AudioFrame peak = AudioFrame(0, 0);
  482. float volume = Math::db_to_linear(bus->volume_db);
  483. if (solo_mode) {
  484. if (!bus->soloed) {
  485. volume = 0.0;
  486. }
  487. } else {
  488. if (bus->mute) {
  489. volume = 0.0;
  490. }
  491. }
  492. //apply volume and compute peak
  493. for (uint32_t j = 0; j < buffer_size; j++) {
  494. buf[j] *= volume;
  495. float l = ABS(buf[j].left);
  496. if (l > peak.left) {
  497. peak.left = l;
  498. }
  499. float r = ABS(buf[j].right);
  500. if (r > peak.right) {
  501. peak.right = r;
  502. }
  503. }
  504. bus->channels.write[k].peak_volume = AudioFrame(Math::linear_to_db(peak.left + AUDIO_PEAK_OFFSET), Math::linear_to_db(peak.right + AUDIO_PEAK_OFFSET));
  505. if (!bus->channels[k].used) {
  506. //see if any audio is contained, because channel was not used
  507. if (MAX(peak.right, peak.left) > Math::db_to_linear(channel_disable_threshold_db)) {
  508. bus->channels.write[k].last_mix_with_audio = mix_frames;
  509. } else if (mix_frames - bus->channels[k].last_mix_with_audio > channel_disable_frames) {
  510. bus->channels.write[k].active = false;
  511. continue; //went inactive, don't mix.
  512. }
  513. }
  514. if (send) {
  515. //if not master bus, send
  516. AudioFrame *target_buf = thread_get_channel_mix_buffer(send->index_cache, k);
  517. for (uint32_t j = 0; j < buffer_size; j++) {
  518. target_buf[j] += buf[j];
  519. }
  520. }
  521. }
  522. }
  523. mix_frames += buffer_size;
  524. to_mix = buffer_size;
  525. }
  526. void AudioServer::_mix_step_for_channel(AudioFrame *p_out_buf, AudioFrame *p_source_buf, AudioFrame p_vol_start, AudioFrame p_vol_final, float p_attenuation_filter_cutoff_hz, float p_highshelf_gain, AudioFilterSW::Processor *p_processor_l, AudioFilterSW::Processor *p_processor_r) {
  527. if (p_highshelf_gain != 0) {
  528. AudioFilterSW filter;
  529. filter.set_mode(AudioFilterSW::HIGHSHELF);
  530. filter.set_sampling_rate(AudioServer::get_singleton()->get_mix_rate());
  531. filter.set_cutoff(p_attenuation_filter_cutoff_hz);
  532. filter.set_resonance(1);
  533. filter.set_stages(1);
  534. filter.set_gain(p_highshelf_gain);
  535. ERR_FAIL_NULL(p_processor_l);
  536. ERR_FAIL_NULL(p_processor_r);
  537. bool is_just_started = p_vol_start.left == 0 && p_vol_start.right == 0;
  538. p_processor_l->set_filter(&filter, /* clear_history= */ is_just_started);
  539. p_processor_l->update_coeffs(buffer_size);
  540. p_processor_r->set_filter(&filter, /* clear_history= */ is_just_started);
  541. p_processor_r->update_coeffs(buffer_size);
  542. for (unsigned int frame_idx = 0; frame_idx < buffer_size; frame_idx++) {
  543. // Make this buffer size invariant if buffer_size ever becomes a project setting.
  544. float lerp_param = (float)frame_idx / buffer_size;
  545. AudioFrame vol = p_vol_final * lerp_param + (1 - lerp_param) * p_vol_start;
  546. AudioFrame mixed = vol * p_source_buf[frame_idx];
  547. p_processor_l->process_one_interp(mixed.left);
  548. p_processor_r->process_one_interp(mixed.right);
  549. p_out_buf[frame_idx] += mixed;
  550. }
  551. } else {
  552. for (unsigned int frame_idx = 0; frame_idx < buffer_size; frame_idx++) {
  553. // Make this buffer size invariant if buffer_size ever becomes a project setting.
  554. float lerp_param = (float)frame_idx / buffer_size;
  555. p_out_buf[frame_idx] += (p_vol_final * lerp_param + (1 - lerp_param) * p_vol_start) * p_source_buf[frame_idx];
  556. }
  557. }
  558. }
  559. AudioServer::AudioStreamPlaybackListNode *AudioServer::_find_playback_list_node(Ref<AudioStreamPlayback> p_playback) {
  560. for (AudioStreamPlaybackListNode *playback_list_node : playback_list) {
  561. if (playback_list_node->stream_playback == p_playback) {
  562. return playback_list_node;
  563. }
  564. }
  565. return nullptr;
  566. }
  567. bool AudioServer::thread_has_channel_mix_buffer(int p_bus, int p_buffer) const {
  568. if (p_bus < 0 || p_bus >= buses.size()) {
  569. return false;
  570. }
  571. if (p_buffer < 0 || p_buffer >= buses[p_bus]->channels.size()) {
  572. return false;
  573. }
  574. return true;
  575. }
  576. AudioFrame *AudioServer::thread_get_channel_mix_buffer(int p_bus, int p_buffer) {
  577. ERR_FAIL_INDEX_V(p_bus, buses.size(), nullptr);
  578. ERR_FAIL_INDEX_V(p_buffer, buses[p_bus]->channels.size(), nullptr);
  579. AudioFrame *data = buses.write[p_bus]->channels.write[p_buffer].buffer.ptrw();
  580. if (!buses[p_bus]->channels[p_buffer].used) {
  581. buses.write[p_bus]->channels.write[p_buffer].used = true;
  582. buses.write[p_bus]->channels.write[p_buffer].active = true;
  583. buses.write[p_bus]->channels.write[p_buffer].last_mix_with_audio = mix_frames;
  584. for (uint32_t i = 0; i < buffer_size; i++) {
  585. data[i] = AudioFrame(0, 0);
  586. }
  587. }
  588. return data;
  589. }
  590. int AudioServer::thread_get_mix_buffer_size() const {
  591. return buffer_size;
  592. }
  593. int AudioServer::thread_find_bus_index(const StringName &p_name) {
  594. if (bus_map.has(p_name)) {
  595. return bus_map[p_name]->index_cache;
  596. } else {
  597. return 0;
  598. }
  599. }
  600. void AudioServer::set_bus_count(int p_count) {
  601. ERR_FAIL_COND(p_count < 1);
  602. ERR_FAIL_INDEX(p_count, 256);
  603. MARK_EDITED
  604. lock();
  605. int cb = buses.size();
  606. if (p_count < buses.size()) {
  607. for (int i = p_count; i < buses.size(); i++) {
  608. bus_map.erase(buses[i]->name);
  609. memdelete(buses[i]);
  610. }
  611. }
  612. buses.resize(p_count);
  613. for (int i = cb; i < buses.size(); i++) {
  614. String attempt = "New Bus";
  615. int attempts = 1;
  616. while (true) {
  617. bool name_free = true;
  618. for (int j = 0; j < i; j++) {
  619. if (buses[j]->name == attempt) {
  620. name_free = false;
  621. break;
  622. }
  623. }
  624. if (!name_free) {
  625. attempts++;
  626. attempt = "New Bus " + itos(attempts);
  627. } else {
  628. break;
  629. }
  630. }
  631. buses.write[i] = memnew(Bus);
  632. buses.write[i]->channels.resize(channel_count);
  633. for (int j = 0; j < channel_count; j++) {
  634. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  635. }
  636. buses[i]->name = attempt;
  637. buses[i]->solo = false;
  638. buses[i]->mute = false;
  639. buses[i]->bypass = false;
  640. buses[i]->volume_db = 0;
  641. if (i > 0) {
  642. buses[i]->send = SceneStringName(Master);
  643. }
  644. bus_map[attempt] = buses[i];
  645. }
  646. unlock();
  647. emit_signal(SNAME("bus_layout_changed"));
  648. }
  649. void AudioServer::remove_bus(int p_index) {
  650. ERR_FAIL_INDEX(p_index, buses.size());
  651. ERR_FAIL_COND(p_index == 0);
  652. MARK_EDITED
  653. lock();
  654. bus_map.erase(buses[p_index]->name);
  655. memdelete(buses[p_index]);
  656. buses.remove_at(p_index);
  657. unlock();
  658. emit_signal(SNAME("bus_layout_changed"));
  659. }
  660. void AudioServer::add_bus(int p_at_pos) {
  661. MARK_EDITED
  662. if (p_at_pos >= buses.size()) {
  663. p_at_pos = -1;
  664. } else if (p_at_pos == 0) {
  665. if (buses.size() > 1) {
  666. p_at_pos = 1;
  667. } else {
  668. p_at_pos = -1;
  669. }
  670. }
  671. String attempt = "New Bus";
  672. int attempts = 1;
  673. while (true) {
  674. bool name_free = true;
  675. for (int j = 0; j < buses.size(); j++) {
  676. if (buses[j]->name == attempt) {
  677. name_free = false;
  678. break;
  679. }
  680. }
  681. if (!name_free) {
  682. attempts++;
  683. attempt = "New Bus " + itos(attempts);
  684. } else {
  685. break;
  686. }
  687. }
  688. Bus *bus = memnew(Bus);
  689. bus->channels.resize(channel_count);
  690. for (int j = 0; j < channel_count; j++) {
  691. bus->channels.write[j].buffer.resize(buffer_size);
  692. }
  693. bus->name = attempt;
  694. bus->solo = false;
  695. bus->mute = false;
  696. bus->bypass = false;
  697. bus->volume_db = 0;
  698. bus_map[attempt] = bus;
  699. if (p_at_pos == -1) {
  700. buses.push_back(bus);
  701. } else {
  702. buses.insert(p_at_pos, bus);
  703. }
  704. emit_signal(SNAME("bus_layout_changed"));
  705. }
  706. void AudioServer::move_bus(int p_bus, int p_to_pos) {
  707. ERR_FAIL_COND(p_bus < 1 || p_bus >= buses.size());
  708. ERR_FAIL_COND(p_to_pos != -1 && (p_to_pos < 1 || p_to_pos > buses.size()));
  709. MARK_EDITED
  710. if (p_bus == p_to_pos) {
  711. return;
  712. }
  713. Bus *bus = buses[p_bus];
  714. buses.remove_at(p_bus);
  715. if (p_to_pos == -1) {
  716. buses.push_back(bus);
  717. } else if (p_to_pos < p_bus) {
  718. buses.insert(p_to_pos, bus);
  719. } else {
  720. buses.insert(p_to_pos - 1, bus);
  721. }
  722. emit_signal(SNAME("bus_layout_changed"));
  723. }
  724. int AudioServer::get_bus_count() const {
  725. return buses.size();
  726. }
  727. void AudioServer::set_bus_name(int p_bus, const String &p_name) {
  728. ERR_FAIL_INDEX(p_bus, buses.size());
  729. if (p_bus == 0 && p_name != "Master") {
  730. return; // Bus 0 is always "Master".
  731. }
  732. MARK_EDITED
  733. lock();
  734. StringName old_name = buses[p_bus]->name;
  735. if (old_name == p_name) {
  736. unlock();
  737. return;
  738. }
  739. String attempt = p_name;
  740. int attempts = 1;
  741. while (true) {
  742. bool name_free = true;
  743. for (int i = 0; i < buses.size(); i++) {
  744. if (buses[i]->name == attempt) {
  745. name_free = false;
  746. break;
  747. }
  748. }
  749. if (name_free) {
  750. break;
  751. }
  752. attempts++;
  753. attempt = p_name + " " + itos(attempts);
  754. }
  755. bus_map.erase(old_name);
  756. buses[p_bus]->name = attempt;
  757. bus_map[attempt] = buses[p_bus];
  758. unlock();
  759. emit_signal(SNAME("bus_renamed"), p_bus, old_name, attempt);
  760. }
  761. String AudioServer::get_bus_name(int p_bus) const {
  762. ERR_FAIL_INDEX_V(p_bus, buses.size(), String());
  763. return buses[p_bus]->name;
  764. }
  765. int AudioServer::get_bus_index(const StringName &p_bus_name) const {
  766. for (int i = 0; i < buses.size(); ++i) {
  767. if (buses[i]->name == p_bus_name) {
  768. return i;
  769. }
  770. }
  771. return -1;
  772. }
  773. void AudioServer::set_bus_volume_db(int p_bus, float p_volume_db) {
  774. ERR_FAIL_INDEX(p_bus, buses.size());
  775. MARK_EDITED
  776. buses[p_bus]->volume_db = p_volume_db;
  777. }
  778. float AudioServer::get_bus_volume_db(int p_bus) const {
  779. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  780. return buses[p_bus]->volume_db;
  781. }
  782. int AudioServer::get_bus_channels(int p_bus) const {
  783. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  784. return buses[p_bus]->channels.size();
  785. }
  786. void AudioServer::set_bus_send(int p_bus, const StringName &p_send) {
  787. ERR_FAIL_INDEX(p_bus, buses.size());
  788. MARK_EDITED
  789. buses[p_bus]->send = p_send;
  790. }
  791. StringName AudioServer::get_bus_send(int p_bus) const {
  792. ERR_FAIL_INDEX_V(p_bus, buses.size(), StringName());
  793. return buses[p_bus]->send;
  794. }
  795. void AudioServer::set_bus_solo(int p_bus, bool p_enable) {
  796. ERR_FAIL_INDEX(p_bus, buses.size());
  797. MARK_EDITED
  798. buses[p_bus]->solo = p_enable;
  799. }
  800. bool AudioServer::is_bus_solo(int p_bus) const {
  801. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  802. return buses[p_bus]->solo;
  803. }
  804. void AudioServer::set_bus_mute(int p_bus, bool p_enable) {
  805. ERR_FAIL_INDEX(p_bus, buses.size());
  806. MARK_EDITED
  807. buses[p_bus]->mute = p_enable;
  808. }
  809. bool AudioServer::is_bus_mute(int p_bus) const {
  810. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  811. return buses[p_bus]->mute;
  812. }
  813. void AudioServer::set_bus_bypass_effects(int p_bus, bool p_enable) {
  814. ERR_FAIL_INDEX(p_bus, buses.size());
  815. MARK_EDITED
  816. buses[p_bus]->bypass = p_enable;
  817. }
  818. bool AudioServer::is_bus_bypassing_effects(int p_bus) const {
  819. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  820. return buses[p_bus]->bypass;
  821. }
  822. void AudioServer::_update_bus_effects(int p_bus) {
  823. for (int i = 0; i < buses[p_bus]->channels.size(); i++) {
  824. buses.write[p_bus]->channels.write[i].effect_instances.resize(buses[p_bus]->effects.size());
  825. for (int j = 0; j < buses[p_bus]->effects.size(); j++) {
  826. Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instantiate();
  827. if (Object::cast_to<AudioEffectCompressorInstance>(*fx)) {
  828. Object::cast_to<AudioEffectCompressorInstance>(*fx)->set_current_channel(i);
  829. }
  830. buses.write[p_bus]->channels.write[i].effect_instances.write[j] = fx;
  831. }
  832. }
  833. }
  834. void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos) {
  835. ERR_FAIL_COND(p_effect.is_null());
  836. ERR_FAIL_INDEX(p_bus, buses.size());
  837. MARK_EDITED
  838. lock();
  839. Bus::Effect fx;
  840. fx.effect = p_effect;
  841. //fx.instance=p_effect->instantiate();
  842. fx.enabled = true;
  843. #ifdef DEBUG_ENABLED
  844. fx.prof_time = 0;
  845. #endif
  846. if (p_at_pos >= buses[p_bus]->effects.size() || p_at_pos < 0) {
  847. buses[p_bus]->effects.push_back(fx);
  848. } else {
  849. buses[p_bus]->effects.insert(p_at_pos, fx);
  850. }
  851. _update_bus_effects(p_bus);
  852. unlock();
  853. }
  854. void AudioServer::remove_bus_effect(int p_bus, int p_effect) {
  855. ERR_FAIL_INDEX(p_bus, buses.size());
  856. MARK_EDITED
  857. lock();
  858. buses[p_bus]->effects.remove_at(p_effect);
  859. _update_bus_effects(p_bus);
  860. unlock();
  861. }
  862. int AudioServer::get_bus_effect_count(int p_bus) {
  863. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  864. return buses[p_bus]->effects.size();
  865. }
  866. Ref<AudioEffectInstance> AudioServer::get_bus_effect_instance(int p_bus, int p_effect, int p_channel) {
  867. ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffectInstance>());
  868. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffectInstance>());
  869. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), Ref<AudioEffectInstance>());
  870. return buses[p_bus]->channels[p_channel].effect_instances[p_effect];
  871. }
  872. Ref<AudioEffect> AudioServer::get_bus_effect(int p_bus, int p_effect) {
  873. ERR_FAIL_INDEX_V(p_bus, buses.size(), Ref<AudioEffect>());
  874. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), Ref<AudioEffect>());
  875. return buses[p_bus]->effects[p_effect].effect;
  876. }
  877. void AudioServer::swap_bus_effects(int p_bus, int p_effect, int p_by_effect) {
  878. ERR_FAIL_INDEX(p_bus, buses.size());
  879. ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
  880. ERR_FAIL_INDEX(p_by_effect, buses[p_bus]->effects.size());
  881. MARK_EDITED
  882. lock();
  883. SWAP(buses.write[p_bus]->effects.write[p_effect], buses.write[p_bus]->effects.write[p_by_effect]);
  884. _update_bus_effects(p_bus);
  885. unlock();
  886. }
  887. void AudioServer::set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled) {
  888. ERR_FAIL_INDEX(p_bus, buses.size());
  889. ERR_FAIL_INDEX(p_effect, buses[p_bus]->effects.size());
  890. MARK_EDITED
  891. buses.write[p_bus]->effects.write[p_effect].enabled = p_enabled;
  892. }
  893. bool AudioServer::is_bus_effect_enabled(int p_bus, int p_effect) const {
  894. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  895. ERR_FAIL_INDEX_V(p_effect, buses[p_bus]->effects.size(), false);
  896. return buses[p_bus]->effects[p_effect].enabled;
  897. }
  898. float AudioServer::get_bus_peak_volume_left_db(int p_bus, int p_channel) const {
  899. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  900. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
  901. return buses[p_bus]->channels[p_channel].peak_volume.left;
  902. }
  903. float AudioServer::get_bus_peak_volume_right_db(int p_bus, int p_channel) const {
  904. ERR_FAIL_INDEX_V(p_bus, buses.size(), 0);
  905. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), 0);
  906. return buses[p_bus]->channels[p_channel].peak_volume.right;
  907. }
  908. bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
  909. ERR_FAIL_INDEX_V(p_bus, buses.size(), false);
  910. ERR_FAIL_INDEX_V(p_channel, buses[p_bus]->channels.size(), false);
  911. return buses[p_bus]->channels[p_channel].active;
  912. }
  913. void AudioServer::set_playback_speed_scale(float p_scale) {
  914. ERR_FAIL_COND(p_scale <= 0);
  915. playback_speed_scale = p_scale;
  916. }
  917. float AudioServer::get_playback_speed_scale() const {
  918. return playback_speed_scale;
  919. }
  920. void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, const StringName &p_bus, Vector<AudioFrame> p_volume_db_vector, float p_start_time, float p_pitch_scale) {
  921. ERR_FAIL_COND(p_playback.is_null());
  922. HashMap<StringName, Vector<AudioFrame>> map;
  923. map[p_bus] = p_volume_db_vector;
  924. start_playback_stream(p_playback, map, p_start_time, p_pitch_scale);
  925. }
  926. void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes, float p_start_time, float p_pitch_scale, float p_highshelf_gain, float p_attenuation_cutoff_hz) {
  927. ERR_FAIL_COND(p_playback.is_null());
  928. AudioStreamPlaybackListNode *playback_node = new AudioStreamPlaybackListNode();
  929. playback_node->stream_playback = p_playback;
  930. playback_node->stream_playback->start(p_start_time);
  931. AudioStreamPlaybackBusDetails *new_bus_details = new AudioStreamPlaybackBusDetails();
  932. int idx = 0;
  933. for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
  934. if (pair.value.size() < channel_count || pair.value.size() != MAX_CHANNELS_PER_BUS) {
  935. delete new_bus_details;
  936. ERR_FAIL();
  937. }
  938. new_bus_details->bus_active[idx] = true;
  939. new_bus_details->bus[idx] = pair.key;
  940. for (int channel_idx = 0; channel_idx < MAX_CHANNELS_PER_BUS; channel_idx++) {
  941. new_bus_details->volume[idx][channel_idx] = pair.value[channel_idx];
  942. }
  943. idx++;
  944. }
  945. playback_node->bus_details = new_bus_details;
  946. playback_node->prev_bus_details = new AudioStreamPlaybackBusDetails();
  947. playback_node->pitch_scale.set(p_pitch_scale);
  948. playback_node->highshelf_gain.set(p_highshelf_gain);
  949. playback_node->attenuation_filter_cutoff_hz.set(p_attenuation_cutoff_hz);
  950. memset(playback_node->prev_bus_details->volume, 0, sizeof(playback_node->prev_bus_details->volume));
  951. for (AudioFrame &frame : playback_node->lookahead) {
  952. frame = AudioFrame(0, 0);
  953. }
  954. playback_node->state.store(AudioStreamPlaybackListNode::PLAYING);
  955. playback_list.insert(playback_node);
  956. }
  957. void AudioServer::stop_playback_stream(Ref<AudioStreamPlayback> p_playback) {
  958. ERR_FAIL_COND(p_playback.is_null());
  959. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  960. if (!playback_node) {
  961. return;
  962. }
  963. AudioStreamPlaybackListNode::PlaybackState new_state, old_state;
  964. do {
  965. old_state = playback_node->state.load();
  966. if (old_state == AudioStreamPlaybackListNode::AWAITING_DELETION) {
  967. break; // Don't fade out again.
  968. }
  969. new_state = AudioStreamPlaybackListNode::FADE_OUT_TO_DELETION;
  970. } while (!playback_node->state.compare_exchange_strong(old_state, new_state));
  971. }
  972. void AudioServer::set_playback_bus_exclusive(Ref<AudioStreamPlayback> p_playback, const StringName &p_bus, Vector<AudioFrame> p_volumes) {
  973. ERR_FAIL_COND(p_volumes.size() != MAX_CHANNELS_PER_BUS);
  974. HashMap<StringName, Vector<AudioFrame>> map;
  975. map[p_bus] = p_volumes;
  976. set_playback_bus_volumes_linear(p_playback, map);
  977. }
  978. void AudioServer::set_playback_bus_volumes_linear(Ref<AudioStreamPlayback> p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) {
  979. ERR_FAIL_COND(p_bus_volumes.size() > MAX_BUSES_PER_PLAYBACK);
  980. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  981. if (!playback_node) {
  982. return;
  983. }
  984. AudioStreamPlaybackBusDetails *old_bus_details, *new_bus_details = new AudioStreamPlaybackBusDetails();
  985. int idx = 0;
  986. for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
  987. if (idx >= MAX_BUSES_PER_PLAYBACK) {
  988. break;
  989. }
  990. ERR_FAIL_COND(pair.value.size() < channel_count);
  991. ERR_FAIL_COND(pair.value.size() != MAX_CHANNELS_PER_BUS);
  992. new_bus_details->bus_active[idx] = true;
  993. new_bus_details->bus[idx] = pair.key;
  994. for (int channel_idx = 0; channel_idx < MAX_CHANNELS_PER_BUS; channel_idx++) {
  995. new_bus_details->volume[idx][channel_idx] = pair.value[channel_idx];
  996. }
  997. idx++;
  998. }
  999. do {
  1000. old_bus_details = playback_node->bus_details.load();
  1001. } while (!playback_node->bus_details.compare_exchange_strong(old_bus_details, new_bus_details));
  1002. bus_details_graveyard.insert(old_bus_details);
  1003. }
  1004. void AudioServer::set_playback_all_bus_volumes_linear(Ref<AudioStreamPlayback> p_playback, Vector<AudioFrame> p_volumes) {
  1005. ERR_FAIL_COND(p_playback.is_null());
  1006. ERR_FAIL_COND(p_volumes.size() != MAX_CHANNELS_PER_BUS);
  1007. HashMap<StringName, Vector<AudioFrame>> map;
  1008. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1009. if (!playback_node) {
  1010. return;
  1011. }
  1012. for (int bus_idx = 0; bus_idx < MAX_BUSES_PER_PLAYBACK; bus_idx++) {
  1013. if (playback_node->bus_details.load()->bus_active[bus_idx]) {
  1014. map[playback_node->bus_details.load()->bus[bus_idx]] = p_volumes;
  1015. }
  1016. }
  1017. set_playback_bus_volumes_linear(p_playback, map);
  1018. }
  1019. void AudioServer::set_playback_pitch_scale(Ref<AudioStreamPlayback> p_playback, float p_pitch_scale) {
  1020. ERR_FAIL_COND(p_playback.is_null());
  1021. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1022. if (!playback_node) {
  1023. return;
  1024. }
  1025. playback_node->pitch_scale.set(p_pitch_scale);
  1026. }
  1027. void AudioServer::set_playback_paused(Ref<AudioStreamPlayback> p_playback, bool p_paused) {
  1028. ERR_FAIL_COND(p_playback.is_null());
  1029. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1030. if (!playback_node) {
  1031. return;
  1032. }
  1033. AudioStreamPlaybackListNode::PlaybackState new_state, old_state;
  1034. do {
  1035. old_state = playback_node->state.load();
  1036. new_state = p_paused ? AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE : AudioStreamPlaybackListNode::PLAYING;
  1037. if (!p_paused && old_state == AudioStreamPlaybackListNode::PLAYING) {
  1038. return; // No-op.
  1039. }
  1040. if (p_paused && (old_state == AudioStreamPlaybackListNode::PAUSED || old_state == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE)) {
  1041. return; // No-op.
  1042. }
  1043. } while (!playback_node->state.compare_exchange_strong(old_state, new_state));
  1044. }
  1045. void AudioServer::set_playback_highshelf_params(Ref<AudioStreamPlayback> p_playback, float p_gain, float p_attenuation_cutoff_hz) {
  1046. ERR_FAIL_COND(p_playback.is_null());
  1047. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1048. if (!playback_node) {
  1049. return;
  1050. }
  1051. playback_node->attenuation_filter_cutoff_hz.set(p_attenuation_cutoff_hz);
  1052. playback_node->highshelf_gain.set(p_gain);
  1053. }
  1054. bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> p_playback) {
  1055. ERR_FAIL_COND_V(p_playback.is_null(), false);
  1056. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1057. if (!playback_node) {
  1058. return false;
  1059. }
  1060. return playback_node->state.load() == AudioStreamPlaybackListNode::PLAYING;
  1061. }
  1062. float AudioServer::get_playback_position(Ref<AudioStreamPlayback> p_playback) {
  1063. ERR_FAIL_COND_V(p_playback.is_null(), 0);
  1064. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1065. if (!playback_node) {
  1066. return 0;
  1067. }
  1068. return playback_node->stream_playback->get_playback_position();
  1069. }
  1070. bool AudioServer::is_playback_paused(Ref<AudioStreamPlayback> p_playback) {
  1071. ERR_FAIL_COND_V(p_playback.is_null(), false);
  1072. AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
  1073. if (!playback_node) {
  1074. return false;
  1075. }
  1076. return playback_node->state.load() == AudioStreamPlaybackListNode::PAUSED || playback_node->state.load() == AudioStreamPlaybackListNode::FADE_OUT_TO_PAUSE;
  1077. }
  1078. uint64_t AudioServer::get_mix_count() const {
  1079. return mix_count;
  1080. }
  1081. uint64_t AudioServer::get_mixed_frames() const {
  1082. return mix_frames;
  1083. }
  1084. void AudioServer::notify_listener_changed() {
  1085. for (CallbackItem *ci : listener_changed_callback_list) {
  1086. ci->callback(ci->userdata);
  1087. }
  1088. }
  1089. void AudioServer::init_channels_and_buffers() {
  1090. channel_count = get_channel_count();
  1091. temp_buffer.resize(channel_count);
  1092. mix_buffer.resize(buffer_size + LOOKAHEAD_BUFFER_SIZE);
  1093. for (int i = 0; i < temp_buffer.size(); i++) {
  1094. temp_buffer.write[i].resize(buffer_size);
  1095. }
  1096. for (int i = 0; i < buses.size(); i++) {
  1097. buses[i]->channels.resize(channel_count);
  1098. for (int j = 0; j < channel_count; j++) {
  1099. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  1100. }
  1101. _update_bus_effects(i);
  1102. }
  1103. }
  1104. void AudioServer::init() {
  1105. channel_disable_threshold_db = GLOBAL_DEF_RST("audio/buses/channel_disable_threshold_db", -60.0);
  1106. channel_disable_frames = float(GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_time", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"), 2.0)) * get_mix_rate();
  1107. buffer_size = 512; //hardcoded for now
  1108. init_channels_and_buffers();
  1109. mix_count = 0;
  1110. set_bus_count(1);
  1111. set_bus_name(0, "Master");
  1112. if (AudioDriver::get_singleton()) {
  1113. AudioDriver::get_singleton()->start();
  1114. }
  1115. #ifdef TOOLS_ENABLED
  1116. set_edited(false); //avoid editors from thinking this was edited
  1117. #endif
  1118. GLOBAL_DEF_RST("audio/video/video_delay_compensation_ms", 0);
  1119. }
  1120. void AudioServer::update() {
  1121. #ifdef DEBUG_ENABLED
  1122. if (EngineDebugger::is_profiling("servers")) {
  1123. // Driver time includes server time + effects times
  1124. // Server time includes effects times
  1125. uint64_t driver_time = AudioDriver::get_singleton()->get_profiling_time();
  1126. uint64_t server_time = prof_time.get();
  1127. // Subtract the server time from the driver time
  1128. if (driver_time > server_time) {
  1129. driver_time -= server_time;
  1130. }
  1131. Array values;
  1132. for (int i = buses.size() - 1; i >= 0; i--) {
  1133. Bus *bus = buses[i];
  1134. if (bus->bypass) {
  1135. continue;
  1136. }
  1137. for (int j = 0; j < bus->effects.size(); j++) {
  1138. if (!bus->effects[j].enabled) {
  1139. continue;
  1140. }
  1141. values.push_back(String(bus->name) + bus->effects[j].effect->get_name());
  1142. values.push_back(USEC_TO_SEC(bus->effects[j].prof_time));
  1143. // Subtract the effect time from the driver and server times
  1144. if (driver_time > bus->effects[j].prof_time) {
  1145. driver_time -= bus->effects[j].prof_time;
  1146. }
  1147. if (server_time > bus->effects[j].prof_time) {
  1148. server_time -= bus->effects[j].prof_time;
  1149. }
  1150. }
  1151. }
  1152. values.push_back("audio_server");
  1153. values.push_back(USEC_TO_SEC(server_time));
  1154. values.push_back("audio_driver");
  1155. values.push_back(USEC_TO_SEC(driver_time));
  1156. values.push_front("audio_thread");
  1157. EngineDebugger::profiler_add_frame_data("servers", values);
  1158. }
  1159. // Reset profiling times
  1160. for (int i = buses.size() - 1; i >= 0; i--) {
  1161. Bus *bus = buses[i];
  1162. if (bus->bypass) {
  1163. continue;
  1164. }
  1165. for (int j = 0; j < bus->effects.size(); j++) {
  1166. if (!bus->effects[j].enabled) {
  1167. continue;
  1168. }
  1169. bus->effects.write[j].prof_time = 0;
  1170. }
  1171. }
  1172. AudioDriver::get_singleton()->reset_profiling_time();
  1173. prof_time.set(0);
  1174. #endif
  1175. for (CallbackItem *ci : update_callback_list) {
  1176. ci->callback(ci->userdata);
  1177. }
  1178. mix_callback_list.maybe_cleanup();
  1179. update_callback_list.maybe_cleanup();
  1180. listener_changed_callback_list.maybe_cleanup();
  1181. playback_list.maybe_cleanup();
  1182. for (AudioStreamPlaybackBusDetails *bus_details : bus_details_graveyard_frame_old) {
  1183. bus_details_graveyard_frame_old.erase(bus_details, [](AudioStreamPlaybackBusDetails *d) { delete d; });
  1184. }
  1185. for (AudioStreamPlaybackBusDetails *bus_details : bus_details_graveyard) {
  1186. bus_details_graveyard_frame_old.insert(bus_details);
  1187. bus_details_graveyard.erase(bus_details);
  1188. }
  1189. bus_details_graveyard.maybe_cleanup();
  1190. bus_details_graveyard_frame_old.maybe_cleanup();
  1191. }
  1192. void AudioServer::load_default_bus_layout() {
  1193. String layout_path = GLOBAL_GET("audio/buses/default_bus_layout");
  1194. if (ResourceLoader::exists(layout_path)) {
  1195. Ref<AudioBusLayout> default_layout = ResourceLoader::load(layout_path);
  1196. if (default_layout.is_valid()) {
  1197. set_bus_layout(default_layout);
  1198. }
  1199. }
  1200. }
  1201. void AudioServer::finish() {
  1202. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  1203. AudioDriverManager::get_driver(i)->finish();
  1204. }
  1205. for (int i = 0; i < buses.size(); i++) {
  1206. memdelete(buses[i]);
  1207. }
  1208. buses.clear();
  1209. }
  1210. /* MISC config */
  1211. void AudioServer::lock() {
  1212. AudioDriver::get_singleton()->lock();
  1213. }
  1214. void AudioServer::unlock() {
  1215. AudioDriver::get_singleton()->unlock();
  1216. }
  1217. AudioServer::SpeakerMode AudioServer::get_speaker_mode() const {
  1218. return (AudioServer::SpeakerMode)AudioDriver::get_singleton()->get_speaker_mode();
  1219. }
  1220. float AudioServer::get_mix_rate() const {
  1221. return AudioDriver::get_singleton()->get_mix_rate();
  1222. }
  1223. float AudioServer::read_output_peak_db() const {
  1224. return 0;
  1225. }
  1226. AudioServer *AudioServer::get_singleton() {
  1227. return singleton;
  1228. }
  1229. double AudioServer::get_output_latency() const {
  1230. return AudioDriver::get_singleton()->get_latency();
  1231. }
  1232. double AudioServer::get_time_to_next_mix() const {
  1233. return AudioDriver::get_singleton()->get_time_to_next_mix();
  1234. }
  1235. double AudioServer::get_time_since_last_mix() const {
  1236. return AudioDriver::get_singleton()->get_time_since_last_mix();
  1237. }
  1238. AudioServer *AudioServer::singleton = nullptr;
  1239. void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) {
  1240. CallbackItem *ci = new CallbackItem();
  1241. ci->callback = p_callback;
  1242. ci->userdata = p_userdata;
  1243. update_callback_list.insert(ci);
  1244. }
  1245. void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) {
  1246. for (CallbackItem *ci : update_callback_list) {
  1247. if (ci->callback == p_callback && ci->userdata == p_userdata) {
  1248. update_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
  1249. }
  1250. }
  1251. }
  1252. void AudioServer::add_mix_callback(AudioCallback p_callback, void *p_userdata) {
  1253. CallbackItem *ci = new CallbackItem();
  1254. ci->callback = p_callback;
  1255. ci->userdata = p_userdata;
  1256. mix_callback_list.insert(ci);
  1257. }
  1258. void AudioServer::remove_mix_callback(AudioCallback p_callback, void *p_userdata) {
  1259. for (CallbackItem *ci : mix_callback_list) {
  1260. if (ci->callback == p_callback && ci->userdata == p_userdata) {
  1261. mix_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
  1262. }
  1263. }
  1264. }
  1265. void AudioServer::add_listener_changed_callback(AudioCallback p_callback, void *p_userdata) {
  1266. CallbackItem *ci = new CallbackItem();
  1267. ci->callback = p_callback;
  1268. ci->userdata = p_userdata;
  1269. listener_changed_callback_list.insert(ci);
  1270. }
  1271. void AudioServer::remove_listener_changed_callback(AudioCallback p_callback, void *p_userdata) {
  1272. for (CallbackItem *ci : listener_changed_callback_list) {
  1273. if (ci->callback == p_callback && ci->userdata == p_userdata) {
  1274. listener_changed_callback_list.erase(ci, [](CallbackItem *c) { delete c; });
  1275. }
  1276. }
  1277. }
  1278. void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
  1279. ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.is_empty());
  1280. lock();
  1281. for (int i = 0; i < buses.size(); i++) {
  1282. memdelete(buses[i]);
  1283. }
  1284. buses.resize(p_bus_layout->buses.size());
  1285. bus_map.clear();
  1286. for (int i = 0; i < p_bus_layout->buses.size(); i++) {
  1287. Bus *bus = memnew(Bus);
  1288. if (i == 0) {
  1289. bus->name = SceneStringName(Master);
  1290. } else {
  1291. bus->name = p_bus_layout->buses[i].name;
  1292. bus->send = p_bus_layout->buses[i].send;
  1293. }
  1294. bus->solo = p_bus_layout->buses[i].solo;
  1295. bus->mute = p_bus_layout->buses[i].mute;
  1296. bus->bypass = p_bus_layout->buses[i].bypass;
  1297. bus->volume_db = p_bus_layout->buses[i].volume_db;
  1298. for (int j = 0; j < p_bus_layout->buses[i].effects.size(); j++) {
  1299. Ref<AudioEffect> fx = p_bus_layout->buses[i].effects[j].effect;
  1300. if (fx.is_valid()) {
  1301. Bus::Effect bfx;
  1302. bfx.effect = fx;
  1303. bfx.enabled = p_bus_layout->buses[i].effects[j].enabled;
  1304. #ifdef DEBUG_ENABLED
  1305. bfx.prof_time = 0;
  1306. #endif
  1307. bus->effects.push_back(bfx);
  1308. }
  1309. }
  1310. bus_map[bus->name] = bus;
  1311. buses.write[i] = bus;
  1312. buses[i]->channels.resize(channel_count);
  1313. for (int j = 0; j < channel_count; j++) {
  1314. buses.write[i]->channels.write[j].buffer.resize(buffer_size);
  1315. }
  1316. _update_bus_effects(i);
  1317. }
  1318. #ifdef TOOLS_ENABLED
  1319. set_edited(false);
  1320. #endif
  1321. unlock();
  1322. }
  1323. Ref<AudioBusLayout> AudioServer::generate_bus_layout() const {
  1324. Ref<AudioBusLayout> state;
  1325. state.instantiate();
  1326. state->buses.resize(buses.size());
  1327. for (int i = 0; i < buses.size(); i++) {
  1328. state->buses.write[i].name = buses[i]->name;
  1329. state->buses.write[i].send = buses[i]->send;
  1330. state->buses.write[i].mute = buses[i]->mute;
  1331. state->buses.write[i].solo = buses[i]->solo;
  1332. state->buses.write[i].bypass = buses[i]->bypass;
  1333. state->buses.write[i].volume_db = buses[i]->volume_db;
  1334. for (int j = 0; j < buses[i]->effects.size(); j++) {
  1335. AudioBusLayout::Bus::Effect fx;
  1336. fx.effect = buses[i]->effects[j].effect;
  1337. fx.enabled = buses[i]->effects[j].enabled;
  1338. state->buses.write[i].effects.push_back(fx);
  1339. }
  1340. }
  1341. return state;
  1342. }
  1343. PackedStringArray AudioServer::get_output_device_list() {
  1344. return AudioDriver::get_singleton()->get_output_device_list();
  1345. }
  1346. String AudioServer::get_output_device() {
  1347. return AudioDriver::get_singleton()->get_output_device();
  1348. }
  1349. void AudioServer::set_output_device(const String &p_name) {
  1350. AudioDriver::get_singleton()->set_output_device(p_name);
  1351. }
  1352. PackedStringArray AudioServer::get_input_device_list() {
  1353. return AudioDriver::get_singleton()->get_input_device_list();
  1354. }
  1355. String AudioServer::get_input_device() {
  1356. return AudioDriver::get_singleton()->get_input_device();
  1357. }
  1358. void AudioServer::set_input_device(const String &p_name) {
  1359. AudioDriver::get_singleton()->set_input_device(p_name);
  1360. }
  1361. void AudioServer::set_enable_tagging_used_audio_streams(bool p_enable) {
  1362. tag_used_audio_streams = p_enable;
  1363. }
  1364. #ifdef TOOLS_ENABLED
  1365. void AudioServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1366. const String pf = p_function;
  1367. if ((p_idx == 0 && pf == "get_bus_index") || (p_idx == 1 && pf == "set_bus_send")) {
  1368. for (const AudioServer::Bus *E : buses) {
  1369. r_options->push_back(String(E->name).quote());
  1370. }
  1371. }
  1372. Object::get_argument_options(p_function, p_idx, r_options);
  1373. }
  1374. #endif
  1375. void AudioServer::_bind_methods() {
  1376. ClassDB::bind_method(D_METHOD("set_bus_count", "amount"), &AudioServer::set_bus_count);
  1377. ClassDB::bind_method(D_METHOD("get_bus_count"), &AudioServer::get_bus_count);
  1378. ClassDB::bind_method(D_METHOD("remove_bus", "index"), &AudioServer::remove_bus);
  1379. ClassDB::bind_method(D_METHOD("add_bus", "at_position"), &AudioServer::add_bus, DEFVAL(-1));
  1380. ClassDB::bind_method(D_METHOD("move_bus", "index", "to_index"), &AudioServer::move_bus);
  1381. ClassDB::bind_method(D_METHOD("set_bus_name", "bus_idx", "name"), &AudioServer::set_bus_name);
  1382. ClassDB::bind_method(D_METHOD("get_bus_name", "bus_idx"), &AudioServer::get_bus_name);
  1383. ClassDB::bind_method(D_METHOD("get_bus_index", "bus_name"), &AudioServer::get_bus_index);
  1384. ClassDB::bind_method(D_METHOD("get_bus_channels", "bus_idx"), &AudioServer::get_bus_channels);
  1385. ClassDB::bind_method(D_METHOD("set_bus_volume_db", "bus_idx", "volume_db"), &AudioServer::set_bus_volume_db);
  1386. ClassDB::bind_method(D_METHOD("get_bus_volume_db", "bus_idx"), &AudioServer::get_bus_volume_db);
  1387. ClassDB::bind_method(D_METHOD("set_bus_send", "bus_idx", "send"), &AudioServer::set_bus_send);
  1388. ClassDB::bind_method(D_METHOD("get_bus_send", "bus_idx"), &AudioServer::get_bus_send);
  1389. ClassDB::bind_method(D_METHOD("set_bus_solo", "bus_idx", "enable"), &AudioServer::set_bus_solo);
  1390. ClassDB::bind_method(D_METHOD("is_bus_solo", "bus_idx"), &AudioServer::is_bus_solo);
  1391. ClassDB::bind_method(D_METHOD("set_bus_mute", "bus_idx", "enable"), &AudioServer::set_bus_mute);
  1392. ClassDB::bind_method(D_METHOD("is_bus_mute", "bus_idx"), &AudioServer::is_bus_mute);
  1393. ClassDB::bind_method(D_METHOD("set_bus_bypass_effects", "bus_idx", "enable"), &AudioServer::set_bus_bypass_effects);
  1394. ClassDB::bind_method(D_METHOD("is_bus_bypassing_effects", "bus_idx"), &AudioServer::is_bus_bypassing_effects);
  1395. ClassDB::bind_method(D_METHOD("add_bus_effect", "bus_idx", "effect", "at_position"), &AudioServer::add_bus_effect, DEFVAL(-1));
  1396. ClassDB::bind_method(D_METHOD("remove_bus_effect", "bus_idx", "effect_idx"), &AudioServer::remove_bus_effect);
  1397. ClassDB::bind_method(D_METHOD("get_bus_effect_count", "bus_idx"), &AudioServer::get_bus_effect_count);
  1398. ClassDB::bind_method(D_METHOD("get_bus_effect", "bus_idx", "effect_idx"), &AudioServer::get_bus_effect);
  1399. ClassDB::bind_method(D_METHOD("get_bus_effect_instance", "bus_idx", "effect_idx", "channel"), &AudioServer::get_bus_effect_instance, DEFVAL(0));
  1400. ClassDB::bind_method(D_METHOD("swap_bus_effects", "bus_idx", "effect_idx", "by_effect_idx"), &AudioServer::swap_bus_effects);
  1401. ClassDB::bind_method(D_METHOD("set_bus_effect_enabled", "bus_idx", "effect_idx", "enabled"), &AudioServer::set_bus_effect_enabled);
  1402. ClassDB::bind_method(D_METHOD("is_bus_effect_enabled", "bus_idx", "effect_idx"), &AudioServer::is_bus_effect_enabled);
  1403. ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
  1404. ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
  1405. ClassDB::bind_method(D_METHOD("set_playback_speed_scale", "scale"), &AudioServer::set_playback_speed_scale);
  1406. ClassDB::bind_method(D_METHOD("get_playback_speed_scale"), &AudioServer::get_playback_speed_scale);
  1407. ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
  1408. ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
  1409. ClassDB::bind_method(D_METHOD("get_speaker_mode"), &AudioServer::get_speaker_mode);
  1410. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioServer::get_mix_rate);
  1411. ClassDB::bind_method(D_METHOD("get_output_device_list"), &AudioServer::get_output_device_list);
  1412. ClassDB::bind_method(D_METHOD("get_output_device"), &AudioServer::get_output_device);
  1413. ClassDB::bind_method(D_METHOD("set_output_device", "name"), &AudioServer::set_output_device);
  1414. ClassDB::bind_method(D_METHOD("get_time_to_next_mix"), &AudioServer::get_time_to_next_mix);
  1415. ClassDB::bind_method(D_METHOD("get_time_since_last_mix"), &AudioServer::get_time_since_last_mix);
  1416. ClassDB::bind_method(D_METHOD("get_output_latency"), &AudioServer::get_output_latency);
  1417. ClassDB::bind_method(D_METHOD("get_input_device_list"), &AudioServer::get_input_device_list);
  1418. ClassDB::bind_method(D_METHOD("get_input_device"), &AudioServer::get_input_device);
  1419. ClassDB::bind_method(D_METHOD("set_input_device", "name"), &AudioServer::set_input_device);
  1420. ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout);
  1421. ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout);
  1422. ClassDB::bind_method(D_METHOD("set_enable_tagging_used_audio_streams", "enable"), &AudioServer::set_enable_tagging_used_audio_streams);
  1423. ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
  1424. ADD_PROPERTY(PropertyInfo(Variant::STRING, "output_device"), "set_output_device", "get_output_device");
  1425. ADD_PROPERTY(PropertyInfo(Variant::STRING, "input_device"), "set_input_device", "get_input_device");
  1426. // The default value may be set to an empty string by the platform-specific audio driver.
  1427. // Override for class reference generation purposes.
  1428. ADD_PROPERTY_DEFAULT("input_device", "Default");
  1429. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed_scale"), "set_playback_speed_scale", "get_playback_speed_scale");
  1430. ADD_SIGNAL(MethodInfo("bus_layout_changed"));
  1431. ADD_SIGNAL(MethodInfo("bus_renamed", PropertyInfo(Variant::INT, "bus_index"), PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
  1432. BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
  1433. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_31);
  1434. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_51);
  1435. BIND_ENUM_CONSTANT(SPEAKER_SURROUND_71);
  1436. }
  1437. AudioServer::AudioServer() {
  1438. singleton = this;
  1439. }
  1440. AudioServer::~AudioServer() {
  1441. singleton = nullptr;
  1442. }
  1443. /////////////////////////////////
  1444. bool AudioBusLayout::_set(const StringName &p_name, const Variant &p_value) {
  1445. String s = p_name;
  1446. if (s.begins_with("bus/")) {
  1447. int index = s.get_slice("/", 1).to_int();
  1448. if (buses.size() <= index) {
  1449. buses.resize(index + 1);
  1450. }
  1451. Bus &bus = buses.write[index];
  1452. String what = s.get_slice("/", 2);
  1453. if (what == "name") {
  1454. bus.name = p_value;
  1455. } else if (what == "solo") {
  1456. bus.solo = p_value;
  1457. } else if (what == "mute") {
  1458. bus.mute = p_value;
  1459. } else if (what == "bypass_fx") {
  1460. bus.bypass = p_value;
  1461. } else if (what == "volume_db") {
  1462. bus.volume_db = p_value;
  1463. } else if (what == "send") {
  1464. bus.send = p_value;
  1465. } else if (what == "effect") {
  1466. int which = s.get_slice("/", 3).to_int();
  1467. if (bus.effects.size() <= which) {
  1468. bus.effects.resize(which + 1);
  1469. }
  1470. Bus::Effect &fx = bus.effects.write[which];
  1471. String fxwhat = s.get_slice("/", 4);
  1472. if (fxwhat == "effect") {
  1473. fx.effect = p_value;
  1474. } else if (fxwhat == "enabled") {
  1475. fx.enabled = p_value;
  1476. } else {
  1477. return false;
  1478. }
  1479. return true;
  1480. } else {
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. return false;
  1486. }
  1487. bool AudioBusLayout::_get(const StringName &p_name, Variant &r_ret) const {
  1488. String s = p_name;
  1489. if (s.begins_with("bus/")) {
  1490. int index = s.get_slice("/", 1).to_int();
  1491. if (index < 0 || index >= buses.size()) {
  1492. return false;
  1493. }
  1494. const Bus &bus = buses[index];
  1495. String what = s.get_slice("/", 2);
  1496. if (what == "name") {
  1497. r_ret = bus.name;
  1498. } else if (what == "solo") {
  1499. r_ret = bus.solo;
  1500. } else if (what == "mute") {
  1501. r_ret = bus.mute;
  1502. } else if (what == "bypass_fx") {
  1503. r_ret = bus.bypass;
  1504. } else if (what == "volume_db") {
  1505. r_ret = bus.volume_db;
  1506. } else if (what == "send") {
  1507. r_ret = bus.send;
  1508. } else if (what == "effect") {
  1509. int which = s.get_slice("/", 3).to_int();
  1510. if (which < 0 || which >= bus.effects.size()) {
  1511. return false;
  1512. }
  1513. const Bus::Effect &fx = bus.effects[which];
  1514. String fxwhat = s.get_slice("/", 4);
  1515. if (fxwhat == "effect") {
  1516. r_ret = fx.effect;
  1517. } else if (fxwhat == "enabled") {
  1518. r_ret = fx.enabled;
  1519. } else {
  1520. return false;
  1521. }
  1522. return true;
  1523. } else {
  1524. return false;
  1525. }
  1526. return true;
  1527. }
  1528. return false;
  1529. }
  1530. void AudioBusLayout::_get_property_list(List<PropertyInfo> *p_list) const {
  1531. for (int i = 0; i < buses.size(); i++) {
  1532. p_list->push_back(PropertyInfo(Variant::STRING, "bus/" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1533. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/solo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1534. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/mute", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1535. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/bypass_fx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1536. p_list->push_back(PropertyInfo(Variant::FLOAT, "bus/" + itos(i) + "/volume_db", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1537. p_list->push_back(PropertyInfo(Variant::FLOAT, "bus/" + itos(i) + "/send", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1538. for (int j = 0; j < buses[i].effects.size(); j++) {
  1539. p_list->push_back(PropertyInfo(Variant::OBJECT, "bus/" + itos(i) + "/effect/" + itos(j) + "/effect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1540. p_list->push_back(PropertyInfo(Variant::BOOL, "bus/" + itos(i) + "/effect/" + itos(j) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  1541. }
  1542. }
  1543. }
  1544. AudioBusLayout::AudioBusLayout() {
  1545. buses.resize(1);
  1546. buses.write[0].name = SceneStringName(Master);
  1547. }