Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
midi_keyboard.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <pulp/view/view.hpp>
5#include <set>
6
7namespace pulp::view {
8
9// ── MidiKeyboard ────────────────────────────────────────────────────────────
10// Piano keyboard widget for note input/display.
11
12class MidiKeyboard : public View {
13 public:
15
16 // Range (MIDI note numbers 0-127)
18 int first_note() const {
19 return first_note_;
20 }
21 int last_note() const {
22 return last_note_;
23 }
24
25 // Active/held notes (for display from external MIDI)
26 void note_on(int note, float velocity = 1.0f);
27 void note_off(int note);
29 bool is_note_on(int note) const;
30
31 // Interaction
32 std::function<void(int note, float velocity)> on_note_on;
33 std::function<void(int note)> on_note_off;
34
41
44 if (orientation_ == o)
45 return;
46 orientation_ = o;
48 }
50 return orientation_;
51 }
52
53 // Display options
55 void set_show_note_names(bool show) {
56 show_names_ = show;
57 }
58 bool show_note_names() const {
59 return show_names_;
60 }
61
63 highlight_color_ = c;
64 }
65
66 void paint(canvas::Canvas& canvas) override;
67 void on_mouse_down(Point pos) override;
68 void on_mouse_up(Point pos) override;
69 void on_mouse_drag(Point pos) override;
70 void on_mouse_event(const MouseEvent& event) override;
71 bool wants_mouse_input() const override {
72 return true;
73 }
74
75 float intrinsic_height() const override {
76 return 80.0f;
77 }
78
79 private:
80 int first_note_ = 36; // C2
81 int last_note_ = 96; // C7
83 bool show_names_ = false;
84 // Transparent sentinel (a==0) means "no explicit highlight set" — paint()
85 // then falls back to the theme's accent.primary for active keys. A default
86 // Color{} is OPAQUE black (a=1), which would make every pressed key render
87 // black instead of the accent; keep this explicitly transparent.
88 Color highlight_color_{0.0f, 0.0f, 0.0f, 0.0f};
89
90 struct NoteState {
91 float velocity = 0;
92 bool active = false;
93 };
94 NoteState note_states_[128] = {};
95 int mouse_note_ = -1;
96
97 bool is_black_key(int note) const;
98 int note_at_position(Point pos) const;
99 Rect key_rect(int note) const;
100 float white_key_width() const;
101 int white_key_count() const;
102};
103
104} // namespace pulp::view
Definition canvas.hpp:240
void set_highlight_color(Color c)
Definition midi_keyboard.hpp:62
void on_mouse_up(Point pos) override
void set_show_note_names(bool show)
Show C octave labels using the MIDI convention where note 60 is C4.
Definition midi_keyboard.hpp:55
bool is_note_on(int note) const
void on_mouse_event(const MouseEvent &event) override
Mouse down with full event context.
void on_mouse_drag(Point pos) override
bool show_note_names() const
Definition midi_keyboard.hpp:58
void note_off(int note)
Orientation
Keyboard geometry.
Definition midi_keyboard.hpp:36
@ horizontal
Definition midi_keyboard.hpp:37
@ vertical_chromatic_rows
Equal semitone rows, with last_note() at the top.
Definition midi_keyboard.hpp:39
@ vertical
Physical piano-key proportions.
Definition midi_keyboard.hpp:38
void set_range(int first_note, int last_note)
void paint(canvas::Canvas &canvas) override
int last_note() const
Definition midi_keyboard.hpp:21
void note_on(int note, float velocity=1.0f)
void set_orientation(Orientation o)
Select geometry. Chromatic rows are an explicitly vertical mode.
Definition midi_keyboard.hpp:43
std::function< void(int note)> on_note_off
Definition midi_keyboard.hpp:33
void on_mouse_down(Point pos) override
std::function< void(int note, float velocity)> on_note_on
Definition midi_keyboard.hpp:32
int first_note() const
Definition midi_keyboard.hpp:18
bool wants_mouse_input() const override
True if this widget handles mouse down/drag through virtual methods even when it does not expose JS p...
Definition midi_keyboard.hpp:71
float intrinsic_height() const override
Definition midi_keyboard.hpp:75
Orientation orientation() const
Definition midi_keyboard.hpp:49
void request_repaint()
Request that this view's host invalidate and schedule a repaint.
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
canvas::Color Color
Definition theme.hpp:12
@ c
Definition input_events.hpp:39
@ o
Definition input_events.hpp:40
Rich mouse/pointer event with position, modifiers, button state, pointer ID, and stylus properties.
Definition input_events.hpp:104
Definition geometry.hpp:20