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.

197 lines
7.1KB

  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. #pragma once
  18. class MainComponent : public Component,
  19. private AudioIODeviceCallback,
  20. private MidiInputCallback,
  21. private MPESetupComponent::Listener
  22. {
  23. public:
  24. //==============================================================================
  25. MainComponent()
  26. : audioSetupComp (audioDeviceManager, 0, 0, 0, 256, true, true, true, false),
  27. zoneLayoutComp (colourPicker),
  28. visualiserComp (colourPicker)
  29. {
  30. setLookAndFeel (&lookAndFeel);
  31. setSize (880, 720);
  32. audioDeviceManager.initialise (0, 2, 0, true, String(), 0);
  33. audioDeviceManager.addMidiInputCallback (String(), this);
  34. audioDeviceManager.addAudioCallback (this);
  35. addAndMakeVisible (audioSetupComp);
  36. addAndMakeVisible (MPESetupComp);
  37. addAndMakeVisible (zoneLayoutComp);
  38. addAndMakeVisible (visualiserViewport);
  39. visualiserViewport.setScrollBarsShown (false, true);
  40. visualiserViewport.setViewedComponent (&visualiserComp, false);
  41. visualiserViewport.setViewPositionProportionately (0.5, 0.0);
  42. MPESetupComp.addListener (&zoneLayoutComp);
  43. MPESetupComp.addListener (this);
  44. visualiserInstrument.addListener (&visualiserComp);
  45. synth.setVoiceStealingEnabled (false);
  46. for (int i = 0; i < 15; ++i)
  47. synth.addVoice (new MPEDemoSynthVoice);
  48. }
  49. ~MainComponent()
  50. {
  51. audioDeviceManager.removeMidiInputCallback (String(), this);
  52. }
  53. //==============================================================================
  54. void resized() override
  55. {
  56. const int visualiserCompWidth = 2800;
  57. const int visualiserCompHeight = 300;
  58. const int zoneLayoutCompHeight = 60;
  59. const float audioSetupCompRelativeWidth = 0.55f;
  60. Rectangle<int> r (getLocalBounds());
  61. visualiserViewport.setBounds (r.removeFromBottom (visualiserCompHeight));
  62. visualiserComp.setBounds (Rectangle<int> (visualiserCompWidth,
  63. visualiserViewport.getHeight() - visualiserViewport.getScrollBarThickness()));
  64. zoneLayoutComp.setBounds (r.removeFromBottom (zoneLayoutCompHeight));
  65. audioSetupComp.setBounds (r.removeFromLeft (proportionOfWidth (audioSetupCompRelativeWidth)));
  66. MPESetupComp.setBounds (r);
  67. }
  68. //==============================================================================
  69. void audioDeviceIOCallback (const float** /*inputChannelData*/, int /*numInputChannels*/,
  70. float** outputChannelData, int numOutputChannels,
  71. int numSamples) override
  72. {
  73. AudioBuffer<float> buffer (outputChannelData, numOutputChannels, numSamples);
  74. buffer.clear();
  75. MidiBuffer incomingMidi;
  76. midiCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
  77. synth.renderNextBlock (buffer, incomingMidi, 0, numSamples);
  78. }
  79. void audioDeviceAboutToStart (AudioIODevice* device) override
  80. {
  81. const double sampleRate = device->getCurrentSampleRate();
  82. midiCollector.reset (sampleRate);
  83. synth.setCurrentPlaybackSampleRate (sampleRate);
  84. }
  85. void audioDeviceStopped() override
  86. {
  87. }
  88. private:
  89. //==============================================================================
  90. void handleIncomingMidiMessage (MidiInput* /*source*/,
  91. const MidiMessage& message) override
  92. {
  93. visualiserInstrument.processNextMidiEvent (message);
  94. midiCollector.addMessageToQueue (message);
  95. }
  96. //==============================================================================
  97. void zoneAdded (MPEZone newZone) override
  98. {
  99. MidiOutput* midiOutput = audioDeviceManager.getDefaultMidiOutput();
  100. if (midiOutput != nullptr)
  101. midiOutput->sendBlockOfMessagesNow (MPEMessages::addZone (newZone));
  102. zoneLayout.addZone (newZone);
  103. visualiserInstrument.setZoneLayout (zoneLayout);
  104. synth.setZoneLayout (zoneLayout);
  105. colourPicker.setZoneLayout (zoneLayout);
  106. }
  107. void allZonesCleared() override
  108. {
  109. MidiOutput* midiOutput = audioDeviceManager.getDefaultMidiOutput();
  110. if (midiOutput != nullptr)
  111. midiOutput->sendBlockOfMessagesNow (MPEMessages::clearAllZones());
  112. zoneLayout.clearAllZones();
  113. visualiserInstrument.setZoneLayout (zoneLayout);
  114. synth.setZoneLayout (zoneLayout);
  115. colourPicker.setZoneLayout (zoneLayout);
  116. }
  117. void legacyModeChanged (bool legacyModeShouldBeEnabled, int pitchbendRange, Range<int> channelRange) override
  118. {
  119. colourPicker.setLegacyModeEnabled (legacyModeShouldBeEnabled);
  120. if (legacyModeShouldBeEnabled)
  121. {
  122. synth.enableLegacyMode (pitchbendRange, channelRange);
  123. visualiserInstrument.enableLegacyMode (pitchbendRange, channelRange);
  124. }
  125. else
  126. {
  127. synth.setZoneLayout (zoneLayout);
  128. visualiserInstrument.setZoneLayout (zoneLayout);
  129. }
  130. }
  131. void voiceStealingEnabledChanged (bool voiceStealingEnabled) override
  132. {
  133. synth.setVoiceStealingEnabled (voiceStealingEnabled);
  134. }
  135. void numberOfVoicesChanged (int numberOfVoices) override
  136. {
  137. if (numberOfVoices < synth.getNumVoices())
  138. synth.reduceNumVoices (numberOfVoices);
  139. else
  140. while (synth.getNumVoices() < numberOfVoices)
  141. synth.addVoice (new MPEDemoSynthVoice);
  142. }
  143. //==============================================================================
  144. LookAndFeel_V3 lookAndFeel;
  145. AudioDeviceManager audioDeviceManager;
  146. MPEZoneLayout zoneLayout;
  147. ZoneColourPicker colourPicker;
  148. AudioDeviceSelectorComponent audioSetupComp;
  149. MPESetupComponent MPESetupComp;
  150. ZoneLayoutComponent zoneLayoutComp;
  151. Visualiser visualiserComp;
  152. Viewport visualiserViewport;
  153. MPEInstrument visualiserInstrument;
  154. MPESynthesiser synth;
  155. MidiMessageCollector midiCollector;
  156. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  157. };