audio_server_javascript.cpp 20 KB

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