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