Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sbl::midi::Sequencer< Outputs > Class Template Reference

Plays a Standard MIDI File to a set of outputs. More...

Detailed Description

template<uint8_t Outputs = 4>
class sbl::midi::Sequencer< Outputs >

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:

  • start(): play from the top. The outputs are silenced, every controller's first value in the file is sent to each output, then play begins. Each run from the top begins the same way.
  • stop(): stop where it is, the outputs silenced; the playhead is kept.
  • resume(): play on from the playhead (MIDI Continue).

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:

void sink(void*, uint8_t output, const sbl::midi::MidiEvent& e) { ... }
sbl::midi::Sequencer<4> seq(Clock::TICKS_PER_SECOND, sink, nullptr);
seq.load(smf);
seq.start();
seq.advance(elapsed_ticks); // from the control tick
Plays a Standard MIDI File to a set of outputs.
Definition sequencer.hpp:83
Template Parameters
OutputsNumber 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
 

Member Typedef Documentation

◆ Sink

template<uint8_t Outputs = 4>
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.

Constructor & Destructor Documentation

◆ Sequencer()

template<uint8_t Outputs = 4>
sbl::midi::Sequencer< Outputs >::Sequencer ( uint32_t  tick_rate,
Sink  sink,
void *  context 
)
inline

Construct a stopped sequencer with no file loaded.

Parameters
tick_rateThe 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.
sinkCalled for every event emitted
contextPassed to sink unchanged

Definition at line 107 of file sequencer.hpp.

References sbl::midi::Sequencer< Outputs >::KEEP_CHANNEL.

Member Function Documentation

◆ load()

template<uint8_t Outputs = 4>
bool sbl::midi::Sequencer< Outputs >::load ( const SmfReader &  smf)
inline

Take a parsed file and apply the default map. Stops playback.

Parameters
smfA reader whose parse() succeeded; the file's bytes must outlive the sequencer
Returns
false if the file was not loaded

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().

◆ set_track_output()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::set_track_output ( uint8_t  track,
uint8_t  output 
)
inline

Format 1: which output a track plays into.

Parameters
trackTrack index
outputOutput 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.

◆ set_channel_output()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::set_channel_output ( uint8_t  channel,
uint8_t  output 
)
inline

Format 0: which output a channel plays into.

Parameters
channelMIDI channel, 0 to 15
outputOutput 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.

◆ set_output_channel()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::set_output_channel ( uint8_t  output,
uint8_t  channel 
)
inline

Send every event on an output on one channel.

Parameters
outputOutput index
channelMIDI 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.

◆ set_output_mute()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::set_output_mute ( uint8_t  output,
bool  on 
)
inline

Mute an output: its sounding notes are released, and it sends nothing until unmuted.

Parameters
outputOutput index
ontrue to mute, false to unmute

Definition at line 194 of file sequencer.hpp.

◆ output_muted()

template<uint8_t Outputs = 4>
bool sbl::midi::Sequencer< Outputs >::output_muted ( uint8_t  output) const
inline

True if the output is muted; false for an index out of range.

Definition at line 205 of file sequencer.hpp.

◆ set_loop()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::set_loop ( bool  on)
inline

Loop on or off: at the end, play again from the top or stop.

Definition at line 208 of file sequencer.hpp.

◆ looping()

template<uint8_t Outputs = 4>
bool sbl::midi::Sequencer< Outputs >::looping ( ) const
inline

True if loop is on.

Definition at line 210 of file sequencer.hpp.

◆ start()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::start ( )
inline

Start: play from the top with every output reset.

Definition at line 215 of file sequencer.hpp.

◆ stop()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::stop ( )
inline

Stop: hold the playhead, silence the outputs.

Definition at line 234 of file sequencer.hpp.

◆ resume()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::resume ( )
inline

Continue: play on from the playhead. Nothing to continue after the end.

Definition at line 243 of file sequencer.hpp.

◆ playing()

template<uint8_t Outputs = 4>
bool sbl::midi::Sequencer< Outputs >::playing ( ) const
inline

True while playing.

Definition at line 250 of file sequencer.hpp.

◆ finished()

template<uint8_t Outputs = 4>
bool sbl::midi::Sequencer< Outputs >::finished ( ) const
inline

True once play reached the end with loop off.

Definition at line 252 of file sequencer.hpp.

◆ loaded()

template<uint8_t Outputs = 4>
bool sbl::midi::Sequencer< Outputs >::loaded ( ) const
inline

True if the last load() took a file.

Definition at line 254 of file sequencer.hpp.

◆ position_us()

template<uint8_t Outputs = 4>
uint64_t sbl::midi::Sequencer< Outputs >::position_us ( ) const
inline

Where the playhead is in the song, in microseconds from the top.

Definition at line 257 of file sequencer.hpp.

◆ advance()

template<uint8_t Outputs = 4>
void sbl::midi::Sequencer< Outputs >::advance ( uint32_t  elapsed_ticks)
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.

Parameters
elapsed_ticksClock 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.

Member Data Documentation

◆ OUTPUTS

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::OUTPUTS = Outputs
staticconstexpr

outputs the sequencer plays into

Definition at line 87 of file sequencer.hpp.

◆ MAX_TRACKS

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::MAX_TRACKS = SmfReader::MAX_TRACKS
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().

◆ CHANNELS

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::CHANNELS = 16
staticconstexpr

◆ NO_OUTPUT

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::NO_OUTPUT = 0xFF
staticconstexpr

◆ KEEP_CHANNEL

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::KEEP_CHANNEL = 0xFF
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().

◆ MAX_SNAPSHOT

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::MAX_SNAPSHOT = 64
staticconstexpr

distinct (output, channel, controller) first values

Definition at line 92 of file sequencer.hpp.

◆ DEFAULT_TEMPO_US

template<uint8_t Outputs = 4>
constexpr uint32_t sbl::midi::Sequencer< Outputs >::DEFAULT_TEMPO_US = 500000
staticconstexpr

120 bpm: the SMF default

Definition at line 93 of file sequencer.hpp.

◆ ALL_NOTES_OFF

template<uint8_t Outputs = 4>
constexpr uint8_t sbl::midi::Sequencer< Outputs >::ALL_NOTES_OFF = 123
staticconstexpr

the All Notes Off controller number

Definition at line 94 of file sequencer.hpp.


The documentation for this class was generated from the following file: