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.

201 lines
7.2KB

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