This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathFoleysSynth.cpp
226 lines (184 loc) · 8.7 KB
/
FoleysSynth.cpp
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
/*
==============================================================================
FoleysSynth.cpp
Created: 23 Nov 2019 8:08:35pm
Author: Daniel Walz
==============================================================================
*/
#include "FoleysSynth.h"
namespace IDs
{
static juce::String paramAttack { "attack" };
static juce::String paramDecay { "decay" };
static juce::String paramSustain { "sustain" };
static juce::String paramRelease { "release" };
static juce::String paramGain { "gain" };
}
//==============================================================================
int FoleysSynth::numOscillators = 8;
void FoleysSynth::addADSRParameters (juce::AudioProcessorValueTreeState::ParameterLayout& layout)
{
auto attack = std::make_unique<juce::AudioParameterFloat>(juce::ParameterID (IDs::paramAttack, 1), "Attack", juce::NormalisableRange<float> (0.001f, 0.5f, 0.01f), 0.10f);
auto decay = std::make_unique<juce::AudioParameterFloat>(juce::ParameterID (IDs::paramDecay, 1), "Decay", juce::NormalisableRange<float> (0.001f, 0.5f, 0.01f), 0.10f);
auto sustain = std::make_unique<juce::AudioParameterFloat>(juce::ParameterID (IDs::paramSustain, 1), "Sustain", juce::NormalisableRange<float> (0.0f, 1.0f, 0.01f), 1.0f);
auto release = std::make_unique<juce::AudioParameterFloat>(juce::ParameterID (IDs::paramRelease, 1), "Release", juce::NormalisableRange<float> (0.001f, 0.5f, 0.01f), 0.10f);
auto group = std::make_unique<juce::AudioProcessorParameterGroup>("adsr", "ADRS", "|",
std::move (attack),
std::move (decay),
std::move (sustain),
std::move (release));
layout.add (std::move (group));
}
void FoleysSynth::addOvertoneParameters (juce::AudioProcessorValueTreeState::ParameterLayout& layout)
{
auto group = std::make_unique<juce::AudioProcessorParameterGroup>("oscillators", "Oscillators", "|");
for (int i = 0; i < FoleysSynth::numOscillators; ++i)
{
group->addChild (std::make_unique<juce::AudioParameterFloat>(juce::ParameterID ("osc" + juce::String (i), 1), "Oscillator " + juce::String (i), juce::NormalisableRange<float>(0.0f, 1.0f, 0.01f), 0.0f));
group->addChild (std::make_unique<juce::AudioParameterFloat>(juce::ParameterID ("detune" + juce::String (i), 1), "Detune " + juce::String (i), juce::NormalisableRange<float>(-0.5f, 0.5f, 0.01f), 0.0f));
}
layout.add (std::move (group));
}
void FoleysSynth::addGainParameters (juce::AudioProcessorValueTreeState::ParameterLayout& layout)
{
auto gain = std::make_unique<juce::AudioParameterFloat>(juce::ParameterID (IDs::paramGain, 1), "Gain", juce::NormalisableRange<float> (0.0f, 1.0f, 0.01f), 0.70f);
layout.add (std::make_unique<juce::AudioProcessorParameterGroup>("output", "Output", "|", std::move (gain)));
}
//==============================================================================
FoleysSynth::FoleysSound::FoleysSound (juce::AudioProcessorValueTreeState& stateToUse)
: state (stateToUse)
{
attack = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter (IDs::paramAttack));
jassert (attack);
decay = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter (IDs::paramDecay));
jassert (decay);
sustain = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter (IDs::paramSustain));
jassert (sustain);
release = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter (IDs::paramRelease));
jassert (release);
gain = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter (IDs::paramGain));
jassert (gain);
}
juce::ADSR::Parameters FoleysSynth::FoleysSound::getADSR()
{
juce::ADSR::Parameters parameters;
parameters.attack = attack->get();
parameters.decay = decay->get();
parameters.sustain = sustain->get();
parameters.release = release->get();
return parameters;
}
//==============================================================================
FoleysSynth::FoleysVoice::FoleysVoice (juce::AudioProcessorValueTreeState& state)
{
for (int i=0; i < FoleysSynth::numOscillators; ++i)
{
oscillators.push_back (std::make_unique<BaseOscillator>());
auto& osc = oscillators.back();
osc->gain = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter ("osc" + juce::String (i)));
osc->detune = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter ("detune" + juce::String (i)));
osc->osc.get<0>().initialise ([](auto arg){return std::sin (arg);}, 512);
osc->multiplier = i + 1;
}
gainParameter = dynamic_cast<juce::AudioParameterFloat*>(state.getParameter (IDs::paramGain));
jassert (gainParameter);
oscillatorBuffer.setSize (1, internalBufferSize);
voiceBuffer.setSize (1, internalBufferSize);
}
bool FoleysSynth::FoleysVoice::canPlaySound (juce::SynthesiserSound* sound)
{
return dynamic_cast<FoleysSound*>(sound) != nullptr;
}
void FoleysSynth::FoleysVoice::startNote (int midiNoteNumber,
float velocity,
juce::SynthesiserSound* sound,
int currentPitchWheelPosition)
{
juce::ignoreUnused (midiNoteNumber, velocity);
if (auto* foleysSound = dynamic_cast<FoleysSound*>(sound))
adsr.setParameters (foleysSound->getADSR());
pitchWheelValue = getDetuneFromPitchWheel (currentPitchWheelPosition);
adsr.noteOn();
for (auto& osc : oscillators)
updateFrequency (*osc, true);
}
void FoleysSynth::FoleysVoice::stopNote (float velocity,
bool allowTailOff)
{
juce::ignoreUnused (velocity);
if (allowTailOff)
{
adsr.noteOff();
}
else
{
adsr.reset();
clearCurrentNote();
}
}
void FoleysSynth::FoleysVoice::pitchWheelMoved (int newPitchWheelValue)
{
pitchWheelValue = getDetuneFromPitchWheel (newPitchWheelValue);
}
void FoleysSynth::FoleysVoice::controllerMoved (int controllerNumber, int newControllerValue)
{
juce::ignoreUnused (controllerNumber, newControllerValue);
}
void FoleysSynth::FoleysVoice::renderNextBlock (juce::AudioBuffer<float>& outputBuffer,
int startSample,
int numSamples)
{
if (! adsr.isActive())
return;
while (numSamples > 0)
{
auto left = std::min (numSamples, oscillatorBuffer.getNumSamples());
auto block = juce::dsp::AudioBlock<float> (oscillatorBuffer).getSingleChannelBlock (0).getSubBlock (0, size_t (left));
juce::dsp::ProcessContextReplacing<float> context (block);
voiceBuffer.clear();
for (auto& osc : oscillators)
{
auto oscGain = osc->gain->get();
if (oscGain < 0.01)
continue;
updateFrequency (*osc);
osc->osc.get<1>().setGainLinear (oscGain);
oscillatorBuffer.clear();
osc->osc.process (context);
voiceBuffer.addFrom (0, 0, oscillatorBuffer.getReadPointer (0), left);
}
adsr.applyEnvelopeToBuffer (voiceBuffer, 0, left);
const auto gain = gainParameter->get();
outputBuffer.addFromWithRamp (0, startSample, voiceBuffer.getReadPointer (0), left, lastGain, gain);
lastGain = gain;
startSample += left;
numSamples -= left;
if (! adsr.isActive())
clearCurrentNote();
}
}
void FoleysSynth::FoleysVoice::setCurrentPlaybackSampleRate (double newRate)
{
juce::SynthesiserVoice::setCurrentPlaybackSampleRate (newRate);
juce::dsp::ProcessSpec spec;
spec.sampleRate = newRate;
spec.maximumBlockSize = juce::uint32 (internalBufferSize);
spec.numChannels = 1;
for (auto& osc : oscillators)
osc->osc.prepare (spec);
}
double FoleysSynth::FoleysVoice::getFrequencyForNote (int noteNumber, double detune, double concertPitch) const
{
return concertPitch * std::pow (2.0, (noteNumber + detune - 69.0) / 12.0);
}
double FoleysSynth::FoleysVoice::getDetuneFromPitchWheel (int wheelValue) const
{
return (wheelValue / 8192.0) - 1.0;
}
void FoleysSynth::FoleysVoice::updateFrequency (BaseOscillator& oscillator, bool noteStart)
{
const auto freq = getFrequencyForNote (getCurrentlyPlayingNote(),
pitchWheelValue * maxPitchWheelSemitones
+ oscillator.detune->get());
oscillator.osc.get<0>().setFrequency (float (freq * oscillator.multiplier), noteStart);
}