summaryrefslogtreecommitdiff
path: root/platform/javascript/audio_driver_javascript.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/javascript/audio_driver_javascript.cpp')
-rw-r--r--platform/javascript/audio_driver_javascript.cpp44
1 files changed, 15 insertions, 29 deletions
diff --git a/platform/javascript/audio_driver_javascript.cpp b/platform/javascript/audio_driver_javascript.cpp
index cd3974669f..9633472cd2 100644
--- a/platform/javascript/audio_driver_javascript.cpp
+++ b/platform/javascript/audio_driver_javascript.cpp
@@ -30,29 +30,19 @@
#include "audio_driver_javascript.h"
#include <emscripten.h>
-#include <string.h>
-
-#define MAX_NUMBER_INTERFACES 3
-#define MAX_NUMBER_OUTPUT_DEVICES 6
-
-/* Structure for passing information to callback function */
-
-//AudioDriverJavaScript* AudioDriverJavaScript::s_ad=NULL;
AudioDriverJavaScript *AudioDriverJavaScript::singleton_js = NULL;
+
const char *AudioDriverJavaScript::get_name() const {
return "JavaScript";
}
-extern "C" {
-
-void js_audio_driver_mix_function(int p_frames) {
+extern "C" EMSCRIPTEN_KEEPALIVE void js_audio_driver_mix_function(int p_frames) {
//print_line("MIXI! "+itos(p_frames));
AudioDriverJavaScript::singleton_js->mix_to_js(p_frames);
}
-}
void AudioDriverJavaScript::mix_to_js(int p_frames) {
@@ -65,11 +55,11 @@ void AudioDriverJavaScript::mix_to_js(int p_frames) {
audio_server_process(p_frames, stream_buffer);
for (int i = 0; i < tomix * internal_buffer_channels; i++) {
- internal_buffer[i] = float(stream_buffer[i] >> 16) * 32768.0;
+ internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.0;
}
/* clang-format off */
- EM_ASM_({
+ EM_ASM_ARGS({
var data = HEAPF32.subarray($0 / 4, $0 / 4 + $2 * 2);
for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
@@ -95,29 +85,24 @@ Error AudioDriverJavaScript::init() {
void AudioDriverJavaScript::start() {
- internal_buffer_channels = 2;
internal_buffer = memnew_arr(float, INTERNAL_BUFFER_SIZE *internal_buffer_channels);
stream_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * 4); //max 4 channels
/* clang-format off */
- EM_ASM(
- _as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
-
- audio_server_mix_function = Module.cwrap('js_audio_driver_mix_function', 'void', ['number']);
- );
-
- int buffer_latency = 16384;
- EM_ASM_( {
- _as_script_node = _as_audioctx.createScriptProcessor($0, 0, 2);
+ mix_rate = EM_ASM_INT({
+ _as_audioctx = new (window.AudioContext || window.webkitAudioContext);
+ _as_script_node = _as_audioctx.createScriptProcessor($0, 0, $1);
_as_script_node.connect(_as_audioctx.destination);
console.log(_as_script_node.bufferSize);
+ var jsAudioDriverMixFunction = cwrap('js_audio_driver_mix_function', null, ['number']);
_as_script_node.onaudioprocess = function(audioProcessingEvent) {
- // The output buffer contains the samples that will be modified and played
+ // The output buffer contains the samples that will be modified and played
_as_output_buffer = audioProcessingEvent.outputBuffer;
- audio_server_mix_function(_as_output_buffer.getChannelData(0).length);
- }
- }, buffer_latency);
+ jsAudioDriverMixFunction([_as_output_buffer.getChannelData(0).length]);
+ };
+ return _as_audioctx.sampleRate;
+ }, INTERNAL_BUFFER_SIZE, internal_buffer_channels);
/* clang-format on */
}
@@ -152,6 +137,7 @@ void AudioDriverJavaScript::finish() {
AudioDriverJavaScript::AudioDriverJavaScript() {
- mix_rate = 44100;
+ internal_buffer_channels = 2;
+ mix_rate = DEFAULT_MIX_RATE;
singleton_js = this;
}