Syncromesh/Audio
Local audio playback API with variable pitch, SFX clips, streamed music, spatial playback, and custom volume buses.
Provides process-local audio playback for scripts running in the orchestrator context and in sibling board runtimes. WAV clips can be preloaded for sound effects, OGG/MP3/FLAC assets can be streamed as music, playback pitch can be changed per track, and output can be routed through custom string-named volume buses.
Import #
import * as Audio from 'Syncromesh/Audio';Functions #
preload
Loads a clip from mounted assets and returns a clip id.
- Returns an existing clip id when
pathis already cached. - Intended for reusable sound effects. WAV is the primary supported clip format.
preload(path: string): Promise<number>play
Starts playback and returns an instance id.
pathOrClipIdcan be an asset path string or a clip id frompreload.volumeis clamped to[0, 1]and defaults to1.0.pitchis a playback frequency ratio:1is unchanged,2is one octave higher/twice as fast, and0.5is one octave lower/half speed. It defaults to1and is clamped to[0.01, 100].busdefaults to"default"and may be any non-empty string.loopdefaults tofalse.- Set
positionto enable spatial playback for a one-shot clip. listenerdefaults to{x:0,y:0}.minDistancedefaults to1,maxDistancedefaults to40, androlloffdefaults to1.
play(pathOrClipId: string | number, options?: { loop?: boolean; volume?: number; pitch?: number; bus?: string; position?: {x:number,y:number}; listener?: {x:number,y:number}; minDistance?: number; maxDistance?: number; rolloff?: number }): Promise<number>playMusic
Streams a music asset and returns a music instance id.
- Supports OGG/Vorbis, MP3, FLAC, and WAV through the SDL_mixer backend.
- Only one music stream is active at a time; starting another replaces the current stream.
pitchhas the same frequency-ratio semantics and[0.01, 100]range asplay.busdefaults to"default".
playMusic(path: string, options?: { loop?: boolean; volume?: number; pitch?: number; bus?: string }): Promise<number>setPitch
Changes an active sound-effect instance's pitch and playback speed.
pitchmust be finite and is clamped to[0.01, 100].- Rejects when
instanceIdis not an active sound-effect instance.
setPitch(instanceId: number, pitch: number): Promise<boolean>setMusicPitch
Changes the active music stream's pitch and playback speed.
pitchmust be finite and is clamped to[0.01, 100].- Rejects when no music stream is active.
setMusicPitch(pitch: number): Promise<boolean>stopMusic
Stops the current music stream.
- Rejects when no music stream is active.
stopMusic(): Promise<boolean>stop
Stops a specific playback instance.
- Resolves to
truewhen the instance existed and was stopped. - Rejects when the instance id is unknown.
stop(instanceId: number): Promise<boolean>stopAll
Stops all active playback instances.
- Always resolves to
true.
stopAll(): Promise<boolean>setListener
Updates the global listener position used by spatial emitters.
- Call this each frame (or on movement) from game code.
- Applies to active emitters and future spatial playback.
setListener(position: {x:number,y:number}): Promise<boolean>setEmitterPosition
Moves an active emitter instance in world space.
- Use with a looped
play(...)instance to create continuously moving emitters. - Rejects when
instanceIddoes not exist.
setEmitterPosition(instanceId: number, position: {x:number,y:number}): Promise<boolean>setBusVolume
Sets a custom bus volume and resolves the clamped value.
- Missing or empty bus names resolve to
"default". - Bus volume is multiplied by each instance volume and the master volume.
setBusVolume(busName: string, value: number): Promise<number>getBusVolume
Returns the current bus volume.
- Unknown buses default to
1.0.
getBusVolume(busName: string): Promise<number>setMasterVolume
Sets global output volume and resolves the clamped value.
- Master volume is applied to all active and future instances.
valuemust be finite; resulting volume is clamped to[0, 1].
setMasterVolume(value: number): Promise<number>getMasterVolume
Returns the current master volume.
getMasterVolume(): Promise<number>Example #
import * as Audio from 'Syncromesh/Audio';
const clipId = await Audio.preload('/rom/sfx/engine.wav');
const emitterId = await Audio.play(clipId, {
loop: true,
volume: 0.6,
pitch: 0.9,
position: { x: 0, y: 0 },
minDistance: 1,
maxDistance: 30,
rolloff: 1.2
});
// update these from your game loop
await Audio.setListener({ x: camera.x, y: camera.y });
await Audio.setEmitterPosition(emitterId, { x: entity.x, y: entity.y });
await Audio.setPitch(emitterId, 1.15);
// ... later
await Audio.stop(emitterId);