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) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. class MainComponent : public Component,
  21. private AudioIODeviceCallback,
  22. private MidiInputCallback,
  23. private MPESetupComponent::Listener
  24. {
  25. public:
  26. //==============================================================================
  27. MainComponent()
  28. : audioSetupComp (audioDeviceManager, 0, 0, 0, 256, true, true, true, false),
  29. zoneLayoutComp (colourPicker),
  30. visualiserComp (colourPicker)
  31. {
  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. 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. };