D.10 Audio Intrinsics
Samples are signed 16-bit integers. Browsers keep audio silent until the page receives a key press or a click; until then a small "Click to Enable Audio" button shows.
Queue (recommended for sound effects and music). The program pushes samples from its own memory whenever it has them; they play in order.
| Intrinsic | Args | Description |
|---|---|---|
__audio_queue_init__(sample_rate channels) | 2 | Start the queue (about 2 s of capacity); returns 0, or -1 when audio is unavailable |
__audio_queue_push__(ptr n_bytes) | 2 | Queue n_bytes of interleaved samples (one per channel per frame); returns the bytes taken |
__audio_queue_size__() | 0 | Bytes queued and not yet played |
Samples pushed while audio is still locked are dropped, so they do not all sound at once when it unlocks. A short effect pushed when nothing is queued starts within about 50 ms.
One shot. __audio_init__(sample_rate n_samples channels) returns a buffer for n_samples samples per channel (interleaved); fill it, then __audio_play__() plays it once. __audio_sample_rate__() returns the rate.
Stream (mono). __audio_stream_init__(sample_rate buffer_size) returns a buffer of 8192 samples; while __audio_stream_needs_samples__() is true, fill the buffer and call __audio_stream_write__(), which returns the next buffer to fill. The runtime calls main() each animation frame in stream mode.
All of these buffers lie past the module's memory, like the canvas buffer.