[HTML5] AudioWorkletAPI implementation.

Rewrote AudioDriverJavaScript to support multiple processor nodes.
The old (and deprecated) ScriptProcessorNode when threads are not
available, and the new AudioWorklet API when threads are enabled.

The new implementation uses two ring buffers and a shared state to
communicated with the AudioWorklet thread.

The audio.worklet.js JavaScript file is always added to the export
template, but only really used (and download) in the thread build.
This commit is contained in:
Fabio Alessandrelli 2020-11-03 17:18:02 +01:00
parent e52ed6d89e
commit 6d939b72f0
8 changed files with 722 additions and 220 deletions

View file

@ -35,32 +35,74 @@
#include "core/os/thread.h"
#include "servers/audio_server.h"
#include "godot_audio.h"
class AudioDriverJavaScript : public AudioDriver {
public:
class AudioNode {
public:
virtual int create(int p_buffer_size, int p_output_channels) = 0;
virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0;
virtual void finish() {}
virtual void lock() {}
virtual void unlock() {}
virtual ~AudioNode() {}
};
class WorkletNode : public AudioNode {
private:
enum {
STATE_LOCK,
STATE_PROCESS,
STATE_SAMPLES_IN,
STATE_SAMPLES_OUT,
STATE_MAX,
};
Mutex *mutex = nullptr;
Thread *thread = nullptr;
bool quit = false;
int32_t state[STATE_MAX] = { 0 };
static void _audio_thread_func(void *p_data);
public:
int create(int p_buffer_size, int p_output_channels) override;
void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
void finish() override;
void lock() override;
void unlock() override;
};
class ScriptProcessorNode : public AudioNode {
private:
static void _process_callback();
public:
int create(int p_buffer_samples, int p_channels) override;
void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
};
private:
float *internal_buffer;
AudioNode *node = nullptr;
int buffer_length;
float *output_rb = nullptr;
float *input_rb = nullptr;
int mix_rate;
int channel_count;
int buffer_length = 0;
int mix_rate = 0;
int channel_count = 0;
int state = 0;
float output_latency = 0.0;
#ifndef NO_THREADS
Mutex *mutex;
Thread *thread;
bool quit;
bool needs_process;
static void _state_change_callback(int p_state);
static void _latency_update_callback(float p_latency);
static void _audio_thread_func(void *p_data);
#endif
static void _audio_driver_process_start();
static void _audio_driver_process_end();
static void _audio_driver_process_capture(float p_sample);
void _audio_driver_process();
protected:
void _audio_driver_process(int p_from = 0, int p_samples = 0);
void _audio_driver_capture(int p_from = 0, int p_samples = 0);
public:
static bool is_available();
void process_capture(float sample);
static AudioDriverJavaScript *singleton;
@ -81,5 +123,4 @@ public:
AudioDriverJavaScript();
};
#endif