Sound Module
The sound module provides audio playback functionality using Rust's rodio and cpal crates.
Requires: a Rust toolchain (cargo) to build it, and an ALSA sound card to play. Loading, decoding, handle bookkeeping and the error paths work without a device; playback simply reports "did not start".make testLynxerskipsstdlib_sound.lynx, with a printed message, on hosts without/dev/snd/controlC*.
Functions
loadSound(path: string) -> intLoads an audio file (WAV, OGG, MP3 or FLAC) into memory: it is decoded here, so playback no longer needs the file and the length is known immediately. Returns a stable handle, or-1on failure.
loadSoundStreaming(path: string) -> intLoads a streaming audio file: only the header is read now and the file is decoded as it plays, which keeps a large file out of memory but means the file must still be there at play time. Returns a stable handle, or-1on failure.
playSound(handle: int) -> boolPlays a loaded sound once. Returnstruewhen playback starts.
playSoundOnce(handle: int) -> boolAlias forplaySound.
loopSound(handle: int) -> boolStarts looping playback. Returnstruewhen playback starts.
stopSound(handle: int) -> boolStops playback. Returnstruewhen the handle was valid.
pauseSound(handle: int) -> boolPauses an active player. Returnsfalsewhen the handle is invalid.
resumeSound(handle: int) -> boolResumes a paused player. Returnsfalsewhen the handle is invalid.
setSoundVolume(handle: int, volume: float) -> boolSets the volume, clamped to[0, 1]. Returnstrueon success.
isSoundPlaying(handle: int) -> boolReturnstrueif an active player is currently playing.
getSoundLength(handle: int) -> floatReturns the duration in seconds, or0.0for an invalid handle. The duration is resolved once, when the handle is created, so this never re-reads the file — it still answers after the file has been moved or deleted.
releaseSound(handle: int) -> boolReleases a loaded sound and invalidates its handle. Returnstrueon success.
soundCount() -> intReturns the number of currently loaded sound handles.
Example
import("sound")
global main(){
int music = global.sound.loadSound("theme.wav");
if(music >= 0){
global.sound.setSoundVolume(music, 0.5);
global.sound.loopSound(music);
println(global.sound.getSoundLength(music));
global.sound.stopSound(music);
global.sound.releaseSound(music);
}
}