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.

209 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: MultiOutSynthPlugin
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Multi-out synthesiser audio plugin.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_plugin_client, juce_audio_processors,
  26. juce_audio_utils, juce_core, juce_data_structures,
  27. juce_events, juce_graphics, juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: AudioProcessor
  31. mainClass: MultiOutSynth
  32. useLocalCopy: 1
  33. pluginCharacteristics: pluginIsSynth, pluginWantsMidiIn
  34. END_JUCE_PIP_METADATA
  35. *******************************************************************************/
  36. #pragma once
  37. #include "../Assets/DemoUtilities.h"
  38. //==============================================================================
  39. class MultiOutSynth : public AudioProcessor
  40. {
  41. public:
  42. enum
  43. {
  44. maxMidiChannel = 16,
  45. maxNumberOfVoices = 5
  46. };
  47. //==============================================================================
  48. MultiOutSynth()
  49. : AudioProcessor (BusesProperties()
  50. .withOutput ("Output #1", AudioChannelSet::stereo(), true)
  51. .withOutput ("Output #2", AudioChannelSet::stereo(), false)
  52. .withOutput ("Output #3", AudioChannelSet::stereo(), false)
  53. .withOutput ("Output #4", AudioChannelSet::stereo(), false)
  54. .withOutput ("Output #5", AudioChannelSet::stereo(), false)
  55. .withOutput ("Output #6", AudioChannelSet::stereo(), false)
  56. .withOutput ("Output #7", AudioChannelSet::stereo(), false)
  57. .withOutput ("Output #8", AudioChannelSet::stereo(), false)
  58. .withOutput ("Output #9", AudioChannelSet::stereo(), false)
  59. .withOutput ("Output #10", AudioChannelSet::stereo(), false)
  60. .withOutput ("Output #11", AudioChannelSet::stereo(), false)
  61. .withOutput ("Output #12", AudioChannelSet::stereo(), false)
  62. .withOutput ("Output #13", AudioChannelSet::stereo(), false)
  63. .withOutput ("Output #14", AudioChannelSet::stereo(), false)
  64. .withOutput ("Output #15", AudioChannelSet::stereo(), false)
  65. .withOutput ("Output #16", AudioChannelSet::stereo(), false))
  66. {
  67. // initialize other stuff (not related to buses)
  68. formatManager.registerBasicFormats();
  69. for (int midiChannel = 0; midiChannel < maxMidiChannel; ++midiChannel)
  70. {
  71. synth.add (new Synthesiser());
  72. for (int i = 0; i < maxNumberOfVoices; ++i)
  73. synth[midiChannel]->addVoice (new SamplerVoice());
  74. }
  75. loadNewSample (createAssetInputStream ("singing.ogg"), "ogg");
  76. }
  77. //==============================================================================
  78. bool canAddBus (bool isInput) const override { return ! isInput; }
  79. bool canRemoveBus (bool isInput) const override { return ! isInput; }
  80. //==============================================================================
  81. void prepareToPlay (double newSampleRate, int samplesPerBlock) override
  82. {
  83. ignoreUnused (samplesPerBlock);
  84. for (auto* s : synth)
  85. s->setCurrentPlaybackSampleRate (newSampleRate);
  86. }
  87. void releaseResources() override {}
  88. void processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiBuffer) override
  89. {
  90. auto busCount = getBusCount (false);
  91. for (auto busNr = 0; busNr < busCount; ++busNr)
  92. {
  93. if (synth.size() <= busNr)
  94. continue;
  95. auto midiChannelBuffer = filterMidiMessagesForChannel (midiBuffer, busNr + 1);
  96. auto audioBusBuffer = getBusBuffer (buffer, false, busNr);
  97. // Voices add to the contents of the buffer. Make sure the buffer is clear before
  98. // rendering, just in case the host left old data in the buffer.
  99. audioBusBuffer.clear();
  100. synth [busNr]->renderNextBlock (audioBusBuffer, midiChannelBuffer, 0, audioBusBuffer.getNumSamples());
  101. }
  102. }
  103. using AudioProcessor::processBlock;
  104. //==============================================================================
  105. AudioProcessorEditor* createEditor() override { return new GenericAudioProcessorEditor (*this); }
  106. bool hasEditor() const override { return true; }
  107. //==============================================================================
  108. const String getName() const override { return "Multi Out Synth PlugIn"; }
  109. bool acceptsMidi() const override { return false; }
  110. bool producesMidi() const override { return false; }
  111. double getTailLengthSeconds() const override { return 0; }
  112. int getNumPrograms() override { return 1; }
  113. int getCurrentProgram() override { return 0; }
  114. void setCurrentProgram (int) override {}
  115. const String getProgramName (int) override { return "None"; }
  116. void changeProgramName (int, const String&) override {}
  117. bool isBusesLayoutSupported (const BusesLayout& layout) const override
  118. {
  119. const auto& outputs = layout.outputBuses;
  120. return layout.inputBuses.isEmpty()
  121. && 1 <= outputs.size()
  122. && std::all_of (outputs.begin(), outputs.end(), [] (const auto& bus)
  123. {
  124. return bus.isDisabled() || bus == AudioChannelSet::stereo();
  125. });
  126. }
  127. //==============================================================================
  128. void getStateInformation (MemoryBlock&) override {}
  129. void setStateInformation (const void*, int) override {}
  130. private:
  131. //==============================================================================
  132. static MidiBuffer filterMidiMessagesForChannel (const MidiBuffer& input, int channel)
  133. {
  134. MidiBuffer output;
  135. for (const auto metadata : input)
  136. {
  137. const auto message = metadata.getMessage();
  138. if (message.getChannel() == channel)
  139. output.addEvent (message, metadata.samplePosition);
  140. }
  141. return output;
  142. }
  143. void loadNewSample (std::unique_ptr<InputStream> soundBuffer, const char* format)
  144. {
  145. std::unique_ptr<AudioFormatReader> formatReader (formatManager.findFormatForFileExtension (format)->createReaderFor (soundBuffer.release(), true));
  146. BigInteger midiNotes;
  147. midiNotes.setRange (0, 126, true);
  148. SynthesiserSound::Ptr newSound = new SamplerSound ("Voice", *formatReader, midiNotes, 0x40, 0.0, 0.0, 10.0);
  149. for (auto* s : synth)
  150. s->removeSound (0);
  151. sound = newSound;
  152. for (auto* s : synth)
  153. s->addSound (sound);
  154. }
  155. //==============================================================================
  156. AudioFormatManager formatManager;
  157. OwnedArray<Synthesiser> synth;
  158. SynthesiserSound::Ptr sound;
  159. //==============================================================================
  160. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiOutSynth)
  161. };