peacock/plugin/ic29.cpp
2024-10-13 21:07:01 +01:00

89 lines
2.3 KiB
C++

/*
Peacock-8 VA polysynth
Copyright 2024 Gordon JC Pearce <gordonjcp@gjcp.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "ic29.hpp"
Synth s;
Synth::Synth() {
d_debug("initialising synth\n");
envAtk = 0x007f;
envDcy = envRls = 0xfe90;
envStn = 0x1fff;
}
void Synth::run() {
// handle a "loop" worth of envelopes, pitch calculations, etc
// callled once every 4.3ms block of samples
for (uint8_t i = 0; i < NUM_VOICES; i++) {
s.voices[i].env.run();
}
}
void Synth::voiceOn(uint8_t voice, uint8_t note) {
// enable synth voice, start it all running
voice &= 0x7f;
s.voices[voice].env.on();
// FIXME determine if we need to reset the counter
s.voices[voice].note = note;
}
void Synth::voiceOff(uint8_t voice) {
// enable synth voice, start it all running
voice &= 0x7f;
s.voices[voice].env.off();
}
Envelope::Envelope() {
level = 0;
phase = ENV_IDLE;
}
void Envelope::run() {
switch (phase) {
case ENV_ATK:
level += s.envAtk;
if (level > 0x3fff) {
level = 0x3fff;
phase = ENV_DCY;
}
break;
case ENV_DCY:
if (level > s.envStn) {
level -= s.envStn;
level = (level * s.envDcy) >> 16;
level += s.envStn;
} else {
level = s.envStn;
}
break;
case ENV_RLS:
level = (level * s.envRls) >> 16;
break;
case ENV_IDLE:
default:
break;
}
}
Voice::Voice() {
}
extern Synth s;