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.

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