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.

250 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. class NoteComponent : public Component
  19. {
  20. public:
  21. NoteComponent (const MPENote& n, Colour colourToUse)
  22. : note (n), colour (colourToUse)
  23. {
  24. }
  25. //==============================================================================
  26. void update (const MPENote& newNote, Point<float> newCentre)
  27. {
  28. note = newNote;
  29. centre = newCentre;
  30. setBounds (getSquareAroundCentre (jmax (getNoteOnRadius(), getNoteOffRadius(), getPressureRadius()))
  31. .getUnion (getTextRectangle())
  32. .getSmallestIntegerContainer()
  33. .expanded (3));
  34. repaint();
  35. }
  36. //==============================================================================
  37. void paint (Graphics& g) override
  38. {
  39. if (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained)
  40. drawPressedNoteCircle (g, colour);
  41. else if (note.keyState == MPENote::sustained)
  42. drawSustainedNoteCircle (g, colour);
  43. else
  44. return;
  45. drawNoteLabel (g, colour);
  46. }
  47. //==============================================================================
  48. MPENote note;
  49. Colour colour;
  50. Point<float> centre;
  51. private:
  52. //==============================================================================
  53. void drawPressedNoteCircle (Graphics& g, Colour zoneColour)
  54. {
  55. g.setColour (zoneColour.withAlpha (0.3f));
  56. g.fillEllipse (translateToLocalBounds (getSquareAroundCentre (getNoteOnRadius())));
  57. g.setColour (zoneColour);
  58. g.drawEllipse (translateToLocalBounds (getSquareAroundCentre (getPressureRadius())), 2.0f);
  59. }
  60. //==============================================================================
  61. void drawSustainedNoteCircle (Graphics& g, Colour zoneColour)
  62. {
  63. g.setColour (zoneColour);
  64. Path circle, dashedCircle;
  65. circle.addEllipse (translateToLocalBounds (getSquareAroundCentre (getNoteOffRadius())));
  66. const float dashLengths[] = { 3.0f, 3.0f };
  67. PathStrokeType (2.0, PathStrokeType::mitered).createDashedStroke (dashedCircle, circle, dashLengths, 2);
  68. g.fillPath (dashedCircle);
  69. }
  70. //==============================================================================
  71. void drawNoteLabel (Graphics& g, Colour zoneColour)
  72. {
  73. Rectangle<int> textBounds = translateToLocalBounds (getTextRectangle()).getSmallestIntegerContainer();
  74. g.drawText ("+", textBounds, Justification::centred);
  75. g.drawText (MidiMessage::getMidiNoteName (note.initialNote, true, true, 3), textBounds, Justification::centredBottom);
  76. g.setFont (Font (22.0f, Font::bold));
  77. g.drawText (String (note.midiChannel), textBounds, Justification::centredTop);
  78. }
  79. //==============================================================================
  80. Rectangle<float> getSquareAroundCentre (float radius) const noexcept
  81. {
  82. return Rectangle<float> (radius * 2.0f, radius * 2.0f).withCentre (centre);
  83. }
  84. Rectangle<float> translateToLocalBounds (Rectangle<float> r) const noexcept
  85. {
  86. return r - getPosition().toFloat();
  87. }
  88. Rectangle<float> getTextRectangle() const noexcept
  89. {
  90. return Rectangle<float> (30.0f, 50.0f).withCentre (centre);
  91. }
  92. float getNoteOnRadius() const noexcept { return note.noteOnVelocity.asUnsignedFloat() * maxNoteRadius; }
  93. float getNoteOffRadius() const noexcept { return note.noteOffVelocity.asUnsignedFloat() * maxNoteRadius; }
  94. float getPressureRadius() const noexcept { return note.pressure.asUnsignedFloat() * maxNoteRadius; }
  95. const float maxNoteRadius = 100.0f;
  96. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoteComponent)
  97. };
  98. //==============================================================================
  99. class Visualiser : public Component,
  100. public MPEInstrument::Listener,
  101. private AsyncUpdater
  102. {
  103. public:
  104. //==============================================================================
  105. Visualiser (const ZoneColourPicker& zoneColourPicker)
  106. : colourPicker (zoneColourPicker)
  107. {}
  108. //==============================================================================
  109. void paint (Graphics& g) override
  110. {
  111. g.fillAll (Colours::black);
  112. float noteDistance = float (getWidth()) / 128;
  113. for (int i = 0; i < 128; ++i)
  114. {
  115. float x = noteDistance * i;
  116. int noteHeight = int (MidiMessage::isMidiNoteBlack (i) ? 0.7 * getHeight() : getHeight());
  117. g.setColour (MidiMessage::isMidiNoteBlack (i) ? Colours::white : Colours::grey);
  118. g.drawLine (x, 0.0f, x, (float) noteHeight);
  119. if (i > 0 && i % 12 == 0)
  120. {
  121. g.setColour (Colours::grey);
  122. int octaveNumber = (i / 12) - 2;
  123. g.drawText ("C" + String (octaveNumber), (int) x - 15, getHeight() - 30, 30, 30, Justification::centredBottom);
  124. }
  125. }
  126. }
  127. //==============================================================================
  128. void noteAdded (MPENote newNote) override
  129. {
  130. const ScopedLock sl (lock);
  131. activeNotes.add (newNote);
  132. triggerAsyncUpdate();
  133. }
  134. void notePressureChanged (MPENote note) override { noteChanged (note); }
  135. void notePitchbendChanged (MPENote note) override { noteChanged (note); }
  136. void noteTimbreChanged (MPENote note) override { noteChanged (note); }
  137. void noteKeyStateChanged (MPENote note) override { noteChanged (note); }
  138. void noteChanged (MPENote changedNote)
  139. {
  140. const ScopedLock sl (lock);
  141. for (auto& note : activeNotes)
  142. if (note.noteID == changedNote.noteID)
  143. note = changedNote;
  144. triggerAsyncUpdate();
  145. }
  146. void noteReleased (MPENote finishedNote) override
  147. {
  148. const ScopedLock sl (lock);
  149. for (int i = activeNotes.size(); --i >= 0;)
  150. if (activeNotes.getReference(i).noteID == finishedNote.noteID)
  151. activeNotes.remove (i);
  152. triggerAsyncUpdate();
  153. }
  154. private:
  155. //==============================================================================
  156. MPENote* findActiveNote (int noteID) const noexcept
  157. {
  158. for (auto& note : activeNotes)
  159. if (note.noteID == noteID)
  160. return &note;
  161. return nullptr;
  162. }
  163. NoteComponent* findNoteComponent (int noteID) const noexcept
  164. {
  165. for (auto& noteComp : noteComponents)
  166. if (noteComp->note.noteID == noteID)
  167. return noteComp;
  168. return nullptr;
  169. }
  170. //==============================================================================
  171. void handleAsyncUpdate() override
  172. {
  173. const ScopedLock sl (lock);
  174. for (int i = noteComponents.size(); --i >= 0;)
  175. if (findActiveNote (noteComponents.getUnchecked(i)->note.noteID) == nullptr)
  176. noteComponents.remove (i);
  177. for (auto& note : activeNotes)
  178. if (findNoteComponent (note.noteID) == nullptr)
  179. addAndMakeVisible (noteComponents.add (new NoteComponent (note, colourPicker.getColourForMidiChannel(note.midiChannel))));
  180. for (auto& noteComp : noteComponents)
  181. if (auto* noteInfo = findActiveNote (noteComp->note.noteID))
  182. noteComp->update (*noteInfo, getCentrePositionForNote (*noteInfo));
  183. }
  184. //==============================================================================
  185. Point<float> getCentrePositionForNote (MPENote note) const
  186. {
  187. float n = float (note.initialNote) + float (note.totalPitchbendInSemitones);
  188. float x = getWidth() * n / 128;
  189. float y = getHeight() * (1 - note.timbre.asUnsignedFloat());
  190. return Point<float> (x, y);
  191. }
  192. //==============================================================================
  193. OwnedArray<NoteComponent> noteComponents;
  194. CriticalSection lock;
  195. Array<MPENote> activeNotes;
  196. const ZoneColourPicker& colourPicker;
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Visualiser)
  198. };