CSNG (File Format)

The CSNG format contains MIDI data. It appears in Metroid Prime 1 and 2. It is essentially MusyX's GameCube SNG music format, with a custom header.

Format
All offsets are relative to the start of the main header (after the custom header).

Overall, SNG functions similar to a Type-1 (multi-track) MIDI file, with (up to) 64 tracks merging their events into 16 General-MIDI sequencer channels. In addition to the multi-track structure, SNG supports storing MIDI regions as indexed patterns, so songs with repetitive refrains can save on memory by referencing patterns more than once.

Timings are represented in ticks. Unlike MIDI, the tick-resolution is fixed at 384 ticks per beat (e.g. 120 beats-per-minute works out to  ticks-per-second).

Custom Header
This 0x14-byte header isn't part of the MusyX format; it appears at the start of the file. After parsing this the rest of the file is copied into a buffer and then passed to the MusyX functions.

Region Info
The track index has offsets relating the first region info for each track.

There is a sequence of at least 2 region info structures populating each track, last one acting as terminator:

Region Data
Here begins a free-form blob of indexed region data. It starts with a variable-length u32 array of SNG offsets for each region, then the region data itself.

Region Commands
After the region data header, the actual playback commands begin. There are only 3 types of commands in SNG: note, control change, and program change.

Delta Time Encoding
Just like MIDI, each command starts with a delta time value telling the sequencer how many ticks to wait after the previous command. Unlike MIDI, Factor5 uses a fixed 16-bit value to store the delta times. In case of a delta time greater than 65535, no-op commands with a 65535 delta time are inserted into the command stream until the target time is attained.

In a theoretical Python streaming API, the delta time can be decoded with no-ops automatically consumed like so:

No-Op Command
When the two bytes following the delta-time are both zero, this is a no-op command.

As mentioned before, no-ops are useful for accumulating delta times greater than 65535.

Note Command
When the two bytes following the delta-time != 0xffff, and the high-bits of both bytes are unset, this is a note command.

Unlike MIDI, which has separate commands for note-on/note-off, SNG attaches a note length value to a note-on command, which is then able to track its own lifetime.

Control Change Command
When the two bytes following the delta-time != 0xffff, and the high-bits of both bytes are set, this is a control change command.

See the standard MIDI control numbers for details.

Program Change Command
When the two bytes following the delta-time != 0xffff, and the high-bit of the first byte is set, this is a program change command.

See the standard MIDI program numbers for details.

End Of Region
When the two bytes following the delta-time == 0xffff, this region has no more commands.

Continuous Pitch / Modulation Data
If the pitch or mod offsets in a region are non-zero, they point to a buffer of (delta-tick, delta-value) pairs. The delta times are encoded as unsigned 7/15 bit values and the delta values are signed 7/15-bit values. The values are promoted to 15-bit if they exceed their 7-bit range. In this case, the highest bit is set on the first byte in the pair (0x80). The decoder must track the absolute time and value, summing each consecutive update for the current time/values.

The byte sequence 0x80 0x00 is a special case that signals the end of the stream.

Similar to the command stream, when the delta time exceeds 15 bit range (>32767), extra pairs with a zeroed value delta are inserted.

Channel Map
This is a simple u8 table mapping 64 SNG tracks to 16 MIDI channels for instrument selection via the SongGroup MIDI-Setup.

Tempo Table
When the SNG has a non-zero tempo table offset, this song features tempo changes. The change events are simple absolute-tick / BPM pairs.