uilleann/synth.cpp

48 lines
1.2 KiB
C++
Raw Normal View History

#include "synth.h"
#include "synth_waveform.h"
2020-11-11 17:05:28 -07:00
void FMVoice::LoadPatch(FMPatch *p) {
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);
// 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-11-11 17:05:28 -07:00
this->outputMixer.gain(i, p->gains[i][NUM_OPERATORS]);
}
2020-11-11 17:05:28 -07:00
this->patch = p;
}
2020-11-11 17:05:28 -07:00
void FMVoice::SetPitch(float freq) {
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-11-11 17:05:28 -07:00
void FMVoice::NoteOn(float freq) {
SetPitch(freq);
for (int i=0; i<4; i++) {
2020-11-11 17:05:28 -07:00
this->envelopes[i].noteOn();
}
2020-11-11 17:05:28 -07:00
this->playing = true;
}
2020-11-11 17:05:28 -07:00
void FMVoice::NoteOff() {
for (int i=0; i<4; i++) {
2020-11-11 17:05:28 -07:00
this->envelopes[i].noteOff();
}
2020-11-11 17:05:28 -07:00
this->playing = false;
}