#ifndef __AbstractSynth_h__
#define __AbstractSynth_h__
#include <climits>
#include "Synth.h"
#include "MidiProcessor.h"
#include "VelocityCurve.h"
class AbstractSynth : public Synth, public MidiProcessor, public VelocityCurve {
protected:
uint8_t note = 60; float pb = 0;
float pb_range = 2/8192.0f;
float mod_range = 0.5/127.0f;
float tuning = 440;
public:
virtual ~AbstractSynth(){}
void setTuning(float value){
tuning = value;
}
float getTuning(){
return tuning;
}
uint8_t getNote(){
return note;
}
virtual void setNote(uint8_t note){
this->note = note;
setFrequency(noteToFrequency(note+pb));
}
float getPitchBend(){
return pb;
}
virtual void setPitchBend(float pb){
this->pb = pb;
setFrequency(noteToFrequency(note+pb));
}
float getPitchBendRange(){
return pb_range;
}
void setPitchBendRange(float range){
this->pb_range = range/8192.0f;
}
void setModulationDepthRange(float range){
mod_range = range / 127.0f;
}
virtual void noteOn(MidiMessage msg) {
setNote(msg.getNote());
setFrequency(noteToFrequency(note+pb));
setGain(velocityToGain(msg.getVelocity()));
gate(true);
}
virtual void noteOff(MidiMessage msg) {
gate(false);
}
virtual void controlChange(MidiMessage msg) {
if(msg.getControllerNumber() == MIDI_CC_MODULATION)
setModulation(msg.getControllerValue()/128.0f);
else if(msg.getControllerNumber() == MIDI_ALL_NOTES_OFF)
allNotesOff();
}
virtual void channelPressure(MidiMessage msg) {
setPressure(msg.getChannelPressure()/128.0f);
}
virtual void polyKeyPressure(MidiMessage msg) {
setPressure(msg.getPolyKeyPressure()/128.0f);
}
virtual void modulate(MidiMessage msg) {
setModulation(mod_range * msg.getControllerValue());
}
virtual void pitchbend(MidiMessage msg) {
setPitchBend(pb_range * msg.getPitchBend());
}
virtual void allNotesOff(){
gate(false);
}
virtual void setModulation(float modulation){}
virtual void setPressure(float pressure){}
inline float frequencyToNote(float freq){
return 12 * log2f(freq / tuning) + 69;
}
inline float noteToFrequency(float note){
return tuning * exp2f((note - 69) / 12);
}
};
#endif