Skip to content

Commit 1ba6ce2

Browse files
committed
Note app now react to midi input
1 parent 1396000 commit 1ba6ce2

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

Applications/Note/Note.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,29 @@ void Note::PlayView() {
322322
playView.AddUIComponent(noteControlBar, Point(0, 8 - CTL_BAR_Y));
323323
}
324324

325+
playView.SetLoopFunc([&]() -> void { // Update Note Highlight (external MIDI input)
326+
327+
MidiPacket midiPacket;
328+
while (MatrixOS::MIDI::Get(&midiPacket))
329+
{
330+
auto handlePadHighlight = [&](NotePad& notePad) -> void {
331+
if (!notePad.rt || !notePad.rt->config) return;
332+
if (midiPacket.Channel() != notePad.rt->config->channel) return;
333+
334+
if (midiPacket.status == NoteOn) {
335+
notePad.SetNoteHighlight(midiPacket.Note(), midiPacket.Velocity() != 0);
336+
} else if (midiPacket.status == NoteOff) {
337+
notePad.SetNoteHighlight(midiPacket.Note(), false);
338+
} else if (midiPacket.status == ControlChange && midiPacket.Controller() == 123) { // All notes off
339+
notePad.ClearNoteHighlight();
340+
}
341+
};
342+
343+
handlePadHighlight(notePad1);
344+
handlePadHighlight(notePad2);
345+
}
346+
});
347+
325348
playView.Start();
326349

327350
if(runtimes[0].noteLatch.IsEnabled())

Applications/Note/NotePad.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "NotePad.h"
22
#include <algorithm>
3+
#include <cstring>
34

45
const Color polyNoteColor[12] = {
56
Color(0x00FFD9),
@@ -138,6 +139,29 @@ void NotePad::UpdateActiveKeyVelocity(Point position, Fract16 velocity) {
138139
}
139140
}
140141

142+
// Note highlighting for visualizing notes from external MIDI input.
143+
void NotePad::SetNoteHighlight(uint8_t note, bool highlight) {
144+
if (note >= 128) return;
145+
uint8_t byteIndex = note / 8;
146+
uint8_t mask = static_cast<uint8_t>(1u << (note % 8));
147+
if (highlight) {
148+
highlightedNotes[byteIndex] |= mask;
149+
} else {
150+
highlightedNotes[byteIndex] &= static_cast<uint8_t>(~mask);
151+
}
152+
}
153+
154+
void NotePad::ClearNoteHighlight() {
155+
memset(highlightedNotes, 0, sizeof(highlightedNotes));
156+
}
157+
158+
bool NotePad::IsNoteHighlighted(uint8_t note) const {
159+
if (note >= 128) return false;
160+
uint8_t byteIndex = note / 8;
161+
uint8_t mask = static_cast<uint8_t>(1u << (note % 8));
162+
return (highlightedNotes[byteIndex] & mask) != 0;
163+
}
164+
141165
void NotePad::GenerateOctaveKeymap() {
142166
noteMap.reserve(dimension.Area());
143167
int16_t root = 12 * rt->config->octave + rt->config->rootKey + rt->config->rootOffset;
@@ -350,7 +374,7 @@ bool NotePad::RenderRootNScale(Point origin) {
350374
if (note == 255) {
351375
MatrixOS::LED::SetColor(globalPos, Color(0));
352376
}
353-
else if (IsNoteActive(note) || rt->midiPipeline.IsNoteActive(note)) { // If find the note is currently active. Show it as white
377+
else if (IsNoteActive(note) || rt->midiPipeline.IsNoteActive(note) || IsNoteHighlighted(note)) { // If find the note is currently active. Show it as white
354378
MatrixOS::LED::SetColor(globalPos, Color::White);
355379
}
356380
else {
@@ -391,7 +415,7 @@ bool NotePad::RenderColorPerKey(Point origin) {
391415
if (note == 255) {
392416
MatrixOS::LED::SetColor(globalPos, Color(0));
393417
}
394-
else if (IsNoteActive(note) || rt->midiPipeline.IsNoteActive(note)) { // If find the note is currently active. Show it as white
418+
else if (IsNoteActive(note) || rt->midiPipeline.IsNoteActive(note) || IsNoteHighlighted(note)) { // If find the note is currently active. Show it as white
395419
MatrixOS::LED::SetColor(globalPos, Color::White);
396420
}
397421
else {

Applications/Note/NotePad.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class NotePad : public UIComponent {
9797
NotePadRuntime* rt;
9898
bool first_scan = true;
9999
std::vector<ActiveKey> activeKeys;
100+
uint8_t highlightedNotes[16] = {0}; // 128 notes as a bitset
100101

101102
NotePad(Dimension dimension, NotePadRuntime* rt);
102103
~NotePad();
@@ -118,6 +119,11 @@ class NotePad : public UIComponent {
118119
void RemoveActiveKey(Point position);
119120
void UpdateActiveKeyVelocity(Point position, Fract16 velocity);
120121

122+
// Note highlighting for visualizing notes from external MIDI input.
123+
void SetNoteHighlight(uint8_t note, bool highlight);
124+
void ClearNoteHighlight();
125+
bool IsNoteHighlighted(uint8_t note) const;
126+
121127
void GenerateOctaveKeymap();
122128
void GenerateOffsetKeymap();
123129
void GenerateChromaticKeymap();
@@ -134,4 +140,4 @@ class NotePad : public UIComponent {
134140

135141
void SetDimension(Dimension dimension);
136142
void SetPadRuntime(NotePadRuntime* rt);
137-
};
143+
};

0 commit comments

Comments
 (0)