Skip to content

AudioBuffer

1 min read

Import: import system.audio;

Read the audio guide for usage, limits, and current platform restrictions.

AudioBuffer

Immutable decoded interleaved PCM, independent of any output device.

public class AudioBuffer

decode

Detects and decodes WAV, MP3 or WebM/Opus bytes on a background thread (a decode worker on the web), so timers, input and audio playback keep running while a large file decodes. The input is copied when decoding starts; changing bytes afterwards has no effect. max_bytes limits the decoded 32-bit float PCM and must be between 4 bytes and 1 GiB. Errors: EINVAL for an invalid limit, ELIMIT when the audio exceeds the limit or has more than two channels, EFORMAT for invalid or unsupported input, ENOMEM when decoding memory runs out, EBUSY when no decoder thread is available, and ECANCELLED when token (or the ambient cancellation token) is cancelled before decoding ends.

public static async fun decode(
    bytes: byte[],
    max_bytes: int = 268435456,
    token: Option<CancellationToken> = Option.None
): Result<AudioBuffer, AudioError>

from_pcm

Copies finite interleaved mono/stereo PCM with complete frames and a positive sample rate.

public static fun from_pcm(
    samples: float[],
    sample_rate: int,
    channels: int
): Result<AudioBuffer, AudioError>

sample_rate

Samples per second per channel.

public get sample_rate(): int

channels

One for mono or two for stereo.

public get channels(): int

frame_count

Number of complete frames in this buffer.

public get frame_count(): int

duration

Total duration in seconds.

public get duration(): double

read_pcm

Copies a frame range; invalid or overflowing ranges return EINVAL.

public fun read_pcm(start_frame: int, frame_count: int): Result<float[], AudioError>