[go: up one dir, main page]

Menu

[r41]: / Source / Synth.cpp  Maximize  Restore  History

Download this file

235 lines (188 with data), 7.3 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
/**************************************************************************
Copyright (C) 2019 Arnaud Champenois arthelion@free.fr
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "Synth.h"
#include "SynthVoice.h"
#include "AudioParameters.h"
#include "PluginProcessor.h"
void Synth::handleController(int /*midiChannel*/, int controllerNumber, int controllerValue)
{
const ScopedLock sl(lock);
// In this synth, a voice is meant to only play a single channel,
// and it MUST receive all the events, whatever it is playing or not.
for (auto* voice : voices)
voice->controllerMoved(controllerNumber, controllerValue);
}
void Synth::handleProgramChange(int midiChannel, int programNumber)
{
mSynthParameters.getProcessor().setCurrentProgram(programNumber);
}
void Synth::setCurrentPlaybackSampleRate(double sampleRateIn)
{
Synthesiser::setCurrentPlaybackSampleRate(sampleRateIn);
mChorus.init(float(sampleRateIn));
mDelay.init(float(sampleRateIn));
mReverb.setSampleRate(sampleRateIn);
mMetronome.init(sampleRateIn);
}
void Synth::updateInternalValues() {
const ScopedLock sl(lock);
mChorus.updateInternalValues();
mDelay.updateInternalValues();
mReverbActive = (float(*mSynthParameters.mReverbMix) > 0.0f);
Reverb::Parameters rParam;
rParam.damping = (float(*mSynthParameters.mReverbDamping));
rParam.dryLevel = (1.0f - float(*mSynthParameters.mReverbMix));
rParam.wetLevel = (float(*mSynthParameters.mReverbMix));
rParam.roomSize = (float(*mSynthParameters.mReverbSize));
rParam.width = (float(*mSynthParameters.mReverbWidth));
mGain = (float(*mSynthParameters.mGain));
bool gainAutoset = bool(*mSynthParameters.mGainAutoset);
if (gainAutoset && !mGainAutoset) {
mGain = 1.0;
*mSynthParameters.mGain = float(mGain);
mGainAutoset = true;
}
else
mGainAutoset = gainAutoset;
mReverb.setParameters(rParam);
PolyMode polyMode = PolyMode(int(*mSynthParameters.mPolyMode));
if (polyMode != mPolyMode) {
mPolyMode = polyMode;
allNotesOff(0, true);
mNoteList.clear();
}
mMetronomOn = *mSynthParameters.mMetronomOn;
mMetronome.setTempo(*mSynthParameters.mMetronomTempo);
}
void Synth::lockVoices()
{
for (auto* voice : voices) {
SynthVoice* svoice = static_cast<SynthVoice*>(voice);
svoice->lockVoices();
}
}
void Synth::unlockVoices()
{
for (auto* voice : voices) {
SynthVoice* svoice = static_cast<SynthVoice*>(voice);
svoice->unlockVoices();
}
}
void Synth::renderVoices(AudioBuffer<float>& outputAudio, int startSample, int numSamples)
{
Synthesiser::renderVoices(outputAudio, startSample, numSamples);
const ScopedLock sl(lock);
int samples = numSamples;
int sampleIn = startSample;
while (--samples >= 0) {
double sampleL = outputAudio.getSample(0, sampleIn);
double sampleR = outputAudio.getSample(1, sampleIn);
if (mChorus.isActive()) {
mChorus.next(sampleL, sampleR);
}
if (mDelay.isActive()) {
mDelay.next(sampleL, sampleR);
}
outputAudio.setSample(0, sampleIn, float(sampleL*mGain));
outputAudio.setSample(1, sampleIn, float(sampleR*mGain));
++sampleIn;
}
if (mReverbActive) {
mReverb.processStereo(outputAudio.getWritePointer(0) + startSample,
outputAudio.getWritePointer(1) + startSample, numSamples);
}
if(mMetronomOn)
mMetronome.process(outputAudio, startSample, numSamples);
if (mGainAutoset) {
double max = outputAudio.getMagnitude(startSample, numSamples);
if (max > 1.0) {
mGain *= 0.9 / max;
const MessageManagerLock mmLock;
*mSynthParameters.mGain = float(mGain);
}
}
}
void Synth::noteOn(const int midiChannel,
const int midiNoteNumber,
const float velocity)
{
const ScopedLock sl(lock);
for (auto* sound : sounds)
{
if (sound->appliesToNote(midiNoteNumber) && sound->appliesToChannel(midiChannel))
{
// If hitting a note that's still ringing, stop it first (it could be
// still playing because of the sustain or sostenuto pedal).
// In mono we alway stop current voice
for (auto* voice : voices) {
// MONO & playing
if ((mPolyMode == MONO && voice->isVoiceActive()) ||
// MONO Legato & playing & note off
(mPolyMode == MONO_LEGATO && voice->isVoiceActive() && !static_cast<SynthVoice*>(mMonoVoice)->isOn()) ||
// playing the same note (all modes)
(mPolyMode != POLY_RINGING && voice->getCurrentlyPlayingNote() == midiNoteNumber && voice->isPlayingChannel(midiChannel))) {
// We stop and find another mono voice
static_cast<SynthVoice*>(voice)->forceExtinction();
if (isMono()) // repeated note : non legato, start another voice
mMonoVoice = findFreeVoice(sound, midiChannel, midiNoteNumber, isNoteStealingEnabled());
}
}
if (isMono()) {
mNoteList.push_back({ midiNoteNumber, velocity });
if (mPolyMode == MONO_LEGATO && static_cast<SynthVoice*>(mMonoVoice)->isOn()) {
static_cast<SynthVoice*>(mMonoVoice)->changeNote(midiNoteNumber);
}
else
// else we start it
startVoice(mMonoVoice, sound, midiChannel, midiNoteNumber, velocity);
return;
}
}
startVoice(findFreeVoice(sound, midiChannel, midiNoteNumber, isNoteStealingEnabled()),
sound, midiChannel, midiNoteNumber, velocity);
}
}
void Synth::noteOff(int midiChannel, int midiNoteNumber, float velocity, bool allowTailOff)
{
const ScopedLock sl(lock);
if (mPolyMode != MONO_LEGATO) // If no legato, just stops note
Synthesiser::noteOff(midiChannel, midiNoteNumber, velocity, allowTailOff);
if (!mNoteList.empty()) {
// The released note is the last mono one
bool triggerOld = (mNoteList.back().midiNoteNumber == midiNoteNumber);
mNoteList.remove({ midiNoteNumber ,0 });
if (!mNoteList.empty() && triggerOld) {
// We released the last one, so we retrigger the previous if any
NoteDescr&d = mNoteList.back();
noteOn(midiChannel, d.midiNoteNumber, d.velocity);
}
else if (mNoteList.empty() && mPolyMode == MONO_LEGATO) {
// No previous, we have to stop the legato chain
mMonoVoice->stopNote(velocity, true);
}
}
}
SynthesiserVoice* Synth::findFreeVoice(SynthesiserSound* soundToPlay,
int midiChannel, int midiNoteNumber,
const bool stealIfNoneAvailable) const
{
const ScopedLock sl(lock);
for (auto* voice : voices) {
if ((!voice->isVoiceActive()) && voice->canPlaySound(soundToPlay))
return voice;
}
if (stealIfNoneAvailable)
return findVoiceToSteal(soundToPlay, midiChannel, midiNoteNumber);
return nullptr;
}