audio_server_javascript.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. EM_ASM_( {
  150. _as_voices[$0]=null;
  151. _as_voice_gain[$0]=_as_audioctx.createGain();
  152. _as_voice_pan[$0]=_as_audioctx.createStereoPanner();
  153. _as_voice_gain[$0].connect(_as_voice_pan[$0]);
  154. _as_voice_pan[$0].connect(_as_audioctx.destination);
  155. },voice_base);
  156. voice_base++;
  157. return voice_owner.make_rid( voice );
  158. }
  159. void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
  160. Voice* voice=voice_owner.get(p_voice);
  161. ERR_FAIL_COND(!voice);
  162. Sample *sample=sample_owner.get(p_sample);
  163. ERR_FAIL_COND(!sample);
  164. // due to how webaudio works, sample cration is deferred until used
  165. // sorry! WebAudio absolutely sucks
  166. if (sample->index==-1) {
  167. //create sample if not created
  168. ERR_FAIL_COND(sample->tmp_data.size()==0);
  169. sample->index=sample_base;
  170. EM_ASM_( {
  171. _as_samples[$0]=_as_audioctx.createBuffer($1,$2,$3);
  172. },sample_base,sample->stereo?2:1,sample->length,sample->mix_rate);
  173. sample_base++;
  174. int chans = sample->stereo?2:1;
  175. for(int i=0;i<chans;i++) {
  176. EM_ASM_({
  177. _as_edited_buffer=_as_samples[$0].getChannelData($1);
  178. },sample->index,i);
  179. for(int j=0;j<sample->length;j++) {
  180. EM_ASM_({
  181. _as_edited_buffer[$0]=$1;
  182. },j,sample->tmp_data[j*chans+i]);
  183. }
  184. }
  185. sample->tmp_data.clear();
  186. }
  187. voice->sample_mix_rate=sample->mix_rate;
  188. if (voice->mix_rate==-1) {
  189. voice->mix_rate=voice->sample_mix_rate;
  190. }
  191. float freq_diff = Math::log(float(voice->mix_rate)/float(voice->sample_mix_rate))/Math::log(2.0);
  192. int detune = int(freq_diff*1200.0);
  193. EM_ASM_( {
  194. if (_as_voices[$0]!==null) {
  195. _as_voices[$0].stop(); //stop and byebye
  196. }
  197. _as_voices[$0]=_as_audioctx.createBufferSource();
  198. _as_voices[$0].connect(_as_voice_gain[$0]);
  199. _as_voices[$0].buffer=_as_samples[$1];
  200. _as_voices[$0].loopStart.value=$1;
  201. _as_voices[$0].loopEnd.value=$2;
  202. _as_voices[$0].loop.value=$3;
  203. _as_voices[$0].detune.value=$6;
  204. _as_voice_pan[$0].pan.value=$4;
  205. _as_voice_gain[$0].gain.value=$5;
  206. _as_voices[$0].start();
  207. _as_voices[$0].onended=function() {
  208. _as_voices[$0].disconnect(_as_voice_gain[$0]);
  209. _as_voices[$0]=null;
  210. }
  211. },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);
  212. voice->active=true;
  213. }
  214. void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume){
  215. Voice* voice=voice_owner.get(p_voice);
  216. ERR_FAIL_COND(!voice);
  217. voice->volume=p_volume;
  218. if (voice->active) {
  219. EM_ASM_( {
  220. _as_voice_gain[$0].gain.value=$1;
  221. },voice->index,voice->volume*fx_volume_scale);
  222. }
  223. }
  224. void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_depth,float height){
  225. Voice* voice=voice_owner.get(p_voice);
  226. ERR_FAIL_COND(!voice);
  227. voice->pan=p_pan;
  228. voice->pan_depth=p_depth;
  229. voice->pan_height=height;
  230. if (voice->active) {
  231. EM_ASM_( {
  232. _as_voice_pan[$0].pan.value=$1;
  233. },voice->index,voice->pan);
  234. }
  235. }
  236. void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain){
  237. }
  238. void AudioServerJavascript::voice_set_chorus(RID p_voice, float p_chorus ){
  239. }
  240. void AudioServerJavascript::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb){
  241. }
  242. void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate){
  243. Voice* voice=voice_owner.get(p_voice);
  244. ERR_FAIL_COND(!voice);
  245. voice->mix_rate=p_mix_rate;
  246. if (voice->active) {
  247. float freq_diff = Math::log(float(voice->mix_rate)/float(voice->sample_mix_rate))/Math::log(2.0);
  248. int detune = int(freq_diff*1200.0);
  249. EM_ASM_( {
  250. _as_voices[$0].detune.value=$1;
  251. },voice->index,detune);
  252. }
  253. }
  254. void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional){
  255. }
  256. float AudioServerJavascript::voice_get_volume(RID p_voice) const{
  257. Voice* voice=voice_owner.get(p_voice);
  258. ERR_FAIL_COND_V(!voice,0);
  259. return voice->volume;
  260. }
  261. float AudioServerJavascript::voice_get_pan(RID p_voice) const{
  262. Voice* voice=voice_owner.get(p_voice);
  263. ERR_FAIL_COND_V(!voice,0);
  264. return voice->pan;
  265. }
  266. float AudioServerJavascript::voice_get_pan_depth(RID p_voice) const{
  267. Voice* voice=voice_owner.get(p_voice);
  268. ERR_FAIL_COND_V(!voice,0);
  269. return voice->pan_depth;
  270. }
  271. float AudioServerJavascript::voice_get_pan_height(RID p_voice) const{
  272. Voice* voice=voice_owner.get(p_voice);
  273. ERR_FAIL_COND_V(!voice,0);
  274. return voice->pan_height;
  275. }
  276. AudioServerJavascript::FilterType AudioServerJavascript::voice_get_filter_type(RID p_voice) const{
  277. return FILTER_NONE;
  278. }
  279. float AudioServerJavascript::voice_get_filter_cutoff(RID p_voice) const{
  280. return 0;
  281. }
  282. float AudioServerJavascript::voice_get_filter_resonance(RID p_voice) const{
  283. return 0;
  284. }
  285. float AudioServerJavascript::voice_get_chorus(RID p_voice) const{
  286. return 0;
  287. }
  288. AudioServerJavascript::ReverbRoomType AudioServerJavascript::voice_get_reverb_type(RID p_voice) const{
  289. return REVERB_SMALL;
  290. }
  291. float AudioServerJavascript::voice_get_reverb(RID p_voice) const{
  292. return 0;
  293. }
  294. int AudioServerJavascript::voice_get_mix_rate(RID p_voice) const{
  295. return 44100;
  296. }
  297. bool AudioServerJavascript::voice_is_positional(RID p_voice) const{
  298. return false;
  299. }
  300. void AudioServerJavascript::voice_stop(RID p_voice){
  301. Voice* voice=voice_owner.get(p_voice);
  302. ERR_FAIL_COND(!voice);
  303. if (voice->active) {
  304. EM_ASM_( {
  305. if (_as_voices[$0]!==null) {
  306. _as_voices[$0].stop();
  307. _as_voices[$0].disconnect(_as_voice_gain[$0]);
  308. _as_voices[$0]=null;
  309. }
  310. },voice->index);
  311. voice->active=false;
  312. }
  313. }
  314. bool AudioServerJavascript::voice_is_active(RID p_voice) const{
  315. Voice* voice=voice_owner.get(p_voice);
  316. ERR_FAIL_COND_V(!voice,false);
  317. return voice->active;
  318. }
  319. /* STREAM API */
  320. RID AudioServerJavascript::audio_stream_create(AudioStream *p_stream) {
  321. Stream *s = memnew(Stream);
  322. s->audio_stream=p_stream;
  323. s->event_stream=NULL;
  324. s->active=false;
  325. s->E=NULL;
  326. s->volume_scale=1.0;
  327. p_stream->set_mix_rate(webaudio_mix_rate);
  328. return stream_owner.make_rid(s);
  329. }
  330. RID AudioServerJavascript::event_stream_create(EventStream *p_stream) {
  331. Stream *s = memnew(Stream);
  332. s->audio_stream=NULL;
  333. s->event_stream=p_stream;
  334. s->active=false;
  335. s->E=NULL;
  336. s->volume_scale=1.0;
  337. //p_stream->set_mix_rate(AudioDriverJavascript::get_singleton()->get_mix_rate());
  338. return stream_owner.make_rid(s);
  339. }
  340. void AudioServerJavascript::stream_set_active(RID p_stream, bool p_active) {
  341. Stream *s = stream_owner.get(p_stream);
  342. ERR_FAIL_COND(!s);
  343. if (s->active==p_active)
  344. return;
  345. s->active=p_active;
  346. if (p_active)
  347. s->E=active_audio_streams.push_back(s);
  348. else {
  349. active_audio_streams.erase(s->E);
  350. s->E=NULL;
  351. }
  352. }
  353. bool AudioServerJavascript::stream_is_active(RID p_stream) const {
  354. Stream *s = stream_owner.get(p_stream);
  355. ERR_FAIL_COND_V(!s,false);
  356. return s->active;
  357. }
  358. void AudioServerJavascript::stream_set_volume_scale(RID p_stream, float p_scale) {
  359. Stream *s = stream_owner.get(p_stream);
  360. ERR_FAIL_COND(!s);
  361. s->volume_scale=p_scale;
  362. }
  363. float AudioServerJavascript::stream_set_volume_scale(RID p_stream) const {
  364. Stream *s = stream_owner.get(p_stream);
  365. ERR_FAIL_COND_V(!s,0);
  366. return s->volume_scale;
  367. }
  368. /* Audio Physics API */
  369. void AudioServerJavascript::free(RID p_id){
  370. if (voice_owner.owns(p_id)) {
  371. Voice* voice=voice_owner.get(p_id);
  372. ERR_FAIL_COND(!voice);
  373. if (voice->active) {
  374. EM_ASM_( {
  375. if (_as_voices[$0]!==null) {
  376. _as_voices[$0].stop();
  377. _as_voices[$0].disconnect(_as_voice_gain[$0]);
  378. }
  379. },voice->index);
  380. }
  381. EM_ASM_( {
  382. delete _as_voices[$0];
  383. _as_voice_gain[$0].disconnect(_as_voice_pan[$0]);
  384. delete _as_voice_gain[$0];
  385. _as_voice_pan[$0].disconnect(_as_audioctx.destination);
  386. delete _as_voice_pan[$0];
  387. },voice->index);
  388. voice_owner.free(p_id);
  389. memdelete(voice);
  390. } else if (sample_owner.owns(p_id)) {
  391. Sample *sample = sample_owner.get(p_id);
  392. ERR_FAIL_COND(!sample);
  393. EM_ASM_( {
  394. delete _as_samples[$0];
  395. },sample->index);
  396. sample_owner.free(p_id);
  397. memdelete(sample);
  398. } else if (stream_owner.owns(p_id)) {
  399. Stream *s=stream_owner.get(p_id);
  400. if (s->active) {
  401. stream_set_active(p_id,false);
  402. }
  403. memdelete(s);
  404. stream_owner.free(p_id);
  405. }
  406. }
  407. extern "C" {
  408. void audio_server_mix_function(int p_frames) {
  409. //print_line("MIXI! "+itos(p_frames));
  410. static_cast<AudioServerJavascript*>(AudioServerJavascript::get_singleton())->mix_to_js(p_frames);
  411. }
  412. }
  413. void AudioServerJavascript::mix_to_js(int p_frames) {
  414. //process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
  415. int todo=p_frames;
  416. int offset=0;
  417. while(todo) {
  418. int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
  419. driver_process_chunk(tomix);
  420. EM_ASM_({
  421. var data = HEAPF32.subarray($0/4, $0/4 + $2*2);
  422. for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
  423. var outputData = _as_output_buffer.getChannelData(channel);
  424. // Loop through samples
  425. for (var sample = 0; sample < $2; sample++) {
  426. // make output equal to the same as the input
  427. outputData[sample+$1] = data[sample*2+channel];
  428. }
  429. }
  430. },internal_buffer,offset,tomix);
  431. todo-=tomix;
  432. offset+=tomix;
  433. }
  434. }
  435. void AudioServerJavascript::init(){
  436. //EM_ASM(
  437. // console.log('server is '+audio_server);
  438. // );
  439. //int latency = GLOBAL_DEF("javascript/audio_latency",16384);
  440. internal_buffer_channels=2;
  441. internal_buffer = memnew_arr(float,INTERNAL_BUFFER_SIZE*internal_buffer_channels);
  442. stream_buffer = memnew_arr(int32_t,INTERNAL_BUFFER_SIZE*4); //max 4 channels
  443. stream_volume=0.3;
  444. int buffer_latency=16384;
  445. EM_ASM_( {
  446. _as_script_node = _as_audioctx.createScriptProcessor($0, 0, 2);
  447. _as_script_node.connect(_as_audioctx.destination);
  448. console.log(_as_script_node.bufferSize);
  449. _as_script_node.onaudioprocess = function(audioProcessingEvent) {
  450. // The output buffer contains the samples that will be modified and played
  451. _as_output_buffer = audioProcessingEvent.outputBuffer;
  452. audio_server_mix_function(_as_output_buffer.getChannelData(0).length);
  453. }
  454. },buffer_latency);
  455. }
  456. void AudioServerJavascript::finish(){
  457. }
  458. void AudioServerJavascript::update(){
  459. for(List<Stream*>::Element *E=active_audio_streams.front();E;) { //stream might be removed durnig this callback
  460. List<Stream*>::Element *N=E->next();
  461. if (E->get()->audio_stream)
  462. E->get()->audio_stream->update();
  463. E=N;
  464. }
  465. }
  466. /* MISC config */
  467. void AudioServerJavascript::lock(){
  468. }
  469. void AudioServerJavascript::unlock(){
  470. }
  471. int AudioServerJavascript::get_default_channel_count() const{
  472. return 1;
  473. }
  474. int AudioServerJavascript::get_default_mix_rate() const{
  475. return 44100;
  476. }
  477. void AudioServerJavascript::set_stream_global_volume_scale(float p_volume){
  478. stream_volume_scale=p_volume;
  479. }
  480. void AudioServerJavascript::set_fx_global_volume_scale(float p_volume){
  481. fx_volume_scale=p_volume;
  482. }
  483. void AudioServerJavascript::set_event_voice_global_volume_scale(float p_volume){
  484. }
  485. float AudioServerJavascript::get_stream_global_volume_scale() const{
  486. return 1;
  487. }
  488. float AudioServerJavascript::get_fx_global_volume_scale() const{
  489. return 1;
  490. }
  491. float AudioServerJavascript::get_event_voice_global_volume_scale() const{
  492. return 1;
  493. }
  494. uint32_t AudioServerJavascript::read_output_peak() const{
  495. return 0;
  496. }
  497. AudioServerJavascript *AudioServerJavascript::singleton=NULL;
  498. AudioServer *AudioServerJavascript::get_singleton() {
  499. return singleton;
  500. }
  501. double AudioServerJavascript::get_mix_time() const{
  502. return 0;
  503. }
  504. double AudioServerJavascript::get_output_delay() const {
  505. return 0;
  506. }
  507. void AudioServerJavascript::driver_process_chunk(int p_frames) {
  508. int samples=p_frames*internal_buffer_channels;
  509. for(int i=0;i<samples;i++) {
  510. internal_buffer[i]=0;
  511. }
  512. for(List<Stream*>::Element *E=active_audio_streams.front();E;E=E->next()) {
  513. ERR_CONTINUE(!E->get()->active); // bug?
  514. AudioStream *as=E->get()->audio_stream;
  515. if (!as)
  516. continue;
  517. int channels=as->get_channel_count();
  518. if (channels==0)
  519. continue; // does not want mix
  520. if (!as->mix(stream_buffer,p_frames))
  521. continue; //nothing was mixed!!
  522. int32_t stream_vol_scale=(stream_volume*stream_volume_scale*E->get()->volume_scale)*(1<<STREAM_SCALE_BITS);
  523. #define STRSCALE(m_val) ((((m_val>>STREAM_SCALE_BITS)*stream_vol_scale)>>8)/8388608.0)
  524. switch(channels) {
  525. case 1: {
  526. for(int i=0;i<p_frames;i++) {
  527. internal_buffer[(i<<1)+0]+=STRSCALE(stream_buffer[i]);
  528. internal_buffer[(i<<1)+1]+=STRSCALE(stream_buffer[i]);
  529. }
  530. } break;
  531. case 2: {
  532. for(int i=0;i<p_frames*2;i++) {
  533. internal_buffer[i]+=STRSCALE(stream_buffer[i]);
  534. }
  535. } break;
  536. case 4: {
  537. for(int i=0;i<p_frames;i++) {
  538. internal_buffer[(i<<2)+0]+=STRSCALE((stream_buffer[(i<<2)+0]+stream_buffer[(i<<2)+2])>>1);
  539. internal_buffer[(i<<2)+1]+=STRSCALE((stream_buffer[(i<<2)+1]+stream_buffer[(i<<2)+3])>>1);
  540. }
  541. } break;
  542. }
  543. #undef STRSCALE
  544. }
  545. }
  546. /*void AudioServerSW::driver_process(int p_frames,int32_t *p_buffer) {
  547. _output_delay=p_frames/double(AudioDriverSW::get_singleton()->get_mix_rate());
  548. //process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
  549. int todo=p_frames;
  550. while(todo) {
  551. int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
  552. driver_process_chunk(tomix,p_buffer);
  553. p_buffer+=tomix;
  554. todo-=tomix;
  555. }
  556. }*/
  557. AudioServerJavascript::AudioServerJavascript() {
  558. singleton=this;
  559. sample_base=1;
  560. voice_base=1;
  561. EM_ASM(
  562. _as_samples={};
  563. _as_voices={};
  564. _as_voice_pan={};
  565. _as_voice_gain={};
  566. _as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
  567. audio_server_mix_function = Module.cwrap('audio_server_mix_function', 'void', ['number']);
  568. );
  569. webaudio_mix_rate = EM_ASM_INT_V(
  570. return _as_audioctx.sampleRate;
  571. );
  572. print_line("WEBAUDIO MIX RATE: "+itos(webaudio_mix_rate));
  573. event_voice_scale=1.0;
  574. fx_volume_scale=1.0;
  575. stream_volume_scale=1.0;
  576. }