tracker_engine_defs.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /* TODO: add 0exy effects here */
  57. TE_EFFECT_EXT_PORTA_UP = 0x0e10,
  58. TE_EFFECT_EXT_PORTA_DN = 0x0e20,
  59. TE_EFFECT_EXT_RETRIGGER = 0x0e90,
  60. TE_EFFECT_EXT_FINE_VOLUME_DOWN = 0x0ea0,
  61. TE_EFFECT_EXT_FINE_VOLUME_UP = 0x0eb0,
  62. TE_EFFECT_EXT_NOTE_CUT = 0x0ec0,
  63. TE_EFFECT_EXT_NOTE_DELAY = 0x0ed0,
  64. TE_EFFECT_SET_SPEED_PROG_PERIOD = 0x0f00,
  65. /* These effects work only in instrument program */
  66. TE_PROGRAM_LOOP_BEGIN = 0x7d00,
  67. TE_PROGRAM_LOOP_END = 0x7e00,
  68. TE_PROGRAM_JUMP = 0x7f00,
  69. TE_PROGRAM_NOP = 0x7ffe,
  70. TE_PROGRAM_END = 0x7fff,
  71. } EffectCommandsOpcodes;
  72. typedef struct
  73. {
  74. uint8_t a, d, s, r, volume;
  75. } InstrumentAdsr;
  76. typedef struct
  77. {
  78. char name[MUS_INST_NAME_LEN + 1];
  79. uint8_t waveform;
  80. uint16_t flags;
  81. uint16_t sound_engine_flags;
  82. uint8_t slide_speed;
  83. InstrumentAdsr adsr;
  84. uint8_t ring_mod, hard_sync; // 0xff = self
  85. uint8_t pw; // store only one byte since we don't have the luxury of virtually unlimited memory!
  86. uint16_t program[INST_PROG_LEN]; // MSB is unite bit (indicates this and next command must be executed at once)
  87. uint8_t program_period;
  88. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  89. uint8_t pwm_speed, pwm_depth, pwm_delay;
  90. uint8_t filter_cutoff, filter_resonance, filter_type;
  91. uint8_t base_note;
  92. int8_t finetune;
  93. } Instrument;
  94. typedef struct
  95. {
  96. Instrument *instrument;
  97. uint16_t flags;
  98. uint8_t channel_flags;
  99. uint16_t note, target_note, last_note, fixed_note;
  100. int16_t arpeggio_note;
  101. uint8_t volume;
  102. uint8_t program_counter, program_tick, program_loop, program_period;
  103. uint16_t filter_cutoff, filter_resonance;
  104. uint8_t filter_type;
  105. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  106. uint8_t pwm_speed, pwm_depth, pwm_delay;
  107. uint32_t vibrato_position, pwm_position; // basically accumulators
  108. uint8_t extarp1, extarp2;
  109. uint16_t pw;
  110. uint8_t slide_speed;
  111. } TrackerEngineChannel;
  112. typedef struct
  113. {
  114. uint8_t note; // MSB is used for instrument number MSB
  115. uint8_t inst_vol; // high nibble + MSB from note = instrument, low nibble = 4 volume LSBs
  116. uint16_t command; // MSB used as volume MSB
  117. } TrackerSongPatternStep;
  118. typedef struct
  119. {
  120. TrackerSongPatternStep *step;
  121. } TrackerSongPattern;
  122. typedef struct
  123. {
  124. uint8_t pattern_indices[SONG_MAX_CHANNELS];
  125. } TrackerSongSequenceStep;
  126. typedef struct
  127. {
  128. TrackerSongSequenceStep sequence_step[MAX_SEQUENCE_LENGTH];
  129. } TrackerSongSequence;
  130. typedef struct
  131. {
  132. Instrument *instrument[MAX_INSTRUMENTS];
  133. TrackerSongPattern pattern[MAX_PATTERNS];
  134. TrackerSongSequence sequence;
  135. uint8_t num_patterns, num_instruments;
  136. uint16_t num_sequence_steps;
  137. uint16_t pattern_length;
  138. char song_name[MUS_SONG_NAME_LEN + 1];
  139. uint8_t speed, rate;
  140. uint8_t loop_start, loop_end;
  141. } TrackerSong;
  142. typedef struct
  143. {
  144. TrackerEngineChannel channel[SONG_MAX_CHANNELS];
  145. TrackerSong *song;
  146. SoundEngine *sound_engine;
  147. uint16_t pattern_position, sequence_position, current_tick;
  148. uint16_t absolute_position; // sequence_position * pattern_length + pattern_position
  149. uint8_t speed, rate;
  150. uint8_t master_volume;
  151. bool playing; // if we reach the end of the song and song does not loop we just stop there
  152. } TrackerEngine;