Audio

Pyxen supports two kinds of audio: sounds for short effects and music for longer tracks. Both are attached to entities as components.

Sounds

A Sound is a short audio clip — a jump, a hit, a coin pickup. You play a sound by spawning an entity with a Sound component:

world.spawn(sound=Sound("jump"))

The sound plays immediately. You can control playback properties:

world.spawn(sound=Sound("laser", loop=True, volume=0.5, pan=-0.5))
PropertyMeaning
loopWhether the sound repeats
volumeVolume (0.0 = silent, 1.0 = full)
panStereo position (-1.0 = left, 0.0 = center, 1.0 = right)

Sounds are loaded from audio files in your project assets.

The Sound Bank

The Sound workspace’s Sound Bank holds the project’s sound effects: ▶ plays one in place, and the line under the list is the code that plays the sound picked. Three buttons at its top add one:

Rest the pointer on a button to see its name, or touch and hold it on iPad. Where the pane is too narrow for the three, they are in one + Sound menu instead. An empty bank names the same three in words.

On a narrow iPad window, the SFX pane’s sound stays at the top with Play, and its controls are the tray’s tools: Presets, Shape, Pitch and Effects.

Used in Code in the bank’s bar lists the lines of your scripts that name its sounds; choose one to go to it. In the code, Cmd+Click a sound’s name, such as Sound("sfx/coin"), to open its bank with that sound picked.

Music

Music works like Sound but is designed for longer tracks. Music is streamed rather than loaded entirely into memory:

world.spawn(music=Music("theme", loop=True))

Music supports the same properties: loop, volume, and pan.

A track can also tell your game where it is. Tracker modules expose their parts and their beat, so a game can mute one part or pulse on every beat; see the Music reference for channels, beat, bar and tempo.

Audio as components

Because audio is attached to entities, you manage it the same way as everything else:

# start background music
bgm = world.spawn(name="bgm", music=Music("overworld", loop=True))

# stop it later
world.destroy(bgm)

This fits naturally with the ECS model — audio is just another component on an entity, visible in the frame inspector like everything else.

Recording a sound

On a Mac or an iPad with a microphone, open Record Sound in a Sound Bank to record a take of up to 10 seconds. Press the round button, or Space, to start, and again to stop.

Once it stops, the whole take is drawn, and Play beside its length plays it with a playhead; Space plays and stops it too. Add to Bank adds the take you heard as a new sound. The round button records a new take in place of this one, without asking; Space never does.

Supported formats

Add audio files to your project with File › Import Images or Sounds….

For the full API, see Sound and Music.