diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-11-22 16:30:19 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-11-22 16:30:19 +0100 |
commit | 533aa1816b2c90b3935504b489e03e6ef87038a6 (patch) | |
tree | 09140f39264c2939a5eb2683d05827894f62dac3 /index.htm | |
parent | eda1d8c0bf70fc582a02b32df82d600b8b1cbe25 (diff) | |
download | reduced_system_layer-533aa1816b2c90b3935504b489e03e6ef87038a6.zip |
Update audio
Diffstat (limited to 'index.htm')
-rw-r--r-- | index.htm | 66 |
1 files changed, 30 insertions, 36 deletions
@@ -26,6 +26,11 @@ let num_samples = output[0][0].length; let num_frames = num_samples * num_channels; + if (num_frames > this.max_num_frames) { + console.error("Sound buffer overflow"); + return true; + } + let frames = new Float32Array(this.ring); if (num_frames <= this.max_num_frames - this.position) @@ -270,51 +275,40 @@ }, p_cleanup : () => {}, p_wait_events_impl : () => { wait_for_events = true; }, - p_sleep_for : (time) => { sleep_duration += time; }, - p_time : Date.now, + p_sleep_for_impl : (time) => { sleep_duration += time; }, + p_time_impl : Date.now, p_handle_audio_impl : (samples_elapsed) => { + if (!sound_ready) + return; + let num_frames = samples_elapsed * sound_num_channels; + if (num_frames > sound_max_num_frames) { + console.error("Sound buffer overflow"); + return; + } + + let dst = new Float32Array(sound_shared_ring); + let src = new Float32Array(memory.subarray(sound_buffer_address, sound_buffer_address + sound_max_num_frames * 4)); + if (num_frames <= sound_max_num_frames - sound_position) - new Uint8Array( - sound_shared_ring.slice( - sound_position * 4, - sound_position * 4 + num_frames * 4 - ) - ).set( - memory.subarray( - sound_buffer_address + sound_position * 4, - sound_buffer_address + sound_position * 4 + num_frames * 4 - ) - ); + for (let i = 0; i < num_frames; ++i) { + dst[sound_position + i] = src[sound_position + i]; + src[sound_position + i] = 0.0; + } else { let part_one = sound_max_num_frames - sound_position; let part_two = num_frames - part_one; - new Uint8Array( - sound_shared_ring.slice( - sound_position * 4, - sound_position * 4 + part_one * 4 - ) - ).set( - memory.subarray( - sound_buffer_address + sound_position * 4, - sound_buffer_address + sound_position * 4 + part_one * 4 - ) - ); - - new Uint8Array( - sound_shared_ring.slice( - 0, - part_two * 4 - ) - ).set( - memory.subarray( - sound_buffer_address, - sound_buffer_address + part_two * 4 - ) - ); + for (let i = 0; i < part_one; ++i) { + dst[sound_position + i] = src[sound_position + i]; + src[sound_position + i] = 0.0; + } + for (let i = 0; i < part_two; ++i) { + dst[i] = src[i]; + src[i] = 0.0; + } } sound_position = (sound_position + samples_elapsed * sound_num_channels) % sound_max_num_frames; |