audio_server.cpp 58 KB

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