tracker_engine_defs.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 TRACKER_ENGINE_VERSION 1
  22. #define MIDDLE_C (12 * 4)
  23. #define MAX_NOTE (12 * 7 + 11)
  24. typedef enum
  25. {
  26. TE_ENABLE_VIBRATO = 1,
  27. TE_ENABLE_PWM = 2,
  28. TE_PROG_NO_RESTART = 4,
  29. TE_SET_CUTOFF = 8,
  30. TE_SET_PW = 16,
  31. TE_RETRIGGER_ON_SLIDE = 32, // call trigger instrument function even if slide command is there
  32. } TrackerEngineFlags;
  33. typedef enum
  34. {
  35. TEC_PLAYING = 1,
  36. TEC_PROGRAM_RUNNING = 2,
  37. TEC_DISABLED = 4,
  38. } TrackerEngineChannelFlags;
  39. typedef enum
  40. {
  41. TE_EFFECT_ARPEGGIO = 0x0000,
  42. TE_EFFECT_PORTAMENTO_UP = 0x0100,
  43. TE_EFFECT_PORTAMENTO_DOWN = 0x0200,
  44. TE_EFFECT_SLIDE = 0x0300,
  45. TE_EFFECT_VIBRATO = 0x0400,
  46. TE_EFFECT_PWM = 0x0500,
  47. TE_EFFECT_SET_PW = 0x0600,
  48. TE_EFFECT_PW_DOWN = 0x0700,
  49. TE_EFFECT_PW_UP = 0x0800,
  50. TE_EFFECT_SET_CUTOFF = 0x0900,
  51. TE_EFFECT_VOLUME_FADE = 0x0a00,
  52. TE_EFFECT_SET_WAVEFORM = 0x0b00,
  53. TE_EFFECT_SET_VOLUME = 0x0c00,
  54. TE_EFFECT_SKIP_PATTERN = 0x0d00,
  55. TE_EFFECT_EXT = 0x0e00,
  56. TE_EFFECT_EXT_TOGGLE_FILTER = 0x0e00,
  57. TE_EFFECT_EXT_PORTA_UP = 0x0e10,
  58. TE_EFFECT_EXT_PORTA_DN = 0x0e20,
  59. TE_EFFECT_EXT_FILTER_MODE = 0x0e30,
  60. TE_EFFECT_EXT_PATTERN_LOOP = 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. /*
  79. TE_EFFECT_ = 0x1a00, //Qxx
  80. */
  81. TE_EFFECT_SET_RING_MOD_SRC = 0x1b00, // Rxx
  82. TE_EFFECT_SET_HARD_SYNC_SRC = 0x1c00, // Sxx
  83. /*
  84. TE_EFFECT_ = 0x1d00, //Txx
  85. TE_EFFECT_ = 0x1e00, //Uxx
  86. TE_EFFECT_ = 0x1f00, //Vxx
  87. TE_EFFECT_ = 0x2000, //Wxx
  88. TE_EFFECT_ = 0x2100, //Xxx
  89. */
  90. TE_EFFECT_ARPEGGIO_ABS = 0x2200, // Yxx
  91. TE_EFFECT_TRIGGER_RELEASE = 0x2300, // Zxx
  92. /* These effects work only in instrument program */
  93. TE_PROGRAM_LOOP_BEGIN = 0x7d00,
  94. TE_PROGRAM_LOOP_END = 0x7e00,
  95. TE_PROGRAM_JUMP = 0x7f00,
  96. TE_PROGRAM_NOP = 0x7ffe,
  97. TE_PROGRAM_END = 0x7fff,
  98. } EffectCommandsOpcodes;
  99. typedef struct
  100. {
  101. uint8_t a, d, s, r, volume;
  102. } InstrumentAdsr;
  103. typedef struct
  104. {
  105. char name[MUS_INST_NAME_LEN + 1];
  106. uint8_t waveform;
  107. uint16_t flags;
  108. uint16_t sound_engine_flags;
  109. uint8_t slide_speed;
  110. InstrumentAdsr adsr;
  111. uint8_t ring_mod, hard_sync; // 0xff = self
  112. uint8_t pw; // store only one byte since we don't have the luxury of virtually unlimited memory!
  113. uint16_t program[INST_PROG_LEN]; // MSB is unite bit (indicates this and next command must be executed at once)
  114. uint8_t program_period;
  115. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  116. uint8_t pwm_speed, pwm_depth, pwm_delay;
  117. uint8_t filter_cutoff, filter_resonance, filter_type;
  118. uint8_t base_note;
  119. int8_t finetune;
  120. } Instrument;
  121. typedef struct
  122. {
  123. Instrument *instrument;
  124. uint16_t flags;
  125. uint8_t channel_flags;
  126. uint16_t note, target_note, last_note, fixed_note;
  127. int16_t arpeggio_note;
  128. uint8_t volume;
  129. uint8_t program_counter, program_tick, program_loop, program_period;
  130. uint16_t filter_cutoff, filter_resonance;
  131. uint8_t filter_type;
  132. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  133. uint8_t pwm_speed, pwm_depth, pwm_delay;
  134. uint32_t vibrato_position, pwm_position; // basically accumulators
  135. uint8_t extarp1, extarp2;
  136. uint16_t pw;
  137. uint8_t slide_speed;
  138. } TrackerEngineChannel;
  139. typedef struct
  140. {
  141. uint8_t note; // MSB is used for instrument number MSB
  142. uint8_t inst_vol; // high nibble + MSB from note = instrument, low nibble = 4 volume LSBs
  143. uint16_t command; // MSB used as volume MSB
  144. } TrackerSongPatternStep;
  145. typedef struct
  146. {
  147. TrackerSongPatternStep *step;
  148. } TrackerSongPattern;
  149. typedef struct
  150. {
  151. uint8_t pattern_indices[SONG_MAX_CHANNELS];
  152. } TrackerSongSequenceStep;
  153. typedef struct
  154. {
  155. TrackerSongSequenceStep sequence_step[MAX_SEQUENCE_LENGTH];
  156. } TrackerSongSequence;
  157. typedef struct
  158. {
  159. Instrument *instrument[MAX_INSTRUMENTS];
  160. TrackerSongPattern pattern[MAX_PATTERNS];
  161. TrackerSongSequence sequence;
  162. uint8_t num_patterns, num_instruments;
  163. uint16_t num_sequence_steps;
  164. uint16_t pattern_length;
  165. char song_name[MUS_SONG_NAME_LEN + 1];
  166. uint8_t speed, rate;
  167. uint8_t loop_start, loop_end;
  168. } TrackerSong;
  169. typedef struct
  170. {
  171. TrackerEngineChannel channel[SONG_MAX_CHANNELS];
  172. TrackerSong *song;
  173. SoundEngine *sound_engine;
  174. uint16_t pattern_position, sequence_position, current_tick;
  175. uint16_t absolute_position; // sequence_position * pattern_length + pattern_position
  176. uint8_t speed, rate;
  177. uint8_t master_volume;
  178. bool playing; // if we reach the end of the song and song does not loop we just stop there
  179. bool in_loop; // for E6X (pattern loop) command
  180. uint8_t loops_left;
  181. } TrackerEngine;