-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPluginProcessor.cpp
More file actions
243 lines (202 loc) · 8.11 KB
/
PluginProcessor.cpp
File metadata and controls
243 lines (202 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
// For when microtonal scale support is implemented...
//double roundToFreqMultiple(double value, double freq, double multiple)
//{
// return 69 + 6 * multiple * (round((value-69) / (6 * multiple) - log(freq/440)/log(multiple)) + log(freq/440)/log(multiple));
//}
RepitchAudioProcessor::RepitchAudioProcessor() :
AudioProcessor (BusesProperties()
.withInput("Input", AudioChannelSet::mono(), true)
.withOutput("Output", AudioChannelSet::stereo(), true)),
parameters (*this, nullptr, Identifier ("Repitch"), {
std::make_unique<AudioParameterFloat> ("pitch",
"Pitch",
NormalisableRange<float> (0., 127., nullptr,
nullptr,
[this](float start, float end, float value) { if (snapParam != nullptr && *snapParam) return float(round(value)); else return value; }
),
60.),
std::make_unique<AudioParameterFloat> ("attack", "Attack", NormalisableRange<float> (0.01, 10., pow10VRF, log10VRF), 0.01),
std::make_unique<AudioParameterFloat> ("decay", "Decay", NormalisableRange<float> (0.01, 10., pow10VRF, log10VRF), 10.),
std::make_unique<AudioParameterFloat> ("sustain", "Sustain", NormalisableRange<float> (0, 1), 1.),
std::make_unique<AudioParameterFloat> ("release", "Release", NormalisableRange<float> (0.01, 10., pow10VRF, log10VRF), 0.01),
std::make_unique<AudioParameterBool>("snap", "Snap", true)
})
{
pitchParam = parameters.getRawParameterValue("pitch");
aParam = parameters.getRawParameterValue("attack");
dParam = parameters.getRawParameterValue("decay");
sParam = parameters.getRawParameterValue("sustain");
rParam = parameters.getRawParameterValue("release");
snapParam = parameters.getRawParameterValue("snap");
}
RepitchAudioProcessor::~RepitchAudioProcessor()
{
}
const String RepitchAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool RepitchAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool RepitchAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool RepitchAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double RepitchAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int RepitchAudioProcessor::getNumPrograms()
{
return 1;
}
int RepitchAudioProcessor::getCurrentProgram()
{
return 0;
}
void RepitchAudioProcessor::setCurrentProgram (int index)
{
}
const String RepitchAudioProcessor::getProgramName (int index)
{
return {};
}
void RepitchAudioProcessor::changeProgramName (int index, const String& newName)
{
}
void RepitchAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
pitchSmoother.reset(sampleRate, samplesPerBlock/sampleRate);
aSmoother.reset(sampleRate, samplesPerBlock/sampleRate);
dSmoother.reset(sampleRate, samplesPerBlock/sampleRate);
sSmoother.reset(sampleRate, samplesPerBlock/sampleRate);
rSmoother.reset(sampleRate, samplesPerBlock/sampleRate);
ring.reset(new RingBuffer(getTotalNumInputChannels(), 64*sampleRate));
for (Voice voice : voices)
voice.envelope.setSampleRate(sampleRate);
}
void RepitchAudioProcessor::releaseResources()
{
}
bool RepitchAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
return true;
}
void RepitchAudioProcessor::processBlock (AudioBuffer<float>& audio, MidiBuffer& midi)
{
ScopedNoDenormals noDenormals;
int totalNumInputChannels = getTotalNumInputChannels();
int totalNumOutputChannels = getTotalNumOutputChannels();
for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
audio.clear(i, 0, audio.getNumSamples());
MidiBuffer::Iterator midiIter (midi);
MidiMessage m;
int mSample = 0;
pitchSmoother.setTargetValue(*pitchParam);
aSmoother.setTargetValue(*aParam);
dSmoother.setTargetValue(*dParam);
sSmoother.setTargetValue(*sParam);
rSmoother.setTargetValue(*rParam);
for (int sample=0; sample<audio.getNumSamples(); ++sample)
{
adsrParameters.attack = aSmoother.getNextValue();
adsrParameters.decay = dSmoother.getNextValue();
adsrParameters.sustain = sSmoother.getNextValue();
adsrParameters.release = rSmoother.getNextValue();
float pitch = pitchSmoother.getNextValue();
float period = getSampleRate() / 440 * pow(2, (69 - pitch) / 12);
// for (const MidiMessageMetadata m : midi)
while (sample==mSample && midiIter.getNextEvent(m, mSample))
{
if (m.isNoteOnOrOff())
{
int note = m.getNoteNumber();
if (m.isNoteOn())
{
if (!voices[note].envelope.isActive())
voices[note].delay = 0;
voices[note].gain = m.getFloatVelocity();
voices[note].envelope.noteOn();
}
else
voices[note].envelope.noteOff();
}
}
for (int channel=0; channel<totalNumInputChannels; ++channel)
{
ring->pushSample(channel, audio.getSample(channel, sample));
audio.clear(channel, sample, 1);
}
for (int note=0; note<128; ++note)
{
Voice& voice = voices[note];
if (voice.envelope.isActive())
{
voice.stride = 1 - pow(2, (note-pitch) / 12.);
voice.envelope.setParameters(adsrParameters);
float envelope = voice.envelope.getNextSample();
for (int channel=0; channel<(totalNumInputChannels==1 ? totalNumOutputChannels : totalNumInputChannels); ++channel)
{
int sourceChannel = totalNumInputChannels==1 ? 0 : channel;
audio.addSample(channel, sample, voice.gain * envelope *
((cos(3.14159265359 * voice.delay / period) / -2 + 0.5) *
ring->getSampleAtDelay(sourceChannel,
voice.delay) +
(cos(3.14159265359 * voice.delay / period) / 2 + 0.5) *
ring->getSampleAtDelay(sourceChannel,
voice.delay + period)));
}
voice.delay = fmod(voice.delay + voice.stride, period);
if (voice.delay<0)
voice.delay += period;
}
}
ring->increment();
}
}
bool RepitchAudioProcessor::hasEditor() const
{
return true;
}
AudioProcessorEditor* RepitchAudioProcessor::createEditor()
{
return new RepitchAudioProcessorEditor (*this, parameters);
}
void RepitchAudioProcessor::getStateInformation (MemoryBlock& destData)
{
auto state = parameters.copyState();
std::unique_ptr<XmlElement> xml (state.createXml());
copyXmlToBinary (*xml, destData);
}
void RepitchAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
std::unique_ptr<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
if (xmlState.get() != nullptr)
if (xmlState->hasTagName (parameters.state.getType()))
parameters.replaceState (ValueTree::fromXml (*xmlState));
}
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new RepitchAudioProcessor();
}