OpenShot Library | OpenShotAudio  0.2.2
juce_ProcessorDuplicator.h
1 
2 /** @weakgroup juce_dsp-processors
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16  27th April 2017).
17 
18  End User License Agreement: www.juce.com/juce-5-licence
19  Privacy Policy: www.juce.com/juce-5-privacy-policy
20 
21  Or: You may also use this code under the terms of the GPL v3 (see
22  www.gnu.org/licenses).
23 
24  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26  DISCLAIMED.
27 
28  ==============================================================================
29 */
30 
31 namespace juce
32 {
33 namespace dsp
34 {
35 
36 /**
37  Converts a mono processor class into a multi-channel version by duplicating it
38  and applying multichannel buffers across an array of instances.
39 
40  When the prepare method is called, it uses the specified number of channels to
41  instantiate the appropriate number of instances, which it then uses in its
42  process() method.
43 
44  @tags{DSP}
45 */
46 template <typename MonoProcessorType, typename StateType>
48 {
49  ProcessorDuplicator() : state (new StateType()) {}
50  ProcessorDuplicator (StateType* stateToUse) : state (stateToUse) {}
51  ProcessorDuplicator (typename StateType::Ptr stateToUse) : state (std::move (stateToUse)) {}
52  ProcessorDuplicator (const ProcessorDuplicator&) = default;
54 
55  void prepare (const ProcessSpec& spec)
56  {
57  processors.removeRange ((int) spec.numChannels, processors.size());
58 
59  while (static_cast<size_t> (processors.size()) < spec.numChannels)
60  processors.add (new MonoProcessorType (state));
61 
62  auto monoSpec = spec;
63  monoSpec.numChannels = 1;
64 
65  for (auto* p : processors)
66  p->prepare (monoSpec);
67  }
68 
69  void reset() noexcept { for (auto* p : processors) p->reset(); }
70 
71  template<typename ProcessContext>
72  void process (const ProcessContext& context) noexcept
73  {
74  jassert ((int) context.getInputBlock().getNumChannels() <= processors.size());
75  jassert ((int) context.getOutputBlock().getNumChannels() <= processors.size());
76 
77  auto numChannels = static_cast<size_t> (jmin (context.getInputBlock().getNumChannels(),
78  context.getOutputBlock().getNumChannels()));
79 
80  for (size_t chan = 0; chan < numChannels; ++chan)
81  processors[(int) chan]->process (MonoProcessContext<ProcessContext> (context, chan));
82  }
83 
84  typename StateType::Ptr state;
85 
86 private:
87  template <typename ProcessContext>
88  struct MonoProcessContext : public ProcessContext
89  {
90  MonoProcessContext (const ProcessContext& multiChannelContext, size_t channelToUse)
91  : ProcessContext (multiChannelContext), channel (channelToUse)
92  {}
93 
94  size_t channel;
95 
96  typename ProcessContext::ConstAudioBlockType getInputBlock() const noexcept { return ProcessContext::getInputBlock() .getSingleChannelBlock (channel); }
97  typename ProcessContext::AudioBlockType getOutputBlock() const noexcept { return ProcessContext::getOutputBlock().getSingleChannelBlock (channel); }
98  };
99 
101 };
102 
103 } // namespace dsp
104 } // namespace juce
105 
106 /** @}*/
uint32 numChannels
The number of channels that the process() method will be expected to handle.
Converts a mono processor class into a multi-channel version by duplicating it and applying multichan...
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
Removes a range of objects from the array.
int size() const noexcept
Returns the number of items currently in the array.
This structure is passed into a DSP algorithm&#39;s prepare() method, and contains information about vari...
ObjectClass * add(ObjectClass *newObject)
Appends a new object to the end of the array.