/**************************************************************************
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/>.
**************************************************************************/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "Chorus.h"
#include "StereoDelay.h"
#include "Metronome.h"
#include <list>
/*!
* The Synth class is the sound generator. It take care of :
* - affecting voice according to mode and polyphony,
* - mixing
* - applying effects Chorus, Delay and Reverb,
* - Adjusting output gain.
* Every computation is done in double precision, to allow algos such as FM
* to be coded very simply without having to manage rounding effects.
* The most of processing is done "inline" as in JUCE philosophy.
*/
class Synth : public Synthesiser {
public:
typedef enum {
POLY = 0,
MONO = 1,
MONO_LEGATO = 2,
POLY_RINGING = 3
} PolyMode;
Synth(SynthParameters¶m) :
mChorus(param), mDelay(param), mSynthParameters(param) {}
virtual void handleController(int midiChannel,
int controllerNumber,
int controllerValue) override;
virtual void handleProgramChange(int midiChannel, int programNumber) override;
virtual void setCurrentPlaybackSampleRate(double sampleRate) override;
void updateInternalValues();
bool isPlaying() const {
for (auto* voice : voices) {
if (voice->isVoiceActive())
return true;
}
return false;
}
bool isMuted() const {
for (auto* voice : voices) {
if (voice->isVoiceActive())
return false;
}
return true;
}
virtual void noteOn(const int midiChannel, const int midiNoteNumber, const float velocity) override;
virtual void noteOff(int midiChannel,
int midiNoteNumber,
float velocity,
bool allowTailOff) override;
void setMonoVoice() {
mMonoVoice = voices[0];
}
bool isMono() const { return (( mPolyMode == MONO) || (mPolyMode == MONO_LEGATO)); }
protected:
virtual void renderVoices(AudioBuffer<float>& outputAudio,
int startSample, int numSamples) override;
virtual SynthesiserVoice* findFreeVoice(SynthesiserSound* soundToPlay,
int midiChannel,
int midiNoteNumber,
bool stealIfNoneAvailable) const;
private:
struct NoteDescr {
int midiNoteNumber; float velocity;
bool operator==(const NoteDescr& d) const { return d.midiNoteNumber == midiNoteNumber; }
};
std::list<NoteDescr> mNoteList;
Chorus mChorus;
StereoDelay mDelay;
Reverb mReverb;
double mGain = 1.0;
bool mGainAutoset = true;
bool mReverbActive = false;
PolyMode mPolyMode = POLY;
SynthParameters& mSynthParameters;
SynthesiserVoice* mMonoVoice;
Metronome mMetronome;
bool mMetronomOn = false;
};