tracker_engine_defs.h 4.6 KB

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