audio_server_javascript.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*************************************************************************/
  2. /* audio_server_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "audio_server_javascript.h"
  31. #include "emscripten.h"
  32. AudioMixer *AudioServerJavascript::get_mixer() {
  33. return NULL;
  34. }
  35. void AudioServerJavascript::audio_mixer_chunk_callback(int p_frames) {
  36. }
  37. RID AudioServerJavascript::sample_create(SampleFormat p_format, bool p_stereo, int p_length) {
  38. Sample *sample = memnew(Sample);
  39. sample->format = p_format;
  40. sample->stereo = p_stereo;
  41. sample->length = p_length;
  42. sample->loop_begin = 0;
  43. sample->loop_end = p_length;
  44. sample->loop_format = SAMPLE_LOOP_NONE;
  45. sample->mix_rate = 44100;
  46. sample->index = -1;
  47. return sample_owner.make_rid(sample);
  48. }
  49. void AudioServerJavascript::sample_set_description(RID p_sample, const String &p_description) {
  50. }
  51. String AudioServerJavascript::sample_get_description(RID p_sample) const {
  52. return String();
  53. }
  54. AudioServerJavascript::SampleFormat AudioServerJavascript::sample_get_format(RID p_sample) const {
  55. return SAMPLE_FORMAT_PCM8;
  56. }
  57. bool AudioServerJavascript::sample_is_stereo(RID p_sample) const {
  58. const Sample *sample = sample_owner.get(p_sample);
  59. ERR_FAIL_COND_V(!sample, false);
  60. return sample->stereo;
  61. }
  62. int AudioServerJavascript::sample_get_length(RID p_sample) const {
  63. const Sample *sample = sample_owner.get(p_sample);
  64. ERR_FAIL_COND_V(!sample, 0);
  65. return sample->length;
  66. }
  67. const void *AudioServerJavascript::sample_get_data_ptr(RID p_sample) const {
  68. return NULL;
  69. }
  70. void AudioServerJavascript::sample_set_data(RID p_sample, const DVector<uint8_t> &p_buffer) {
  71. Sample *sample = sample_owner.get(p_sample);
  72. ERR_FAIL_COND(!sample);
  73. int chans = sample->stereo ? 2 : 1;
  74. Vector<float> buffer;
  75. buffer.resize(sample->length * chans);
  76. DVector<uint8_t>::Read r = p_buffer.read();
  77. if (sample->format == SAMPLE_FORMAT_PCM8) {
  78. const int8_t *ptr = (const int8_t *)r.ptr();
  79. for (int i = 0; i < sample->length * chans; i++) {
  80. buffer[i] = ptr[i] / 128.0;
  81. }
  82. } else if (sample->format == SAMPLE_FORMAT_PCM16) {
  83. const int16_t *ptr = (const int16_t *)r.ptr();
  84. for (int i = 0; i < sample->length * chans; i++) {
  85. buffer[i] = ptr[i] / 32768.0;
  86. }
  87. } else {
  88. ERR_EXPLAIN("Unsupported for now");
  89. ERR_FAIL();
  90. }
  91. sample->tmp_data = buffer;
  92. }
  93. DVector<uint8_t> AudioServerJavascript::sample_get_data(RID p_sample) const {
  94. return DVector<uint8_t>();
  95. }
  96. void AudioServerJavascript::sample_set_mix_rate(RID p_sample, int p_rate) {
  97. Sample *sample = sample_owner.get(p_sample);
  98. ERR_FAIL_COND(!sample);
  99. sample->mix_rate = p_rate;
  100. }
  101. int AudioServerJavascript::sample_get_mix_rate(RID p_sample) const {
  102. const Sample *sample = sample_owner.get(p_sample);
  103. ERR_FAIL_COND_V(!sample, 0);
  104. return sample->mix_rate;
  105. }
  106. void AudioServerJavascript::sample_set_loop_format(RID p_sample, SampleLoopFormat p_format) {
  107. Sample *sample = sample_owner.get(p_sample);
  108. ERR_FAIL_COND(!sample);
  109. sample->loop_format = p_format;
  110. }
  111. AudioServerJavascript::SampleLoopFormat AudioServerJavascript::sample_get_loop_format(RID p_sample) const {
  112. const Sample *sample = sample_owner.get(p_sample);
  113. ERR_FAIL_COND_V(!sample, SAMPLE_LOOP_NONE);
  114. return sample->loop_format;
  115. }
  116. void AudioServerJavascript::sample_set_loop_begin(RID p_sample, int p_pos) {
  117. Sample *sample = sample_owner.get(p_sample);
  118. ERR_FAIL_COND(!sample);
  119. sample->loop_begin = p_pos;
  120. }
  121. int AudioServerJavascript::sample_get_loop_begin(RID p_sample) const {
  122. const Sample *sample = sample_owner.get(p_sample);
  123. ERR_FAIL_COND_V(!sample, 0);
  124. return sample->loop_begin;
  125. }
  126. void AudioServerJavascript::sample_set_loop_end(RID p_sample, int p_pos) {
  127. Sample *sample = sample_owner.get(p_sample);
  128. ERR_FAIL_COND(!sample);
  129. sample->loop_end = p_pos;
  130. }
  131. int AudioServerJavascript::sample_get_loop_end(RID p_sample) const {
  132. const Sample *sample = sample_owner.get(p_sample);
  133. ERR_FAIL_COND_V(!sample, 0);
  134. return sample->loop_end;
  135. }
  136. /* VOICE API */
  137. RID AudioServerJavascript::voice_create() {
  138. Voice *voice = memnew(Voice);
  139. voice->index = voice_base;
  140. voice->volume = 1.0;
  141. voice->pan = 0.0;
  142. voice->pan_depth = .0;
  143. voice->pan_height = 0.0;
  144. voice->chorus = 0;
  145. voice->reverb_type = REVERB_SMALL;
  146. voice->reverb = 0;
  147. voice->mix_rate = -1;
  148. voice->positional = false;
  149. voice->active = false;
  150. /* clang-format off */
  151. EM_ASM_( {
  152. _as_voices[$0] = null;
  153. _as_voice_gain[$0] = _as_audioctx.createGain();
  154. _as_voice_pan[$0] = _as_audioctx.createStereoPanner();
  155. _as_voice_gain[$0].connect(_as_voice_pan[$0]);
  156. _as_voice_pan[$0].connect(_as_audioctx.destination);
  157. }, voice_base);
  158. /* clang-format on */
  159. voice_base++;
  160. return voice_owner.make_rid(voice);
  161. }
  162. void AudioServerJavascript::voice_play(RID p_voice, RID p_sample) {
  163. Voice *voice = voice_owner.get(p_voice);
  164. ERR_FAIL_COND(!voice);
  165. Sample *sample = sample_owner.get(p_sample);
  166. ERR_FAIL_COND(!sample);
  167. // due to how webaudio works, sample cration is deferred until used
  168. // sorry! WebAudio absolutely sucks
  169. if (sample->index == -1) {
  170. //create sample if not created
  171. ERR_FAIL_COND(sample->tmp_data.size() == 0);
  172. sample->index = sample_base;
  173. /* clang-format off */
  174. EM_ASM_({
  175. _as_samples[$0] = _as_audioctx.createBuffer($1, $2, $3);
  176. }, sample_base, sample->stereo ? 2 : 1, sample->length, sample->mix_rate);
  177. /* clang-format on */
  178. sample_base++;
  179. int chans = sample->stereo ? 2 : 1;
  180. for (int i = 0; i < chans; i++) {
  181. /* clang-format off */
  182. EM_ASM_({
  183. _as_edited_buffer = _as_samples[$0].getChannelData($1);
  184. }, sample->index, i);
  185. /* clang-format on */
  186. for (int j = 0; j < sample->length; j++) {
  187. /* clang-format off */
  188. EM_ASM_({
  189. _as_edited_buffer[$0] = $1;
  190. }, j, sample->tmp_data[j * chans + i]);
  191. /* clang-format on */
  192. }
  193. }
  194. sample->tmp_data.clear();
  195. }
  196. voice->sample_mix_rate = sample->mix_rate;
  197. if (voice->mix_rate == -1) {
  198. voice->mix_rate = voice->sample_mix_rate;
  199. }
  200. float freq_diff = Math::log(float(voice->mix_rate) / float(voice->sample_mix_rate)) / Math::log(2.0);
  201. int detune = int(freq_diff * 1200.0);
  202. /* clang-format off */
  203. EM_ASM_({
  204. if (_as_voices[$0] !== null) {
  205. _as_voices[$0].stop(); //stop and byebye
  206. }
  207. _as_voices[$0] = _as_audioctx.createBufferSource();
  208. _as_voices[$0].connect(_as_voice_gain[$0]);
  209. _as_voices[$0].buffer = _as_samples[$1];
  210. _as_voices[$0].loopStart.value = $1;
  211. _as_voices[$0].loopEnd.value = $2;
  212. _as_voices[$0].loop.value = $3;
  213. _as_voices[$0].detune.value = $6;
  214. _as_voice_pan[$0].pan.value = $4;
  215. _as_voice_gain[$0].gain.value = $5;
  216. _as_voices[$0].start();
  217. _as_voices[$0].onended = function() {
  218. _as_voices[$0].disconnect(_as_voice_gain[$0]);
  219. _as_voices[$0] = null;
  220. }
  221. }, voice->index, sample->index, sample->mix_rate * sample->loop_begin, sample->mix_rate * sample->loop_end, sample->loop_format != SAMPLE_LOOP_NONE, voice->pan, voice->volume * fx_volume_scale, detune);
  222. /* clang-format on */
  223. voice->active = true;
  224. }
  225. void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume) {
  226. Voice *voice = voice_owner.get(p_voice);
  227. ERR_FAIL_COND(!voice);
  228. voice->volume = p_volume;
  229. if (voice->active) {
  230. /* clang-format off */
  231. EM_ASM_({
  232. _as_voice_gain[$0].gain.value = $1;
  233. }, voice->index, voice->volume * fx_volume_scale);
  234. /* clang-format on */
  235. }
  236. }
  237. void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_depth, float height) {
  238. Voice *voice = voice_owner.get(p_voice);
  239. ERR_FAIL_COND(!voice);
  240. voice->pan = p_pan;
  241. voice->pan_depth = p_depth;
  242. voice->pan_height = height;
  243. if (voice->active) {
  244. /* clang-format off */
  245. EM_ASM_({
  246. _as_voice_pan[$0].pan.value = $1;
  247. }, voice->index, voice->pan);
  248. /* clang-format on */
  249. }
  250. }
  251. void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain) {
  252. }
  253. void AudioServerJavascript::voice_set_chorus(RID p_voice, float p_chorus) {
  254. }
  255. void AudioServerJavascript::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb) {
  256. }
  257. void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate) {
  258. Voice *voice = voice_owner.get(p_voice);
  259. ERR_FAIL_COND(!voice);
  260. voice->mix_rate = p_mix_rate;
  261. if (voice->active) {
  262. float freq_diff = Math::log(float(voice->mix_rate) / float(voice->sample_mix_rate)) / Math::log(2.0);
  263. int detune = int(freq_diff * 1200.0);
  264. /* clang-format off */
  265. EM_ASM_({
  266. _as_voices[$0].detune.value = $1;
  267. }, voice->index, detune);
  268. /* clang-format on */
  269. }
  270. }
  271. void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional) {
  272. }
  273. float AudioServerJavascript::voice_get_volume(RID p_voice) const {
  274. Voice *voice = voice_owner.get(p_voice);
  275. ERR_FAIL_COND_V(!voice, 0);
  276. return voice->volume;
  277. }
  278. float AudioServerJavascript::voice_get_pan(RID p_voice) const {
  279. Voice *voice = voice_owner.get(p_voice);
  280. ERR_FAIL_COND_V(!voice, 0);
  281. return voice->pan;
  282. }
  283. float AudioServerJavascript::voice_get_pan_depth(RID p_voice) const {
  284. Voice *voice = voice_owner.get(p_voice);
  285. ERR_FAIL_COND_V(!voice, 0);
  286. return voice->pan_depth;
  287. }
  288. float AudioServerJavascript::voice_get_pan_height(RID p_voice) const {
  289. Voice *voice = voice_owner.get(p_voice);
  290. ERR_FAIL_COND_V(!voice, 0);
  291. return voice->pan_height;
  292. }
  293. AudioServerJavascript::FilterType AudioServerJavascript::voice_get_filter_type(RID p_voice) const {
  294. return FILTER_NONE;
  295. }
  296. float AudioServerJavascript::voice_get_filter_cutoff(RID p_voice) const {
  297. return 0;
  298. }
  299. float AudioServerJavascript::voice_get_filter_resonance(RID p_voice) const {
  300. return 0;
  301. }
  302. float AudioServerJavascript::voice_get_chorus(RID p_voice) const {
  303. return 0;
  304. }
  305. AudioServerJavascript::ReverbRoomType AudioServerJavascript::voice_get_reverb_type(RID p_voice) const {
  306. return REVERB_SMALL;
  307. }
  308. float AudioServerJavascript::voice_get_reverb(RID p_voice) const {
  309. return 0;
  310. }
  311. int AudioServerJavascript::voice_get_mix_rate(RID p_voice) const {
  312. return 44100;
  313. }
  314. bool AudioServerJavascript::voice_is_positional(RID p_voice) const {
  315. return false;
  316. }
  317. void AudioServerJavascript::voice_stop(RID p_voice) {
  318. Voice *voice = voice_owner.get(p_voice);
  319. ERR_FAIL_COND(!voice);
  320. if (voice->active) {
  321. /* clang-format off */
  322. EM_ASM_({
  323. if (_as_voices[$0] !== null) {
  324. _as_voices[$0].stop();
  325. _as_voices[$0].disconnect(_as_voice_gain[$0]);
  326. _as_voices[$0] = null;
  327. }
  328. }, voice->index);
  329. /* clang-format on */
  330. voice->active = false;
  331. }
  332. }
  333. bool AudioServerJavascript::voice_is_active(RID p_voice) const {
  334. Voice *voice = voice_owner.get(p_voice);
  335. ERR_FAIL_COND_V(!voice, false);
  336. return voice->active;
  337. }
  338. /* STREAM API */
  339. RID AudioServerJavascript::audio_stream_create(AudioStream *p_stream) {
  340. Stream *s = memnew(Stream);
  341. s->audio_stream = p_stream;
  342. s->event_stream = NULL;
  343. s->active = false;
  344. s->E = NULL;
  345. s->volume_scale = 1.0;
  346. p_stream->set_mix_rate(webaudio_mix_rate);
  347. return stream_owner.make_rid(s);
  348. }
  349. RID AudioServerJavascript::event_stream_create(EventStream *p_stream) {
  350. Stream *s = memnew(Stream);
  351. s->audio_stream = NULL;
  352. s->event_stream = p_stream;
  353. s->active = false;
  354. s->E = NULL;
  355. s->volume_scale = 1.0;
  356. //p_stream->set_mix_rate(AudioDriverJavascript::get_singleton()->get_mix_rate());
  357. return stream_owner.make_rid(s);
  358. }
  359. void AudioServerJavascript::stream_set_active(RID p_stream, bool p_active) {
  360. Stream *s = stream_owner.get(p_stream);
  361. ERR_FAIL_COND(!s);
  362. if (s->active == p_active)
  363. return;
  364. s->active = p_active;
  365. if (p_active)
  366. s->E = active_audio_streams.push_back(s);
  367. else {
  368. active_audio_streams.erase(s->E);
  369. s->E = NULL;
  370. }
  371. }
  372. bool AudioServerJavascript::stream_is_active(RID p_stream) const {
  373. Stream *s = stream_owner.get(p_stream);
  374. ERR_FAIL_COND_V(!s, false);
  375. return s->active;
  376. }
  377. void AudioServerJavascript::stream_set_volume_scale(RID p_stream, float p_scale) {
  378. Stream *s = stream_owner.get(p_stream);
  379. ERR_FAIL_COND(!s);
  380. s->volume_scale = p_scale;
  381. }
  382. float AudioServerJavascript::stream_set_volume_scale(RID p_stream) const {
  383. Stream *s = stream_owner.get(p_stream);
  384. ERR_FAIL_COND_V(!s, 0);
  385. return s->volume_scale;
  386. }
  387. /* Audio Physics API */
  388. void AudioServerJavascript::free(RID p_id) {
  389. if (voice_owner.owns(p_id)) {
  390. Voice *voice = voice_owner.get(p_id);
  391. ERR_FAIL_COND(!voice);
  392. if (voice->active) {
  393. /* clang-format off */
  394. EM_ASM_({
  395. if (_as_voices[$0] !== null) {
  396. _as_voices[$0].stop();
  397. _as_voices[$0].disconnect(_as_voice_gain[$0]);
  398. }
  399. }, voice->index);
  400. /* clang-format on */
  401. }
  402. /* clang-format off */
  403. EM_ASM_({
  404. delete _as_voices[$0];
  405. _as_voice_gain[$0].disconnect(_as_voice_pan[$0]);
  406. delete _as_voice_gain[$0];
  407. _as_voice_pan[$0].disconnect(_as_audioctx.destination);
  408. delete _as_voice_pan[$0];
  409. }, voice->index);
  410. /* clang-format on */
  411. voice_owner.free(p_id);
  412. memdelete(voice);
  413. } else if (sample_owner.owns(p_id)) {
  414. Sample *sample = sample_owner.get(p_id);
  415. ERR_FAIL_COND(!sample);
  416. /* clang-format off */
  417. EM_ASM_({
  418. delete _as_samples[$0];
  419. }, sample->index);
  420. /* clang-format on */
  421. sample_owner.free(p_id);
  422. memdelete(sample);
  423. } else if (stream_owner.owns(p_id)) {
  424. Stream *s = stream_owner.get(p_id);
  425. if (s->active) {
  426. stream_set_active(p_id, false);
  427. }
  428. memdelete(s);
  429. stream_owner.free(p_id);
  430. }
  431. }
  432. extern "C" {
  433. void audio_server_mix_function(int p_frames) {
  434. //print_line("MIXI! "+itos(p_frames));
  435. static_cast<AudioServerJavascript *>(AudioServerJavascript::get_singleton())->mix_to_js(p_frames);
  436. }
  437. }
  438. void AudioServerJavascript::mix_to_js(int p_frames) {
  439. //process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
  440. int todo = p_frames;
  441. int offset = 0;
  442. while (todo) {
  443. int tomix = MIN(todo, INTERNAL_BUFFER_SIZE);
  444. driver_process_chunk(tomix);
  445. /* clang-format off */
  446. EM_ASM_({
  447. var data = HEAPF32.subarray($0 / 4, $0 / 4 + $2 * 2);
  448. for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
  449. var outputData = _as_output_buffer.getChannelData(channel);
  450. // Loop through samples
  451. for (var sample = 0; sample < $2; sample++) {
  452. // make output equal to the same as the input
  453. outputData[sample + $1] = data[sample * 2 + channel];
  454. }
  455. }
  456. }, internal_buffer, offset, tomix);
  457. /* clang-format on */
  458. todo -= tomix;
  459. offset += tomix;
  460. }
  461. }
  462. void AudioServerJavascript::init() {
  463. /*
  464. // clang-format off
  465. EM_ASM(
  466. console.log('server is ' + audio_server);
  467. );
  468. // clang-format on
  469. */
  470. //int latency = GLOBAL_DEF("javascript/audio_latency",16384);
  471. internal_buffer_channels = 2;
  472. internal_buffer = memnew_arr(float, INTERNAL_BUFFER_SIZE *internal_buffer_channels);
  473. stream_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * 4); //max 4 channels
  474. stream_volume = 0.3;
  475. int buffer_latency = 16384;
  476. /* clang-format off */
  477. EM_ASM_( {
  478. _as_script_node = _as_audioctx.createScriptProcessor($0, 0, 2);
  479. _as_script_node.connect(_as_audioctx.destination);
  480. console.log(_as_script_node.bufferSize);
  481. _as_script_node.onaudioprocess = function(audioProcessingEvent) {
  482. // The output buffer contains the samples that will be modified and played
  483. _as_output_buffer = audioProcessingEvent.outputBuffer;
  484. audio_server_mix_function(_as_output_buffer.getChannelData(0).length);
  485. }
  486. }, buffer_latency);
  487. /* clang-format on */
  488. }
  489. void AudioServerJavascript::finish() {
  490. }
  491. void AudioServerJavascript::update() {
  492. for (List<Stream *>::Element *E = active_audio_streams.front(); E;) { //stream might be removed durnig this callback
  493. List<Stream *>::Element *N = E->next();
  494. if (E->get()->audio_stream)
  495. E->get()->audio_stream->update();
  496. E = N;
  497. }
  498. }
  499. /* MISC config */
  500. void AudioServerJavascript::lock() {
  501. }
  502. void AudioServerJavascript::unlock() {
  503. }
  504. int AudioServerJavascript::get_default_channel_count() const {
  505. return 1;
  506. }
  507. int AudioServerJavascript::get_default_mix_rate() const {
  508. return 44100;
  509. }
  510. void AudioServerJavascript::set_stream_global_volume_scale(float p_volume) {
  511. stream_volume_scale = p_volume;
  512. }
  513. void AudioServerJavascript::set_fx_global_volume_scale(float p_volume) {
  514. fx_volume_scale = p_volume;
  515. }
  516. void AudioServerJavascript::set_event_voice_global_volume_scale(float p_volume) {
  517. }
  518. float AudioServerJavascript::get_stream_global_volume_scale() const {
  519. return 1;
  520. }
  521. float AudioServerJavascript::get_fx_global_volume_scale() const {
  522. return 1;
  523. }
  524. float AudioServerJavascript::get_event_voice_global_volume_scale() const {
  525. return 1;
  526. }
  527. uint32_t AudioServerJavascript::read_output_peak() const {
  528. return 0;
  529. }
  530. AudioServerJavascript *AudioServerJavascript::singleton = NULL;
  531. AudioServer *AudioServerJavascript::get_singleton() {
  532. return singleton;
  533. }
  534. double AudioServerJavascript::get_mix_time() const {
  535. return 0;
  536. }
  537. double AudioServerJavascript::get_output_delay() const {
  538. return 0;
  539. }
  540. void AudioServerJavascript::driver_process_chunk(int p_frames) {
  541. int samples = p_frames * internal_buffer_channels;
  542. for (int i = 0; i < samples; i++) {
  543. internal_buffer[i] = 0;
  544. }
  545. for (List<Stream *>::Element *E = active_audio_streams.front(); E; E = E->next()) {
  546. ERR_CONTINUE(!E->get()->active); // bug?
  547. AudioStream *as = E->get()->audio_stream;
  548. if (!as)
  549. continue;
  550. int channels = as->get_channel_count();
  551. if (channels == 0)
  552. continue; // does not want mix
  553. if (!as->mix(stream_buffer, p_frames))
  554. continue; //nothing was mixed!!
  555. int32_t stream_vol_scale = (stream_volume * stream_volume_scale * E->get()->volume_scale) * (1 << STREAM_SCALE_BITS);
  556. #define STRSCALE(m_val) ((((m_val >> STREAM_SCALE_BITS) * stream_vol_scale) >> 8) / 8388608.0)
  557. switch (channels) {
  558. case 1: {
  559. for (int i = 0; i < p_frames; i++) {
  560. internal_buffer[(i << 1) + 0] += STRSCALE(stream_buffer[i]);
  561. internal_buffer[(i << 1) + 1] += STRSCALE(stream_buffer[i]);
  562. }
  563. } break;
  564. case 2: {
  565. for (int i = 0; i < p_frames * 2; i++) {
  566. internal_buffer[i] += STRSCALE(stream_buffer[i]);
  567. }
  568. } break;
  569. case 4: {
  570. for (int i = 0; i < p_frames; i++) {
  571. internal_buffer[(i << 2) + 0] += STRSCALE((stream_buffer[(i << 2) + 0] + stream_buffer[(i << 2) + 2]) >> 1);
  572. internal_buffer[(i << 2) + 1] += STRSCALE((stream_buffer[(i << 2) + 1] + stream_buffer[(i << 2) + 3]) >> 1);
  573. }
  574. } break;
  575. }
  576. #undef STRSCALE
  577. }
  578. }
  579. /*void AudioServerSW::driver_process(int p_frames,int32_t *p_buffer) {
  580. _output_delay=p_frames/double(AudioDriverSW::get_singleton()->get_mix_rate());
  581. //process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
  582. int todo=p_frames;
  583. while(todo) {
  584. int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
  585. driver_process_chunk(tomix,p_buffer);
  586. p_buffer+=tomix;
  587. todo-=tomix;
  588. }
  589. }*/
  590. AudioServerJavascript::AudioServerJavascript() {
  591. singleton = this;
  592. sample_base = 1;
  593. voice_base = 1;
  594. /* clang-format off */
  595. EM_ASM(
  596. _as_samples = {};
  597. _as_voices = {};
  598. _as_voice_pan = {};
  599. _as_voice_gain = {};
  600. _as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
  601. audio_server_mix_function = cwrap('audio_server_mix_function', null, ['number']);
  602. );
  603. /* clang-format on */
  604. /* clang-format off */
  605. webaudio_mix_rate = EM_ASM_INT_V(
  606. return _as_audioctx.sampleRate;
  607. );
  608. /* clang-format on */
  609. print_line("WEBAUDIO MIX RATE: " + itos(webaudio_mix_rate));
  610. event_voice_scale = 1.0;
  611. fx_volume_scale = 1.0;
  612. stream_volume_scale = 1.0;
  613. }