Vital
Loading...
Searching...
No Matches
MidiKeyboard Class Reference

A visual and interactive MIDI keyboard component. More...

#include <midi_keyboard.h>

Inheritance diagram for MidiKeyboard:
OpenGlComponent

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.
 
- Public Member Functions inherited from OpenGlComponent
 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 Public Member Functions inherited from OpenGlComponent
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

- Protected Member Functions inherited from OpenGlComponent
bool setViewPort (OpenGlWrapper &open_gl)
 Sets the viewport for this component using the current OpenGlWrapper.
 
- Protected Attributes inherited from OpenGlComponent
std::unique_ptr< OpenGlCornerscorners_
 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 SynthSectionparent_
 Pointer to parent SynthSection for skin lookups.
 
Skin::SectionOverride skin_override_
 Skin override for custom appearance.
 
const vital::StatusOutputnum_voices_readout_
 StatusOutput for voice count lookups.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MidiKeyboard()

MidiKeyboard::MidiKeyboard ( MidiKeyboardState & state)

Constructs the MidiKeyboard component.

Parameters
stateReference to a MidiKeyboardState managing current pressed notes.

Member Function Documentation

◆ destroy()

void MidiKeyboard::destroy ( OpenGlWrapper & open_gl)
inlineoverridevirtual

Destroys OpenGL resources when the component is removed or reinitialized.

Parameters
open_glThe OpenGL wrapper.

Reimplemented from OpenGlComponent.

◆ getNoteAtPosition()

int MidiKeyboard::getNoteAtPosition ( Point< float > position)

Determines the MIDI note at a given mouse position.

Parameters
positionThe mouse position relative to the component.
Returns
The MIDI note number, or -1 if the position doesn't correspond to a note.

◆ getVelocityForNote()

float MidiKeyboard::getVelocityForNote ( int midi,
Point< float > position )

Calculates the note velocity based on vertical mouse click position.

Parameters
midiThe MIDI note being triggered.
positionThe mouse position relative to the component.
Returns
A velocity value in the range [0, 1].

◆ init()

void MidiKeyboard::init ( OpenGlWrapper & open_gl)
inlineoverridevirtual

Initializes OpenGL resources for rendering keys.

Parameters
open_glThe OpenGL wrapper.

Reimplemented from OpenGlComponent.

◆ isBlackKeyHeight()

bool MidiKeyboard::isBlackKeyHeight ( Point< float > position)
inline

Checks if a given position falls within the vertical range of a black key.

Parameters
positionThe point to check.
Returns
True if the point is within the black key vertical range.

◆ isWhiteKey()

static force_inline bool MidiKeyboard::isWhiteKey ( int midi)
inlinestatic

Determines if a given MIDI note number corresponds to a white key.

Parameters
midiThe MIDI note number.
Returns
True if it's a white key, false if it's black.

◆ mouseDown()

void MidiKeyboard::mouseDown ( const MouseEvent & e)
inlineoverride

Handles mouse down events to trigger note-on messages.

Parameters
eThe mouse event.

◆ mouseDrag()

void MidiKeyboard::mouseDrag ( const MouseEvent & e)
inlineoverride

Handles mouse drag events, allowing note slides across keys.

Parameters
eThe mouse event.

◆ mouseEnter()

void MidiKeyboard::mouseEnter ( const MouseEvent & e)
inlineoverride

Handles mouse enter events to update the hovered note.

Parameters
eThe mouse event.

◆ mouseExit()

void MidiKeyboard::mouseExit ( const MouseEvent & e)
inlineoverride

Handles mouse exit events to clear the hovered note.

Parameters
eThe mouse event.

◆ mouseMove()

void MidiKeyboard::mouseMove ( const MouseEvent & e)
inlineoverride

Handles mouse move events to update which note is hovered.

Parameters
eThe mouse event.

◆ mouseUp()

void MidiKeyboard::mouseUp ( const MouseEvent & e)
inlineoverride

Handles mouse up events to trigger note-off messages.

Parameters
eThe mouse event.

◆ paintBackground()

void MidiKeyboard::paintBackground ( Graphics & g)
overridevirtual

Paints any background elements of the keyboard, typically key boundaries.

Parameters
gThe graphics context.

Reimplemented from OpenGlComponent.

◆ parentHierarchyChanged()

void MidiKeyboard::parentHierarchyChanged ( )
overridevirtual

Called when the parent hierarchy changes.

Used to apply color schemes from the parent SynthGuiInterface if available.

Reimplemented from OpenGlComponent.

◆ render()

void MidiKeyboard::render ( OpenGlWrapper & open_gl,
bool animate )
overridevirtual

Renders the keyboard and its keys using OpenGL.

Parameters
open_glThe OpenGL wrapper.
animateIf true, animate transitions or changes in state.

Implements OpenGlComponent.

◆ resized()

void MidiKeyboard::resized ( )
overridevirtual

Called when the component is resized.

Recalculates key positions and updates OpenGL quads for keys.

Reimplemented from OpenGlComponent.

◆ setColors()

void MidiKeyboard::setColors ( )

Updates the color scheme of the keys.

◆ setMidiChannel()

void MidiKeyboard::setMidiChannel ( int channel)
inline

Sets the MIDI channel used by note-on and note-off messages.

Parameters
channelThe MIDI channel number (1-based).

◆ setPressedKeyPositions()

void MidiKeyboard::setPressedKeyPositions ( )

Updates which keys are displayed as pressed according to the current MidiKeyboardState.

Member Data Documentation

◆ kBlackKeyHeightRatio

float MidiKeyboard::kBlackKeyHeightRatio = 0.7f
staticconstexpr

Ratio of keyboard height covered by black keys.

◆ kBlackKeyOffsets

const float MidiKeyboard::kBlackKeyOffsets
static
Initial value:
= {
1.0f - 0.6f * kBlackKeyWidthRatio,
2.0f - 0.4f * kBlackKeyWidthRatio,
4.0f - 0.7f * kBlackKeyWidthRatio,
5.0f - 0.5f * kBlackKeyWidthRatio,
6.0f - 0.3f * kBlackKeyWidthRatio,
}
static constexpr float kBlackKeyWidthRatio
Horizontal ratio of the black key width relative to white keys.
Definition midi_keyboard.h:46

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.

◆ kBlackKeyWidthRatio

float MidiKeyboard::kBlackKeyWidthRatio = 0.8f
staticconstexpr

Horizontal ratio of the black key width relative to white keys.

◆ kNumBlackKeys

int MidiKeyboard::kNumBlackKeys = vital::kMidiSize - kNumWhiteKeys
staticconstexpr

Total number of black keys across the entire MIDI range.

◆ kNumBlackKeysPerOctave

int MidiKeyboard::kNumBlackKeysPerOctave = vital::kNotesPerOctave - kNumWhiteKeysPerOctave
staticconstexpr

Number of black keys per octave.

◆ kNumWhiteKeys

int MidiKeyboard::kNumWhiteKeys = 75
staticconstexpr

Total number of white keys across the entire MIDI range.

◆ kNumWhiteKeysPerOctave

int MidiKeyboard::kNumWhiteKeysPerOctave = 7
staticconstexpr

Number of white keys per octave.

◆ kWhiteKeys

const bool MidiKeyboard::kWhiteKeys
static
Initial value:
= {
true, false, true, false, true, true, false, true, false, true, false, true
}

Array indicating which notes (semitones) in an octave are white keys.

True values represent white keys, false values represent black keys.


The documentation for this class was generated from the following files: