2020-10-24 20:15:18 -06:00
|
|
|
#include "synth.h"
|
2020-10-24 21:12:36 -06:00
|
|
|
#include "synth_waveform.h"
|
2020-10-24 20:15:18 -06:00
|
|
|
|
2020-11-11 17:05:28 -07:00
|
|
|
void FMVoice::LoadPatch(FMPatch *p) {
|
2020-11-25 17:13:54 -07:00
|
|
|
bool playing = this->playing;
|
|
|
|
float pitch = this->pitch;
|
|
|
|
|
|
|
|
NoteOff();
|
2020-10-24 20:15:18 -06:00
|
|
|
for (int i=0; i<NUM_OPERATORS; i++) {
|
|
|
|
FMOperator op = p->operators[i];
|
|
|
|
|
2020-11-11 17:05:28 -07:00
|
|
|
this->oscillators[i].frequencyModulation(1);
|
|
|
|
this->oscillators[i].begin(op.waveform);
|
|
|
|
this->envelopes[i].delay(op.delayTime);
|
|
|
|
this->envelopes[i].attack(op.attackTime);
|
|
|
|
this->oscillators[i].amplitude(op.holdAmplitude);
|
|
|
|
this->envelopes[i].hold(op.holdTime);
|
|
|
|
this->envelopes[i].decay(op.decayTime);
|
|
|
|
this->envelopes[i].sustain(op.sustainAmplitude / op.holdAmplitude);
|
|
|
|
this->envelopes[i].release(op.releaseTime);
|
2020-10-24 20:15:18 -06:00
|
|
|
|
|
|
|
// This feels wasteful 🙁
|
|
|
|
for (int j=0; j<NUM_OPERATORS; j++) {
|
2020-11-11 17:05:28 -07:00
|
|
|
this->mixers[i].gain(j, p->gains[i][j]);
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
2020-11-11 17:05:28 -07:00
|
|
|
this->outputMixer.gain(i, p->gains[i][NUM_OPERATORS]);
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
2020-11-11 17:05:28 -07:00
|
|
|
this->patch = p;
|
2020-11-25 17:13:54 -07:00
|
|
|
if (playing) {
|
|
|
|
NoteOn(pitch);
|
|
|
|
}
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:05:28 -07:00
|
|
|
void FMVoice::SetPitch(float freq) {
|
2020-10-24 20:15:18 -06:00
|
|
|
for (int i=0; i<4; i++) {
|
2020-11-11 17:05:28 -07:00
|
|
|
FMOperator op = this->patch->operators[i];
|
|
|
|
this->oscillators[i].frequency(op.offset + (freq * op.multiplier));
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
2020-11-25 17:13:54 -07:00
|
|
|
this->pitch = freq;
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:05:28 -07:00
|
|
|
void FMVoice::NoteOn(float freq) {
|
|
|
|
SetPitch(freq);
|
2020-10-24 20:15:18 -06:00
|
|
|
for (int i=0; i<4; i++) {
|
2020-11-11 17:05:28 -07:00
|
|
|
this->envelopes[i].noteOn();
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
2020-11-11 17:05:28 -07:00
|
|
|
this->playing = true;
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:05:28 -07:00
|
|
|
void FMVoice::NoteOff() {
|
2020-10-24 20:15:18 -06:00
|
|
|
for (int i=0; i<4; i++) {
|
2020-11-11 17:05:28 -07:00
|
|
|
this->envelopes[i].noteOff();
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|
2020-11-11 17:05:28 -07:00
|
|
|
this->playing = false;
|
2020-10-24 20:15:18 -06:00
|
|
|
}
|