diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-11-18 02:33:59 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-11-18 02:33:59 +0100 |
commit | 0269c40d9b94f49576b1c7ece0f768eb551686e5 (patch) | |
tree | ba87195c3be249e0f43f17bea76e867d567d6901 | |
parent | 96f9236bff9907fc0508f1920ae98bf3076f4755 (diff) | |
download | reduced_system_layer-0269c40d9b94f49576b1c7ece0f768eb551686e5.zip |
Update web audio impl
-rw-r--r-- | index.htm | 15 | ||||
-rwxr-xr-x | reduced_system_layer.c | 43 |
2 files changed, 53 insertions, 5 deletions
@@ -18,6 +18,14 @@ } process(input, output, parameters) { // TODO + + let sample_rate = sampleRate; + let num_channels = output[0].length; + let num_samples = output[0][0].length; + let max_num_frames = this.state.max_num_frames; + + let position = this.state.program.instance.exports.js_sound_playback(num_samples); + return true; } } @@ -159,7 +167,9 @@ let sound_node; let sound_state = { + program : null, memory : null, + max_num_frames : 0, buffer_address : 0, }; @@ -181,6 +191,11 @@ await sound_context.audioWorklet.addModule(URL.createObjectURL(blob)); + sound_state.program = program; + sound_state.memory = memory; + sound_state.max_num_frames = program.instance.exports.js_max_num_audio_frames(); + sound_state.buffer_address = program.instance.exports.js_sound_buffer(); + sound_node = new AudioWorkletNode( sound_context, "Sound_Ring", diff --git a/reduced_system_layer.c b/reduced_system_layer.c index dfc8b4f..bbb2a87 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -944,7 +944,6 @@ void p_handle_audio(i64 samples_elapsed) { fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s)); memset(_sound_ring + _sound_position, 0, num_frames); - _sound_position += num_frames; } else { i64 part_one = MAX_NUM_AUDIO_FRAMES - _sound_position; i64 part_two = num_frames - part_one; @@ -959,8 +958,9 @@ void p_handle_audio(i64 samples_elapsed) { memset(_sound_ring + _sound_position, 0, part_one); memset(_sound_ring, 0, part_two); - _sound_position = part_two; } + + _sound_position = (_sound_position + num_frames) % MAX_NUM_AUDIO_FRAMES; } void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { @@ -1685,8 +1685,15 @@ __attribute__((export_name("js_keyup"))) void js_keyup(u32 key, u32 mod) { #ifdef __wasm__ +i64 _sound_write = 0; +i64 _sound_position = 0; +f32 _sound_ring[MAX_NUM_AUDIO_FRAMES] = {0}; + void p_handle_audio(i64 samples_elapsed) { - // TODO + if (samples_elapsed <= 0) + return; + + _sound_write = (_sound_write + samples_elapsed * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES; } void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { @@ -1701,12 +1708,38 @@ __attribute__((export_name("js_num_channels"))) i32 js_num_channels(void) { return AUDIO_NUM_CHANNELS; } +__attribute__((export_name("js_max_num_audio_frames"))) i32 js_max_num_audio_frames(void) { + return MAX_NUM_AUDIO_FRAMES; +} + __attribute__((export_name("js_sound_buffer"))) void *js_sound_buffer(void) { return NULL; } -__attribute__((export_name("js_sound_playback"))) void js_sound_playback(i64 num_samples) { - // TODO +__attribute__((export_name("js_sound_sync"))) i64 js_sound_sync(void) { + _sound_write = AUDIO_AVAIL_MIN; + _sound_position = 0; + for (i64 i = 0; i < MAX_NUM_AUDIO_FRAMES; ++i) + _sound_ring[i] = 0.f; +} + +__attribute__((export_name("js_sound_playback"))) i64 js_sound_playback(i64 num_samples) { + i64 position = _sound_position; + i64 num_frames = AUDIO_NUM_CHANNELS * num_samples; + + if (_sound_position + num_frames <= MAX_NUM_AUDIO_FRAMES) { + for (i64 i = _sound_position; i < _sound_position + num_frames; ++i) + _sound_ring[i] = 0.f; + } else { + for (i64 i = _sound_position; i < MAX_NUM_AUDIO_FRAMES; ++i) + _sound_ring[i] = 0.f; + i64 i_end = _sound_position + num_frames - MAX_NUM_AUDIO_FRAMES; + for (i64 i = 0; i < i_end; ++i) + _sound_ring[i] = 0.f; + } + + _sound_position = (_sound_position + num_frames) % MAX_NUM_AUDIO_FRAMES; + return position; } #endif // __wasm__ |