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.

100 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. /**
  23. Converts a mono processor class into a multi-channel version by duplicating it
  24. and applying multichannel buffers across an array of instances.
  25. When the prepare method is called, it uses the specified number of channels to
  26. instantiate the appropriate number of instances, which it then uses in its
  27. process() method.
  28. @tags{DSP}
  29. */
  30. template <typename MonoProcessorType, typename StateType>
  31. struct ProcessorDuplicator
  32. {
  33. ProcessorDuplicator() : state (new StateType()) {}
  34. ProcessorDuplicator (StateType* stateToUse) : state (stateToUse) {}
  35. ProcessorDuplicator (typename StateType::Ptr stateToUse) : state (std::move (stateToUse)) {}
  36. ProcessorDuplicator (const ProcessorDuplicator&) = default;
  37. ProcessorDuplicator (ProcessorDuplicator&&) = default;
  38. void prepare (const ProcessSpec& spec)
  39. {
  40. processors.removeRange ((int) spec.numChannels, processors.size());
  41. while (static_cast<size_t> (processors.size()) < spec.numChannels)
  42. processors.add (new MonoProcessorType (state));
  43. auto monoSpec = spec;
  44. monoSpec.numChannels = 1;
  45. for (auto* p : processors)
  46. p->prepare (monoSpec);
  47. }
  48. void reset() noexcept { for (auto* p : processors) p->reset(); }
  49. template <typename ProcessContext>
  50. void process (const ProcessContext& context) noexcept
  51. {
  52. jassert ((int) context.getInputBlock().getNumChannels() <= processors.size());
  53. jassert ((int) context.getOutputBlock().getNumChannels() <= processors.size());
  54. auto numChannels = static_cast<size_t> (jmin (context.getInputBlock().getNumChannels(),
  55. context.getOutputBlock().getNumChannels()));
  56. for (size_t chan = 0; chan < numChannels; ++chan)
  57. processors[(int) chan]->process (MonoProcessContext<ProcessContext> (context, chan));
  58. }
  59. typename StateType::Ptr state;
  60. private:
  61. template <typename ProcessContext>
  62. struct MonoProcessContext : public ProcessContext
  63. {
  64. MonoProcessContext (const ProcessContext& multiChannelContext, size_t channelToUse)
  65. : ProcessContext (multiChannelContext), channel (channelToUse)
  66. {}
  67. size_t channel;
  68. typename ProcessContext::ConstAudioBlockType getInputBlock() const noexcept { return ProcessContext::getInputBlock() .getSingleChannelBlock (channel); }
  69. typename ProcessContext::AudioBlockType getOutputBlock() const noexcept { return ProcessContext::getOutputBlock().getSingleChannelBlock (channel); }
  70. };
  71. juce::OwnedArray<MonoProcessorType> processors;
  72. };
  73. } // namespace dsp
  74. } // namespace juce