sequencer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright 2020 Rodolphe Belouin
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. // Maximum number of steps: 256
  20. #ifndef SEQUENCER_STEPS
  21. # define SEQUENCER_STEPS 16
  22. #endif
  23. // Maximum number of tracks: 8
  24. #ifndef SEQUENCER_TRACKS
  25. # define SEQUENCER_TRACKS 8
  26. #endif
  27. #ifndef SEQUENCER_TRACK_THROTTLE
  28. # define SEQUENCER_TRACK_THROTTLE 3
  29. #endif
  30. #ifndef SEQUENCER_PHASE_RELEASE_TIMEOUT
  31. # define SEQUENCER_PHASE_RELEASE_TIMEOUT 30
  32. #endif
  33. /**
  34. * Make sure that the items of this enumeration follow the powers of 2, separated by a ternary variant.
  35. * Check the implementation of `get_step_duration` for further explanation.
  36. */
  37. typedef enum {
  38. SQ_RES_2, //
  39. SQ_RES_2T,
  40. SQ_RES_4,
  41. SQ_RES_4T,
  42. SQ_RES_8,
  43. SQ_RES_8T,
  44. SQ_RES_16,
  45. SQ_RES_16T,
  46. SQ_RES_32,
  47. SEQUENCER_RESOLUTIONS
  48. } sequencer_resolution_t;
  49. typedef struct {
  50. bool enabled;
  51. uint8_t steps[SEQUENCER_STEPS];
  52. uint16_t track_notes[SEQUENCER_TRACKS];
  53. uint8_t tempo; // Is a maximum tempo of 255 reasonable?
  54. sequencer_resolution_t resolution;
  55. } sequencer_config_t;
  56. /**
  57. * Because Digital Audio Workstations get overwhelmed when too many MIDI signals are sent concurrently,
  58. * We use a "phase" state machine to delay some of the events.
  59. */
  60. typedef enum sequencer_phase_t {
  61. SEQUENCER_PHASE_ATTACK, // t=0ms, send the MIDI note on signal
  62. SEQUENCER_PHASE_RELEASE, // t=SEQUENCER_PHASE_RELEASE_TIMEOUT ms, send the MIDI note off signal
  63. SEQUENCER_PHASE_PAUSE // t=step duration ms, loop
  64. } sequencer_phase_t;
  65. typedef struct {
  66. uint8_t active_tracks;
  67. uint8_t current_track;
  68. uint8_t current_step;
  69. uint16_t timer;
  70. sequencer_phase_t phase;
  71. } sequencer_state_t;
  72. extern sequencer_config_t sequencer_config;
  73. // We expose the internal state to make the feature more "unit-testable"
  74. extern sequencer_state_t sequencer_internal_state;
  75. bool is_sequencer_on(void);
  76. void sequencer_toggle(void);
  77. void sequencer_on(void);
  78. void sequencer_off(void);
  79. void sequencer_set_track_notes(const uint16_t track_notes[SEQUENCER_TRACKS]);
  80. bool is_sequencer_track_active(uint8_t track);
  81. void sequencer_set_track_activation(uint8_t track, bool value);
  82. void sequencer_toggle_track_activation(uint8_t track);
  83. void sequencer_toggle_single_active_track(uint8_t track);
  84. #define sequencer_activate_track(track) sequencer_set_track_activation(track, true)
  85. #define sequencer_deactivate_track(track) sequencer_set_track_activation(track, false)
  86. bool is_sequencer_step_on(uint8_t step);
  87. bool is_sequencer_step_on_for_track(uint8_t step, uint8_t track);
  88. void sequencer_set_step(uint8_t step, bool value);
  89. void sequencer_toggle_step(uint8_t step);
  90. void sequencer_set_all_steps(bool value);
  91. #define sequencer_set_step_on(step) sequencer_set_step(step, true)
  92. #define sequencer_set_step_off(step) sequencer_set_step(step, false)
  93. #define sequencer_set_all_steps_on() sequencer_set_all_steps(true)
  94. #define sequencer_set_all_steps_off() sequencer_set_all_steps(false)
  95. uint8_t sequencer_get_tempo(void);
  96. void sequencer_set_tempo(uint8_t tempo);
  97. void sequencer_increase_tempo(void);
  98. void sequencer_decrease_tempo(void);
  99. sequencer_resolution_t sequencer_get_resolution(void);
  100. void sequencer_set_resolution(sequencer_resolution_t resolution);
  101. void sequencer_increase_resolution(void);
  102. void sequencer_decrease_resolution(void);
  103. uint8_t sequencer_get_current_step(void);
  104. uint16_t sequencer_get_beat_duration(void);
  105. uint16_t sequencer_get_step_duration(void);
  106. uint16_t get_beat_duration(uint8_t tempo);
  107. uint16_t get_step_duration(uint8_t tempo, sequencer_resolution_t resolution);
  108. void sequencer_task(void);