tracker_engine_defs.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include "../sound_engine/sound_engine_defs.h"
  5. #define INST_PROG_LEN 16
  6. #define MUS_SONG_NAME_LEN 16
  7. #define MUS_INST_NAME_LEN (MUS_SONG_NAME_LEN - 3)
  8. #define SONG_MAX_CHANNELS NUM_CHANNELS
  9. #define MAX_INSTRUMENTS 31
  10. #define MAX_PATTERN_LENGTH 256
  11. #define MAX_PATTERNS 256
  12. #define MAX_SEQUENCE_LENGTH 256
  13. #define MUS_NOTE_NONE 127
  14. #define MUS_NOTE_RELEASE 126
  15. #define MUS_NOTE_CUT 125
  16. #define MUS_NOTE_INSTRUMENT_NONE 31
  17. #define MUS_NOTE_VOLUME_NONE 31
  18. #define SONG_FILE_SIG "FZT!SONG"
  19. #define TRACKER_ENGINE_VERSION 1
  20. #define MIDDLE_C (12 * 4)
  21. #define MAX_NOTE (12 * 7 + 11)
  22. typedef enum
  23. {
  24. TE_ENABLE_VIBRATO = 1,
  25. TE_ENABLE_PWM = 2,
  26. TE_PROG_NO_RESTART = 4,
  27. TE_SET_CUTOFF = 8,
  28. TE_SET_PW = 16,
  29. TE_KEY_SYNC = 32, // sync oscillators on keydown
  30. TE_RETRIGGER_ON_SLIDE = 64, // call trigger instrument function even if slide command is there
  31. } TrackerEngineFlags;
  32. typedef enum
  33. {
  34. TEC_PLAYING = 1,
  35. TEC_PROGRAM_RUNNING = 2,
  36. TEC_DISABLED = 4,
  37. } TrackerEngineChannelFlags;
  38. typedef struct
  39. {
  40. uint8_t a, d, s, r, volume;
  41. } InstrumentAdsr;
  42. typedef struct
  43. {
  44. char name[MUS_INST_NAME_LEN + 1];
  45. uint8_t waveform;
  46. uint16_t flags;
  47. uint16_t sound_engine_flags;
  48. uint8_t slide_speed;
  49. InstrumentAdsr adsr;
  50. uint8_t ring_mod, hard_sync; // 0xff = self
  51. uint8_t pw; // store only one byte since we don't have the luxury of virtually unlimited memory!
  52. uint16_t program[INST_PROG_LEN]; // MSB is unite bit (indicates this and next command must be executed at once)
  53. uint8_t program_period;
  54. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  55. uint8_t pwm_speed, pwm_depth, pwm_delay;
  56. uint8_t filter_cutoff, filter_resonance, filter_type;
  57. uint8_t base_note;
  58. int8_t finetune;
  59. } Instrument;
  60. typedef struct
  61. {
  62. Instrument *instrument;
  63. uint16_t flags;
  64. uint8_t channel_flags;
  65. uint16_t note, target_note, last_note, fixed_note;
  66. int16_t arpeggio_note;
  67. uint8_t volume;
  68. uint8_t program_counter, program_tick, program_loop, program_period;
  69. uint16_t filter_cutoff, filter_resonance;
  70. uint8_t filter_type;
  71. uint8_t vibrato_speed, vibrato_depth, vibrato_delay;
  72. uint8_t pwm_speed, pwm_depth, pwm_delay;
  73. uint16_t vibrato_position, pwm_position;
  74. uint8_t extarp1, extarp2;
  75. uint16_t pw;
  76. uint8_t slide_speed;
  77. } TrackerEngineChannel;
  78. typedef struct
  79. {
  80. uint8_t note; // MSB is used for instrument number MSB
  81. uint8_t inst_vol; // high nibble + MSB from note = instrument, low nibble = 4 volume LSBs
  82. uint16_t command; // MSB used as volume MSB
  83. } TrackerSongPatternStep;
  84. typedef struct
  85. {
  86. TrackerSongPatternStep *step;
  87. } TrackerSongPattern;
  88. typedef struct
  89. {
  90. uint8_t pattern_indices[SONG_MAX_CHANNELS];
  91. } TrackerSongSequenceStep;
  92. typedef struct
  93. {
  94. TrackerSongSequenceStep sequence_step[MAX_SEQUENCE_LENGTH];
  95. } TrackerSongSequence;
  96. typedef struct
  97. {
  98. Instrument *instrument[MAX_INSTRUMENTS];
  99. TrackerSongPattern pattern[MAX_PATTERNS];
  100. TrackerSongSequence sequence;
  101. uint8_t num_patterns, num_sequence_steps, num_instruments;
  102. uint16_t pattern_length;
  103. char song_name[MUS_SONG_NAME_LEN + 1];
  104. uint8_t speed, rate;
  105. uint8_t loop_start, loop_end;
  106. } TrackerSong;
  107. typedef struct
  108. {
  109. TrackerEngineChannel channel[SONG_MAX_CHANNELS];
  110. TrackerSong *song;
  111. SoundEngine *sound_engine;
  112. uint16_t pattern_position, sequence_position, current_tick;
  113. uint16_t absolute_position; // sequence_position * pattern_length + pattern_position
  114. uint8_t speed, rate;
  115. uint8_t master_volume;
  116. bool playing; // if we reach the end of the song and song does not loop we just stop there
  117. } TrackerEngine;