Audio
Clay backend audio implementation providing comprehensive sound management.
This class handles:
- Audio resource loading and caching with reference counting
- Sound playback control (play, pause, stop, loop)
- Audio properties manipulation (volume, pan, pitch, position)
- Streaming audio support for large files
- Real-time audio filters and effects processing
- Multi-bus audio system for grouping and mixing
- Web Audio context management for browser compatibility
- Platform-specific optimizations (native vs web)
The audio system uses Clay's underlying audio engine which supports:
- On web: Web Audio API with AudioWorklet support
- On native: Custom audio mixing with SDL audio backend
Audio resources are cached and reference counted to avoid redundant loading and ensure proper memory management.
Instance Members
Loads an audio resource from a file path or URL.
The method handles:
- Local file loading (relative to assets path)
- URL loading (http/https)
- Cached resource reuse with reference counting
- Asynchronous and synchronous loading modes
- Streaming mode for large audio files
Name | Type | Default | Description |
---|---|---|---|
path |
String | The path to the audio file (relative to assets or absolute/URL) | |
options |
Null<LoadAudioOptions> | (optional) | Optional loading configuration: - loadMethod: SYNC for synchronous loading, ASYNC (default) for asynchronous - stream: true to stream large files instead of loading entirely in memory - immediate: Custom immediate callback queue for synchronization |
_done |
Function | Callback invoked with the loaded resource or null on failure |
createFromSamplesBuffer(buffer: Float32Array, samples: Int, channels: Int, sampleRate: Float, interleaved: Bool): AudioResource
Creates an audio resource from raw PCM sample data.
This is useful for:
- Procedurally generated audio
- Audio synthesis
- Converting from other audio formats
Name | Type | Description |
---|---|---|
buffer |
Float32Array | The raw PCM samples as 32-bit floats |
samples |
Int | Number of sample frames (total samples / channels) |
channels |
Int | Number of audio channels (1 for mono, 2 for stereo) |
sampleRate |
Float | Sample rate in Hz (e.g., 44100, 48000) |
interleaved |
Bool | Whether samples are interleaved (LRLRLR) or planar (LLL...RRR...) |
Returns | Description |
---|---|
AudioResource | The created audio resource, or null on failure |
Gets the duration of an audio resource in seconds.
Name | Type | Description |
---|---|---|
resource |
AudioResource | The audio resource to query |
Returns | Description |
---|---|
Float | Duration in seconds, or 0 if unknown |
No-op on native platforms where audio context is always active.
Name | Type | Description |
---|---|---|
done |
Function | Always called with true |
Indicates whether this backend supports hot-reloading audio files.
Returns | Description |
---|---|
Bool | true (Clay backend supports audio hot-reload) |
Destroys an audio resource and frees associated memory.
Uses reference counting - the resource is only truly destroyed when all references are released.
Name | Type | Description |
---|---|---|
audio |
AudioResource | The audio resource to destroy |
Creates a muted audio handle (not implemented in Clay backend).
Name | Type | Description |
---|---|---|
audio |
AudioResource | The audio resource |
Returns | Description |
---|---|
AudioHandle | -1 (invalid handle) |
play(audio: AudioResource, ?volume: Float = 0.5, ?pan: Float = 0, ?pitch: Float = 1, ?position: Float = 0, ?loop: Bool = false, ?bus: Int = 0): AudioHandle
Plays an audio resource with specified parameters.
Name | Type | Default | Description |
---|---|---|---|
audio |
AudioResource | The audio resource to play | |
volume |
Float | 0.5 |
Playback volume (0.0 to 1.0, default 0.5) |
pan |
Float | 0 |
Stereo panning (-1.0 left to 1.0 right, 0 center) |
pitch |
Float | 1 |
Playback speed/pitch multiplier (1.0 = normal) |
position |
Float | 0 |
Start position in seconds |
loop |
Bool | false |
Whether to loop the audio |
bus |
Int | 0 |
Audio bus index for routing (0 = master) |
Returns | Description |
---|---|
AudioHandle | Handle for controlling this audio instance, or -1 if failed |
Pauses audio playback.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Resumes paused audio playback.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Stops audio playback and releases the handle.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Gets the current volume of an audio instance.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Returns | Description |
---|---|
Float | Volume level (0.0 to 1.0) |
Sets the volume of an audio instance.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
volume |
Float | New volume level (0.0 to 1.0) |
Gets the current stereo pan position.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Returns | Description |
---|---|
Float | Pan value (-1.0 left to 1.0 right, 0 center) |
Sets the stereo pan position. Note: Pan cannot be changed for streaming sounds.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
pan |
Float | New pan value (-1.0 left to 1.0 right, 0 center) |
Gets the current pitch/speed multiplier.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Returns | Description |
---|---|
Float | Pitch multiplier (1.0 = normal speed) |
Sets the pitch/speed multiplier. Note: Pitch cannot be changed for streaming sounds.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
pitch |
Float | New pitch multiplier (1.0 = normal, 2.0 = double speed) |
Gets the current playback position in seconds.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
Returns | Description |
---|---|
Float | Position in seconds from the start |
Sets the playback position (seeks to a time). Note: Position cannot be changed for streaming sounds.
Name | Type | Description |
---|---|---|
handle |
AudioHandle | The audio instance handle |
position |
Float | New position in seconds |
Adds an audio filter to a specific bus for real-time processing.
Filters are processed in the order they are added. The system supports:
- Multiple filters per bus
- Real-time parameter updates
- AudioWorklet processing on web
- Native filter processing on desktop/mobile
Name | Type | Description |
---|---|---|
bus |
Int | The audio bus to add the filter to (0 = master) |
filter |
ceramic.AudioFilter | The audio filter instance to add |
onReady |
Function | Callback when the filter is ready for processing |
Removes an audio filter from a bus.
Name | Type | Description |
---|---|---|
bus |
Int | The audio bus containing the filter |
filterId |
Int | The unique ID of the filter to remove |
Notifies the audio system that filter parameters have changed.
This triggers an update of the filter's processing parameters. On web, parameters are sent to the AudioWorklet. On native, parameters are marked dirty for the next process cycle.
Name | Type | Description |
---|---|---|
bus |
Int | The audio bus containing the filter |
filterId |
Int | The unique ID of the filter that changed |
Creates a new Audio backend instance.
Private Members
Global lock for thread-safe filter operations
Per-bus locks for fine-grained thread safety
Active filters indexed by bus number
Tracks which buses have active filters
Callbacks waiting for bus filter creation
Callbacks to execute after worklet synchronization
Callbacks for audio resources currently loading
Cached audio resources indexed by path
Reference count for each loaded audio resource