torque-tracker-engine 0.1.0

Audio Backend for Torque Tracker, an old school music tracker
Documentation
taken from: https://github.com/schismtracker/schismtracker/wiki/ITTECH.TXT

Header Layout:
        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
      ┌───┬───┬───┬───┬───────────────────────────────────────────────┐
0000: │'I'│'M'│'P'│'M'│ Song Name, max 26 characters, includes NULL   │
      ├───┴───┴───┴───┴───────────────────────────────────────┬───────┤
0010: │.......................................................│PHiligt│
      ├───────┬───────┬───────┬───────┬───────┬───────┬───────┼───────┤
0020: │OrdNum │InsNum │SmpNum │PatNum │ Cwt/v │ Cmwt  │ Flags │Special│
      ├───┬───┼───┬───┼───┬───┼───────┼───────┴───────┼───────┴───────┤
0030: │GV │MV │IS │IT │Sep│PWD│MsgLgth│Message Offset │   Reserved    │
      ├───┴───┴───┴───┴───┴───┴───────┴───────────────┴───────────────┤
0040: │ Chnl Pan (64 bytes)...........................................│
      ├───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┤

      ├───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┤
0080: │ Chnl Vol (64 bytes)...........................................│
      ├───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┤

      ├───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┤
00C0: │ Orders, Length = OrdNum                                       │
      ├───────────────────────────────────────────────────────────────┤
xxxx: │ 'Long' Offset of instruments, Length = InsNum*4 (1)           │
      ├───────────────────────────────────────────────────────────────┤
xxxx: │ 'Long' Offset of samples headers, Length = SmpNum*4 (2)       │
      ├───────────────────────────────────────────────────────────────┤
xxxx: │ 'Long' Offset of patterns, Length = PatNum*4 (3)              │
      └───────────────────────────────────────────────────────────────┘

      (1) Offset = 00C0h+OrdNum
      (2) Offset = 00C0h+OrdNum+InsNum*4
      (3) Offset = 00C0h+OrdNum+InsNum*4+SmpNum*4

        Note that if the (long) offset to a pattern = 0, then the
        pattern is assumed to be a 64 row empty pattern.

      PHiliht = Pattern row hilight information. Only relevant for pattern
                editing situations.

      Cwt:      Created with tracker.
                 Impulse Tracker y.xx = 0yxxh
      Cmwt:     Compatible with tracker with version greater than value.
                 (ie. format version)
      OrdNum:   Number of orders in song.
      InsNum:   Number of instruments in song
      SmpNum:   Number of samples in song
      PatNum:   Number of patterns in song
      Flags:    Bit 0: On = Stereo, Off = Mono
                Bit 1: Vol0MixOptimizations - If on, no mixing occurs if
                       the volume at mixing time is 0 (redundant v1.04+)
                Bit 2: On = Use instruments, Off = Use samples.
                Bit 3: On = Linear slides, Off = Amiga slides.
                Bit 4: On = Old Effects, Off = IT Effects
                        Differences:
                       - Vibrato is updated EVERY frame in IT mode, whereas
                          it is updated every non-row frame in other formats.
                          Also, it is two times deeper with Old Effects ON
                       - Command Oxx will set the sample offset to the END
                         of a sample instead of ignoring the command under
                         old effects mode.
                       - (More to come, probably)
                Bit 5: On = Link Effect G's memory with Effect E/F. Also
                            Gxx with an instrument present will cause the
                            envelopes to be retriggered. If you change a
                            sample on a row with Gxx, it'll adjust the
                            frequency of the current note according to:

                              NewFrequency = OldFrequency * NewC5 / OldC5;
                Bit 6: Use MIDI pitch controller, Pitch depth given by PWD
                Bit 7: Request embedded MIDI configuration
                       (Coded this way to permit cross-version saving)

      Special:  Bit 0: On = song message attached.
                       Song message:
                        Stored at offset given by "Message Offset" field.
                        Length = MsgLgth.
                        NewLine = 0Dh (13 dec)
                        EndOfMsg = 0

                       Note: v1.04+ of IT may have song messages of up to
                             8000 bytes included.
                Bit 1: Reserved
                Bit 2: Reserved
                Bit 3: MIDI configuration embedded
                Bit 4-15: Reserved

      GV:       Global volume. (0->128) All volumes are adjusted by this
      MV:       Mix volume (0->128) During mixing, this value controls
                the magnitude of the wave being mixed.
      IS:       Initial Speed of song.
      IT:       Initial Tempo of song
      Sep:      Panning separation between channels (0->128, 128 is max sep.)
      PWD:      Pitch wheel depth for MIDI controllers
      Chnl Vol: Volume for each channel. Ranges from 0->64
      Chnl Pan: Each byte contains a panning value for a channel. Ranges from
                 0 (absolute left) to 64 (absolute right). 32 = central pan,
                 100 = Surround sound.
                 +128 = disabled channel (notes will not be played, but note
                                          that effects in muted channels are
                                          still processed)
      Orders:   This is the order in which the patterns are played.
                 Valid values are from 0->199.
                 255 = "---", End of song marker
                 254 = "+++", Skip to next order

