XSharp

Declarative level audio

Prefer the level manifest for a stage’s default music, or an ambient_generic entity for tracks controlled by entity I/O. Scripting remains a third option for conditional behavior. All three use the existing sound cache, WAV/FLAC/MP3 decoder and native audio loop manifests. They do not decode assets or probe sidecar files during gameplay.

Default music in the level manifest

The .xlevel ZIP’s level.json accepts an optional music object:

{
  "music": {
    "name": "Chill Penguin",
    "file": "OST/X1/12 - Chill Penguin.mp3",
    "stopTime": 83,
    "loopTime": 50.152
  }
}

This is the declaration shipped in the Penguin demo’s imported asset. The original geometry is unchanged. Its door script is also contained in the package; there is no Penguin-specific C# level class, and Lua does not select or start the default music.

file is relative to BaseEngine.SoundAssetsDirectory (normally Assets/Sounds). Rooted paths, traversal, empty components and unsupported extensions are rejected before loading. The asset can be supplied by the host’s registered asset packages, like other sounds. Omitting name generates a stable path-derived cache alias. An explicit name must not conflict with another file already in the host’s sound cache.

The loader precaches the asset during level loading. Ready starts it on the music channel (3), before the optional C# OnReady callback. Neither changing palette/resource state nor restoring a save state restarts it: audio-channel serialization restores the actual source/cursor/loop state. Respawning/reloading the level starts its normal music again.

stopTime and loopTime are optional seconds. With both omitted (default -1), the existing audio asset manifest supplies its sample-accurate loop/stop points. This is the preferred declaration for AudioExtractor’s native FLAC/WAV/MP3 assets. Explicit legacy time coordinates are supported for existing OST files. An explicit nonnegative loop must precede an explicit positive stop.

Sound emitter entity

Add an entity to entities/entities.json:

{
  "ClassName": "ambient_generic",
  "Properties": {
    "targetname": "stage_music",
    "name": "My Stage Track",
    "file": "Music/MyStage.flac",
    "channel": 3,
    "startSilent": true
  }
}

The loader declares/preloads its sound before gameplay and creates an AmbientGeneric. channel defaults to 3; valid shared channels are 0..7. stopTime / loopTime have the same meaning as the manifest fields. With startSilent: false (default), the first enabled controller update starts it. With true, it waits for an input. It is nonphysical and survives offscreen.

Inputs are PlaySound, StopSound and ToggleSound; the ordinary entity I/O system can address its target name. Active stores the last play/stop intent. A fresh spawn resets that intent; full save states serialize it and any pending startup, while the audio subsystem serializes the playback cursor. Stopping or removing an emitter only stops its own sound if that sound still occupies the channel; it never stops a replacement boss track.

This is the nonspatial XSharp subset of Valve’s ambient_generic, with the Source input names. It does not implement Source’s attenuation, pitch modulation or fade envelopes. XSharp’s normal shared-channel priority rules remain in effect. Disable prevents startup/play; StopSound remains available.

A C# level can create the same entity after explicitly precaching its sound:

engine.PrecacheSound("My Stage Track", @"Music\MyStage.flac");
AmbientGeneric music = engine.Entities.Create<AmbientGeneric>(new
{
    SoundName = "My Stage Track", Channel = 3, StartSilent = true
});
music.TargetName = "stage_music";
music.Spawn();

Script control

Use a deliberately restricted entity input capability:

engine.call("sound_input", "stage_music", "PlaySound")
engine.call("sound_input", "stage_music", "StopSound")

engine.call("play_music", name, stop, loop) is also available for explicitly precached assets. Neither call accepts an arbitrary path, CLR method or object. During script save-state reconstruction, recorded responses are replayed without calling these audio inputs again.