Vital
|
A visual and interactive MIDI keyboard component. More...
#include <midi_keyboard.h>
Public Member Functions | |
MidiKeyboard (MidiKeyboardState &state) | |
Constructs the MidiKeyboard component. | |
void | paintBackground (Graphics &g) override |
Paints any background elements of the keyboard, typically key boundaries. | |
void | parentHierarchyChanged () override |
Called when the parent hierarchy changes. | |
void | resized () override |
Called when the component is resized. | |
int | getNoteAtPosition (Point< float > position) |
Determines the MIDI note at a given mouse position. | |
bool | isBlackKeyHeight (Point< float > position) |
Checks if a given position falls within the vertical range of a black key. | |
float | getVelocityForNote (int midi, Point< float > position) |
Calculates the note velocity based on vertical mouse click position. | |
void | init (OpenGlWrapper &open_gl) override |
Initializes OpenGL resources for rendering keys. | |
void | render (OpenGlWrapper &open_gl, bool animate) override |
Renders the keyboard and its keys using OpenGL. | |
void | setPressedKeyPositions () |
Updates which keys are displayed as pressed according to the current MidiKeyboardState. | |
void | destroy (OpenGlWrapper &open_gl) override |
Destroys OpenGL resources when the component is removed or reinitialized. | |
void | mouseDown (const MouseEvent &e) override |
Handles mouse down events to trigger note-on messages. | |
void | mouseUp (const MouseEvent &e) override |
Handles mouse up events to trigger note-off messages. | |
void | mouseEnter (const MouseEvent &e) override |
Handles mouse enter events to update the hovered note. | |
void | mouseExit (const MouseEvent &e) override |
Handles mouse exit events to clear the hovered note. | |
void | mouseDrag (const MouseEvent &e) override |
Handles mouse drag events, allowing note slides across keys. | |
void | mouseMove (const MouseEvent &e) override |
Handles mouse move events to update which note is hovered. | |
void | setMidiChannel (int channel) |
Sets the MIDI channel used by note-on and note-off messages. | |
void | setColors () |
Updates the color scheme of the keys. | |
![]() | |
OpenGlComponent (String name="") | |
Constructs an OpenGlComponent. | |
virtual | ~OpenGlComponent () |
Destructor. | |
void | addRoundedCorners () |
Adds rounded corners to the component's edges. | |
void | addBottomRoundedCorners () |
Adds rounded corners only at the bottom of the component. | |
void | renderCorners (OpenGlWrapper &open_gl, bool animate, Colour color, float rounding) |
Renders the corner shapes using the given color and rounding amount. | |
void | renderCorners (OpenGlWrapper &open_gl, bool animate) |
Renders corners with default body color and rounding. | |
void | repaintBackground () |
Requests a repaint of the component's background on the OpenGL layer. | |
Colour | getBodyColor () const |
Retrieves the component's body color. | |
void | setParent (const SynthSection *parent) |
Sets a pointer to the parent SynthSection for skin value lookups. | |
float | findValue (Skin::ValueId value_id) |
Finds a float value from the skin associated with this component's parent. | |
void | setSkinValues (const Skin &skin) |
Applies the skin overrides to this component's colors. | |
void | setSkinOverride (Skin::SectionOverride skin_override) |
Sets a skin override to control the component's color scheme. | |
force_inline void | checkGlError () |
Checks for and asserts that there are no OpenGL errors. | |
void | setBackgroundColor (const Colour &color) |
Sets the background color of the component for painting operations. | |
Static Public Member Functions | |
static force_inline bool | isWhiteKey (int midi) |
Determines if a given MIDI note number corresponds to a white key. | |
![]() | |
static bool | setViewPort (Component *component, Rectangle< int > bounds, OpenGlWrapper &open_gl) |
Sets the OpenGL viewport to match a specified rectangle within a component. | |
static bool | setViewPort (Component *component, OpenGlWrapper &open_gl) |
Convenience overload that sets the viewport for the entire component's local bounds. | |
static void | setScissor (Component *component, OpenGlWrapper &open_gl) |
Sets the OpenGL scissor region to the entire component's local bounds. | |
static void | setScissorBounds (Component *component, Rectangle< int > bounds, OpenGlWrapper &open_gl) |
Sets the OpenGL scissor region to a specified rectangle within a component. | |
static std::unique_ptr< OpenGLShaderProgram::Uniform > | getUniform (const OpenGlWrapper &open_gl, const OpenGLShaderProgram &program, const char *name) |
Retrieves a uniform from the shader program if it exists. | |
static std::unique_ptr< OpenGLShaderProgram::Attribute > | getAttribute (const OpenGlWrapper &open_gl, const OpenGLShaderProgram &program, const char *name) |
Retrieves an attribute from the shader program if it exists. | |
static String | translateFragmentShader (const String &code) |
Translates a fragment shader code snippet to be compatible with the current GL version. | |
static String | translateVertexShader (const String &code) |
Translates a vertex shader code snippet to be compatible with the current GL version. | |
Static Public Attributes | |
static const float | kBlackKeyOffsets [] |
Horizontal offsets for black keys relative to white keys, per octave. | |
static const bool | kWhiteKeys [] |
Array indicating which notes (semitones) in an octave are white keys. | |
static constexpr int | kNumWhiteKeys = 75 |
Total number of white keys across the entire MIDI range. | |
static constexpr int | kNumWhiteKeysPerOctave = 7 |
Number of white keys per octave. | |
static constexpr int | kNumBlackKeys = vital::kMidiSize - kNumWhiteKeys |
Total number of black keys across the entire MIDI range. | |
static constexpr int | kNumBlackKeysPerOctave = vital::kNotesPerOctave - kNumWhiteKeysPerOctave |
Number of black keys per octave. | |
static constexpr float | kBlackKeyHeightRatio = 0.7f |
Ratio of keyboard height covered by black keys. | |
static constexpr float | kBlackKeyWidthRatio = 0.8f |
Horizontal ratio of the black key width relative to white keys. | |
Additional Inherited Members | |
![]() | |
bool | setViewPort (OpenGlWrapper &open_gl) |
Sets the viewport for this component using the current OpenGlWrapper. | |
![]() | |
std::unique_ptr< OpenGlCorners > | corners_ |
Optional corners for rounded edges. | |
bool | only_bottom_corners_ |
Flag to round only the bottom corners. | |
Colour | background_color_ |
The background color of the component. | |
Colour | body_color_ |
The body color of the component. | |
const SynthSection * | parent_ |
Pointer to parent SynthSection for skin lookups. | |
Skin::SectionOverride | skin_override_ |
Skin override for custom appearance. | |
const vital::StatusOutput * | num_voices_readout_ |
StatusOutput for voice count lookups. | |
A visual and interactive MIDI keyboard component.
The MidiKeyboard component displays a piano keyboard that can be interacted with using the mouse. Users can press keys to produce note-on messages and release them to produce note-off messages. Keys are dynamically colored based on their current state (pressed, hovered, white/black), and velocities are determined by the vertical click position. The component supports real-time rendering using OpenGL for performance.
MidiKeyboard::MidiKeyboard | ( | MidiKeyboardState & | state | ) |
Constructs the MidiKeyboard component.
state | Reference to a MidiKeyboardState managing current pressed notes. |
|
inlineoverridevirtual |
Destroys OpenGL resources when the component is removed or reinitialized.
open_gl | The OpenGL wrapper. |
Reimplemented from OpenGlComponent.
int MidiKeyboard::getNoteAtPosition | ( | Point< float > | position | ) |
Determines the MIDI note at a given mouse position.
position | The mouse position relative to the component. |
float MidiKeyboard::getVelocityForNote | ( | int | midi, |
Point< float > | position ) |
Calculates the note velocity based on vertical mouse click position.
midi | The MIDI note being triggered. |
position | The mouse position relative to the component. |
|
inlineoverridevirtual |
Initializes OpenGL resources for rendering keys.
open_gl | The OpenGL wrapper. |
Reimplemented from OpenGlComponent.
|
inline |
Checks if a given position falls within the vertical range of a black key.
position | The point to check. |
|
inlinestatic |
Determines if a given MIDI note number corresponds to a white key.
midi | The MIDI note number. |
|
inlineoverride |
Handles mouse down events to trigger note-on messages.
e | The mouse event. |
|
inlineoverride |
Handles mouse drag events, allowing note slides across keys.
e | The mouse event. |
|
inlineoverride |
Handles mouse enter events to update the hovered note.
e | The mouse event. |
|
inlineoverride |
Handles mouse exit events to clear the hovered note.
e | The mouse event. |
|
inlineoverride |
Handles mouse move events to update which note is hovered.
e | The mouse event. |
|
inlineoverride |
Handles mouse up events to trigger note-off messages.
e | The mouse event. |
|
overridevirtual |
Paints any background elements of the keyboard, typically key boundaries.
g | The graphics context. |
Reimplemented from OpenGlComponent.
|
overridevirtual |
Called when the parent hierarchy changes.
Used to apply color schemes from the parent SynthGuiInterface if available.
Reimplemented from OpenGlComponent.
|
overridevirtual |
Renders the keyboard and its keys using OpenGL.
open_gl | The OpenGL wrapper. |
animate | If true, animate transitions or changes in state. |
Implements OpenGlComponent.
|
overridevirtual |
Called when the component is resized.
Recalculates key positions and updates OpenGL quads for keys.
Reimplemented from OpenGlComponent.
void MidiKeyboard::setColors | ( | ) |
Updates the color scheme of the keys.
|
inline |
Sets the MIDI channel used by note-on and note-off messages.
channel | The MIDI channel number (1-based). |
void MidiKeyboard::setPressedKeyPositions | ( | ) |
Updates which keys are displayed as pressed according to the current MidiKeyboardState.
|
staticconstexpr |
Ratio of keyboard height covered by black keys.
|
static |
Horizontal offsets for black keys relative to white keys, per octave.
These offsets define the placement of black keys within each octave, ensuring a visually accurate piano layout.
|
staticconstexpr |
Horizontal ratio of the black key width relative to white keys.
|
staticconstexpr |
Total number of black keys across the entire MIDI range.
|
staticconstexpr |
Number of black keys per octave.
|
staticconstexpr |
Total number of white keys across the entire MIDI range.
|
staticconstexpr |
Number of white keys per octave.
|
static |
Array indicating which notes (semitones) in an octave are white keys.
True values represent white keys, false values represent black keys.