audio_server_javascript.cpp 18 KB

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