audio_server_javascript.cpp 21 KB

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