Deluge Firmware
1.3.0
Build date: 2026.08.29
Toggle main menu visibility
Loading...
Searching...
No Matches
notes_state.h
1
/*
2
* Copyright © 2016-2023 Synthstrom Audible Limited
3
*
4
* This file is part of The Synthstrom Audible Deluge Firmware.
5
*
6
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7
* terms of the GNU General Public License as published by the Free Software Foundation,
8
* either version 3 of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
* See the GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License along with this program.
15
* If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
#pragma once
19
20
#include "definitions_cxx.hpp"
21
#include <array>
22
#include <bitset>
23
#include <climits>
24
#include <cstdint>
25
#include <cstring>
26
27
constexpr
uint8_t kMaxNumActiveNotes = 10;
28
29
namespace
deluge::gui::ui::keyboard {
30
31
struct
PressedPad
: Cartesian {
32
uint32_t timeLastPadPress;
33
bool
padPressHeld;
34
bool
active;
35
// all evaluatePads will be called at least once with pad.active == false on release. Following
36
// that, dead will be set to true to avoid repeatedly processing releases.
37
// exception - if the pad is "used up" by switching keyboard columns it will be set dead immediately to prevent
38
// processing its release, while still being set as active
39
bool
dead;
40
};
41
42
struct
NoteState
{
43
uint8_t note = 0;
46
uint8_t
activationCount
= 0;
47
uint8_t velocity = 0;
48
int16_t mpeValues[3] = {0};
50
bool
generatedNote
=
false
;
51
};
52
53
// Always needs to be 0 currently for the math to math
54
constexpr
uint8_t kLowestKeyboardNote = 0;
55
constexpr
uint8_t kHighestKeyboardNote = kOctaveSize * 12;
56
struct
NotesState
{
57
using
NoteArray = std::array<NoteState, kMaxNumActiveNotes>;
58
59
std::bitset<kHighestKeyboardNote> states;
60
NoteArray notes;
61
uint8_t count = 0;
62
63
[[nodiscard]] NoteArray::iterator begin() {
return
notes.begin(); }
64
65
[[nodiscard]] NoteArray::iterator end() {
return
notes.begin() + count; }
66
67
[[nodiscard]] NoteArray::const_iterator begin()
const
{
return
notes.begin(); }
68
69
[[nodiscard]] NoteArray::const_iterator end()
const
{
return
notes.begin() + count; }
70
71
NoteArray::size_type enableNote(uint8_t note, uint8_t velocity,
bool
generatedNote =
false
,
72
int16_t* mpeValues =
nullptr
) {
73
// Out-of-range notes (e.g. from chord voicings transposed past the top of the keyboard) must be
74
// ignored: `states` is a fixed-size bitset and indexing it out of bounds corrupts memory.
75
if
(note >= kHighestKeyboardNote) {
76
return
count;
77
}
78
if
(noteEnabled(note)) {
79
for
(
auto
i = 0; i < notes.size(); ++i) {
80
auto
& noteState = notes[i];
81
if
(noteState.note == note) {
82
noteState.activationCount++;
83
return
i;
84
}
85
}
86
}
87
if
(count == kMaxNumActiveNotes) {
88
return
count;
89
}
90
91
NoteArray::size_type idx = count++;
92
NoteState
& state = notes[idx];
93
state.note = note;
94
state.velocity = velocity;
95
state.
generatedNote
= generatedNote;
96
if
(mpeValues !=
nullptr
) {
97
memcpy(&state.mpeValues, mpeValues,
sizeof
(state.mpeValues));
98
}
99
100
states[note] =
true
;
101
102
return
idx;
103
}
104
105
[[nodiscard]]
constexpr
bool
noteEnabled(uint8_t note)
const
{
return
note < kHighestKeyboardNote && states[note]; }
106
};
107
108
};
// namespace deluge::gui::ui::keyboard
deluge::gui::ui::keyboard::NoteState
Definition
notes_state.h:42
deluge::gui::ui::keyboard::NoteState::activationCount
uint8_t activationCount
Definition
notes_state.h:46
deluge::gui::ui::keyboard::NoteState::generatedNote
bool generatedNote
Generated notes will only create sound and not be used for interaction (e.g. setting root note).
Definition
notes_state.h:50
deluge::gui::ui::keyboard::NotesState
Definition
notes_state.h:56
deluge::gui::ui::keyboard::PressedPad
Definition
notes_state.h:31