OpenShot Audio Library | OpenShotAudio  0.3.0
juce_ProcessorDuplicator.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 namespace dsp
30 {
31 
42 template <typename MonoProcessorType, typename StateType>
44 {
45  ProcessorDuplicator() : state (new StateType()) {}
46  ProcessorDuplicator (StateType* stateToUse) : state (stateToUse) {}
47  ProcessorDuplicator (typename StateType::Ptr stateToUse) : state (std::move (stateToUse)) {}
48  ProcessorDuplicator (const ProcessorDuplicator&) = default;
50 
51  void prepare (const ProcessSpec& spec)
52  {
53  processors.removeRange ((int) spec.numChannels, processors.size());
54 
55  while (static_cast<size_t> (processors.size()) < spec.numChannels)
56  processors.add (new MonoProcessorType (state));
57 
58  auto monoSpec = spec;
59  monoSpec.numChannels = 1;
60 
61  for (auto* p : processors)
62  p->prepare (monoSpec);
63  }
64 
65  void reset() noexcept { for (auto* p : processors) p->reset(); }
66 
67  template<typename ProcessContext>
68  void process (const ProcessContext& context) noexcept
69  {
70  jassert ((int) context.getInputBlock().getNumChannels() <= processors.size());
71  jassert ((int) context.getOutputBlock().getNumChannels() <= processors.size());
72 
73  auto numChannels = static_cast<size_t> (jmin (context.getInputBlock().getNumChannels(),
74  context.getOutputBlock().getNumChannels()));
75 
76  for (size_t chan = 0; chan < numChannels; ++chan)
77  processors[(int) chan]->process (MonoProcessContext<ProcessContext> (context, chan));
78  }
79 
80  typename StateType::Ptr state;
81 
82 private:
83  template <typename ProcessContext>
84  struct MonoProcessContext : public ProcessContext
85  {
86  MonoProcessContext (const ProcessContext& multiChannelContext, size_t channelToUse)
87  : ProcessContext (multiChannelContext), channel (channelToUse)
88  {}
89 
90  size_t channel;
91 
92  typename ProcessContext::ConstAudioBlockType getInputBlock() const noexcept { return ProcessContext::getInputBlock() .getSingleChannelBlock (channel); }
93  typename ProcessContext::AudioBlockType getOutputBlock() const noexcept { return ProcessContext::getOutputBlock().getSingleChannelBlock (channel); }
94  };
95 
97 };
98 
99 } // namespace dsp
100 } // namespace juce
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
int size() const noexcept
ObjectClass * add(ObjectClass *newObject)