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.

219 lines
7.7KB

  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. //==============================================================================
  20. class NoteComponent : public Component
  21. {
  22. public:
  23. NoteComponent (const ExpressiveMidiNote& n) : note (n)
  24. {
  25. }
  26. //==========================================================================
  27. void update (const ExpressiveMidiNote& newNote, Point<float> newCentre)
  28. {
  29. note = newNote;
  30. centre = newCentre;
  31. setBounds (getSquareAroundCentre (jmax (getNoteOnRadius(), getPressureRadius()))
  32. .getUnion (getTextRectangle())
  33. .getSmallestIntegerContainer()
  34. .expanded (3));
  35. repaint();
  36. }
  37. //==========================================================================
  38. void paint (Graphics& g) override
  39. {
  40. Colour colour (Colours::red); // TODO
  41. g.setColour (colour.withAlpha (0.3f));
  42. g.fillEllipse (translateToLocalBounds (getSquareAroundCentre (getNoteOnRadius())));
  43. g.setColour (colour); // TODO
  44. g.drawEllipse (translateToLocalBounds (getSquareAroundCentre (getPressureRadius())), 2.0f);
  45. Rectangle<int> textBounds = translateToLocalBounds (getTextRectangle()).getSmallestIntegerContainer();
  46. g.drawText ("+", textBounds, Justification::centred);
  47. g.drawText (MidiMessage::getMidiNoteName (note.initialNote, true, true, 3), textBounds, Justification::centredBottom);
  48. g.setFont (Font (22.0f, Font::bold));
  49. g.drawText (String (note.midiChannel), textBounds, Justification::centredTop);
  50. }
  51. //==========================================================================
  52. ExpressiveMidiNote note;
  53. Point<float> centre;
  54. private:
  55. //==========================================================================
  56. Rectangle<float> getSquareAroundCentre (float radius) const noexcept
  57. {
  58. return Rectangle<float> (radius * 2.0f, radius * 2.0f).withCentre (centre);
  59. }
  60. Rectangle<float> translateToLocalBounds (Rectangle<float> r) const noexcept
  61. {
  62. return r - getPosition().toFloat();
  63. }
  64. Rectangle<float> getTextRectangle() const noexcept
  65. {
  66. return Rectangle<float> (30.0f, 50.0f).withCentre (centre);
  67. }
  68. float getNoteOnRadius() const { return note.noteOnVelocity.asUnsignedFloat() * maxNoteRadius; }
  69. float getPressureRadius() const { return note.pressure.asUnsignedFloat() * maxNoteRadius; }
  70. const float maxNoteRadius = 100.0f;
  71. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoteComponent)
  72. };
  73. //==============================================================================
  74. class Visualiser : public Component,
  75. public ExpressiveMidiInstrument::Listener,
  76. private AsyncUpdater
  77. {
  78. public:
  79. //==========================================================================
  80. Visualiser() {}
  81. //==========================================================================
  82. void paint (Graphics& g) override
  83. {
  84. g.fillAll (Colours::black);
  85. float noteDistance = float (getWidth()) / 128;
  86. for (int i = 0; i < 128; ++i)
  87. {
  88. float x = noteDistance * i;
  89. int noteHeight = MidiMessage::isMidiNoteBlack (i) ? 0.7 * getHeight() : getHeight();
  90. g.setColour (MidiMessage::isMidiNoteBlack (i) ? Colours::white : Colours::grey);
  91. g.drawLine (x, 0.0f, x, noteHeight);
  92. if (i > 0 && i % 12 == 0)
  93. {
  94. g.setColour (Colours::grey);
  95. int octaveNumber = (i / 12) - 2;
  96. g.drawText ("C" + String (octaveNumber), x - 15, getHeight() - 30, 30, 30, Justification::centredBottom); // TIMUR TODO: beautify this!
  97. }
  98. }
  99. }
  100. //==========================================================================
  101. void noteAdded (ExpressiveMidiNote newNote) override
  102. {
  103. const ScopedLock sl (lock);
  104. activeNotes.add (newNote);
  105. triggerAsyncUpdate();
  106. }
  107. void noteChanged (ExpressiveMidiNote changedNote) override
  108. {
  109. const ScopedLock sl (lock);
  110. for (auto& note : activeNotes)
  111. if (note.noteID == changedNote.noteID)
  112. note = changedNote;
  113. triggerAsyncUpdate();
  114. }
  115. void noteReleased (ExpressiveMidiNote finishedNote) override
  116. {
  117. const ScopedLock sl (lock);
  118. for (int i = activeNotes.size(); --i >= 0;)
  119. if (activeNotes.getReference(i).noteID == finishedNote.noteID)
  120. activeNotes.remove (i);
  121. triggerAsyncUpdate();
  122. }
  123. private:
  124. //==========================================================================
  125. ExpressiveMidiNote* findActiveNote (int noteID) const noexcept
  126. {
  127. for (auto& note : activeNotes)
  128. if (note.noteID == noteID)
  129. return &note;
  130. return nullptr;
  131. }
  132. NoteComponent* findNoteComponent (int noteID) const noexcept
  133. {
  134. for (auto& noteComp : noteComponents)
  135. if (noteComp->note.noteID == noteID)
  136. return noteComp;
  137. return nullptr;
  138. }
  139. //==========================================================================
  140. void handleAsyncUpdate() override
  141. {
  142. const ScopedLock sl (lock);
  143. for (int i = noteComponents.size(); --i >= 0;)
  144. if (findActiveNote (noteComponents.getUnchecked(i)->note.noteID) == nullptr)
  145. noteComponents.remove (i);
  146. for (auto& note : activeNotes)
  147. if (findNoteComponent (note.noteID) == nullptr)
  148. addAndMakeVisible (noteComponents.add (new NoteComponent (note)));
  149. for (auto& noteComp : noteComponents)
  150. if (auto* noteInfo = findActiveNote (noteComp->note.noteID))
  151. noteComp->update (*noteInfo, getCentrePositionForNote (*noteInfo));
  152. }
  153. //==========================================================================
  154. Point<float> getCentrePositionForNote (ExpressiveMidiNote note) const
  155. {
  156. float pitchbendRange = 24.0f; // TIMUR TODO: get actual range !!!
  157. float n = float (note.initialNote) + note.pitchbend.asPitchbendInSemitones (pitchbendRange);
  158. float x = getWidth() * n / 128;
  159. float y = getHeight() * (1 - note.timbre.asUnsignedFloat());
  160. return Point<float> (x, y);
  161. }
  162. //==========================================================================
  163. OwnedArray<NoteComponent> noteComponents;
  164. CriticalSection lock;
  165. Array<ExpressiveMidiNote> activeNotes;
  166. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Visualiser)
  167. };
  168. #endif // VISUALISER_H_INCLUDED