Instrument Format:
        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
      ┌───┬───┬───┬───┬───────────────────────────────────────────────┐
0000: │'I'│'M'│'P'│'I'│ DOS FileName (12345678.123)                   │
      ├───┼───┼───┼───┼───────┬───┬───┬───┬───┬───┬───┬───────┬───┬───┤
0010: │00h│NNA│DCT│DCA│FadeOut│PPS│PPC│GbV│DfP│RV │RP │TrkVers│NoS│ x │
      ├───┴───┴───┴───┴───────┴───┴───┴───┴───┴───┴───┴───────┴───┴───┤
0020: │ Instrument Name, max 26 bytes, includes NUL...................│
      ├───────────────────────────────────────┬───┬───┬───┬───┬───────┤
0030: │.......................................│IFC│IFR│MCh│MPr│MIDIBnk│
      ├───────────────────────────────────────┴───┴───┴───┴───┴───────┤
0040: │ Note-Sample/Keyboard Table, Length = 240 bytes................│
      ├───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┤

      ├───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┤
0130: │ Envelopes.....................................................│
      ├───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┤

        IFC = Initial Filter cutoff
        IFR = Initial Filter resonance

        NNA = New Note Action
                0 = Cut                 1 = Continue
                2 = Note off            3 = Note fade

        DCT = Duplicate Check Type
                0 = Off                 1 = Note
                2 = Sample              3 = Instrument

        DCA: Duplicate Check Action
                0 = Cut
                1 = Note Off
                2 = Note fade

      FadeOut:  Ranges between 0 and 128, but the fadeout "Count" is 1024
                See the Last section on how this works.
                Fade applied when:
                1) Note fade NNA is selected and triggered (by another note)
                2) Note off NNA is selected with no volume envelope
                   or volume envelope loop
                3) Volume envelope end is reached

        PPS: Pitch-Pan separation, range -32 -> +32
        PPC: Pitch-Pan center: C-0 to B-9 represented as 0->119 inclusive

        GbV: Global Volume, 0->128
        DfP: Default Pan, 0->64, &128 => Don't use
        RV: Random volume variation (percentage)
        RP: Random panning variation (panning change - not implemented yet)

        MCh = MIDI Channel
        MPr = MIDI Program (Instrument)

      TrkVers:  Tracker version used to save the instrument. This is only
                used in the instrument files.
      NoS:      Number of samples associated with instrument. This is only
                used in the instrument files.

      Note-Sample/Keyboard Table.
       Each note of the instrument is first converted to a sample number
       and a note (C-0 -> B-9). These are stored as note/sample byte pairs
       (note first, range 0->119 for C-0 to B-9, sample ranges from
       1-99, 0=no sample)

Envelope Layout:
        Envelopes: 3 structures, first for volume (130h), second for
                   panning (182h), third for pitch (1D4h).

                   Each is structured as such:

        0   1   2   3   4   5   6.......
      ┌───┬───┬───┬───┬───┬───┬───────────────────────────────────┬───┐
