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.

101 lines
3.7KB

  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. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. /**
  24. Converts a mono processor class into a multi-channel version by duplicating it
  25. and applying multichannel buffers across an array of instances.
  26. When the prepare method is called, it uses the specified number of channels to
  27. instantiate the appropriate number of instances, which it then uses in its
  28. process() method.
  29. @tags{DSP}
  30. */
  31. template <typename MonoProcessorType, typename StateType>
  32. struct ProcessorDuplicator
  33. {
  34. ProcessorDuplicator() : state (new StateType()) {}
  35. ProcessorDuplicator (StateType* stateToUse) : state (stateToUse) {}
  36. ProcessorDuplicator (typename StateType::Ptr stateToUse) : state (static_cast<typename StateType::Ptr&&> (stateToUse)) {}
  37. ProcessorDuplicator (const ProcessorDuplicator&) = default;
  38. ProcessorDuplicator (ProcessorDuplicator&&) = default;
  39. void prepare (const ProcessSpec& spec)
  40. {
  41. processors.removeRange ((int) spec.numChannels, processors.size());
  42. while (static_cast<size_t> (processors.size()) < spec.numChannels)
  43. processors.add (new MonoProcessorType (state));
  44. auto monoSpec = spec;
  45. monoSpec.numChannels = 1;
  46. for (auto* p : processors)
  47. p->prepare (monoSpec);
  48. }
  49. void reset() noexcept { for (auto* p : processors) p->reset(); }
  50. template<typename ProcessContext>
  51. void process (const ProcessContext& context) noexcept
  52. {
  53. jassert ((int) context.getInputBlock().getNumChannels() <= processors.size());
  54. jassert ((int) context.getOutputBlock().getNumChannels() <= processors.size());
  55. auto numChannels = static_cast<size_t> (jmin (context.getInputBlock().getNumChannels(),
  56. context.getOutputBlock().getNumChannels()));
  57. for (size_t chan = 0; chan < numChannels; ++chan)
  58. processors[(int) chan]->process (MonoProcessContext<ProcessContext> (context, chan));
  59. }
  60. typename StateType::Ptr state;
  61. private:
  62. template <typename ProcessContext>
  63. struct MonoProcessContext : public ProcessContext
  64. {
  65. MonoProcessContext (const ProcessContext& multiChannelContext, size_t channelToUse)
  66. : ProcessContext (multiChannelContext), channel (channelToUse)
  67. {}
  68. size_t channel;
  69. typename ProcessContext::AudioBlockType getInputBlock() const noexcept { return ProcessContext::getInputBlock().getSingleChannelBlock (channel); }
  70. typename ProcessContext::AudioBlockType getOutputBlock() const noexcept { return ProcessContext::getOutputBlock().getSingleChannelBlock (channel); }
  71. };
  72. juce::OwnedArray<MonoProcessorType> processors;
  73. };
  74. } // namespace dsp
  75. } // namespace juce