Development: Formats/XM

From OpenMPT Wiki
Jump to navigation Jump to search

General information concerning the XM file format.

Tracker IDs[edit]

Tracker should indicate their name in the XM header at offset 38.

Detecting other trackers[edit]

Some trackers disguise as FastTracker 2, but there are ways to uncover this disguise.

One important tool for this is the "reserved" field right before the sample name, which is actually the string length of the sample name (like in Pascal strings). If the value is greater than 22, or any bytes in the sample name after this string length are not space characters, the file must have been written with a program other than FT2.

ModPlug Tracker (general)[edit]

ModPlug Tracker uses zero-padded strings rather than space-padded strings for sample, instrument and song names. Note that FT2 does not sanitize some fields, so it is possible that a file made with e.g. ModPlug Tracker and then re-saved in FT2 will still be detected as file made with MPT.

ModPlug Tracker 1.0 alpha / beta[edit]

Tracker ID is "FastTracker v 2.00" (mind the extra space). Instrument header size is either 245 (MPT 1.0 alpha / beta 1) or 263 (MPT 1.0 beta 2 onwards).

ModPlug Tracker 1.0 - 1.11[edit]

For ping-pong-looped samples, both "normal loop" and "ping-pong loop" flags are set, while only the latter flag should be set.

DigiTrakker[edit]

DigiTrakker stores the artist name in the tracker ID. This makes it difficult to detect DigiTrakker reliably. However, if the user did not customize this field, the tracker ID often starts with "*Converted ", making it somewhat easier to detect. Instrument header size is 29.

PlayerPRO[edit]

The song title is null-terminated (if there is room for it), but after the null terminator, the buffer is filled with space characters.

The reserved field in the sample headers right before the sample name contains uninitialized memory - It may contain a random value, but it should always be the same random value between all sample slots. Instrument and sample names are null-terminated. Sample panning is always 128. Song restart position is not supported.

In older versions, the sample header size field also contains uninitialized garbage if the instrument contains no samples. This garbage usually differs between instruments, and it will happen to any instruments without samples. In contrast, Fasttracker 2 has a similar bug it but it only happens for the instrument slots before the first instrument that has any samples associated, and the garbage value will always be the same.

Newer versions of PlayerPRO set the envelope loop start and end to 255 if the envelope is disabled. Older versions do not appear to do that. This may help telling PlayerPRO apart from ModPlug Tracker.

Header size fields[edit]

The main header size is variable and can be shortened to not store the entire order list (256 bytes), but some less compatible players (e.g. WinAmp 5.666 and older) will choke on this. Thus, the most compatible way is to always write the entire order list.

The instrument header size (first field of the instrument header) must be respected, otherwise some files will not load correctly. If the size in the file is smaller than the actual header size, pad it with zeros. However, the sample header size is completely ignored by FastTracker 2. Early versions of Sk@leTracker wrote 0 in this field, and there are files in the wild with other, completely arbitrary values (e.g. cybernostra weekend.xm). Hence, ignore this value completely.

The mysterious "reserved" byte[edit]

In each sample header, there is a "reserved" byte right before the sample name. In fact, FT2 stores the sample name as a Pascal string, so the byte before the name tells the length of the string. FT2 always writes space-padded strings, but if the user manually adds space characters at the end of the sample name, they will count towards the string length, so it's not possible to simply compare the length byte against the string length after trimming space characters. Instead, we can probably be sure that a file was not made with FT2 if the actual string length is longer than the length indicated by the reserved byte, or if the reserved value is larger than 22.

ADPCM samples[edit]

The aforementioned reserved byte doubles as an indication for ModPlug 4-bit ADPCM samples: If its value is 0xAD and neither the 16-bit nor stereo flags are set, the sample uses ADPCM compression.

Stereo samples[edit]

ModPlug Tracker added support for stereo samples to the XM format, which is not part of the original format specifications. The sample data is stored like in most other module formats with stereo sample support: First the complete left channel, followed by the complete right channel. As with 16-bit samples, the length, loop start and loop length fields are doubled. Bit 5 of the sample flags (0x20) indicates the fact that it is a stereo sample.

Vorbis samples[edit]

The OggMod tool can be used to reduce the size of XM files to be used with the FMOD library by converting samples to Ogg Vorbis. OggMod does not change the file other than converting the sample data, so it is suggested to thoroughly validate the Vorbis stream before deciding whether to decode Vorbis samples or not (to avoid the chance that a random PCM-encoded sample with "OggS" magic bytes in the right place will be mis-detected). Vorbis samples are determined by a 32-bit field indicating the decoded sample size in bytes, directly followed by the Ogg Vorbis stream.

Note that the original OggMod tool is not aware of stereo samples, but it also does not clear the stereo flag from such samples. As a result, such samples will be encoded as mono Vorbis streams, but the stereo flag is still set. As it is difficult to properly recover the right channel after this conversion (the delta PCM encoding is extremely susceptible to distortion even if the Vorbis sample is off by just ±1), it may be a good idea to completely truncate the right channel and just import the first half of the Vorbis stream as a mono sample.

OpenMPT Hacks[edit]

For a list of all the file format hacks that ModPlug Tracker / OpenMPT added to the XM format over the years, see the OpenMPT Format Extensions article.