The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A component that displays a piano keyboard, whose notes can be clicked on.
  23. This component will mimic a physical midi keyboard, showing the current state of
  24. a MidiKeyboardState object. When the on-screen keys are clicked on, it will play these
  25. notes by calling the noteOn() and noteOff() methods of its MidiKeyboardState object.
  26. Another feature is that the computer keyboard can also be used to play notes. By
  27. default it maps the top two rows of a standard qwerty keyboard to the notes, but
  28. these can be remapped if needed. It will only respond to keypresses when it has
  29. the keyboard focus, so to disable this feature you can call setWantsKeyboardFocus (false).
  30. The component is also a ChangeBroadcaster, so if you want to be informed when the
  31. keyboard is scrolled, you can register a ChangeListener for callbacks.
  32. @see MidiKeyboardState
  33. @tags{Audio}
  34. */
  35. class JUCE_API MidiKeyboardComponent : public KeyboardComponentBase,
  36. private MidiKeyboardState::Listener,
  37. private Timer
  38. {
  39. public:
  40. //==============================================================================
  41. /** Creates a MidiKeyboardComponent.
  42. @param state the midi keyboard model that this component will represent
  43. @param orientation whether the keyboard is horizontal or vertical
  44. */
  45. MidiKeyboardComponent (MidiKeyboardState& state, Orientation orientation);
  46. /** Destructor. */
  47. ~MidiKeyboardComponent() override;
  48. //==============================================================================
  49. /** Changes the velocity used in midi note-on messages that are triggered by clicking
  50. on the component.
  51. Values are 0 to 1.0, where 1.0 is the heaviest.
  52. @see setMidiChannel
  53. */
  54. void setVelocity (float velocity, bool useMousePositionForVelocity);
  55. //==============================================================================
  56. /** Changes the midi channel number that will be used for events triggered by clicking
  57. on the component.
  58. The channel must be between 1 and 16 (inclusive). This is the channel that will be
  59. passed on to the MidiKeyboardState::noteOn() method when the user clicks the component.
  60. Although this is the channel used for outgoing events, the component can display
  61. incoming events from more than one channel - see setMidiChannelsToDisplay()
  62. @see setVelocity
  63. */
  64. void setMidiChannel (int midiChannelNumber);
  65. /** Returns the midi channel that the keyboard is using for midi messages.
  66. @see setMidiChannel
  67. */
  68. int getMidiChannel() const noexcept { return midiChannel; }
  69. /** Sets a mask to indicate which incoming midi channels should be represented by
  70. key movements.
  71. The mask is a set of bits, where bit 0 = midi channel 1, bit 1 = midi channel 2, etc.
  72. If the MidiKeyboardState has a key down for any of the channels whose bits are set
  73. in this mask, the on-screen keys will also go down.
  74. By default, this mask is set to 0xffff (all channels displayed).
  75. @see setMidiChannel
  76. */
  77. void setMidiChannelsToDisplay (int midiChannelMask);
  78. /** Returns the current set of midi channels represented by the component.
  79. This is the value that was set with setMidiChannelsToDisplay().
  80. */
  81. int getMidiChannelsToDisplay() const noexcept { return midiInChannelMask; }
  82. //==============================================================================
  83. /** Deletes all key-mappings.
  84. @see setKeyPressForNote
  85. */
  86. void clearKeyMappings();
  87. /** Maps a key-press to a given note.
  88. @param key the key that should trigger the note
  89. @param midiNoteOffsetFromC how many semitones above C the triggered note should
  90. be. The actual midi note that gets played will be
  91. this value + (12 * the current base octave). To change
  92. the base octave, see setKeyPressBaseOctave()
  93. */
  94. void setKeyPressForNote (const KeyPress& key, int midiNoteOffsetFromC);
  95. /** Removes any key-mappings for a given note.
  96. For a description of what the note number means, see setKeyPressForNote().
  97. */
  98. void removeKeyPressForNote (int midiNoteOffsetFromC);
  99. /** Changes the base note above which key-press-triggered notes are played.
  100. The set of key-mappings that trigger notes can be moved up and down to cover
  101. the entire scale using this method.
  102. The value passed in is an octave number between 0 and 10 (inclusive), and
  103. indicates which C is the base note to which the key-mapped notes are
  104. relative.
  105. */
  106. void setKeyPressBaseOctave (int newOctaveNumber);
  107. //==============================================================================
  108. /** A set of colour IDs to use to change the colour of various aspects of the keyboard.
  109. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  110. methods.
  111. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  112. */
  113. enum ColourIds
  114. {
  115. whiteNoteColourId = 0x1005000,
  116. blackNoteColourId = 0x1005001,
  117. keySeparatorLineColourId = 0x1005002,
  118. mouseOverKeyOverlayColourId = 0x1005003, /**< This colour will be overlaid on the normal note colour. */
  119. keyDownOverlayColourId = 0x1005004, /**< This colour will be overlaid on the normal note colour. */
  120. textLabelColourId = 0x1005005,
  121. shadowColourId = 0x1005006
  122. };
  123. //==============================================================================
  124. /** Use this method to draw a white note of the keyboard in a given rectangle.
  125. isOver indicates whether the mouse is over the key, isDown indicates whether the key is
  126. currently pressed down.
  127. When doing this, be sure to note the keyboard's orientation.
  128. */
  129. virtual void drawWhiteNote (int midiNoteNumber, Graphics& g, Rectangle<float> area,
  130. bool isDown, bool isOver, Colour lineColour, Colour textColour);
  131. /** Use this method to draw a black note of the keyboard in a given rectangle.
  132. isOver indicates whether the mouse is over the key, isDown indicates whether the key is
  133. currently pressed down.
  134. When doing this, be sure to note the keyboard's orientation.
  135. */
  136. virtual void drawBlackNote (int midiNoteNumber, Graphics& g, Rectangle<float> area,
  137. bool isDown, bool isOver, Colour noteFillColour);
  138. /** Callback when the mouse is clicked on a key.
  139. You could use this to do things like handle right-clicks on keys, etc.
  140. Return true if you want the click to trigger the note, or false if you
  141. want to handle it yourself and not have the note played.
  142. @see mouseDraggedToKey
  143. */
  144. virtual bool mouseDownOnKey (int midiNoteNumber, const MouseEvent& e);
  145. /** Callback when the mouse is dragged from one key onto another.
  146. Return true if you want the drag to trigger the new note, or false if you
  147. want to handle it yourself and not have the note played.
  148. @see mouseDownOnKey
  149. */
  150. virtual bool mouseDraggedToKey (int midiNoteNumber, const MouseEvent& e);
  151. /** Callback when the mouse is released from a key.
  152. @see mouseDownOnKey
  153. */
  154. virtual void mouseUpOnKey (int midiNoteNumber, const MouseEvent& e);
  155. /** Allows text to be drawn on the white notes.
  156. By default this is used to label the C in each octave, but could be used for other things.
  157. @see setOctaveForMiddleC
  158. */
  159. virtual String getWhiteNoteText (int midiNoteNumber);
  160. //==============================================================================
  161. /** @internal */
  162. void mouseMove (const MouseEvent&) override;
  163. /** @internal */
  164. void mouseDrag (const MouseEvent&) override;
  165. /** @internal */
  166. void mouseDown (const MouseEvent&) override;
  167. /** @internal */
  168. void mouseUp (const MouseEvent&) override;
  169. /** @internal */
  170. void mouseEnter (const MouseEvent&) override;
  171. /** @internal */
  172. void mouseExit (const MouseEvent&) override;
  173. /** @internal */
  174. void timerCallback() override;
  175. /** @internal */
  176. bool keyStateChanged (bool isKeyDown) override;
  177. /** @internal */
  178. bool keyPressed (const KeyPress&) override;
  179. /** @internal */
  180. void focusLost (FocusChangeType) override;
  181. /** @internal */
  182. void colourChanged() override;
  183. private:
  184. //==============================================================================
  185. void drawKeyboardBackground (Graphics& g, Rectangle<float> area) override final;
  186. void drawWhiteKey (int midiNoteNumber, Graphics& g, Rectangle<float> area) override final;
  187. void drawBlackKey (int midiNoteNumber, Graphics& g, Rectangle<float> area) override final;
  188. void handleNoteOn (MidiKeyboardState*, int, int, float) override;
  189. void handleNoteOff (MidiKeyboardState*, int, int, float) override;
  190. //==============================================================================
  191. void resetAnyKeysInUse();
  192. void updateNoteUnderMouse (Point<float>, bool isDown, int fingerNum);
  193. void updateNoteUnderMouse (const MouseEvent&, bool isDown);
  194. void repaintNote (int midiNoteNumber);
  195. //==============================================================================
  196. MidiKeyboardState& state;
  197. int midiChannel = 1, midiInChannelMask = 0xffff;
  198. int keyMappingOctave = 6;
  199. float velocity = 1.0f;
  200. bool useMousePositionForVelocity = true;
  201. Array<int> mouseOverNotes, mouseDownNotes;
  202. Array<KeyPress> keyPresses;
  203. Array<int> keyPressNotes;
  204. BigInteger keysPressed, keysCurrentlyDrawnDown;
  205. std::atomic<bool> noPendingUpdates { true };
  206. //==============================================================================
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiKeyboardComponent)
  208. };
  209. } // namespace juce