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.

182 lines
6.9KB

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