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
midi_keys.hpp
Go to the documentation of this file.
1/**
2 * @file midi_keys.hpp
3 * @brief MIDI notes to pitch, gate and velocity for one voice
4 *
5 * The edge adapter between MIDI's note numbers and the voice: notes stay
6 * here, and what leaves is pitch in octaves relative to REFERENCE_NOTE, a
7 * gate and a velocity in [0, 1]. Mono with last-note priority over a stack
8 * of held notes: a new note sounds at once; releasing it returns to the
9 * most recent note still held with the gate kept high (legato); releasing
10 * the last note drops the gate and leaves pitch and velocity where they
11 * were, so a release tail keeps its pitch. Pressing a held note again moves
12 * it to the top. When the stack is full the oldest note is forgotten.
13 *
14 * Retrigger as a setting, other priorities, pitch bend and the sustain
15 * pedal are not implemented (sound-byte-libs#118).
16 * Fed from the Drain stage; no MIDI type is needed here, the app routes
17 * note on and note off to it.
18 */
19
20#ifndef SBL_CONTROL_MIDI_KEYS_HPP_
21#define SBL_CONTROL_MIDI_KEYS_HPP_
22
23#include <cstdint>
24
25namespace sbl::control {
26
27class MidiKeys {
28public:
29 static constexpr uint8_t REFERENCE_NOTE = 60; /**< octave 0: middle C */
30 static constexpr uint8_t HELD_NOTES = 8; /**< the stack's depth */
31
32 /** A note on. Velocity 0 is a note off, as MIDI has it. */
33 void note_on(uint8_t note, uint8_t velocity) {
34 if (velocity == 0) {
35 note_off(note);
36 return;
37 }
38 remove(note);
39 if (count_ == HELD_NOTES) {
40 for (uint8_t i = 1; i < count_; ++i) {
41 held_[i - 1] = held_[i];
42 }
43 --count_;
44 }
45 held_[count_++] = {note, velocity};
46 sound(held_[count_ - 1]);
47 gate_ = true;
48 }
49
50 /** A note off. Releasing the sounding note returns to the newest held note, or drops the gate. */
51 void note_off(uint8_t note) {
52 if (!remove(note)) {
53 return;
54 }
55 if (count_ > 0) {
56 sound(held_[count_ - 1]);
57 } else {
58 gate_ = false;
59 }
60 }
61
62 /** Every note released: the gate drops, pitch and velocity stay. */
64 count_ = 0;
65 gate_ = false;
66 }
67
68 /** Octaves relative to REFERENCE_NOTE of the sounding note, or the last one. */
69 float pitch_octaves() const { return pitch_; }
70
71 /** High while any note is held. */
72 bool gate() const { return gate_; }
73
74 /** The sounding note's velocity in [0, 1], 127 at 1; the last one's after release. */
75 float velocity() const { return velocity_; }
76
77 /** How many notes are held. */
78 uint8_t held() const { return count_; }
79
80private:
81 struct Held {
82 uint8_t note;
83 uint8_t velocity;
84 };
85
86 /** Remove a note from the stack, keeping order. True if it was there. */
87 bool remove(uint8_t note) {
88 for (uint8_t i = 0; i < count_; ++i) {
89 if (held_[i].note != note) {
90 continue;
91 }
92 for (uint8_t j = i + 1; j < count_; ++j) {
93 held_[j - 1] = held_[j];
94 }
95 --count_;
96 return true;
97 }
98 return false;
99 }
100
101 void sound(const Held& h) {
102 pitch_ = static_cast<float>(static_cast<int>(h.note) - REFERENCE_NOTE) / 12.0f;
103 velocity_ = static_cast<float>(h.velocity) / 127.0f;
104 }
105
106 Held held_[HELD_NOTES]{};
107 uint8_t count_ = 0;
108 bool gate_ = false;
109 float pitch_ = 0.0f;
110 float velocity_ = 0.0f;
111};
112
113} // namespace sbl::control
114
115#endif // SBL_CONTROL_MIDI_KEYS_HPP_
static constexpr uint8_t REFERENCE_NOTE
Definition midi_keys.hpp:29
void note_on(uint8_t note, uint8_t velocity)
Definition midi_keys.hpp:33
float pitch_octaves() const
Definition midi_keys.hpp:69
uint8_t held() const
Definition midi_keys.hpp:78
static constexpr uint8_t HELD_NOTES
Definition midi_keys.hpp:30
float velocity() const
Definition midi_keys.hpp:75
void note_off(uint8_t note)
Definition midi_keys.hpp:51
The control stack: scheduler, event queue, merge, CC mapping, calibration.