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.

147 lines
5.6KB

  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 MAINCOMPONENT_H_INCLUDED
  18. #define MAINCOMPONENT_H_INCLUDED
  19. //==============================================================================
  20. class MainComponent : public Component,
  21. private AudioIODeviceCallback,
  22. private MidiInputCallback,
  23. private ZoneLayoutComponent::Listener
  24. {
  25. public:
  26. //==========================================================================
  27. MainComponent()
  28. : audioSetupComp (audioDeviceManager, 0, 0, 0, 256, true, true, true, false)
  29. {
  30. setLookAndFeel (&lookAndFeel);
  31. setSize (880, 670);
  32. audioDeviceManager.initialise (0, 2, 0, true, String::empty, 0);
  33. audioDeviceManager.addMidiInputCallback(String::empty, this);
  34. audioDeviceManager.addAudioCallback (this);
  35. addAndMakeVisible (audioSetupComp);
  36. addAndMakeVisible (expressiveMidiSetupComp);
  37. addAndMakeVisible (zoneLayoutComp);
  38. addAndMakeVisible (visualiserViewport);
  39. visualiserViewport.setScrollBarsShown (false, true);
  40. visualiserViewport.setViewedComponent (&visualiserComp, false);
  41. visualiserViewport.setViewPositionProportionately (0.5, 0.0);
  42. expressiveMidiSetupComp.addListener (&zoneLayoutComp);
  43. expressiveMidiSetupComp.addListener (this);
  44. visualiserInstrument.addListener (&visualiserComp);
  45. }
  46. ~MainComponent()
  47. {
  48. audioDeviceManager.removeMidiInputCallback (String::empty, this);
  49. }
  50. //==========================================================================
  51. void resized() override
  52. {
  53. const int visualiserCompWidth = 2800;
  54. const int visualiserCompHeight = 300;
  55. const int zoneLayoutCompHeight = 60;
  56. const float audioSetupCompRelativeWidth = 0.6f;
  57. Rectangle<int> r (getLocalBounds());
  58. visualiserViewport.setBounds (r.removeFromBottom (visualiserCompHeight));
  59. visualiserComp.setBounds (Rectangle<int> (visualiserCompWidth,
  60. visualiserViewport.getHeight() - visualiserViewport.getScrollBarThickness()));
  61. zoneLayoutComp.setBounds (r.removeFromBottom (zoneLayoutCompHeight));
  62. audioSetupComp.setBounds (r.removeFromLeft (proportionOfWidth (audioSetupCompRelativeWidth)));
  63. expressiveMidiSetupComp.setBounds (r);
  64. }
  65. //==========================================================================
  66. void audioDeviceIOCallback (const float** inputChannelData, int numInputChannels,
  67. float** outputChannelData, int numOutputChannels,
  68. int numSamples) override
  69. {
  70. AudioBuffer<float> buffer (outputChannelData, numOutputChannels, numSamples);
  71. buffer.clear();
  72. MidiBuffer incomingMidi;
  73. midiCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
  74. synth.renderNextBlock (buffer, incomingMidi, 0, numSamples);
  75. }
  76. void audioDeviceAboutToStart (AudioIODevice* device) override
  77. {
  78. const double sampleRate = device->getCurrentSampleRate();
  79. midiCollector.reset (sampleRate);
  80. synth.setCurrentPlaybackSampleRate (sampleRate);
  81. }
  82. void audioDeviceStopped() override
  83. {
  84. }
  85. private:
  86. //==========================================================================
  87. void handleIncomingMidiMessage (MidiInput* /*source*/,
  88. const MidiMessage& message) override
  89. {
  90. visualiserInstrument.processNextMidiEvent (message);
  91. midiCollector.addMessageToQueue (message);
  92. }
  93. //==========================================================================
  94. void expressiveMidiZoneLayoutChanged (ExpressiveMidiZoneLayout newLayout) override
  95. {
  96. MidiOutput* midiOutput = audioDeviceManager.getDefaultMidiOutput();
  97. if (midiOutput != nullptr)
  98. midiOutput->sendBlockOfMessagesNow (ExpressiveMidiMessages::setZoneLayout (newLayout));
  99. visualiserInstrument.setZoneLayout (newLayout);
  100. synth.setZoneLayout (newLayout);
  101. }
  102. //==========================================================================
  103. LookAndFeel_V3 lookAndFeel;
  104. AudioDeviceManager audioDeviceManager;
  105. AudioDeviceSelectorComponent audioSetupComp;
  106. ExpressiveMidiSetupComponent expressiveMidiSetupComp;
  107. ZoneLayoutComponent zoneLayoutComp;
  108. Visualiser visualiserComp;
  109. Viewport visualiserViewport;
  110. ExpressiveMidiInstrument visualiserInstrument;
  111. DemoSynth synth;
  112. MidiMessageCollector midiCollector;
  113. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  114. };
  115. #endif // MAINCOMPONENT_H_INCLUDED