xxxx: │Flg│Num│LpB│LpE│SLB│SLE│ Node points, 25 sets, 75 bytes....│ x │
      ├───┼───┼───┼───┼───┼───┼───┬───┬───┬───┬───┬───┬───┬───┬───┼───┤

        Flg: Bit 0: Envelope on/off, 1 = on, 0 = off
             Bit 1: Loop on/off, 1 = on, 0 = off
             Bit 2: SusLoop on/off, 1 = on, 0 = off

        For Pitch envelope only:
             Bit 7: Use pitch envelope as filter envelope instead.

        Num = Number of node points

        LpB = Loop beginning            SLB = Sustain loop beginning
        LpE = Loop end                  SLE = Sustain loop end

        Node point = 1 byte for y-value
                        (0->64 for vol, -32->+32 for panning or pitch)
                     1 word (2 bytes) for tick number (0->9999)

        Total length of an instrument is 547 bytes, but 554 bytes are
        written, just to simplify the loading of the old format. (Hence
        there are 7 'wasted' bytes per instrument)

Sample Header Layout:
        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
      ┌───┬───┬───┬───┬───────────────────────────────────────────────┐
0000: │'I'│'M'│'P'│'S'│ DOS Filename (12345678.123)                   │
      ├───┼───┼───┼───┼───────────────────────────────────────────────┤
0010: │00h│GvL│Flg│Vol│ Sample Name, max 26 bytes, includes NUL.......│
      ├───┴───┴───┴───┴───────────────────────────────────────┬───┬───┤
0020: │.......................................................│Cvt│DfP│
      ├───────────────┬───────────────┬───────────────┬───────┴───┴───┤
0030: │ Length        │ Loop Begin    │ Loop End      │ C5Speed       │
      ├───────────────┼───────────────┼───────────────┼───┬───┬───┬───┤
0040: │ SusLoop Begin │ SusLoop End   │ SamplePointer │ViS│ViD│ViR│ViT│
      └───────────────┴───────────────┴───────────────┴───┴───┴───┴───┘

The cache file has the following pieces of information added on:

        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
      ┌───────────────┬───────┬───────┬───┬───────────────────────────┐
0050: │ File Size     │ Date  │ Time  │Fmt│...........................│
      └───────────────┴───────┴───────┴───┴───────────────────────────┘

        Fmt. 0 = unchecked. 1 = directory, 2 = it sample, 3 = st sample

      GvL:      Global volume for instrument, ranges from 0->64
      Flg:      Bit 0. On = sample associated with header.
                Bit 1. On = 16 bit, Off = 8 bit.
                Bit 2. On = stereo, Off = mono. Stereo samples not supported yet
                Bit 3. On = compressed samples.
                Bit 4. On = Use loop
                Bit 5. On = Use sustain loop
                Bit 6. On = Ping Pong loop, Off = Forwards loop
                Bit 7. On = Ping Pong Sustain loop, Off = Forwards Sustain loop
      Vol:      Default volume for instrument

      Length:   Length of sample in no. of samples NOT no. of bytes
      LoopBeg:  Start of loop (no of samples in, not bytes)
      Loop End: Sample no. AFTER end of loop
      C5Speed:  Number of bytes a second for C-5 (ranges from 0->9999999)
      SusLBeg:  Start of sustain loop
      SusLEnd:  Sample no. AFTER end of sustain loop

      SmpPoint: 'Long' Offset of sample in file.

      ViS:      Vibrato Speed, ranges from 0->64
      ViD:      Vibrato Depth, ranges from 0->64
      ViT:      Vibrato waveform type.
                        0=Sine wave
                        1=Ramp down
                        2=Square wave
                        3=Random (speed is irrelevant)
      ViR:      Vibrato Rate, rate at which vibrato is applied (0->64)

        The depth of the vibrato at any point is worked out in the following
        way:
          Every processing cycle, the following occurs:
                1) Mov AX, [SomeVariableNameRelatingToVibrato]
                2) Add AL, Rate
                3) AdC AH, 0
                4) AH contains the depth of the vibrato as a fine-linear slide.
                5) Mov [SomeVariableNameRelatingToVibrato], AX  ; For the next
                                                                ; cycle.

        For those that don't understand assembly, then the depth is
        basically the running-sum of the rate divided by 256.

        Sample vibrato uses a table 256-bytes long

   Convert - bits other than bit 0 are used internally for the loading
             of alternative formats.
        Bit 0:
         Off: Samples are unsigned   } IT 2.01 and below use unsigned samples
          On: Samples are signed     } IT 2.02 and above use signed samples
        Bit 1:
         Off: Intel lo-hi byte order for 16-bit samples    } Safe to ignore
         On: Motorola hi-lo byte order for 16-bit samples  } these values...
        Bit 2:                                             }
         Off: Samples are stored as PCM values             }
          On: Samples are stored as Delta values           }
        Bit 3:                                             }
          On: Samples are stored as byte delta values      }
              (for PTM loader)                             }
        Bit 4:                                             }
          On: Samples are stored as TX-Wave 12-bit values  }
        Bit 5:                                             }
          On: Left/Right/All Stereo prompt                 }
        Bit 6: Reserved
        Bit 7: Reserved

   DfP - Default Pan. Bits 0->6 = Pan value, Bit 7 ON to USE (opposite of inst)


