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-11-03 17:18:02 +01:00
|
|
|
AudioDriverJavaScript *AudioDriverJavaScript::singleton = nullptr;
|
2017-11-04 20:24:06 +01:00
|
|
|
|
2020-06-29 18:51:53 +02:00
|
|
|
bool AudioDriverJavaScript::is_available() {
|
2020-09-28 15:43:48 +02:00
|
|
|
return godot_audio_is_available() != 0;
|
2020-06-29 18:51:53 +02: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";
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
void AudioDriverJavaScript::_state_change_callback(int p_state) {
|
|
|
|
singleton->state = p_state;
|
2020-09-28 15:43:48 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
void AudioDriverJavaScript::_latency_update_callback(float p_latency) {
|
|
|
|
singleton->output_latency = p_latency;
|
2020-09-28 15:43:48 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
void AudioDriverJavaScript::_audio_driver_process(int p_from, int p_samples) {
|
|
|
|
int32_t *stream_buffer = reinterpret_cast<int32_t *>(output_rb);
|
|
|
|
const int max_samples = memarr_len(output_rb);
|
2017-10-27 03:34:49 +02:00
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
int write_pos = p_from;
|
|
|
|
int to_write = p_samples;
|
|
|
|
if (to_write == 0) {
|
|
|
|
to_write = max_samples;
|
|
|
|
}
|
|
|
|
// High part
|
|
|
|
if (write_pos + to_write > max_samples) {
|
|
|
|
const int samples_high = max_samples - write_pos;
|
|
|
|
audio_server_process(samples_high / channel_count, &stream_buffer[write_pos]);
|
|
|
|
for (int i = write_pos; i < max_samples; i++) {
|
|
|
|
output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f;
|
|
|
|
}
|
|
|
|
to_write -= samples_high;
|
|
|
|
write_pos = 0;
|
|
|
|
}
|
|
|
|
// Leftover
|
|
|
|
audio_server_process(to_write / channel_count, &stream_buffer[write_pos]);
|
|
|
|
for (int i = write_pos; i < write_pos + to_write; i++) {
|
|
|
|
output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f;
|
|
|
|
}
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
void AudioDriverJavaScript::_audio_driver_capture(int p_from, int p_samples) {
|
|
|
|
if (get_input_buffer().size() == 0) {
|
|
|
|
return; // Input capture stopped.
|
2017-11-05 23:48:13 +01:00
|
|
|
}
|
2020-11-03 17:18:02 +01:00
|
|
|
const int max_samples = memarr_len(input_rb);
|
2017-10-27 03:34:49 +02:00
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
int read_pos = p_from;
|
|
|
|
int to_read = p_samples;
|
|
|
|
if (to_read == 0) {
|
|
|
|
to_read = max_samples;
|
|
|
|
}
|
|
|
|
// High part
|
|
|
|
if (read_pos + to_read > max_samples) {
|
|
|
|
const int samples_high = max_samples - read_pos;
|
|
|
|
for (int i = read_pos; i < max_samples; i++) {
|
|
|
|
input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16));
|
|
|
|
}
|
|
|
|
to_read -= samples_high;
|
|
|
|
read_pos = 0;
|
|
|
|
}
|
|
|
|
// Leftover
|
|
|
|
for (int i = read_pos; i < read_pos + to_read; i++) {
|
|
|
|
input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16));
|
|
|
|
}
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 23:48:13 +01:00
|
|
|
Error AudioDriverJavaScript::init() {
|
2020-09-28 15:43:48 +02:00
|
|
|
mix_rate = GLOBAL_GET("audio/mix_rate");
|
2020-05-18 02:31:38 +02:00
|
|
|
int latency = GLOBAL_GET("audio/output_latency");
|
2020-05-18 02:19:35 +02:00
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
channel_count = godot_audio_init(mix_rate, latency, &_state_change_callback, &_latency_update_callback);
|
|
|
|
buffer_length = closest_power_of_2((latency * mix_rate / 1000));
|
|
|
|
#ifndef NO_THREADS
|
|
|
|
node = memnew(WorkletNode);
|
|
|
|
#else
|
|
|
|
node = memnew(ScriptProcessorNode);
|
|
|
|
#endif
|
|
|
|
buffer_length = node->create(buffer_length, channel_count);
|
|
|
|
if (output_rb) {
|
|
|
|
memdelete_arr(output_rb);
|
2017-10-27 03:34:49 +02:00
|
|
|
}
|
2020-11-03 17:18:02 +01:00
|
|
|
output_rb = memnew_arr(float, buffer_length *channel_count);
|
|
|
|
if (!output_rb) {
|
|
|
|
return ERR_OUT_OF_MEMORY;
|
2017-11-05 23:48:13 +01:00
|
|
|
}
|
2020-11-03 17:18:02 +01:00
|
|
|
if (input_rb) {
|
|
|
|
memdelete_arr(input_rb);
|
|
|
|
}
|
|
|
|
input_rb = memnew_arr(float, buffer_length *channel_count);
|
|
|
|
if (!input_rb) {
|
2020-09-28 15:43:48 +02:00
|
|
|
return ERR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return OK;
|
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() {
|
2020-11-03 17:18:02 +01:00
|
|
|
if (node) {
|
|
|
|
node->start(output_rb, memarr_len(output_rb), input_rb, memarr_len(input_rb));
|
|
|
|
}
|
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() {
|
2020-11-03 17:18:02 +01:00
|
|
|
if (state == 0) { // 'suspended'
|
|
|
|
godot_audio_resume();
|
|
|
|
}
|
2019-01-20 14:25:15 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 02:19:35 +02:00
|
|
|
float AudioDriverJavaScript::get_latency() {
|
2020-11-03 17:18:02 +01:00
|
|
|
return output_latency + (float(buffer_length) / mix_rate);
|
2020-05-18 02:19:35 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int AudioDriverJavaScript::get_mix_rate() const {
|
2020-09-28 15:43:48 +02:00
|
|
|
return mix_rate;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
|
2020-09-28 15:43:48 +02:00
|
|
|
return get_speaker_mode_by_total_channels(channel_count);
|
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::lock() {
|
2020-11-03 17:18:02 +01:00
|
|
|
if (node) {
|
|
|
|
node->unlock();
|
2020-09-28 15:43:48 +02:00
|
|
|
}
|
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() {
|
2020-11-03 17:18:02 +01:00
|
|
|
if (node) {
|
|
|
|
node->unlock();
|
2020-09-28 15:43:48 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2020-01-19 11:44:19 +01:00
|
|
|
void AudioDriverJavaScript::finish() {
|
2020-11-03 17:18:02 +01:00
|
|
|
if (node) {
|
|
|
|
node->finish();
|
|
|
|
memdelete(node);
|
|
|
|
node = nullptr;
|
|
|
|
}
|
|
|
|
if (output_rb) {
|
|
|
|
memdelete_arr(output_rb);
|
|
|
|
output_rb = nullptr;
|
|
|
|
}
|
|
|
|
if (input_rb) {
|
|
|
|
memdelete_arr(input_rb);
|
|
|
|
input_rb = nullptr;
|
2018-10-08 20:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverJavaScript::capture_start() {
|
2020-11-03 17:18:02 +01:00
|
|
|
lock();
|
2020-01-20 21:35:13 +01:00
|
|
|
input_buffer_init(buffer_length);
|
2020-11-03 17:18:02 +01:00
|
|
|
unlock();
|
2020-12-08 11:18:03 +01:00
|
|
|
if (godot_audio_capture_start()) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
2018-10-08 20:46:21 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverJavaScript::capture_stop() {
|
2020-09-28 15:43:48 +02:00
|
|
|
godot_audio_capture_stop();
|
2020-11-03 17:18:02 +01:00
|
|
|
lock();
|
2020-01-20 21:35:13 +01:00
|
|
|
input_buffer.clear();
|
2020-11-03 17:18:02 +01:00
|
|
|
unlock();
|
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-11-03 17:18:02 +01:00
|
|
|
singleton = this;
|
|
|
|
}
|
2020-09-28 15:43:48 +02:00
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
#ifdef NO_THREADS
|
|
|
|
/// ScriptProcessorNode implementation
|
|
|
|
void AudioDriverJavaScript::ScriptProcessorNode::_process_callback() {
|
|
|
|
AudioDriverJavaScript::singleton->_audio_driver_capture();
|
|
|
|
AudioDriverJavaScript::singleton->_audio_driver_process();
|
|
|
|
}
|
2018-10-08 20:46:21 +02:00
|
|
|
|
2020-11-03 17:18:02 +01:00
|
|
|
int AudioDriverJavaScript::ScriptProcessorNode::create(int p_buffer_samples, int p_channels) {
|
|
|
|
return godot_audio_script_create(p_buffer_samples, p_channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverJavaScript::ScriptProcessorNode::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
|
|
|
|
godot_audio_script_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, &_process_callback);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/// AudioWorkletNode implementation
|
|
|
|
void AudioDriverJavaScript::WorkletNode::_audio_thread_func(void *p_data) {
|
|
|
|
AudioDriverJavaScript::WorkletNode *obj = static_cast<AudioDriverJavaScript::WorkletNode *>(p_data);
|
|
|
|
AudioDriverJavaScript *driver = AudioDriverJavaScript::singleton;
|
|
|
|
const int out_samples = memarr_len(driver->output_rb);
|
|
|
|
const int in_samples = memarr_len(driver->input_rb);
|
|
|
|
int wpos = 0;
|
|
|
|
int to_write = out_samples;
|
|
|
|
int rpos = 0;
|
|
|
|
int to_read = 0;
|
|
|
|
int32_t step = 0;
|
|
|
|
while (!obj->quit) {
|
|
|
|
if (to_read) {
|
|
|
|
driver->lock();
|
|
|
|
driver->_audio_driver_capture(rpos, to_read);
|
|
|
|
godot_audio_worklet_state_add(obj->state, STATE_SAMPLES_IN, -to_read);
|
|
|
|
driver->unlock();
|
|
|
|
rpos += to_read;
|
|
|
|
if (rpos >= in_samples) {
|
|
|
|
rpos -= in_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (to_write) {
|
|
|
|
driver->lock();
|
|
|
|
driver->_audio_driver_process(wpos, to_write);
|
|
|
|
godot_audio_worklet_state_add(obj->state, STATE_SAMPLES_OUT, to_write);
|
|
|
|
driver->unlock();
|
|
|
|
wpos += to_write;
|
|
|
|
if (wpos >= out_samples) {
|
|
|
|
wpos -= out_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
step = godot_audio_worklet_state_wait(obj->state, STATE_PROCESS, step, 1);
|
|
|
|
to_write = out_samples - godot_audio_worklet_state_get(obj->state, STATE_SAMPLES_OUT);
|
|
|
|
to_read = godot_audio_worklet_state_get(obj->state, STATE_SAMPLES_IN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioDriverJavaScript::WorkletNode::create(int p_buffer_size, int p_channels) {
|
|
|
|
godot_audio_worklet_create(p_channels);
|
|
|
|
return p_buffer_size;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-11-03 17:18:02 +01:00
|
|
|
|
|
|
|
void AudioDriverJavaScript::WorkletNode::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
|
|
|
|
godot_audio_worklet_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, state);
|
|
|
|
mutex = Mutex::create();
|
|
|
|
thread = Thread::create(_audio_thread_func, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverJavaScript::WorkletNode::lock() {
|
|
|
|
if (mutex) {
|
|
|
|
mutex->lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverJavaScript::WorkletNode::unlock() {
|
|
|
|
if (mutex) {
|
|
|
|
mutex->unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverJavaScript::WorkletNode::finish() {
|
|
|
|
quit = true; // Ask thread to quit.
|
|
|
|
Thread::wait_to_finish(thread);
|
|
|
|
memdelete(thread);
|
|
|
|
thread = nullptr;
|
|
|
|
memdelete(mutex);
|
|
|
|
mutex = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|