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.

93 lines
3.3KB

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