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.

183 lines
6.8KB

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