|
Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
|
Plays a Standard MIDI File to a set of outputs. More...
Plays a Standard MIDI File to a set of outputs.
The sequencer owns a playhead over an SmfReader's tracks. It is told how many ticks of the control clock have passed and never assumes a call rate: advance() walks every track in SMF tick order, converts SMF ticks to time through the tempo map, and hands each event that is due to a sink with the output it belongs to. Events come out in the call where they fall due, so their timing is as fine as the calls: to the clock tick if advance() is called per tick, to the base period if it is called from a 1 kHz scheduler.
Time is exact integer arithmetic. SMF tick -> microseconds carries its remainder across segments, and "due" compares microseconds x (rate / g) against clock ticks x (10^6 / g), g = gcd(rate, 10^6), so nothing drifts over a long song or many loops. The reduction keeps a 1 MHz clock (g = 10^6) in plain ticks; scaling by 10^6 would overflow 64 bits in 213 days.
Routing. A format 1 file's track 0 is the conductor (the tempo map): its tempo events apply, its channel events go nowhere unless mapped. Tracks 1..N go to outputs 0..N-1. A format 0 file has one track, so it is split by channel: channels 0..N-1 go to outputs 0..N-1. Either map can be changed, and an output can rewrite every event's channel to one channel.
Transport is standard MIDI:
With loop on, reaching the end silences the outputs and plays again from the top; with loop off the sequencer stops and silences them.
Silencing sends a Note Off for every note the sequencer has sounding, on the output and channel it went to, then All Notes Off (CC 123) on every channel each output uses. Some receivers ignore CC 123, so the Note Offs end each note on its own (sound-byte-libs#149). A note the file ends itself, with a Note Off or a Note On at velocity 0, is not sounding. Notes still sounding when a new file is loaded are released by the next silencing.
A muted output sends nothing. Muting releases the output's sounding notes as silencing does; an output unmuted while the song plays joins at its next event, and the controller values it missed are not sent again.
Program Changes play on the first pass of a Start only. A program is the voice's setup, not the song: a file names its sounds at tick 0, and a run from the top should load them so every run begins the same - but a loop wrap is the same run continuing, and a preset reloading on every wrap would undo any change the player has made since.
The SmfReader's bytes must outlive the sequencer.
Usage:
| Outputs | Number of outputs, at least 1 |
Definition at line 83 of file sequencer.hpp.
#include <sequencer.hpp>
Public Types | |
| using | Sink = void(*)(void *context, uint8_t output, const MidiEvent &event) |
| Receives each event the sequencer emits: the context pointer, the output index and the event. | |
Public Member Functions | |
| Sequencer (uint32_t tick_rate, Sink sink, void *context) | |
| Construct a stopped sequencer with no file loaded. | |
| bool | load (const SmfReader &smf) |
| Take a parsed file and apply the default map. Stops playback. | |
| void | set_track_output (uint8_t track, uint8_t output) |
| Format 1: which output a track plays into. | |
| void | set_channel_output (uint8_t channel, uint8_t output) |
| Format 0: which output a channel plays into. | |
| void | set_output_channel (uint8_t output, uint8_t channel) |
| Send every event on an output on one channel. | |
| void | set_output_mute (uint8_t output, bool on) |
| Mute an output: its sounding notes are released, and it sends nothing until unmuted. | |
| bool | output_muted (uint8_t output) const |
| True if the output is muted; false for an index out of range. | |
| void | set_loop (bool on) |
| Loop on or off: at the end, play again from the top or stop. | |
| bool | looping () const |
| True if loop is on. | |
| void | start () |
| void | stop () |
| void | resume () |
| bool | playing () const |
| True while playing. | |
| bool | finished () const |
| True once play reached the end with loop off. | |
| bool | loaded () const |
| True if the last load() took a file. | |
| uint64_t | position_us () const |
| Where the playhead is in the song, in microseconds from the top. | |
| void | advance (uint32_t elapsed_ticks) |
Let elapsed_ticks of the clock pass: emit every event that falls due. | |
Static Public Attributes | |
| static constexpr uint8_t | OUTPUTS = Outputs |
| static constexpr uint8_t | MAX_TRACKS = SmfReader::MAX_TRACKS |
| static constexpr uint8_t | CHANNELS = 16 |
| static constexpr uint8_t | NO_OUTPUT = 0xFF |
| static constexpr uint8_t | KEEP_CHANNEL = 0xFF |
| static constexpr uint8_t | MAX_SNAPSHOT = 64 |
| static constexpr uint32_t | DEFAULT_TEMPO_US = 500000 |
| static constexpr uint8_t | ALL_NOTES_OFF = 123 |
| using sbl::midi::Sequencer< Outputs >::Sink = void (*)(void* context, uint8_t output, const MidiEvent& event) |
Receives each event the sequencer emits: the context pointer, the output index and the event.
Definition at line 97 of file sequencer.hpp.
|
inline |
Construct a stopped sequencer with no file loaded.
| tick_rate | The clock's ticks per second, what advance() counts in (Clock::TICKS_PER_SECOND). Required, no default; a zero rate counts as one tick per second. |
| sink | Called for every event emitted |
| context | Passed to sink unchanged |
Definition at line 107 of file sequencer.hpp.
References sbl::midi::Sequencer< Outputs >::KEEP_CHANNEL.
|
inline |
Take a parsed file and apply the default map. Stops playback.
| smf | A reader whose parse() succeeded; the file's bytes must outlive the sequencer |
Definition at line 122 of file sequencer.hpp.
References sbl::midi::Sequencer< Outputs >::CHANNELS, sbl::midi::SmfReader::error(), sbl::midi::SmfReader::format(), sbl::midi::Sequencer< Outputs >::MAX_TRACKS, sbl::midi::Sequencer< Outputs >::NO_OUTPUT, sbl::midi::None, sbl::midi::SmfReader::ticks_per_quarter(), sbl::midi::SmfReader::track(), and sbl::midi::SmfReader::track_count().
|
inline |
Format 1: which output a track plays into.
| track | Track index |
| output | Output index, or NO_OUTPUT for none |
Definition at line 149 of file sequencer.hpp.
References sbl::midi::Sequencer< Outputs >::MAX_TRACKS, and sbl::midi::Sequencer< Outputs >::NO_OUTPUT.
|
inline |
Format 0: which output a channel plays into.
| channel | MIDI channel, 0 to 15 |
| output | Output index, or NO_OUTPUT for none |
Definition at line 164 of file sequencer.hpp.
References sbl::midi::Sequencer< Outputs >::CHANNELS, and sbl::midi::Sequencer< Outputs >::NO_OUTPUT.
|
inline |
Send every event on an output on one channel.
| output | Output index |
| channel | MIDI channel, 0 to 15, or KEEP_CHANNEL to leave each event's channel |
Definition at line 179 of file sequencer.hpp.
References sbl::midi::Sequencer< Outputs >::CHANNELS, and sbl::midi::Sequencer< Outputs >::KEEP_CHANNEL.
|
inline |
Mute an output: its sounding notes are released, and it sends nothing until unmuted.
| output | Output index |
| on | true to mute, false to unmute |
Definition at line 194 of file sequencer.hpp.
|
inline |
True if the output is muted; false for an index out of range.
Definition at line 205 of file sequencer.hpp.
|
inline |
Loop on or off: at the end, play again from the top or stop.
Definition at line 208 of file sequencer.hpp.
|
inline |
True if loop is on.
Definition at line 210 of file sequencer.hpp.
|
inline |
Start: play from the top with every output reset.
Definition at line 215 of file sequencer.hpp.
|
inline |
Stop: hold the playhead, silence the outputs.
Definition at line 234 of file sequencer.hpp.
|
inline |
Continue: play on from the playhead. Nothing to continue after the end.
Definition at line 243 of file sequencer.hpp.
|
inline |
True while playing.
Definition at line 250 of file sequencer.hpp.
|
inline |
True once play reached the end with loop off.
Definition at line 252 of file sequencer.hpp.
|
inline |
True if the last load() took a file.
Definition at line 254 of file sequencer.hpp.
|
inline |
Where the playhead is in the song, in microseconds from the top.
Definition at line 257 of file sequencer.hpp.
|
inline |
Let elapsed_ticks of the clock pass: emit every event that falls due.
Time counts while stopped too, so a resume picks up at the playhead without replaying the pause.
| elapsed_ticks | Clock ticks since the last call |
Definition at line 267 of file sequencer.hpp.
References sbl::midi::SmfEvent::kind, sbl::midi::SmfEvent::midi, sbl::midi::SmfTrack::peek(), sbl::midi::SmfTrack::pop(), sbl::midi::SmfEvent::tempo_us, and sbl::midi::SmfEvent::tick.
|
staticconstexpr |
outputs the sequencer plays into
Definition at line 87 of file sequencer.hpp.
|
staticconstexpr |
tracks a file may have
Definition at line 88 of file sequencer.hpp.
Referenced by sbl::midi::Sequencer< Outputs >::load(), and sbl::midi::Sequencer< Outputs >::set_track_output().
|
staticconstexpr |
MIDI channels
Definition at line 89 of file sequencer.hpp.
Referenced by sbl::midi::Sequencer< Outputs >::load(), sbl::midi::Sequencer< Outputs >::set_channel_output(), and sbl::midi::Sequencer< Outputs >::set_output_channel().
|
staticconstexpr |
a track or channel mapped to no output
Definition at line 90 of file sequencer.hpp.
Referenced by sbl::midi::Sequencer< Outputs >::load(), sbl::midi::Sequencer< Outputs >::set_channel_output(), and sbl::midi::Sequencer< Outputs >::set_track_output().
|
staticconstexpr |
an output that leaves each event's channel
Definition at line 91 of file sequencer.hpp.
Referenced by sbl::midi::Sequencer< Outputs >::Sequencer(), and sbl::midi::Sequencer< Outputs >::set_output_channel().
|
staticconstexpr |
distinct (output, channel, controller) first values
Definition at line 92 of file sequencer.hpp.
|
staticconstexpr |
120 bpm: the SMF default
Definition at line 93 of file sequencer.hpp.
|
staticconstexpr |
the All Notes Off controller number
Definition at line 94 of file sequencer.hpp.