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.

254 lines
9.2KB

  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. #ifndef VISUALISER_H_INCLUDED
  18. #define VISUALISER_H_INCLUDED
  19. class NoteComponent : public Component
  20. {
  21. public:
  22. NoteComponent (const MPENote& n, Colour colourToUse)
  23. : note (n), colour (colourToUse)
  24. {
  25. }
  26. //==============================================================================
  27. void update (const MPENote& newNote, Point<float> newCentre)
  28. {
  29. note = newNote;
  30. centre = newCentre;
  31. setBounds (getSquareAroundCentre (jmax (getNoteOnRadius(), getNoteOffRadius(), getPressureRadius()))
  32. .getUnion (getTextRectangle())
  33. .getSmallestIntegerContainer()
  34. .expanded (3));
  35. repaint();
  36. }
  37. //==============================================================================
  38. void paint (Graphics& g) override
  39. {
  40. if (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained)
  41. drawPressedNoteCircle (g, colour);
  42. else if (note.keyState == MPENote::sustained)
  43. drawSustainedNoteCircle (g, colour);
  44. else
  45. return;
  46. drawNoteLabel (g, colour);
  47. }
  48. //==============================================================================
  49. MPENote note;
  50. Colour colour;
  51. Point<float> centre;
  52. private:
  53. //==============================================================================
  54. void drawPressedNoteCircle (Graphics& g, Colour zoneColour)
  55. {
  56. g.setColour (zoneColour.withAlpha (0.3f));
  57. g.fillEllipse (translateToLocalBounds (getSquareAroundCentre (getNoteOnRadius())));
  58. g.setColour (zoneColour);
  59. g.drawEllipse (translateToLocalBounds (getSquareAroundCentre (getPressureRadius())), 2.0f);
  60. }
  61. //==============================================================================
  62. void drawSustainedNoteCircle (Graphics& g, Colour zoneColour)
  63. {
  64. g.setColour (zoneColour);
  65. Path circle, dashedCircle;
  66. circle.addEllipse (translateToLocalBounds (getSquareAroundCentre (getNoteOffRadius())));
  67. const float dashLengths[] = { 3.0f, 3.0f };
  68. PathStrokeType (2.0, PathStrokeType::mitered).createDashedStroke (dashedCircle, circle, dashLengths, 2);
  69. g.fillPath (dashedCircle);
  70. }
  71. //==============================================================================
  72. void drawNoteLabel (Graphics& g, Colour zoneColour)
  73. {
  74. Rectangle<int> textBounds = translateToLocalBounds (getTextRectangle()).getSmallestIntegerContainer();
  75. g.drawText ("+", textBounds, Justification::centred);
  76. g.drawText (MidiMessage::getMidiNoteName (note.initialNote, true, true, 3), textBounds, Justification::centredBottom);
  77. g.setFont (Font (22.0f, Font::bold));
  78. g.drawText (String (note.midiChannel), textBounds, Justification::centredTop);
  79. }
  80. //==============================================================================
  81. Rectangle<float> getSquareAroundCentre (float radius) const noexcept
  82. {
  83. return Rectangle<float> (radius * 2.0f, radius * 2.0f).withCentre (centre);
  84. }
  85. Rectangle<float> translateToLocalBounds (Rectangle<float> r) const noexcept
  86. {
  87. return r - getPosition().toFloat();
  88. }
  89. Rectangle<float> getTextRectangle() const noexcept
  90. {
  91. return Rectangle<float> (30.0f, 50.0f).withCentre (centre);
  92. }
  93. float getNoteOnRadius() const noexcept { return note.noteOnVelocity.asUnsignedFloat() * maxNoteRadius; }
  94. float getNoteOffRadius() const noexcept { return note.noteOffVelocity.asUnsignedFloat() * maxNoteRadius; }
  95. float getPressureRadius() const noexcept { return note.pressure.asUnsignedFloat() * maxNoteRadius; }
  96. const float maxNoteRadius = 100.0f;
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoteComponent)
  98. };
  99. //==============================================================================
  100. class Visualiser : public Component,
  101. public MPEInstrument::Listener,
  102. private AsyncUpdater
  103. {
  104. public:
  105. //==============================================================================
  106. Visualiser (const ZoneColourPicker& zoneColourPicker)
  107. : colourPicker (zoneColourPicker)
  108. {}
  109. //==============================================================================
  110. void paint (Graphics& g) override
  111. {
  112. g.fillAll (Colours::black);
  113. float noteDistance = float (getWidth()) / 128;
  114. for (int i = 0; i < 128; ++i)
  115. {
  116. float x = noteDistance * i;
  117. int noteHeight = int (MidiMessage::isMidiNoteBlack (i) ? 0.7 * getHeight() : getHeight());
  118. g.setColour (MidiMessage::isMidiNoteBlack (i) ? Colours::white : Colours::grey);
  119. g.drawLine (x, 0.0f, x, (float) noteHeight);
  120. if (i > 0 && i % 12 == 0)
  121. {
  122. g.setColour (Colours::grey);
  123. int octaveNumber = (i / 12) - 2;
  124. g.drawText ("C" + String (octaveNumber), (int) x - 15, getHeight() - 30, 30, 30, Justification::centredBottom);
  125. }
  126. }
  127. }
  128. //==============================================================================
  129. void noteAdded (MPENote newNote) override
  130. {
  131. const ScopedLock sl (lock);
  132. activeNotes.add (newNote);
  133. triggerAsyncUpdate();
  134. }
  135. void notePressureChanged (MPENote note) override { noteChanged (note); }
  136. void notePitchbendChanged (MPENote note) override { noteChanged (note); }
  137. void noteTimbreChanged (MPENote note) override { noteChanged (note); }
  138. void noteKeyStateChanged (MPENote note) override { noteChanged (note); }
  139. void noteChanged (MPENote changedNote)
  140. {
  141. const ScopedLock sl (lock);
  142. for (auto& note : activeNotes)
  143. if (note.noteID == changedNote.noteID)
  144. note = changedNote;
  145. triggerAsyncUpdate();
  146. }
  147. void noteReleased (MPENote finishedNote) override
  148. {
  149. const ScopedLock sl (lock);
  150. for (int i = activeNotes.size(); --i >= 0;)
  151. if (activeNotes.getReference(i).noteID == finishedNote.noteID)
  152. activeNotes.remove (i);
  153. triggerAsyncUpdate();
  154. }
  155. private:
  156. //==============================================================================
  157. MPENote* findActiveNote (int noteID) const noexcept
  158. {
  159. for (auto& note : activeNotes)
  160. if (note.noteID == noteID)
  161. return &note;
  162. return nullptr;
  163. }
  164. NoteComponent* findNoteComponent (int noteID) const noexcept
  165. {
  166. for (auto& noteComp : noteComponents)
  167. if (noteComp->note.noteID == noteID)
  168. return noteComp;
  169. return nullptr;
  170. }
  171. //==============================================================================
  172. void handleAsyncUpdate() override
  173. {
  174. const ScopedLock sl (lock);
  175. for (int i = noteComponents.size(); --i >= 0;)
  176. if (findActiveNote (noteComponents.getUnchecked(i)->note.noteID) == nullptr)
  177. noteComponents.remove (i);
  178. for (auto& note : activeNotes)
  179. if (findNoteComponent (note.noteID) == nullptr)
  180. addAndMakeVisible (noteComponents.add (new NoteComponent (note, colourPicker.getColourForMidiChannel(note.midiChannel))));
  181. for (auto& noteComp : noteComponents)
  182. if (auto* noteInfo = findActiveNote (noteComp->note.noteID))
  183. noteComp->update (*noteInfo, getCentrePositionForNote (*noteInfo));
  184. }
  185. //==============================================================================
  186. Point<float> getCentrePositionForNote (MPENote note) const
  187. {
  188. float n = float (note.initialNote) + float (note.totalPitchbendInSemitones);
  189. float x = getWidth() * n / 128;
  190. float y = getHeight() * (1 - note.timbre.asUnsignedFloat());
  191. return Point<float> (x, y);
  192. }
  193. //==============================================================================
  194. OwnedArray<NoteComponent> noteComponents;
  195. CriticalSection lock;
  196. Array<MPENote> activeNotes;
  197. const ZoneColourPicker& colourPicker;
  198. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Visualiser)
  199. };
  200. #endif // VISUALISER_H_INCLUDED