2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "audio_driver_javascript.h"
|
|
|
|
|
2017-10-27 01:34:49 +00:00
|
|
|
#include <emscripten.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-10-27 01:34:49 +00:00
|
|
|
AudioDriverJavaScript *AudioDriverJavaScript::singleton_js = NULL;
|
2017-11-04 19:24:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const char *AudioDriverJavaScript::get_name() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return "JavaScript";
|
|
|
|
}
|
|
|
|
|
2017-11-04 19:24:06 +00:00
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE void js_audio_driver_mix_function(int p_frames) {
|
2017-10-27 01:34:49 +00:00
|
|
|
|
|
|
|
//print_line("MIXI! "+itos(p_frames));
|
|
|
|
AudioDriverJavaScript::singleton_js->mix_to_js(p_frames);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverJavaScript::mix_to_js(int p_frames) {
|
|
|
|
|
|
|
|
int todo = p_frames;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
while (todo) {
|
|
|
|
|
|
|
|
int tomix = MIN(todo, INTERNAL_BUFFER_SIZE);
|
|
|
|
|
|
|
|
audio_server_process(p_frames, stream_buffer);
|
|
|
|
for (int i = 0; i < tomix * internal_buffer_channels; i++) {
|
2017-11-04 19:24:06 +00:00
|
|
|
internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.0;
|
2017-10-27 01:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clang-format off */
|
2017-11-04 19:24:06 +00:00
|
|
|
EM_ASM_ARGS({
|
2017-10-27 01:34:49 +00:00
|
|
|
var data = HEAPF32.subarray($0 / 4, $0 / 4 + $2 * 2);
|
|
|
|
|
|
|
|
for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
|
|
|
|
var outputData = _as_output_buffer.getChannelData(channel);
|
|
|
|
// Loop through samples
|
|
|
|
for (var sample = 0; sample < $2; sample++) {
|
|
|
|
// make output equal to the same as the input
|
|
|
|
outputData[sample + $1] = data[sample * 2 + channel];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, internal_buffer, offset, tomix);
|
|
|
|
/* clang-format on */
|
|
|
|
|
|
|
|
todo -= tomix;
|
|
|
|
offset += tomix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error AudioDriverJavaScript::init() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverJavaScript::start() {
|
2017-10-27 01:34:49 +00:00
|
|
|
|
|
|
|
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 */
|
2017-11-04 19:24:06 +00:00
|
|
|
mix_rate = EM_ASM_INT({
|
|
|
|
_as_audioctx = new (window.AudioContext || window.webkitAudioContext);
|
|
|
|
_as_script_node = _as_audioctx.createScriptProcessor($0, 0, $1);
|
2017-10-27 01:34:49 +00:00
|
|
|
_as_script_node.connect(_as_audioctx.destination);
|
|
|
|
console.log(_as_script_node.bufferSize);
|
2017-11-04 19:24:06 +00:00
|
|
|
var jsAudioDriverMixFunction = cwrap('js_audio_driver_mix_function', null, ['number']);
|
2017-10-27 01:34:49 +00:00
|
|
|
|
|
|
|
_as_script_node.onaudioprocess = function(audioProcessingEvent) {
|
2017-11-04 19:24:06 +00:00
|
|
|
// The output buffer contains the samples that will be modified and played
|
2017-10-27 01:34:49 +00:00
|
|
|
_as_output_buffer = audioProcessingEvent.outputBuffer;
|
2017-11-04 19:24:06 +00:00
|
|
|
jsAudioDriverMixFunction([_as_output_buffer.getChannelData(0).length]);
|
|
|
|
};
|
|
|
|
return _as_audioctx.sampleRate;
|
|
|
|
}, INTERNAL_BUFFER_SIZE, internal_buffer_channels);
|
2017-10-27 01:34:49 +00:00
|
|
|
/* clang-format on */
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int AudioDriverJavaScript::get_mix_rate() const {
|
|
|
|
|
2017-10-27 01:34:49 +00:00
|
|
|
return mix_rate;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
|
2017-01-16 18:19:45 +00:00
|
|
|
|
|
|
|
return SPEAKER_MODE_STEREO;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverJavaScript::lock() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-10-27 01:34:49 +00:00
|
|
|
/*no locking, as threads are not supported
|
2017-01-14 11:26:56 +00:00
|
|
|
if (active && mutex)
|
|
|
|
mutex->lock();
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void AudioDriverJavaScript::unlock() {
|
|
|
|
|
2017-10-27 01:34:49 +00:00
|
|
|
/*no locking, as threads are not supported
|
2017-01-14 11:26:56 +00:00
|
|
|
if (active && mutex)
|
|
|
|
mutex->unlock();
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverJavaScript::finish() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioDriverJavaScript::AudioDriverJavaScript() {
|
2017-10-27 01:34:49 +00:00
|
|
|
|
2017-11-04 19:24:06 +00:00
|
|
|
internal_buffer_channels = 2;
|
|
|
|
mix_rate = DEFAULT_MIX_RATE;
|
2017-10-27 01:34:49 +00:00
|
|
|
singleton_js = this;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|