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.

210 lines
7.9KB

  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. #include "PluginProcessor.h"
  20. #include "PluginEditor.h"
  21. //==============================================================================
  22. // This is a handy slider subclass that controls an AudioProcessorParameter
  23. // (may move this class into the library itself at some point in the future..)
  24. class JuceDemoPluginAudioProcessorEditor::ParameterSlider : public Slider,
  25. private Timer
  26. {
  27. public:
  28. ParameterSlider (AudioProcessorParameter& p)
  29. : Slider (p.getName (256)), param (p)
  30. {
  31. setRange (0.0, 1.0, 0.0);
  32. startTimerHz (30);
  33. updateSliderPos();
  34. }
  35. void valueChanged() override { param.setValueNotifyingHost ((float) Slider::getValue()); }
  36. void timerCallback() override { updateSliderPos(); }
  37. void startedDragging() override { param.beginChangeGesture(); }
  38. void stoppedDragging() override { param.endChangeGesture(); }
  39. double getValueFromText (const String& text) override { return param.getValueForText (text); }
  40. String getTextFromValue (double value) override { return param.getText ((float) value, 1024); }
  41. void updateSliderPos()
  42. {
  43. const float newValue = param.getValue();
  44. if (newValue != (float) Slider::getValue() && ! isMouseButtonDown())
  45. Slider::setValue (newValue, NotificationType::dontSendNotification);
  46. }
  47. AudioProcessorParameter& param;
  48. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ParameterSlider)
  49. };
  50. //==============================================================================
  51. JuceDemoPluginAudioProcessorEditor::JuceDemoPluginAudioProcessorEditor (JuceDemoPluginAudioProcessor& owner)
  52. : AudioProcessorEditor (owner),
  53. midiKeyboard (owner.keyboardState, MidiKeyboardComponent::horizontalKeyboard),
  54. timecodeDisplayLabel (String()),
  55. gainLabel (String(), "Throughput level:"),
  56. delayLabel (String(), "Delay:")
  57. {
  58. // add some sliders..
  59. addAndMakeVisible (gainSlider = new ParameterSlider (*owner.gainParam));
  60. gainSlider->setSliderStyle (Slider::Rotary);
  61. addAndMakeVisible (delaySlider = new ParameterSlider (*owner.delayParam));
  62. delaySlider->setSliderStyle (Slider::Rotary);
  63. // add some labels for the sliders..
  64. gainLabel.attachToComponent (gainSlider, false);
  65. gainLabel.setFont (Font (11.0f));
  66. delayLabel.attachToComponent (delaySlider, false);
  67. delayLabel.setFont (Font (11.0f));
  68. // add the midi keyboard component..
  69. addAndMakeVisible (midiKeyboard);
  70. // add a label that will display the current timecode and status..
  71. addAndMakeVisible (timecodeDisplayLabel);
  72. timecodeDisplayLabel.setFont (Font (Font::getDefaultMonospacedFontName(), 15.0f, Font::plain));
  73. // set resize limits for this plug-in
  74. setResizeLimits (400, 200, 1024, 700);
  75. // set our component's initial size to be the last one that was stored in the filter's settings
  76. setSize (owner.lastUIWidth,
  77. owner.lastUIHeight);
  78. updateTrackProperties();
  79. // start a timer which will keep our timecode display updated
  80. startTimerHz (30);
  81. }
  82. JuceDemoPluginAudioProcessorEditor::~JuceDemoPluginAudioProcessorEditor()
  83. {
  84. }
  85. //==============================================================================
  86. void JuceDemoPluginAudioProcessorEditor::paint (Graphics& g)
  87. {
  88. g.setColour (backgroundColour);
  89. g.fillAll();
  90. }
  91. void JuceDemoPluginAudioProcessorEditor::resized()
  92. {
  93. // This lays out our child components...
  94. Rectangle<int> r (getLocalBounds().reduced (8));
  95. timecodeDisplayLabel.setBounds (r.removeFromTop (26));
  96. midiKeyboard.setBounds (r.removeFromBottom (70));
  97. r.removeFromTop (20);
  98. Rectangle<int> sliderArea (r.removeFromTop (60));
  99. gainSlider->setBounds (sliderArea.removeFromLeft (jmin (180, sliderArea.getWidth() / 2)));
  100. delaySlider->setBounds (sliderArea.removeFromLeft (jmin (180, sliderArea.getWidth())));
  101. getProcessor().lastUIWidth = getWidth();
  102. getProcessor().lastUIHeight = getHeight();
  103. }
  104. //==============================================================================
  105. void JuceDemoPluginAudioProcessorEditor::timerCallback()
  106. {
  107. updateTimecodeDisplay (getProcessor().lastPosInfo);
  108. }
  109. void JuceDemoPluginAudioProcessorEditor::hostMIDIControllerIsAvailable (bool controllerIsAvailable)
  110. {
  111. midiKeyboard.setVisible (! controllerIsAvailable);
  112. }
  113. void JuceDemoPluginAudioProcessorEditor::updateTrackProperties ()
  114. {
  115. auto trackColour = getProcessor().trackProperties.colour;
  116. auto& lf = getLookAndFeel();
  117. backgroundColour = (trackColour == Colour() ? lf.findColour (ResizableWindow::backgroundColourId)
  118. : trackColour.withAlpha (1.0f).withBrightness (0.266f));
  119. repaint();
  120. }
  121. //==============================================================================
  122. // quick-and-dirty function to format a timecode string
  123. static String timeToTimecodeString (double seconds)
  124. {
  125. const int millisecs = roundToInt (seconds * 1000.0);
  126. const int absMillisecs = std::abs (millisecs);
  127. return String::formatted ("%02d:%02d:%02d.%03d",
  128. millisecs / 3600000,
  129. (absMillisecs / 60000) % 60,
  130. (absMillisecs / 1000) % 60,
  131. absMillisecs % 1000);
  132. }
  133. // quick-and-dirty function to format a bars/beats string
  134. static String quarterNotePositionToBarsBeatsString (double quarterNotes, int numerator, int denominator)
  135. {
  136. if (numerator == 0 || denominator == 0)
  137. return "1|1|000";
  138. const int quarterNotesPerBar = (numerator * 4 / denominator);
  139. const double beats = (fmod (quarterNotes, quarterNotesPerBar) / quarterNotesPerBar) * numerator;
  140. const int bar = ((int) quarterNotes) / quarterNotesPerBar + 1;
  141. const int beat = ((int) beats) + 1;
  142. const int ticks = ((int) (fmod (beats, 1.0) * 960.0 + 0.5));
  143. return String::formatted ("%d|%d|%03d", bar, beat, ticks);
  144. }
  145. // Updates the text in our position label.
  146. void JuceDemoPluginAudioProcessorEditor::updateTimecodeDisplay (AudioPlayHead::CurrentPositionInfo pos)
  147. {
  148. MemoryOutputStream displayText;
  149. displayText << "[" << SystemStats::getJUCEVersion() << "] "
  150. << String (pos.bpm, 2) << " bpm, "
  151. << pos.timeSigNumerator << '/' << pos.timeSigDenominator
  152. << " - " << timeToTimecodeString (pos.timeInSeconds)
  153. << " - " << quarterNotePositionToBarsBeatsString (pos.ppqPosition,
  154. pos.timeSigNumerator,
  155. pos.timeSigDenominator);
  156. if (pos.isRecording)
  157. displayText << " (recording)";
  158. else if (pos.isPlaying)
  159. displayText << " (playing)";
  160. timecodeDisplayLabel.setText (displayText.toString(), dontSendNotification);
  161. }