flix

0.81.0

Audio

Making noise.

The second of the two modules in src/ that touch Java, and the only one that touches the sound card. Everything above it deals in SoundCmd values.

The waveforms are synthesized here at startup rather than loaded from files: an arcade bleep is a swept square wave and a burst of noise, both of which are a few lines of arithmetic, and this keeps the repository free of audio assets.

Nothing here is allowed to take the game down. A machine with no sound card -- a CI runner, a headless server, a laptop with the output muted at the driver level -- raises an IllegalArgumentException from deep inside AudioSystem rather than the declared LineUnavailableException, so every entry point catches Exception and degrades to silence.

Type Aliases

type alias Voices = List[(SoundCmd, javax.sound.sampled.Clip)]§ Source

A pool of pre-opened clips, tagged with the sound each one plays.

Clip.start() returns immediately, which is what the animation thread needs, but a single clip cannot overlap with itself -- restarting it cuts the sound in flight. So each sound gets several voices and a trigger takes the first idle one.

Definitions

def close(voices: Voices): Unit \ IO§ Source

Releases every line. Safe to call when audio never opened.

def open(): Voices \ IO§ Source

Synthesizes every sound and opens its voices.

Returns an empty list if audio is unavailable, which every other function here treats as "play nothing".

def play(voices: Voices, cmd: SoundCmd): Unit \ IO§ Source

Triggers cmd on its first idle voice.

If every voice for that sound is still playing the trigger is dropped, which is ordinary game-audio voice stealing and keeps this O(pool size) with no allocation on the animation thread.