summaryrefslogtreecommitdiff
path: root/reduced_system_layer.c
diff options
context:
space:
mode:
Diffstat (limited to 'reduced_system_layer.c')
-rwxr-xr-xreduced_system_layer.c59
1 files changed, 35 insertions, 24 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 17956b3..e65653e 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -1686,19 +1686,50 @@ __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_impl(i64 samples_elapsed);
+
void p_handle_audio(i64 samples_elapsed) {
if (samples_elapsed <= 0)
return;
- _sound_write = (_sound_write + samples_elapsed * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES;
+ _sound_position = (_sound_position + samples_elapsed * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES;
+
+ p_handle_audio_impl(samples_elapsed);
}
void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) {
- // TODO
+ if (frames == NULL)
+ return;
+
+ if (delay_in_samples < 0) {
+ num_samples -= delay_in_samples;
+ delay_in_samples = 0;
+ }
+
+ if (num_samples <= 0)
+ return;
+
+ i64 num_frames = num_samples * AUDIO_NUM_CHANNELS;
+ if (num_frames > MAX_NUM_AUDIO_FRAMES)
+ return;
+
+ i64 begin = (_sound_position + delay_in_samples) % 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];
+ else {
+ i64 part_one = MAX_NUM_AUDIO_FRAMES - begin;
+ i64 part_two = num_frames - part_one;
+
+ 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];
+ }
}
__attribute__((export_name("js_sample_rate"))) f64 js_sample_rate(void) {
@@ -1718,31 +1749,11 @@ __attribute__((export_name("js_sound_buffer"))) void *js_sound_buffer(void) {
}
__attribute__((export_name("js_sound_sync"))) i64 js_sound_sync(void) {
- _sound_write = AUDIO_AVAIL_MIN;
- _sound_position = 0;
+ _sound_position = AUDIO_AVAIL_MIN % MAX_NUM_AUDIO_FRAMES;
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__
// ================================================================