diff options
Diffstat (limited to 'reduced_system_layer.c')
-rwxr-xr-x | reduced_system_layer.c | 43 |
1 files changed, 38 insertions, 5 deletions
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__ |