Parent directory

sequencer.h

4435 bytes
  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
 17#pragma once
 18
 19#include <stdbool.h>
 20#include <stdint.h>
 21
 22// Maximum number of steps: 256
 23#ifndef SEQUENCER_STEPS
 24#    define SEQUENCER_STEPS 16
 25#endif
 26
 27// Maximum number of tracks: 8
 28#ifndef SEQUENCER_TRACKS
 29#    define SEQUENCER_TRACKS 8
 30#endif
 31
 32#ifndef SEQUENCER_TRACK_THROTTLE
 33#    define SEQUENCER_TRACK_THROTTLE 3
 34#endif
 35
 36#ifndef SEQUENCER_PHASE_RELEASE_TIMEOUT
 37#    define SEQUENCER_PHASE_RELEASE_TIMEOUT 30
 38#endif
 39
 40/**
 41 * Make sure that the items of this enumeration follow the powers of 2, separated by a ternary variant.
 42 * Check the implementation of `get_step_duration` for further explanation.
 43 */
 44typedef enum {
 45    SQ_RES_2, //
 46    SQ_RES_2T,
 47    SQ_RES_4,
 48    SQ_RES_4T,
 49    SQ_RES_8,
 50    SQ_RES_8T,
 51    SQ_RES_16,
 52    SQ_RES_16T,
 53    SQ_RES_32,
 54    SEQUENCER_RESOLUTIONS
 55} sequencer_resolution_t;
 56
 57typedef struct {
 58    bool                   enabled;
 59    uint8_t                steps[SEQUENCER_STEPS];
 60    uint16_t               track_notes[SEQUENCER_TRACKS];
 61    uint8_t                tempo; // Is a maximum tempo of 255 reasonable?
 62    sequencer_resolution_t resolution;
 63} sequencer_config_t;
 64
 65/**
 66 * Because Digital Audio Workstations get overwhelmed when too many MIDI signals are sent concurrently,
 67 * We use a "phase" state machine to delay some of the events.
 68 */
 69typedef enum sequencer_phase_t {
 70    SEQUENCER_PHASE_ATTACK,  // t=0ms, send the MIDI note on signal
 71    SEQUENCER_PHASE_RELEASE, // t=SEQUENCER_PHASE_RELEASE_TIMEOUT ms, send the MIDI note off signal
 72    SEQUENCER_PHASE_PAUSE    // t=step duration ms, loop
 73} sequencer_phase_t;
 74
 75typedef struct {
 76    uint8_t           active_tracks;
 77    uint8_t           current_track;
 78    uint8_t           current_step;
 79    uint16_t          timer;
 80    sequencer_phase_t phase;
 81} sequencer_state_t;
 82
 83extern sequencer_config_t sequencer_config;
 84
 85// We expose the internal state to make the feature more "unit-testable"
 86extern sequencer_state_t sequencer_internal_state;
 87
 88bool is_sequencer_on(void);
 89void sequencer_toggle(void);
 90void sequencer_on(void);
 91void sequencer_off(void);
 92
 93void sequencer_set_track_notes(const uint16_t track_notes[SEQUENCER_TRACKS]);
 94
 95bool is_sequencer_track_active(uint8_t track);
 96void sequencer_set_track_activation(uint8_t track, bool value);
 97void sequencer_toggle_track_activation(uint8_t track);
 98void sequencer_toggle_single_active_track(uint8_t track);
 99
100#define sequencer_activate_track(track) sequencer_set_track_activation(track, true)
101#define sequencer_deactivate_track(track) sequencer_set_track_activation(track, false)
102
103bool is_sequencer_step_on(uint8_t step);
104bool is_sequencer_step_on_for_track(uint8_t step, uint8_t track);
105void sequencer_set_step(uint8_t step, bool value);
106void sequencer_toggle_step(uint8_t step);
107void sequencer_set_all_steps(bool value);
108
109#define sequencer_set_step_on(step) sequencer_set_step(step, true)
110#define sequencer_set_step_off(step) sequencer_set_step(step, false)
111#define sequencer_set_all_steps_on() sequencer_set_all_steps(true)
112#define sequencer_set_all_steps_off() sequencer_set_all_steps(false)
113
114uint8_t sequencer_get_tempo(void);
115void    sequencer_set_tempo(uint8_t tempo);
116void    sequencer_increase_tempo(void);
117void    sequencer_decrease_tempo(void);
118
119sequencer_resolution_t sequencer_get_resolution(void);
120void                   sequencer_set_resolution(sequencer_resolution_t resolution);
121void                   sequencer_increase_resolution(void);
122void                   sequencer_decrease_resolution(void);
123
124uint8_t sequencer_get_current_step(void);
125
126uint16_t sequencer_get_beat_duration(void);
127uint16_t sequencer_get_step_duration(void);
128
129uint16_t get_beat_duration(uint8_t tempo);
130uint16_t get_step_duration(uint8_t tempo, sequencer_resolution_t resolution);
131
132void sequencer_task(void);