diff options
Diffstat (limited to 'reduced_system_layer.c')
-rwxr-xr-x | reduced_system_layer.c | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c index e65653e..c272711 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -163,7 +163,7 @@ enum { AUDIO_NUM_CHANNELS = 2, AUDIO_SAMPLE_RATE = 44100, - AUDIO_AVAIL_MIN = 64, + AUDIO_AVAIL_MIN = 256, MAX_NUM_AUDIO_FRAMES = 10 * AUDIO_SAMPLE_RATE * AUDIO_NUM_CHANNELS, // 10 seconds @@ -940,25 +940,25 @@ void p_handle_audio(i64 samples_elapsed) { i32 s; if (num_frames <= MAX_NUM_AUDIO_FRAMES - _sound_position) { - s = snd_pcm_writei(_sound_out, _sound_ring + _sound_position, num_frames); + s = snd_pcm_writei(_sound_out, _sound_ring + _sound_position, num_frames / AUDIO_NUM_CHANNELS); if (s < 0) 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); + memset(_sound_ring + _sound_position, 0, num_frames * sizeof *_sound_ring); } else { i64 part_one = MAX_NUM_AUDIO_FRAMES - _sound_position; - i64 part_two = num_frames - part_one; + i64 part_two = num_frames - part_one; - s = snd_pcm_writei(_sound_out, _sound_ring + _sound_position, part_one); + s = snd_pcm_writei(_sound_out, _sound_ring + _sound_position, part_one / AUDIO_NUM_CHANNELS); if (s < 0) fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s)); - s = snd_pcm_writei(_sound_out, _sound_ring, part_two); + s = snd_pcm_writei(_sound_out, _sound_ring, part_two / AUDIO_NUM_CHANNELS); if (s < 0) fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s)); - memset(_sound_ring + _sound_position, 0, part_one); - memset(_sound_ring, 0, part_two); + memset(_sound_ring + _sound_position, 0, part_one * sizeof *_sound_ring); + memset(_sound_ring, 0, part_two * sizeof *_sound_ring); } _sound_position = (_sound_position + num_frames) % MAX_NUM_AUDIO_FRAMES; @@ -971,6 +971,7 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { return; if (delay_in_samples < 0) { + frames += -delay_in_samples * AUDIO_NUM_CHANNELS; num_samples -= delay_in_samples; delay_in_samples = 0; } @@ -990,13 +991,16 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { i64 begin = (_sound_position + delay_in_samples) % MAX_NUM_AUDIO_FRAMES; if (num_frames <= MAX_NUM_AUDIO_FRAMES - begin) - memcpy(_sound_ring + begin, frames, num_frames * sizeof *frames); + for (i64 i = 0; i < num_frames; ++i) + _sound_ring[begin + i] += frames[i]; else { i64 part_one = MAX_NUM_AUDIO_FRAMES - begin; i64 part_two = num_frames - part_one; - memcpy(_sound_ring + begin, frames, part_one * sizeof *frames); - memcpy(_sound_ring, frames + part_one, part_two * sizeof *frames); + for (i64 i = 0; i < part_one; ++i) + _sound_ring[begin + i] += frames[i]; + for (i64 i = 0; i < part_two; ++i) + _sound_ring[i] += frames[part_one + i]; } } @@ -1563,6 +1567,18 @@ static i32 _num_events = 0; static i32 _input_size = 0; static b8 _key_pressed[MAX_NUM_KEYS] = {0}; +i32 p_time_impl(void); + +i64 p_time(void) { + return p_time_impl(); +} + +void p_sleep_for_impl(i32 duration); + +void p_sleep_for(i64 duration) { + p_sleep_for_impl((i32) duration); +} + i32 p_handle_events(void) { platform.input_size = _input_size; _input_size = 0; @@ -1689,15 +1705,15 @@ __attribute__((export_name("js_keyup"))) void js_keyup(u32 key, u32 mod) { i64 _sound_position = 0; f32 _sound_ring[MAX_NUM_AUDIO_FRAMES] = {0}; -void p_handle_audio_impl(i64 samples_elapsed); +void p_handle_audio_impl(i32 samples_elapsed); void p_handle_audio(i64 samples_elapsed) { if (samples_elapsed <= 0) return; - _sound_position = (_sound_position + samples_elapsed * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES; - p_handle_audio_impl(samples_elapsed); + + _sound_position = (_sound_position + samples_elapsed * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES; } void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { @@ -1705,6 +1721,7 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { return; if (delay_in_samples < 0) { + frames += -delay_in_samples * AUDIO_NUM_CHANNELS; num_samples -= delay_in_samples; delay_in_samples = 0; } @@ -1716,19 +1733,19 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { if (num_frames > MAX_NUM_AUDIO_FRAMES) return; - i64 begin = (_sound_position + delay_in_samples) % MAX_NUM_AUDIO_FRAMES; + i64 begin = (_sound_position + AUDIO_AVAIL_MIN + delay_in_samples * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES; if (num_frames <= MAX_NUM_AUDIO_FRAMES - begin) for (i64 i = 0; i < num_frames; ++i) - _sound_ring[begin + i] = frames[i]; + _sound_ring[begin + i] += frames[i]; else { i64 part_one = MAX_NUM_AUDIO_FRAMES - begin; - i64 part_two = num_frames - part_one; + i64 part_two = num_frames - part_one; for (i64 i = 0; i < part_one; ++i) - _sound_ring[begin + i] = frames[i]; + _sound_ring[begin + i] += frames[i]; for (i64 i = 0; i < part_two; ++i) - _sound_ring[i] = frames[part_one + i]; + _sound_ring[i] += frames[part_one + i]; } } @@ -1748,8 +1765,8 @@ __attribute__((export_name("js_sound_buffer"))) void *js_sound_buffer(void) { return _sound_ring; } -__attribute__((export_name("js_sound_sync"))) i64 js_sound_sync(void) { - _sound_position = AUDIO_AVAIL_MIN % MAX_NUM_AUDIO_FRAMES; +__attribute__((export_name("js_sound_sync"))) void js_sound_sync(void) { + _sound_position = 0; for (i64 i = 0; i < MAX_NUM_AUDIO_FRAMES; ++i) _sound_ring[i] = 0.f; } |