tracker_engine_defs.h 4.4 KB

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