2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "audio_driver_javascript.h"
|
|
|
|
|
2020-05-18 02:19:35 +02:00
|
|
|
#include "core/project_settings.h"
|
|
|
|
|
2017-10-27 03:34:49 +02:00
|
|
|
#include <emscripten.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
AudioDriverJavaScript *AudioDriverJavaScript::singleton = nullptr;
|
2017-11-04 20:24:06 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *AudioDriverJavaScript::get_name() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return "JavaScript";
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_js_mix() {
|
|
|
|
AudioDriverJavaScript::singleton->mix_to_js();
|
2017-10-27 03:34:49 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 20:46:21 +02:00
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_capture(float sample) {
|
|
|
|
AudioDriverJavaScript::singleton->process_capture(sample);
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
void AudioDriverJavaScript::mix_to_js() {
|
|
|
|
int channel_count = get_total_channels_by_speaker_mode(get_speaker_mode());
|
|
|
|
int sample_count = memarr_len(internal_buffer) / channel_count;
|
|
|
|
int32_t *stream_buffer = reinterpret_cast<int32_t *>(internal_buffer);
|
|
|
|
audio_server_process(sample_count, stream_buffer);
|
|
|
|
for (int i = 0; i < sample_count * channel_count; i++) {
|
2018-10-08 20:46:21 +02:00
|
|
|
internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.f;
|
2017-11-05 23:48:13 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 03:34:49 +02:00
|
|
|
|
2018-10-08 20:46:21 +02:00
|
|
|
void AudioDriverJavaScript::process_capture(float sample) {
|
|
|
|
int32_t sample32 = int32_t(sample * 32768.f) * (1U << 16);
|
2020-01-20 21:35:13 +01:00
|
|
|
input_buffer_write(sample32);
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
Error AudioDriverJavaScript::init() {
|
2020-05-18 02:31:38 +02:00
|
|
|
int mix_rate = GLOBAL_GET("audio/mix_rate");
|
|
|
|
int latency = GLOBAL_GET("audio/output_latency");
|
2020-05-18 02:19:35 +02:00
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format off */
|
2020-03-11 11:32:45 +01:00
|
|
|
_driver_id = EM_ASM_INT({
|
2020-05-18 02:19:35 +02:00
|
|
|
const MIX_RATE = $0;
|
|
|
|
const LATENCY = $1;
|
2020-03-11 11:32:45 +01:00
|
|
|
return Module.IDHandler.add({
|
2020-05-18 02:19:35 +02:00
|
|
|
'context': new (window.AudioContext || window.webkitAudioContext)({ sampleRate: MIX_RATE, latencyHint: LATENCY}),
|
2020-03-11 11:32:45 +01:00
|
|
|
'input': null,
|
|
|
|
'stream': null,
|
|
|
|
'script': null
|
|
|
|
});
|
2017-11-05 23:48:13 +01:00
|
|
|
});
|
|
|
|
/* clang-format on */
|
2017-10-27 03:34:49 +02:00
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
int channel_count = get_total_channels_by_speaker_mode(get_speaker_mode());
|
2020-05-18 02:19:35 +02:00
|
|
|
buffer_length = closest_power_of_2((latency * mix_rate / 1000) * channel_count);
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format off */
|
2018-10-08 20:46:21 +02:00
|
|
|
buffer_length = EM_ASM_INT({
|
2020-03-11 11:32:45 +01:00
|
|
|
var ref = Module.IDHandler.get($0);
|
2020-05-18 02:19:35 +02:00
|
|
|
const ctx = ref['context'];
|
|
|
|
const BUFFER_LENGTH = $1;
|
|
|
|
const CHANNEL_COUNT = $2;
|
|
|
|
|
|
|
|
var script = ctx.createScriptProcessor(BUFFER_LENGTH, 2, CHANNEL_COUNT);
|
2020-03-11 11:32:45 +01:00
|
|
|
script.connect(ctx.destination);
|
|
|
|
ref['script'] = script;
|
|
|
|
return script.bufferSize;
|
2020-05-18 02:19:35 +02:00
|
|
|
}, _driver_id, buffer_length, channel_count);
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format on */
|
|
|
|
if (!buffer_length) {
|
|
|
|
return FAILED;
|
2017-10-27 03:34:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-06-15 12:11:30 +02:00
|
|
|
if (!internal_buffer || (int)memarr_len(internal_buffer) != buffer_length * channel_count) {
|
2017-11-05 23:48:13 +01:00
|
|
|
if (internal_buffer)
|
|
|
|
memdelete_arr(internal_buffer);
|
|
|
|
internal_buffer = memnew_arr(float, buffer_length *channel_count);
|
|
|
|
}
|
2018-10-08 20:46:21 +02:00
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
return internal_buffer ? OK : ERR_OUT_OF_MEMORY;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioDriverJavaScript::start() {
|
2017-10-27 03:34:49 +02:00
|
|
|
/* clang-format off */
|
2017-11-05 23:48:13 +01:00
|
|
|
EM_ASM({
|
2020-03-11 11:32:45 +01:00
|
|
|
const ref = Module.IDHandler.get($0);
|
|
|
|
var INTERNAL_BUFFER_PTR = $1;
|
2017-11-05 23:48:13 +01:00
|
|
|
|
|
|
|
var audioDriverMixFunction = cwrap('audio_driver_js_mix');
|
2018-10-08 20:46:21 +02:00
|
|
|
var audioDriverProcessCapture = cwrap('audio_driver_process_capture', null, ['number']);
|
2020-03-11 11:32:45 +01:00
|
|
|
ref['script'].onaudioprocess = function(audioProcessingEvent) {
|
2017-11-05 23:48:13 +01:00
|
|
|
audioDriverMixFunction();
|
2018-10-08 20:46:21 +02:00
|
|
|
|
|
|
|
var input = audioProcessingEvent.inputBuffer;
|
2017-11-05 23:48:13 +01:00
|
|
|
var output = audioProcessingEvent.outputBuffer;
|
2018-10-08 20:46:21 +02:00
|
|
|
var internalBuffer = HEAPF32.subarray(
|
2017-11-05 23:48:13 +01:00
|
|
|
INTERNAL_BUFFER_PTR / HEAPF32.BYTES_PER_ELEMENT,
|
|
|
|
INTERNAL_BUFFER_PTR / HEAPF32.BYTES_PER_ELEMENT + output.length * output.numberOfChannels);
|
|
|
|
|
|
|
|
for (var channel = 0; channel < output.numberOfChannels; channel++) {
|
|
|
|
var outputData = output.getChannelData(channel);
|
|
|
|
// Loop through samples.
|
|
|
|
for (var sample = 0; sample < outputData.length; sample++) {
|
2018-10-08 20:46:21 +02:00
|
|
|
outputData[sample] = internalBuffer[sample * output.numberOfChannels + channel];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 11:32:45 +01:00
|
|
|
if (ref['input']) {
|
2018-10-08 20:46:21 +02:00
|
|
|
var inputDataL = input.getChannelData(0);
|
|
|
|
var inputDataR = input.getChannelData(1);
|
|
|
|
for (var i = 0; i < inputDataL.length; i++) {
|
|
|
|
audioDriverProcessCapture(inputDataL[i]);
|
|
|
|
audioDriverProcessCapture(inputDataR[i]);
|
2017-11-05 23:48:13 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 20:24:06 +01:00
|
|
|
};
|
2020-03-11 11:32:45 +01:00
|
|
|
}, _driver_id, internal_buffer);
|
2017-10-27 03:34:49 +02:00
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2019-01-20 14:25:15 +01:00
|
|
|
void AudioDriverJavaScript::resume() {
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM({
|
2020-03-11 11:32:45 +01:00
|
|
|
const ref = Module.IDHandler.get($0);
|
|
|
|
if (ref && ref['context'] && ref['context'].resume)
|
|
|
|
ref['context'].resume();
|
|
|
|
}, _driver_id);
|
2019-01-20 14:25:15 +01:00
|
|
|
/* clang-format on */
|
|
|
|
}
|
|
|
|
|
2020-05-18 02:19:35 +02:00
|
|
|
float AudioDriverJavaScript::get_latency() {
|
|
|
|
/* clang-format off */
|
|
|
|
return EM_ASM_DOUBLE({
|
|
|
|
const ref = Module.IDHandler.get($0);
|
|
|
|
var latency = 0;
|
|
|
|
if (ref && ref['context']) {
|
|
|
|
const ctx = ref['context'];
|
|
|
|
if (ctx.baseLatency) {
|
|
|
|
latency += ctx.baseLatency;
|
|
|
|
}
|
|
|
|
if (ctx.outputLatency) {
|
|
|
|
latency += ctx.outputLatency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return latency;
|
|
|
|
}, _driver_id);
|
|
|
|
/* clang-format on */
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int AudioDriverJavaScript::get_mix_rate() const {
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format off */
|
2020-03-11 11:32:45 +01:00
|
|
|
return EM_ASM_INT({
|
|
|
|
const ref = Module.IDHandler.get($0);
|
|
|
|
return ref && ref['context'] ? ref['context'].sampleRate : 0;
|
|
|
|
}, _driver_id);
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format off */
|
2020-03-11 11:32:45 +01:00
|
|
|
return get_speaker_mode_by_total_channels(EM_ASM_INT({
|
|
|
|
const ref = Module.IDHandler.get($0);
|
|
|
|
return ref && ref['context'] ? ref['context'].destination.channelCount : 0;
|
|
|
|
}, _driver_id));
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
// No locking, as threads are not supported.
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioDriverJavaScript::lock() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void AudioDriverJavaScript::unlock() {
|
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
void AudioDriverJavaScript::finish_async() {
|
|
|
|
// Close the context, add the operation to the async_finish list in module.
|
|
|
|
int id = _driver_id;
|
|
|
|
_driver_id = 0;
|
2017-11-05 23:48:13 +01:00
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM({
|
2020-05-01 14:45:45 +02:00
|
|
|
var ref = Module.IDHandler.get($0);
|
|
|
|
Module.async_finish.push(new Promise(function(accept, reject) {
|
|
|
|
if (!ref) {
|
|
|
|
console.log("Ref not found!", $0, Module.IDHandler);
|
|
|
|
setTimeout(accept, 0);
|
|
|
|
} else {
|
|
|
|
const context = ref['context'];
|
|
|
|
// Disconnect script and input.
|
|
|
|
ref['script'].disconnect();
|
|
|
|
if (ref['input'])
|
|
|
|
ref['input'].disconnect();
|
|
|
|
ref = null;
|
|
|
|
context.close().then(function() {
|
|
|
|
accept();
|
|
|
|
}).catch(function(e) {
|
|
|
|
accept();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
2020-03-11 11:32:45 +01:00
|
|
|
Module.IDHandler.remove($0);
|
2020-05-01 14:45:45 +02:00
|
|
|
}, id);
|
2017-11-05 23:48:13 +01:00
|
|
|
/* clang-format on */
|
2020-05-01 14:45:45 +02:00
|
|
|
}
|
2018-10-08 20:46:21 +02:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
void AudioDriverJavaScript::finish() {
|
2018-10-08 20:46:21 +02:00
|
|
|
if (internal_buffer) {
|
|
|
|
memdelete_arr(internal_buffer);
|
2020-04-02 01:20:12 +02:00
|
|
|
internal_buffer = nullptr;
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverJavaScript::capture_start() {
|
2020-01-20 21:35:13 +01:00
|
|
|
input_buffer_init(buffer_length);
|
2018-10-08 20:46:21 +02:00
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM({
|
|
|
|
function gotMediaInput(stream) {
|
2020-03-11 11:32:45 +01:00
|
|
|
var ref = Module.IDHandler.get($0);
|
|
|
|
ref['stream'] = stream;
|
|
|
|
ref['input'] = ref['context'].createMediaStreamSource(stream);
|
|
|
|
ref['input'].connect(ref['script']);
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function gotMediaInputError(e) {
|
2019-02-24 04:40:06 +01:00
|
|
|
out(e);
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (navigator.mediaDevices.getUserMedia) {
|
|
|
|
navigator.mediaDevices.getUserMedia({"audio": true}).then(gotMediaInput, gotMediaInputError);
|
|
|
|
} else {
|
|
|
|
if (!navigator.getUserMedia)
|
|
|
|
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
|
|
navigator.getUserMedia({"audio": true}, gotMediaInput, gotMediaInputError);
|
|
|
|
}
|
2020-03-11 11:32:45 +01:00
|
|
|
}, _driver_id);
|
2018-10-08 20:46:21 +02:00
|
|
|
/* clang-format on */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverJavaScript::capture_stop() {
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM({
|
2020-03-11 11:32:45 +01:00
|
|
|
var ref = Module.IDHandler.get($0);
|
|
|
|
if (ref['stream']) {
|
|
|
|
const tracks = ref['stream'].getTracks();
|
2018-10-08 20:46:21 +02:00
|
|
|
for (var i = 0; i < tracks.length; i++) {
|
|
|
|
tracks[i].stop();
|
|
|
|
}
|
2020-03-11 11:32:45 +01:00
|
|
|
ref['stream'] = null;
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 11:32:45 +01:00
|
|
|
if (ref['input']) {
|
|
|
|
ref['input'].disconnect();
|
|
|
|
ref['input'] = null;
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 11:32:45 +01:00
|
|
|
}, _driver_id);
|
2018-10-08 20:46:21 +02:00
|
|
|
/* clang-format on */
|
|
|
|
|
2020-01-20 21:35:13 +01:00
|
|
|
input_buffer.clear();
|
|
|
|
|
2018-10-08 20:46:21 +02:00
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioDriverJavaScript::AudioDriverJavaScript() {
|
2020-03-11 11:32:45 +01:00
|
|
|
_driver_id = 0;
|
2020-04-02 01:20:12 +02:00
|
|
|
internal_buffer = nullptr;
|
2020-03-11 11:32:45 +01:00
|
|
|
buffer_length = 0;
|
2018-10-08 20:46:21 +02:00
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
singleton = this;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|