tracker_engine_defs.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "../sound_engine/sound_engine_defs.h"
  6. #define INST_PROG_LEN 16
  7. #define MUS_SONG_NAME_LEN 16
  8. #define MUS_INST_NAME_LEN (MUS_SONG_NAME_LEN - 3)
  9. #define SONG_MAX_CHANNELS NUM_CHANNELS
  10. #define MAX_INSTRUMENTS 31
  11. #define MAX_PATTERN_LENGTH 256
  12. #define MAX_PATTERNS 256
  13. #define MAX_SEQUENCE_LENGTH 256
  14. #define MUS_NOTE_NONE 127
  15. #define MUS_NOTE_RELEASE 126
  16. #define MUS_NOTE_CUT 125
  17. #define MUS_NOTE_INSTRUMENT_NONE 31
  18. #define MUS_NOTE_VOLUME_NONE 31
  19. #define SONG_FILE_SIG "FZT!SONG"
  20. #define SONG_FILE_EXT ".fzt"
  21. #define INST_FILE_SIG "FZT!INST"
  22. #define INST_FILE_EXT ".fzi"
  23. #define TRACKER_ENGINE_VERSION 1
  24. #define MIDDLE_C (12 * 4)
  25. #define MAX_NOTE (12 * 7 + 11)
  26. typedef enum {
  27. TE_ENABLE_VIBRATO = 1,
  28. TE_ENABLE_PWM = 2,
  29. TE_PROG_NO_RESTART = 4,
  30. TE_SET_CUTOFF = 8,
  31. TE_SET_PW = 16,
  32. TE_RETRIGGER_ON_SLIDE = 32, // call trigger instrument function even if slide command is there
  33. } TrackerEngineFlags;
  34. typedef enum {
  35. TEC_PLAYING = 1,
  36. TEC_PROGRAM_RUNNING = 2,
  37. TEC_DISABLED = 4,
  38. } TrackerEngineChannelFlags;
  39. typedef enum {
  40. TE_EFFECT_ARPEGGIO = 0x0000,
  41. TE_EFFECT_PORTAMENTO_UP = 0x0100,
  42. TE_EFFECT_PORTAMENTO_DOWN = 0x0200,
  43. TE_EFFECT_SLIDE = 0x0300,
  44. TE_EFFECT_VIBRATO = 0x0400,
  45. TE_EFFECT_PWM = 0x0500,
  46. TE_EFFECT_SET_PW = 0x0600,
  47. TE_EFFECT_PW_DOWN = 0x0700,
  48. TE_EFFECT_PW_UP = 0x0800,
  49. TE_EFFECT_SET_CUTOFF = 0x0900,
  50. TE_EFFECT_VOLUME_FADE = 0x0a00,
  51. TE_EFFECT_SET_WAVEFORM = 0x0b00,
  52. TE_EFFECT_SET_VOLUME = 0x0c00,
  53. TE_EFFECT_SKIP_PATTERN = 0x0d00,
  54. TE_EFFECT_EXT = 0x0e00,
  55. TE_EFFECT_EXT_TOGGLE_FILTER = 0x0e00,
  56. TE_EFFECT_EXT_PORTA_UP = 0x0e10,
  57. TE_EFFECT_EXT_PORTA_DN = 0x0e20,
  58. TE_EFFECT_EXT_FILTER_MODE = 0x0e30,
  59. TE_EFFECT_EXT_PATTERN_LOOP =
  60. 0x0e60, // e60 = start, e61-e6f = end and indication how many loops you want
  61. TE_EFFECT_EXT_RETRIGGER = 0x0e90,
  62. TE_EFFECT_EXT_FINE_VOLUME_DOWN = 0x0ea0,
  63. TE_EFFECT_EXT_FINE_VOLUME_UP = 0x0eb0,
  64. TE_EFFECT_EXT_NOTE_CUT = 0x0ec0,
  65. TE_EFFECT_EXT_NOTE_DELAY = 0x0ed0,
  66. TE_EFFECT_EXT_PHASE_RESET = 0x0ef0,
  67. TE_EFFECT_SET_SPEED_PROG_PERIOD = 0x0f00,
  68. TE_EFFECT_CUTOFF_UP = 0x1000, // Gxx
  69. TE_EFFECT_CUTOFF_DOWN = 0x1100, // Hxx
  70. TE_EFFECT_SET_RESONANCE = 0x1200, // Ixx
  71. TE_EFFECT_RESONANCE_UP = 0x1300, // Jxx
  72. TE_EFFECT_RESONANCE_DOWN = 0x1400, // Kxx
  73. TE_EFFECT_SET_ATTACK = 0x1500, // Lxx
  74. TE_EFFECT_SET_DECAY = 0x1600, // Mxx
  75. TE_EFFECT_SET_SUSTAIN = 0x1700, // Nxx
  76. TE_EFFECT_SET_RELEASE = 0x1800, // Oxx
  77. TE_EFFECT_PROGRAM_RESTART = 0x1900, // Pxx
  78. TE_EFFECT_SET_RATE = 0x1a00, //Qxx
  79. TE_EFFECT_SET_RING_MOD_SRC = 0x1b00, // Rxx
  80. TE_EFFECT_SET_HARD_SYNC_SRC = 0x1c00, // Sxx
  81. TE_EFFECT_PORTA_UP_SEMITONE = 0x1d00, // Txx
  82. TE_EFFECT_PORTA_DOWN_SEMITONE = 0x1e00, // Uxx
  83. /*
  84. TE_EFFECT_ = 0x1f00, //Vxx
  85. TE_EFFECT_ = 0x2000, //Wxx
  86. */
  87. TE_EFFECT_LEGATO = 0x2100, // Xxx
  88. TE_EFFECT_ARPEGGIO_ABS = 0x2200, // Yxx
  89. TE_EFFECT_TRIGGER_RELEASE = 0x2300, // Zxx
  90. /* These effects work only in instrument program */
  91. TE_PROGRAM_LOOP_BEGIN = 0x7d00,
  92. TE_PROGRAM_LOOP_END = 0x7e00,
  93. TE_PROGRAM_JUMP = 0x7f00,
  94. TE_PROGRAM_NOP = 0x7ffe,
  95. TE_PROGRAM_END = 0x7fff,
  96. } EffectCommandsOpcodes;
  97. typedef struct {
  98. uint8_t a, d, s, r, volume;
  99. } InstrumentAdsr;
  100. typedef struct {
  101. char name[MUS_INST_NAME_LEN + 1];
  102. uint8_t waveform;
  103. uint16_t flags;
  104. uint16_t sound_engine_flags;
  105. uint8_t slide_speed;
  106. InstrumentAdsr adsr;
  107. uint8_t ring_mod, hard_sync; // 0xff = self
  108. uint8_t pw; // store only one byte since we don't have the luxury of virtually unlimited memory!
  109. uint16_t program
  110. [INST_PROG_LEN]; // MSB is unite bit (indicates this and next command must be executed at once)
  111. uint8_t program_period;
  112. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  113. uint8_t pwm_speed, pwm_depth, pwm_delay;
  114. uint8_t filter_cutoff, filter_resonance, filter_type;
  115. uint8_t base_note;
  116. int8_t finetune;
  117. } Instrument;
  118. typedef struct {
  119. Instrument* instrument;
  120. uint16_t flags;
  121. uint8_t channel_flags;
  122. uint16_t note, target_note, last_note, fixed_note;
  123. int16_t arpeggio_note;
  124. uint8_t volume;
  125. uint8_t program_counter, program_tick, program_loop, program_period;
  126. uint16_t filter_cutoff, filter_resonance;
  127. uint8_t filter_type;
  128. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  129. uint8_t pwm_speed, pwm_depth, pwm_delay;
  130. uint32_t vibrato_position, pwm_position; // basically accumulators
  131. uint8_t extarp1, extarp2;
  132. uint16_t pw;
  133. uint8_t slide_speed;
  134. } TrackerEngineChannel;
  135. typedef struct {
  136. uint8_t note; // MSB is used for instrument number MSB
  137. uint8_t inst_vol; // high nibble + MSB from note = instrument, low nibble = 4 volume LSBs
  138. uint16_t command; // MSB used as volume MSB
  139. } TrackerSongPatternStep;
  140. typedef struct {
  141. TrackerSongPatternStep* step;
  142. } TrackerSongPattern;
  143. typedef struct {
  144. uint8_t pattern_indices[SONG_MAX_CHANNELS];
  145. } TrackerSongSequenceStep;
  146. typedef struct {
  147. TrackerSongSequenceStep sequence_step[MAX_SEQUENCE_LENGTH];
  148. } TrackerSongSequence;
  149. typedef struct {
  150. Instrument* instrument[MAX_INSTRUMENTS];
  151. TrackerSongPattern pattern[MAX_PATTERNS];
  152. TrackerSongSequence sequence;
  153. uint8_t num_patterns, num_instruments;
  154. uint16_t num_sequence_steps;
  155. uint16_t pattern_length;
  156. char song_name[MUS_SONG_NAME_LEN + 1];
  157. uint8_t speed, rate;
  158. uint8_t loop_start, loop_end;
  159. } TrackerSong;
  160. typedef struct {
  161. TrackerEngineChannel channel[SONG_MAX_CHANNELS];
  162. TrackerSong* song;
  163. SoundEngine* sound_engine;
  164. uint16_t pattern_position, sequence_position;
  165. int16_t current_tick;
  166. uint16_t absolute_position; // sequence_position * pattern_length + pattern_position
  167. uint8_t speed, rate;
  168. uint8_t master_volume;
  169. bool playing; // if we reach the end of the song and song does not loop we just stop there
  170. bool in_loop; // for E6X (pattern loop) command
  171. uint8_t loops_left;
  172. // uint32_t counter; //for debug
  173. } TrackerEngine;