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.

252 lines
9.1KB

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