Pattern Layout:
        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
      ┌───────┬───────┬───┬───┬───┬───┬───────────────────────────────┐
0000: │Length │ Rows  │ x │ x │ x │ x │ Packed data................   │
      ├───┬───┼───┬───┼───┼───┼───┼───┼───┬───┬───┬───┬───┬───┬───┬───┤

      Length:   Length of packed pattern, not including the 8 byte header
                Note that the pattern + the 8 byte header will ALWAYS
                be less than 64k
      Rows:     Number of rows in this pattern (Ranges from 32->200)

      Patterns are unpacked by the following pseudocode... (this may look
      horrible, but in practise, it's just as convenient as the S3M
      pattern format for playback (but not for display))

      GetNextChannelMarker:
        Read byte into channelvariable.
        if(channelvariable = 0) then end of row
        Channel = (channelvariable-1) & 63              ; Channel is 0 based.
        if(channelvariable & 128) then read byte into maskvariable
          else maskvariable = previousmaskvariable for current channel

        if(maskvariable & 1), then read note. (byte value)
                // Note ranges from 0->119 (C-0 -> B-9)
                // 255 = note off, 254 = notecut
                // Others = note fade (already programmed into IT's player
                //                     but not available in the editor)

        if(maskvariable & 2), then read instrument (byte value)
                // Instrument ranges from 1->99

        if(maskvariable & 4), then read volume/panning (byte value)
                // Volume ranges from 0->64
                // Panning ranges from 0->64, mapped onto 128->192
                // Prepare for the following also:
                //  65->74 = Fine volume up
                //  75->84 = Fine volume down
                //  85->94 = Volume slide up
                //  95->104 = Volume slide down
                //  105->114 = Pitch Slide down
                //  115->124 = Pitch Slide up
                //  193->202 = Portamento to
                //  203->212 = Vibrato

        Effects 65 is equivalent to D0F, 66 is equivalent to D1F -> 74 = D9F
        Similarly for 75-84 (DFx), 85-94 (Dx0), 95->104 (D0x).

        (Fine) Volume up/down all share the same memory (NOT shared with Dxx
        in the effect column tho).

        Pitch slide up/down affect E/F/(G)'s memory - a Pitch slide
        up/down of x is equivalent to a normal slide by x*4

        Portamento to (Gx) affects the memory for Gxx and has the equivalent
        slide given by this table:

        SlideTable      DB      1, 4, 8, 16, 32, 64, 96, 128, 255

        Vibrato uses the same 'memory' as Hxx/Uxx.

        if(maskvariable & 8), then read command (byte value) and commandvalue
                // Valid ranges from 0->31 (0=no effect, 1=A, 2=B, 3=C, etc.)

        if(maskvariable & 16), then note = lastnote for channel
        if(maskvariable & 32), then instrument = lastinstrument for channel
        if(maskvariable & 64), then volume/pan = lastvolume/pan for channel
        if(maskvariable & 128), then {
                command = lastcommand for channel and
                commandvalue = lastcommandvalue for channel
        }
        Goto GetNextChannelMarker