An XM file only says what to play; the sound depends on how a program steps through it.
The explorer's replayer is a port of the one in 8bitbubsy's FastTracker 2 clone
(ft2_replayer.c), which reproduces FastTracker 2 2.08 and 2.09, bugs included,
with the clone's mixer reduced to linear interpolation. This page describes what it does;
the commands themselves are on the effects
page.
Ticks, rows and tempo
Playback runs in ticks. A row lasts speed ticks (the header's default tempo, 6, changed by Fxx with xx below 32). A tick lasts 2.5 / BPM seconds (the header's default BPM, 125, changed by Fxx with xx of 32 or more), so at 125 BPM a tick is 20 ms and a row at speed 6 is 120 ms. The replayer makes each tick a whole number of output frames, floor(2.5 × rate / BPM), which is what FT2 and OpenMPT do (OpenMPT's classic tempo mode); at 44,100 Hz and 118 BPM a tick is 934 frames, though it is 934.3 exactly. A new tempo takes effect on the tick that sets it.
On each tick the replayer first does the work of that tick for every channel, from channel 1 to the last, then the voices are updated and the tick's frames are mixed. On the first tick of a row the cells are read (volume column, then effect column); on the other ticks the continuing effects run. Envelopes, fadeout and auto-vibrato run on every tick.
Order and rows
- After the last tick of a row the next row is chosen. Normally it is the row after, and when the pattern ends, row 0 of the next position of the order list.
- A Dxx (pattern break) goes to the next position at row xx (decimal; above 63 means row 0). A Bxx (position jump) goes to position xx at row 0, or at the row of a Dxx on the same row. A position past the end starts at position 0.
- E60 and E6x (pattern loop) go back to the remembered row. FT2 keeps the loop's row when the pattern changes, so the next pattern starts at that row unless a Bxx or Dxx reset it (see the extended commands). A row number beyond the next pattern's length becomes 0.
- EEx holds the row for x more passes. The cells are read once; on the held passes only the continuing ("other tick") effects run.
- When the last position has played, the song goes on at the restart position (the header gives it). The explorer measures and plays the song up to that point once.
- F00 sets the speed to 0, and FT2 then stands still; the explorer stops there.
- A song that never reaches the end (a Bxx back to a row already played, or an endless pattern loop) is cut when it jumps back to a row it has already played, or after an hour.
Reading a row
For each channel the cell is handled in this order. The volume column and the effect column both run in the same tick; the rules are those of the clone.
- The volume column byte is stored for the later ticks.
- If the previous row had a vibrato or arpeggio that this row does not continue, the period goes back to the note's period.
- An instrument number from 1 to 128 selects the channel's instrument (an invalid one is ignored).
- A note delay (ED1 to EDF) stops here: nothing else is done on the first tick.
- Unless the effect is E90: with a tone portamento (3xx, 5xy or a volume column Fx), the note sets the target period and is not played, and an instrument number resets the volume and panning and the envelopes (not the sound). With K00 the note is released. Otherwise, with no note, an instrument number resets the volume, panning and envelopes; the sound goes on.
- With a note (1 to 96) the sample is chosen from the instrument's map; the sample's default volume and panning are loaded; the period is set from the note, the relative note and the finetune (E5x replaces it); the voice is started (at the offset a 9xx gives); the vibrato and tremolo positions, the retrigger counter and the key-off flag are reset; the volume and panning envelopes restart, the fadeout is reset to 32768 and the auto-vibrato starts.
- A note of 97 releases the note.
- An instrument number with a note also loads the sample's default volume and panning; without one, the volume and panning of the sound playing are reset to the new instrument's.
- The volume column and effect column then run their "first tick" part.
The format description's table of events (from the xmp documents) agrees with this, with the two cases it calls "Switch" and "Play":
| Event | No instrument | Same instrument as before | Another valid instrument | Invalid instrument |
|---|---|---|---|---|
| New note | Switch: a new note at the current volume | Play: a new note at the sample's volume | Play | Cut (but if there was no instrument yet, Switch) |
| New instrument, no note | (none) | Old volume: the default volume is set, the sound goes on | Old volume | Old volume |
| Tone portamento | Continue | Old volume | Old volume | Old volume |
Volume and panning
The channel volume (0 to 64) is multiplied by the global volume (0 to 64), the fadeout (32768 down to 0) and the volume envelope (0 to 64) to give a value from 0 to 1. The panning (0 to 255) is the channel's, moved by the panning envelope: the envelope's distance from 32 is scaled by how far the panning is from the middle. The left side gets the square root of (256 − pan) / 256 and the right side the square root of pan / 256.
When a voice's volume changes it is ramped to avoid clicks: over the length of a tick for a slide or an envelope step, and over 5 ms (rate / 200 frames) when a note starts, the volume is set, or a note is cut. A note that replaces another one fades the old one out over 5 ms while the new one fades in from zero. At the start of every tick the previous ramp is taken as finished.
Envelopes
Each channel keeps a tick counter and a value for each envelope. The counter restarts (at 65535, so that it becomes 0 on the first step) when a note or an instrument number starts, and Lxx sets it. Each tick:
- The counter goes up by one.
- If it equals the tick of the current point, the value becomes that point's (value × 256); the next point is chosen; if the loop is on and the point is the loop end the envelope goes back to the loop start (unless it is the sustain point and the note has been released); a sustain point holds the envelope there while the note is held; the slope to the next point is ((next − this) × 256) / (ticks between), in whole steps; if the ticks between two points are 0 the slope is 0.
- Otherwise the value moves by the slope. A value that leaves 0 to 64 is cut to the end of the range.
A key off moves the counter back to the tick of the current point, so a released envelope carries on from the sustain point. A key off on an instrument with its volume envelope off sets the volume to 0 at once. An envelope that reaches its last point stays there. A panning envelope uses the same steps but works on the panning, and (an FT2 bug) the key off adjustment above is made when the panning envelope is off.
Fadeout
After a key off the fadeout volume (32768 when the note started) is reduced by the instrument's fadeout value each tick, down to 0, and the channel's volume follows it. It is part of the volume formula above, so it only matters while the volume envelope is on: with it off, a key off silences the note at once.
Auto-vibrato
Each tick, when the instrument's depth is not 0, a position runs on by the rate (0 to 255, wrapping) and the period is changed by (wave × amplitude) / 16384, with the wave from −64 to 64 (a sine table, a square of ±64, or two ramps) and the amplitude the depth × 256 once the sweep has finished. During the sweep the amplitude grows by depth × 256 / sweep each tick until its top byte exceeds the depth, and stops growing at a key off. The result is added to the period for that tick only; a result above 31,999 plays at 0 Hz.
Vibrato and tremolo
The vibrato position goes up by the speed each tick (wrapping at 256). The wave value comes from the position divided by 4, within 0 to 31: the sine is a table of 32 values (0, 24, 49, 74, 97, 120, 141, 161, 180, 197, 212, 224, 235, 244, 250, 253, 255, then back down); the ramp is the index times 8 (inverted for the second half); the square is 255. The value is multiplied by the depth and divided by 32 and added to the period in the first half of the cycle (positions 0 to 127) and subtracted in the second. Tremolo is the same on the volume, divided by 64. E4x and E7x choose the waveform.
Periods, frequencies and sample steps
The pitch of a channel is a period. For a note it comes from a table indexed by the note (plus the relative note) and the finetune (frequency tables). Portamentos, vibrato and arpeggio change the period. The voice's step through the sample (in sample points per output frame) is the frequency divided by the output rate. In the linear table the frequency is 8363 × 2(4608 − period) / 768 Hz; in the Amiga table 8363 × 1712 / period. A period of 0 plays at 0 Hz.
Mixing
Each voice reads its sample at its step, interpolating between two neighbouring points. At the end of a forward loop the next point is the loop's first; the sample after the end of a ping-pong loop is the loop's last point, and the position reflects. A sample without a loop stops at its end, and its last point is what the interpolation sees after it. A stereo sample (MilkyTracker, OpenMPT; FastTracker 2 has none) keeps its two channels, as OpenMPT plays it: the left channel goes to the left output and the right channel to the right, each with the voice's gain for that side, which is how OpenMPT mixes it (FT2 itself has no stereo samples; the FT2 clone mixes them down to the mean of the two). The voices are added, scaled by 12/32 (FastTracker 2's amplification of 12) and clipped to ±1. Sample start positions beyond the sample's end silence the voice.
The explorer renders at 44,100 Hz, or 32,000 or 22,050 Hz for a very long song, and cuts a song that would not fit in memory; the title of the Song section says so.
Where players differ
FastTracker 2's behaviour is not specified anywhere but its own code, and other players decide differently in the edge cases:
- Global volume (Gxx) reaches the channels to the right of it on the same row at once, and the ones to the left of it only when their volume is next recalculated. OpenMPT applies it to all channels at once.
- Empty patterns have 64 rows in FT2 and the clone; the explorer, OpenMPT and libxmp use the row count in the header.
- Loops outside a sample (loop start plus length past the end) are turned off by the clone and cut at the end of the sample by OpenMPT.
- Stereo samples are mixed down in the clone; OpenMPT and libxmp play them in stereo, and so does the explorer.
- Notes started mid-tick: OpenMPT starts a note a few frames later than the clone does (its ramp and tick boundaries differ); this shows as a shift of about three frames when two renders are compared.
- Volume ramping and interpolation differ in detail: FT2 and the clone ramp over 5 ms and a tick as above, and the clone offers linear, cubic and sinc interpolation, where this replayer has linear only.
- OpenMPT only: the explorer plays OpenMPT's pitch and filter envelopes, resonant filter and Zxx macros, pitch/pan separation, swings, volume ramp and volume fields (see below). It does not play the sound of mix plug-ins or instrument-level resampling modes.
OpenMPT's extension fields in the replay
An XM saved by OpenMPT can carry instrument
fields that FastTracker 2 has no place for. libopenmpt reads them for XM files; what the explorer
applies, and what libopenmpt does with the rest, from soundlib/Sndmix.cpp,
Snd_fx.cpp and InstrumentExtensions.cpp in the 0.8.9 source:
- Pitch envelope (
PiE.,PiP[,PiE[, the loop, sustain and flag fields; played). The envelope starts with every note. Each tick its value (0 to 64, interpolated linearly between points) is turned into a number from -256 to 256 byGetValueFromPosition(position, 512, 64) - 256, and the channel's period is multiplied by 2-l/192 for a positive value or 2l/192 for a negative one, with l the value's size limited to 255 (OpenMPT'sLinearSlideDownTableandLinearSlideUpTable, which equal 65536 × 2±l/192 rounded for all 256 entries). Because an XM's linear period counts 768 to the octave, this moves the pitch by far more than the 16 semitones the envelope is meant for: a full-scale point changes the period by a factor of 2.5, which is 3.5 octaves up or about 9 down. The position then advances as FT2's envelopes do: a loop jumps back when the position reaches the loop end's tick (not when it passes it), a sustain point holds until key off (unless the loop end is the sustain end and the key is off), and without either the position stops at the last point. An envelope marked as the filter envelope is not played. - Pitch/pan separation (
PPS.,PPC.; played): the panning moves by (note − centre) × separation ÷ 2, where OpenMPT's note is the XM note plus 12 and the centre counts from zero, clipped to the panning range. - New note actions (
NNA.,DCT.,DNA.): stored but not used. OpenMPT'sCheckNNAalways cuts the old note unless the module is IT, MPTM or MT2, so an XM never has more voices than channels. - Resonant filter and Zxx macros (played). Each channel has a cutoff (0 to 127, 127 is
open), a resonance (0 to 127) and a mode (low-pass or high-pass). They start at 127, 0 and low-pass; a note
with an instrument that has
IFC.,IFR.orFM..takes those, and a note starting with the cutoff below 127 restarts the filter. The filter is a two-pole filter run on the sample before its volume (SetupChannelFilterinSnd_flt.cpp, the mixing loop inIntMixer.h): the cutoff becomes 110 × 20.25 + cutoff ÷ 24 Hz (÷ 20 when the module header has the extended filter range bit, 0x1000), limited to 120 Hz to 20 kHz and to half the playing rate; the damping is 10-resonance × 24 ÷ 128 ÷ 20. The filter envelope (a pitch envelope marked as the filter envelope) scales the cutoff by (value + 256) ÷ 256, the value running from -256 to 256 (a note's own cutoff is the full 256). The Zxx command (effect 35) runs a macro on the first tick: a value below 0x80 runs SF0, a value from 0x80 up runs Z80 to ZFF with the whole value as its parameter. A macro is text of hex digits andz(the parameter); the internal messagesF0 F0 00 xx(cutoff),F0 F0 01 xx(resonance) andF0 F0 02 xx(filter mode, its high nibble) act, others would go to a plug-in. The macros are those of theMIDIchunk; a file without it gets OpenMPT's defaults (SF0 sets the cutoff, Z80 to Z8F the resonance in steps of 8), except a file OpenMPT takes to be FastTracker 2's own, which gets none: FT2 lets you type Zxx and does nothing. - Instrument volume ramp (
VR.., played): when a voice's volume rises, the ramp lasts VR ÷ 100000 seconds instead of FT2's 5 ms. - Volumes (played): the instrument's global volume
GV..(of 64) multiplies the voice volume, the song's default global volumeDGV.(of 256) sets the starting global volume, and the sample pre-ampSPA.scales the output (48 is the default, the level of the explorer's replay). - Random swings (
VS..,PS..,CS..,RS..; played with the formulas ofSnd_fx.cpp): each note adds a random amount to the volume, panning, cutoff or resonance. OpenMPT seeds its generator from the system, so two renders of one module differ and no render can be compared sample by sample; the explorer uses a fixed seed, so its replay is the same each time. - Not played, and listed under not described: mix plug-ins (the sound they make) and the per-instrument resampling mode.
How it was checked
The replayer's output was compared with libopenmpt 0.8.9's, built for the purpose (Specs/xm in
the research folder, run as openmpt123 --render with linear interpolation). Each
file was rendered by both for the first 30 seconds at 44,100 Hz; the comparison finds the shift of at most 8 frames
at which the two agree best, scales one by a single gain, and reports the signal-to-error ratio.
| Result over 97 real XM files (30 seconds each) | |
|---|---|
| Median signal-to-error ratio | 27 dB (correlation 0.999) |
| At 20 dB or better | 66 files |
| Below 10 dB | 15 files (correlation 0.85 to 0.95) |
| Best | 50 dB |
| Fitted gain | 0.92 to 1.0: the explorer's level matches libopenmpt's |
| Playing time | the explorer's and libopenmpt's agree to within 0.1 s on the files compared (306.0 s against 306.03 s for one 5-minute song) |
The files that agree worst are chip-style songs that use instruments with auto-vibrato, very short looped waves and tiny finetune offsets, where a difference of a fraction of a period unit builds up as drift over a long song and the error ratio falls although the two sound alike. The cause was not found; the clone's code was followed, and in one of them (KB_ML2) the match improved when the auto-vibrato was switched off in the comparison, so OpenMPT's auto-vibrato and the clone's may differ in some detail. libxmp's own player, rendered the same way, agrees with the explorer less well than libopenmpt does on the files tried. The libxmp and OpenMPT test modules for single FT2 behaviours were also rendered and differ from libopenmpt's output in places where OpenMPT does not copy FT2 (the ones listed above, such as the global volume order), and where a test module's signal is a steady level, so that a ratio of errors says little.
The newer parts were checked the same way, with small modules made for the purpose
(scripts/xm/make-oxm.mjs and make-ext.mjs in the research folder), and libopenmpt rebuilt with
its stb_vorbis decoder for the OggMod samples:
| Case | Result against libopenmpt 0.8.9 |
|---|---|
| OggMod sample, mono (672 points) and stereo (44,100 points) | samples within one 16-bit step of ffmpeg's decoding; replays at 7 dB (a 15 ms hi-hat sample, mostly edge effects) and 33.5 dB |
| A true stereo delta-coded sample (two different sines) | 31.7 dB; as a mono mix it could not agree |
| Pitch envelope with a loop and sustain | 19.5 to 22 dB |
| Pitch envelope running to its end (3.5 octaves up, then down to 0 Hz) | the frequency agrees to 0.03 % at the start and the zero crossings stay within 8 frames after 17,000; the waveforms drift apart in phase, so the signal-to-error ratio is low (0 to 10 dB) though the pitch tracks are the same |
| Resonant filter: Zxx cutoff and resonance macros (a sawtooth sample); default cutoff and resonance of the instrument; the filter envelope | 30.3 dB; 31.2 dB; 30.5 dB (the same sawtooth module without a filter agrees at 31.2 dB) |
| Instrument volume ramp of 20 ms | 36.2 dB |
| Instrument global volume, song default global volume, sample pre-amp | the level of libopenmpt's output is 0.500, 0.500 and 2.666 (128 ÷ 48) times the level without the field, and the explorer applies the same factors |
| Pitch/pan separation | left and right levels equal libopenmpt's to 0.1 % (13.57 and 35.92 against 13.58 and 35.92) |
FastTracker 2 itself: version 2.09 (the 1998 DOS release, FT2.EXE of 540,912 bytes from the
Internet Archive item demoscene_Fasttracker2-Triton) was downloaded. The file starts with a
small real-mode program that unpacks the rest ("Triton integrated 386 DOS-Extender version 2.12" is what it prints). It runs in
DOSBox (js-dos under Node) with a Sound Blaster 16, loads an XM and plays it, and the emulated sound card's output can be
captured. In that capture a 64-point sine at C-5 has a period of 336.04 samples where the clone's formula gives 337.49 (0.43 %
higher), which comes from the emulation or FT2's mixing rate and has not been explained. Comparing whole
renders needs that clock difference taken out, and that was not finished, so no behaviour on this site is checked against FT2's own
output; its code was not unpacked either. The replayer follows the FT2 clone's
source, which its author reports as matching FT2 2.08 and 2.09 exactly, and its results agree with
libopenmpt's on the real files above.