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.

169 lines
7.4KB

  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. #include "../JuceLibraryCode/JuceHeader.h"
  18. #include "../../GenericEditor.h"
  19. //==============================================================================
  20. /**
  21. */
  22. class MultiOutSynth : public AudioProcessor
  23. {
  24. public:
  25. enum
  26. {
  27. maxMidiChannel = 16,
  28. maxNumberOfVoices = 5
  29. };
  30. //==============================================================================
  31. MultiOutSynth()
  32. : AudioProcessor (BusesProperties()
  33. .withOutput ("Output #1", AudioChannelSet::stereo(), true)
  34. .withOutput ("Output #2", AudioChannelSet::stereo(), false)
  35. .withOutput ("Output #3", AudioChannelSet::stereo(), false)
  36. .withOutput ("Output #4", AudioChannelSet::stereo(), false)
  37. .withOutput ("Output #5", AudioChannelSet::stereo(), false)
  38. .withOutput ("Output #6", AudioChannelSet::stereo(), false)
  39. .withOutput ("Output #7", AudioChannelSet::stereo(), false)
  40. .withOutput ("Output #8", AudioChannelSet::stereo(), false)
  41. .withOutput ("Output #9", AudioChannelSet::stereo(), false)
  42. .withOutput ("Output #10", AudioChannelSet::stereo(), false)
  43. .withOutput ("Output #11", AudioChannelSet::stereo(), false)
  44. .withOutput ("Output #12", AudioChannelSet::stereo(), false)
  45. .withOutput ("Output #13", AudioChannelSet::stereo(), false)
  46. .withOutput ("Output #14", AudioChannelSet::stereo(), false)
  47. .withOutput ("Output #15", AudioChannelSet::stereo(), false)
  48. .withOutput ("Output #16", AudioChannelSet::stereo(), false))
  49. {
  50. // initialize other stuff (not related to buses)
  51. formatManager.registerBasicFormats();
  52. for (int midiChannel = 0; midiChannel < maxMidiChannel; ++midiChannel)
  53. {
  54. synth.add (new Synthesiser());
  55. for (int i = 0; i < maxNumberOfVoices; ++i)
  56. synth[midiChannel]->addVoice (new SamplerVoice());
  57. }
  58. loadNewSample (BinaryData::singing_ogg, BinaryData::singing_oggSize);
  59. }
  60. ~MultiOutSynth() {}
  61. //==============================================================================
  62. bool canAddBus (bool isInput) const override { return (! isInput && getBusCount (false) < maxMidiChannel); }
  63. bool canRemoveBus (bool isInput) const override { return (! isInput && getBusCount (false) > 1); }
  64. //==============================================================================
  65. void prepareToPlay (double newSampleRate, int samplesPerBlock) override
  66. {
  67. ignoreUnused (samplesPerBlock);
  68. for (int midiChannel = 0; midiChannel < maxMidiChannel; ++midiChannel)
  69. synth[midiChannel]->setCurrentPlaybackSampleRate (newSampleRate);
  70. }
  71. void releaseResources() override {}
  72. void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiBuffer) override
  73. {
  74. const int busCount = getBusCount (false);
  75. for (int busNr = 0; busNr < busCount; ++busNr)
  76. {
  77. MidiBuffer midiChannelBuffer = filterMidiMessagesForChannel (midiBuffer, busNr + 1);
  78. AudioSampleBuffer audioBusBuffer = getBusBuffer (buffer, false, busNr);
  79. synth [busNr]->renderNextBlock (audioBusBuffer, midiChannelBuffer, 0, audioBusBuffer.getNumSamples());
  80. }
  81. }
  82. //==============================================================================
  83. AudioProcessorEditor* createEditor() override { return new GenericEditor (*this); }
  84. bool hasEditor() const override { return true; }
  85. //==============================================================================
  86. const String getName() const override { return "Gain PlugIn"; }
  87. bool acceptsMidi() const override { return false; }
  88. bool producesMidi() const override { return false; }
  89. double getTailLengthSeconds() const override { return 0; }
  90. int getNumPrograms() override { return 1; }
  91. int getCurrentProgram() override { return 0; }
  92. void setCurrentProgram (int) override {}
  93. const String getProgramName (int) override { return String(); }
  94. void changeProgramName (int , const String& ) override { }
  95. //==============================================================================
  96. void getStateInformation (MemoryBlock&) override {}
  97. void setStateInformation (const void*, int) override {}
  98. private:
  99. //==============================================================================
  100. static MidiBuffer filterMidiMessagesForChannel (const MidiBuffer& input, int channel)
  101. {
  102. MidiMessage msg;
  103. int samplePosition;
  104. MidiBuffer output;
  105. for (MidiBuffer::Iterator it (input); it.getNextEvent (msg, samplePosition);)
  106. if (msg.getChannel() == channel) output.addEvent (msg, samplePosition);
  107. return output;
  108. }
  109. void loadNewSample (const void* data, int dataSize)
  110. {
  111. MemoryInputStream* soundBuffer = new MemoryInputStream (data, static_cast<std::size_t> (dataSize), false);
  112. ScopedPointer<AudioFormatReader> formatReader (formatManager.findFormatForFileExtension ("ogg")->createReaderFor (soundBuffer, true));
  113. BigInteger midiNotes;
  114. midiNotes.setRange (0, 126, true);
  115. SynthesiserSound::Ptr newSound = new SamplerSound ("Voice", *formatReader, midiNotes, 0x40, 0.0, 0.0, 10.0);
  116. for (int channel = 0; channel < maxMidiChannel; ++channel)
  117. synth[channel]->removeSound (0);
  118. sound = newSound;
  119. for (int channel = 0; channel < maxMidiChannel; ++channel)
  120. synth[channel]->addSound (sound);
  121. }
  122. //==============================================================================
  123. AudioFormatManager formatManager;
  124. OwnedArray<Synthesiser> synth;
  125. SynthesiserSound::Ptr sound;
  126. //==============================================================================
  127. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiOutSynth)
  128. };
  129. //==============================================================================
  130. // This creates new instances of the plugin..
  131. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  132. {
  133. return new MultiOutSynth();
  134. }