summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-11-23 04:16:19 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-11-23 04:16:19 +0100
commit820298fa61835c53694c4ec58d6c5328947ad4ca (patch)
tree8d6d933c8b8c5ea7d2220045c2ff23a973397ce1
parent533aa1816b2c90b3935504b489e03e6ef87038a6 (diff)
downloadreduced_system_layer-web_audio.zip
Fix web audio f32 arraysweb_audio
-rwxr-xr-xexamples/sinewave.c18
-rw-r--r--index.htm5
-rwxr-xr-xreduced_system_layer.c11
3 files changed, 21 insertions, 13 deletions
diff --git a/examples/sinewave.c b/examples/sinewave.c
index e45f71e..a1f19f8 100755
--- a/examples/sinewave.c
+++ b/examples/sinewave.c
@@ -49,12 +49,14 @@ b8 ui_button(f64 x, f64 y, f64 width, f64 height) {
}
void update_and_render_frame(void) {
- p_handle_events();
+ i32 num_events = p_handle_events();
- fill_rectangle(OP_SET, 0x202020, 0, 0, platform.frame_width, platform.frame_height);
+ if (num_events > 0) {
+ fill_rectangle(OP_SET, 0x202020, 0, 0, platform.frame_width, platform.frame_height);
- if (ui_button(100, 100, 200, 200))
- p_queue_sound(0, AUDIO_SAMPLE_RATE, frames);
+ if (ui_button(100, 100, 200, 200))
+ p_queue_sound(0, AUDIO_SAMPLE_RATE, frames);
+ }
i64 samples_elapsed = ((p_time() - time_0) * AUDIO_SAMPLE_RATE) / 1000 - audio_samples;
audio_samples += samples_elapsed;
@@ -73,17 +75,17 @@ i32 main(i32 argc, c8 **argv) {
.graceful_exit = 1,
};
- f64 frequency = 440. * 6;
+ f64 frequency = 440.;
for (i64 i = 0; i < AUDIO_SAMPLE_RATE; ++i) {
f64 t = ((f64) i) / AUDIO_SAMPLE_RATE;
- f64 x = sin(t * frequency);
+ f64 x = sin(t * (M_PI * 2.) * frequency);
if (t < .005)
x *= t / .005;
if (t > .1)
x *= (1. - t) / .9;
- frames[i * 2] = (f32) x * .5;
- frames[i * 2 + 1] = (f32) x * .5;
+ frames[i * 2] = (f32) (x * .5);
+ frames[i * 2 + 1] = (f32) (x * .5);
}
time_0 = p_time();
diff --git a/index.htm b/index.htm
index d4729bf..ded4780 100644
--- a/index.htm
+++ b/index.htm
@@ -194,7 +194,7 @@
let sound_max_num_frames;
let sound_shared_ring;
- let sound_position = 0;
+ let sound_position;
canvas = attrs.canvas;
context = canvas.getContext("2d");
@@ -219,6 +219,7 @@
sound_buffer_address = program.instance.exports.js_sound_buffer();
sound_shared_ring = new SharedArrayBuffer(sound_max_num_frames * 4);
+ sound_position = 0;
sound_node = new AudioWorkletNode(
sound_context,
@@ -290,7 +291,7 @@
}
let dst = new Float32Array(sound_shared_ring);
- let src = new Float32Array(memory.subarray(sound_buffer_address, sound_buffer_address + sound_max_num_frames * 4));
+ let src = new Float32Array(memory.buffer, sound_buffer_address, sound_max_num_frames);
if (num_frames <= sound_max_num_frames - sound_position)
for (let i = 0; i < num_frames; ++i) {
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index c272711..64b7cfb 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 = 256,
+ AUDIO_AVAIL_MIN = 512,
MAX_NUM_AUDIO_FRAMES = 10 * AUDIO_SAMPLE_RATE * AUDIO_NUM_CHANNELS, // 10 seconds
@@ -399,6 +399,10 @@ f64 atan2(f64 y, f64 x);
#ifndef NULL
#define NULL ((void *) 0)
#endif
+
+#ifndef M_PI
+#define M_PI 3.14159265358979323846
+#endif
#endif
#ifdef __cplusplus
@@ -1626,6 +1630,7 @@ __attribute__((export_name("js_main"))) void js_main(c8 *href) {
}
__attribute__((export_name("js_init"))) void js_init(void) {
+ ++_num_events;
platform.pixels = _buffer;
platform.input = _input;
platform.done = 1;
@@ -1733,7 +1738,7 @@ 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 + AUDIO_AVAIL_MIN + delay_in_samples * AUDIO_NUM_CHANNELS) % MAX_NUM_AUDIO_FRAMES;
+ i64 begin = (_sound_position + 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)
@@ -1766,7 +1771,7 @@ __attribute__((export_name("js_sound_buffer"))) void *js_sound_buffer(void) {
}
__attribute__((export_name("js_sound_sync"))) void js_sound_sync(void) {
- _sound_position = 0;
+ _sound_position = AUDIO_AVAIL_MIN;
for (i64 i = 0; i < MAX_NUM_AUDIO_FRAMES; ++i)
_sound_ring[i] = 0.f;
}