/* Peacock-8 VA polysynth Copyright 2024 Gordon JC Pearce 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" #include "ic29tables.hpp" Synth ic29; Synth::Synth() { d_debug("initialising synth\n"); portaCoeff = 0x0; noise = new float [4096]; } void Synth::buildTables(double sampleRate) { for (uint8_t i = 0; i < 104; i++) { // slightly flat middle C from ROM divider table // actually adjusted a little so that the notes are bang on // on the real synth the tuning knob is tweaked a little off to pull it in pitchTable[i] = 260.15f * powf(2, (i - 36) / 12.0f) / sampleRate; } // precompute a table of values to map the filter value to a filter coefficient // the ROM preset for adjusting the VCF scale and centre presets cutoff to $31 // and key scale to $7f, which corresponds to C4 = 248Hz and C6 = 992Hz, B3 and B5 for (uint16_t i = 0; i< 256; i++) { } } void Synth::run() { // handle a "loop" worth of envelopes, pitch calculations, etc // callled once every 4.3ms block of samples ic29.lfo.run(); masterPitch = 0x1818; uint16_t vcoLfoDepth = ic29.lfoDepthTable[ic29.patchRam.vcoLfoMod] + ic29.modWheel; vcoLfoDepth = (vcoLfoDepth < 0xff) ? vcoLfoDepth : 0xff; masterPitch += (lfo.lfoOut * vcoLfoDepth) >> 11; if (pitchBend < 0x0100) pitchBend = 0x0100; masterPitch += (((pitchBend >> 8) - 0x20) * vcoBend) / 1.281; // need to calculate VCF "base" setting // various on/off switches // PWM is bit 0 sw2, 0 = fixed 1 = lfo // 0 sets EA to 0x3fff, 1 adds uint16_t pwmVal = 0x2000 - ic29.lfo.lfoOut; if (ic29.patchRam.switch2 & 0x01) pwmVal = 0x3fff; ic29.pwm = 0.5 - pwmVal / 32768.0f * (ic29.patchRam.pwmLfoMod / 106.0f); // generate the voices, then for (uint32_t i=0; i 0x1fff) { lfoOut = 0x1fff; phase = 0; } if (lfoOut < -0x1fff) { lfoOut = -0x1fff; phase = 1; } // printf("lfoOut=%04x\n", lfoOut); } Envelope::Envelope() { level = 0; phase = ENV_RLS; } void Envelope::run() { uint16_t tempStn = ic29.patchRam.sustain << 7; switch (phase) { case ENV_ATK: level += atkTable[ic29.patchRam.attack]; if (level > 0x3fff) { level = 0x3fff; phase = ENV_DCY; } break; case ENV_DCY: if (level > tempStn) { level = (((level - tempStn) * dcyTable[ic29.patchRam.decay]) >> 16) + tempStn; } else { level = tempStn; } break; case ENV_RLS: level = (level * dcyTable[ic29.patchRam.release]) >> 16; break; case ENV_IDLE: default: break; } } Voice::Voice() { subosc = .1; } void Voice::calcPitch() { uint16_t target = note << 8; // Portamento is a linear change of pitch - it'll take twice as long // to jump two octaves as it takes to jump one // By comparison "glide" is like an RC filter, for example in the TB303 // This is implemented here by adding on a step value until you pass // the desired final pitch. Once that happens the value is clamped to the // desired pitch. if (ic29.portaCoeff != 0) { // portamento up if (pitch < target) { pitch += ic29.portaCoeff; if (pitch > target) pitch = target; } // portamento down if (pitch > target) { pitch -= ic29.portaCoeff; if (pitch < target) pitch = target; } } else { pitch = target; } pitch += ic29.masterPitch; if (pitch < 0x3000) pitch = 0x3000; // lowest note if (pitch > 0x9700) pitch = 0x6700; // highest note pitch -= 0x3000; // interpolate between the two table values double o1 = ic29.pitchTable[pitch >> 8]; double o2 = ic29.pitchTable[(pitch >> 8) + 1]; double frac = (pitch & 0xff) / 256.0f; omega = ((o2 - o1) * frac) + o1; } void Voice::update() { // calculate the once-per-block values env.run(); calcPitch(); pw = (ic29.patchRam.switch1 & 0x08) ? ic29.pwm : 0.0f; saw = (ic29.patchRam.switch1 & 0x10) ? 1.0f : 0.0f; sub = ic29.patchRam.subLevel / 128.0f; ic29.lfo.rate = ic29.patchRam.lfoRate; // do filter values } void Voice::on(uint8_t key) { voiceState = V_ON; if (note != key) { phase = 0; } note = key; if (env.phase == env.ENV_RLS) { env.on(); // FIXME move to synth update code } } void Voice::off() { // sustain - I need to rethink this bit FIXME voiceState = V_OFF; if (!ic29.sustained) { env.off(); } }