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.

147 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A component that displays an MPE-compatible keyboard, whose notes can be clicked on.
  18. This component will mimic a physical MPE-compatible keyboard, showing the current state
  19. of an MPEInstrument object. When the on-screen keys are clicked on, it will play these
  20. notes by calling the noteOn() and noteOff() methods of its MPEInstrument object. Moving
  21. the mouse will update the pitchbend and timbre dimensions of the MPEInstrument.
  22. @see MPEInstrument
  23. @tags{Audio}
  24. */
  25. class JUCE_API MPEKeyboardComponent : public KeyboardComponentBase,
  26. private MPEInstrument::Listener,
  27. private Timer
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an MPEKeyboardComponent.
  32. @param instrument the MPEInstrument that this component represents
  33. @param orientation whether the keyboard is horizontal or vertical
  34. */
  35. MPEKeyboardComponent (MPEInstrument& instrument, Orientation orientation);
  36. /** Destructor. */
  37. virtual ~MPEKeyboardComponent() override;
  38. //==============================================================================
  39. /** Sets the note-on velocity, or "strike", value that will be used when triggering new notes. */
  40. void setVelocity (float newVelocity) { velocity = jlimit (newVelocity, 0.0f, 1.0f); }
  41. /** Sets the pressure value that will be used for new notes. */
  42. void setPressure (float newPressure) { pressure = jlimit (newPressure, 0.0f, 1.0f); }
  43. /** Sets the note-off velocity, or "lift", value that will be used when notes are released. */
  44. void setLift (float newLift) { lift = jlimit (newLift, 0.0f, 1.0f); }
  45. /** Use this to enable the mouse source pressure to be used for the initial note-on
  46. velocity, or "strike", value if the mouse source supports it.
  47. */
  48. void setUseMouseSourcePressureForStrike (bool usePressureForStrike) { useMouseSourcePressureForStrike = usePressureForStrike; }
  49. //==============================================================================
  50. /** A set of colour IDs to use to change the colour of various aspects of the keyboard.
  51. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  52. methods.
  53. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  54. */
  55. enum ColourIds
  56. {
  57. whiteNoteColourId = 0x1006000,
  58. blackNoteColourId = 0x1006001,
  59. textLabelColourId = 0x1006002,
  60. noteCircleFillColourId = 0x1006003,
  61. noteCircleOutlineColourId = 0x1006004
  62. };
  63. //==============================================================================
  64. /** @internal */
  65. void mouseDrag (const MouseEvent&) override;
  66. /** @internal */
  67. void mouseDown (const MouseEvent&) override;
  68. /** @internal */
  69. void mouseUp (const MouseEvent&) override;
  70. /** @internal */
  71. void focusLost (FocusChangeType) override;
  72. /** @internal */
  73. void colourChanged() override;
  74. private:
  75. //==========================================================================
  76. struct MPENoteComponent;
  77. //==============================================================================
  78. void drawKeyboardBackground (Graphics& g, Rectangle<float> area) override;
  79. void drawWhiteKey (int midiNoteNumber, Graphics& g, Rectangle<float> area) override;
  80. void drawBlackKey (int midiNoteNumber, Graphics& g, Rectangle<float> area) override;
  81. void updateNoteData (MPENote&);
  82. void noteAdded (MPENote) override;
  83. void notePressureChanged (MPENote) override;
  84. void notePitchbendChanged (MPENote) override;
  85. void noteTimbreChanged (MPENote) override;
  86. void noteReleased (MPENote) override;
  87. void zoneLayoutChanged() override;
  88. void timerCallback() override;
  89. //==============================================================================
  90. MPEValue mousePositionToPitchbend (int, Point<float>);
  91. MPEValue mousePositionToTimbre (Point<float>);
  92. void addNewNote (MPENote);
  93. void removeNote (MPENote);
  94. void handleNoteOns (std::set<MPENote>&);
  95. void handleNoteOffs (std::set<MPENote>&);
  96. void updateNoteComponentBounds (const MPENote&, MPENoteComponent&);
  97. void updateNoteComponents();
  98. void updateZoneLayout();
  99. //==============================================================================
  100. MPEInstrument& instrument;
  101. std::unique_ptr<MPEChannelAssigner> channelAssigner;
  102. CriticalSection activeNotesLock;
  103. std::vector<std::pair<MPENote, bool>> activeNotes;
  104. std::vector<std::unique_ptr<MPENoteComponent>> noteComponents;
  105. std::map<int, uint16> sourceIDMap;
  106. float velocity = 0.7f, pressure = 1.0f, lift = 0.0f;
  107. bool useMouseSourcePressureForStrike = false;
  108. int perNotePitchbendRange = 48;
  109. //==============================================================================
  110. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MPEKeyboardComponent)
  111. };
  112. } // namespace juce