VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_dsp / widgets / juce_Phaser.cpp
blob50b17d5f04e495a03fb5ea98c2ed19974e38d4e5
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
23 ==============================================================================
26 namespace juce
28 namespace dsp
31 //==============================================================================
32 template <typename SampleType>
33 Phaser<SampleType>::Phaser()
35 auto oscFunction = [] (SampleType x) { return std::sin (x); };
36 osc.initialise (oscFunction);
38 for (auto n = 0; n < numStages; ++n)
40 filters.add (new FirstOrderTPTFilter<SampleType>());
41 filters[n]->setType (FirstOrderTPTFilterType::allpass);
44 dryWet.setMixingRule (DryWetMixingRule::linear);
47 template <typename SampleType>
48 void Phaser<SampleType>::setRate (SampleType newRateHz)
50 jassert (isPositiveAndBelow (newRateHz, static_cast<SampleType> (100.0)));
52 rate = newRateHz;
53 update();
56 template <typename SampleType>
57 void Phaser<SampleType>::setDepth (SampleType newDepth)
59 jassert (isPositiveAndNotGreaterThan (newDepth, static_cast<SampleType> (1.0)));
61 depth = newDepth;
62 update();
65 template <typename SampleType>
66 void Phaser<SampleType>::setCentreFrequency (SampleType newCentreHz)
68 jassert (isPositiveAndBelow (newCentreHz, static_cast<SampleType> (sampleRate * 0.5)));
70 centreFrequency = newCentreHz;
71 normCentreFrequency = mapFromLog10 (centreFrequency, static_cast<SampleType> (20.0), static_cast<SampleType> (jmin (20000.0, 0.49 * sampleRate)));
74 template <typename SampleType>
75 void Phaser<SampleType>::setFeedback (SampleType newFeedback)
77 jassert (newFeedback >= static_cast<SampleType> (-1.0) && newFeedback <= static_cast<SampleType> (1.0));
79 feedback = newFeedback;
80 update();
83 template <typename SampleType>
84 void Phaser<SampleType>::setMix (SampleType newMix)
86 jassert (isPositiveAndNotGreaterThan (newMix, static_cast<SampleType> (1.0)));
88 mix = newMix;
89 update();
92 //==============================================================================
93 template <typename SampleType>
94 void Phaser<SampleType>::prepare (const ProcessSpec& spec)
96 jassert (spec.sampleRate > 0);
97 jassert (spec.numChannels > 0);
99 sampleRate = spec.sampleRate;
101 for (auto n = 0; n < numStages; ++n)
102 filters[n]->prepare (spec);
104 dryWet.prepare (spec);
105 feedbackVolume.resize (spec.numChannels);
106 lastOutput.resize (spec.numChannels);
108 auto specDown = spec;
109 specDown.sampleRate /= (double) maxUpdateCounter;
110 specDown.maximumBlockSize = specDown.maximumBlockSize / (uint32) maxUpdateCounter + 1;
112 osc.prepare (specDown);
113 bufferFrequency.setSize (1, (int) specDown.maximumBlockSize, false, false, true);
115 update();
116 reset();
119 template <typename SampleType>
120 void Phaser<SampleType>::reset()
122 std::fill (lastOutput.begin(), lastOutput.end(), static_cast<SampleType> (0));
124 for (auto n = 0; n < numStages; ++n)
125 filters[n]->reset();
127 osc.reset();
128 dryWet.reset();
130 oscVolume.reset (sampleRate / (double) maxUpdateCounter, 0.05);
132 for (auto& vol : feedbackVolume)
133 vol.reset (sampleRate, 0.05);
135 updateCounter = 0;
138 template <typename SampleType>
139 void Phaser<SampleType>::update()
141 osc.setFrequency (rate);
142 oscVolume.setTargetValue (depth * (SampleType) 0.5);
143 dryWet.setWetMixProportion (mix);
145 for (auto& vol : feedbackVolume)
146 vol.setTargetValue (feedback);
149 //==============================================================================
150 template class Phaser<float>;
151 template class Phaser<double>;
153 } // namespace dsp
154 } // namespace juce