audio_server.cpp 58 KB

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