Skip to content

Development source guide · workspace 0.1.0

Sound effects and music

Audio has three separate tasks: create or obtain a sound, include its bytes in the game and trigger playback at the right time. Generating a file does not automatically attach it to an interaction or music state.

Generate one sound effect

From the working folder used in the tutorials:

AIGameEngine sfx ui-click --out ./first-game/audio --prefix first-game- --seed 1

Expected result. The command writes first-game/audio/first-game-ui-click.pcm and reports its byte count, duration and rate. This form was checked against the source catalog; it was not executed for this guide.

The sound-effect generator renders DSP recipes directly. This generic operation does not require the external music-rendering tools. The seed controls recipe variation, while the prefix controls output naming and can select a special catalog for particular game integrations. Keep a neutral project prefix unless you deliberately want such a catalog.

Warning. Generation writes its destination files. Use a new output name or preserve earlier versions before rerunning. The command is not an asset-version manager.

To see valid IDs when an identifier is rejected, read the error's available-name list. all selects a catalog batch, not a single sound; start with one known recipe before creating many files.

Interpret PCM correctly

The generated .pcm file contains raw little-endian 16-bit samples, not a WAV container. The PCM playback helper takes both bytes and a sample rate. The standard PCM rate is 22050 Hz; the mixer output is 48000 Hz stereo.

Keep the generation report or recipe information with your asset notes so the game uses the correct interpretation. Playing samples at the wrong rate changes pitch and duration. Renaming a file extension does not resample it or remove a container header.

Put the file under a packed directory, read it through pack::read and construct the appropriate clip in game code. The game must then trigger playback in response to the actual action. No generic “play this file whenever a button is drawn” behavior is implied.

Generate a short music cue

Music generation requires runnable fluidsynth and ffmpeg with the required Opus encoding support, plus a usable soundfont. Choose a soundfont you have permission to use.

With an existing soundfont file at the example relative location:

AIGameEngine music sting-victory --out ./first-game/audio --prefix first-game- --soundfont ./soundfonts/example.sf2

Expected result. The output is first-game/audio/first-game-sting-victory.opus, with a report of its size and duration. Replace the example soundfont path with a real file; the command does not download one for you.

Without --soundfont, the renderer can try its configured/discovered soundfont. An unavailable font or external renderer is a prerequisite failure, not a reason to assume the CLI silently produced another format.

The music library contains short stingers, loops and longer pieces. Select a known piece ID or read the available-name error. Rendering and clean/lo-fi library APIs are explicit choices, not a promise that the CLI automatically falls back through every option.

Connect playback to the game

The application enables audio by default, but your game still chooses what to play and when. The audio layer supports effects, music transitions and stem controls through its actual API.

The implemented mix buses are:

Bus Intended separation
Master Overall mix
Sfx Sound effects
Music Music playback
Engine Engine/vehicle-related sound channel
Ui Interface audio

Bus volume inputs clamp to the range 0 to 1. A label in a design document does not create an additional built-in bus; if your game needs another policy or grouping, implement it deliberately.

The audio engine can open in silent mode when output is unavailable or AIGAME_AUDIO=off is selected. A silent-mode no-op is not proof that the file was decoded and heard. Music loading/playback can also report failure; check the actual result before updating the UI to claim music is playing.

Test one event end to end

  1. Generate or obtain one known clip.
  2. Confirm the key and format expected by the game's loader.
  3. Trigger it from one deliberate action, preferably one press rather than every update a key remains held.
  4. Check that it does not repeat unexpectedly.
  5. Check the intended bus and volume.
  6. Repeat in a release build with embedded assets.

If sound repeats rapidly, inspect the input edge and event trigger before changing the clip. If it is absent only in release, inspect packing.

Browser audio

Browser policy can suspend audio until a user gesture. Click or interact with the game before treating initial silence as a missing asset. The current web output includes gesture-resume handling and browser-specific worklet/fallback paths; older research notes describing web audio as entirely absent are not a reliable statement of the current source.

That implementation is not a guarantee for every embedded page or browser policy. Test sound in the intended hosted environment, including after focus changes. The native control harness is not a way to bypass browser gesture requirements.

Keep design sketches separate

The engine's audio design material discusses richer sound-event queues, instrument/envelope models and genre policies. Those descriptions are not automatically part of a new game. Build on the implemented recipe, playback and mixing interfaces, then document the extra game-specific behavior you actually connect.