tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Phaser.cpp
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14PhaserPlugin::PhaserPlugin (PluginCreationInfo info) : Plugin (info)
15{
16 auto um = getUndoManager();
17
18 depth.referTo (state, IDs::depth, um, 5.0f);
19 rate.referTo (state, IDs::rate, um, 0.4f);
20 feedbackGain.referTo (state, IDs::feedback, um, 0.7f);
21}
22
23PhaserPlugin::~PhaserPlugin()
24{
25 notifyListenersOfDeletion();
26}
27
28const char* PhaserPlugin::xmlTypeName = "phaser";
29
30void PhaserPlugin::initialise (const PluginInitialisationInfo& info)
31{
32 sampleRate = info.sampleRate;
33 std::memset (filterVals, 0, sizeof (filterVals));
34
35 const float delayMs = 100.0f;
36 sweep = minSweep = (juce::MathConstants<double>::pi * delayMs) / sampleRate;
37 sweepFactor = 1.001f;
38}
39
40void PhaserPlugin::deinitialise()
41{
42}
43
44void PhaserPlugin::applyToBuffer (const PluginRenderContext& fc)
45{
46 if (fc.destBuffer == nullptr)
47 return;
48
49 SCOPED_REALTIME_CHECK
50
51 const double range = pow (2.0, (double) depth);
52 const double sweepUp = pow (range, rate / (sampleRate / 2));
53 const double sweepDown = 1.0 / sweepUp;
54
55 const float delayMs = 100.0f;
56 const float maxSweep = (float) ((juce::MathConstants<double>::pi * delayMs * range) / sampleRate);
57
58 double swpFactor = sweepFactor > 1.0 ? sweepUp : sweepDown;
59 double swp = sweep;
60
61 clearChannels (*fc.destBuffer, 2, -1, fc.bufferStartSample, fc.bufferNumSamples);
62
63 for (int chan = std::min (2, fc.destBuffer->getNumChannels()); --chan >= 0;)
64 {
65 float* b = fc.destBuffer->getWritePointer (chan, fc.bufferStartSample);
66 swp = sweep;
67
68 for (int i = fc.bufferNumSamples; --i >= 0;)
69 {
70 float inval = *b;
71 const double coef = (1.0 - swp) / (1.0 + swp);
72 double* const fv = filterVals[chan];
73
74 double t = inval + feedbackGain * fv[7];
75
77
78 fv[1] = coef * (fv[1] + t) - fv[0];
79 JUCE_UNDENORMALISE (fv[1]);
80 fv[0] = t;
81
82 fv[3] = coef * (fv[3] + fv[1]) - fv[2];
83 JUCE_UNDENORMALISE (fv[3]);
84 fv[2] = fv[1];
85
86 fv[5] = coef * (fv[5] + fv[3]) - fv[4];
87 JUCE_UNDENORMALISE (fv[5]);
88 fv[4] = fv[3];
89
90 fv[7] = coef * (fv[7] + fv[5]) - fv[6];
91 JUCE_UNDENORMALISE (fv[7]);
92 fv[6] = fv[5];
93
94 inval += (float)fv[7];
95 JUCE_UNDENORMALISE (inval);
96
97 *b++ = inval;
98
99 swp *= swpFactor;
100
101 if (swp > maxSweep) swpFactor = sweepDown;
102 else if (swp < minSweep) swpFactor = sweepUp;
103 }
104 }
105
106 zeroDenormalisedValuesIfNeeded (*fc.destBuffer);
107
108 sweep = swp;
109 sweepFactor = swpFactor;
110}
111
112juce::String PhaserPlugin::getSelectableDescription()
113{
114 return TRANS("Phaser Plugin");
115}
116
117void PhaserPlugin::restorePluginStateFromValueTree (const juce::ValueTree& v)
118{
119 copyPropertiesToCachedValues (v, depth, rate, feedbackGain);
120
121 for (auto p : getAutomatableParameters())
122 p->updateFromAttachedValue();
123}
124
125}} // namespace tracktion { inline namespace engine
Type * getWritePointer(int channelNumber) noexcept
int getNumChannels() const noexcept
#define TRANS(stringLiteral)
#define JUCE_UNDENORMALISE(x)
typedef float
T memset(T... args)
T min(T... args)
Passed into Plugins when they are being initialised, to give them useful contextual information that ...
pow
The context passed to plugin render methods to provide it with buffers to fill.
int bufferNumSamples
The number of samples to write into the audio buffer.
juce::AudioBuffer< float > * destBuffer
The target audio buffer which needs to be filled.
int bufferStartSample
The index of the start point in the audio buffer from which data must be written.