/* Chassis polysynth framework 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 "chassis.hpp" #include "ic1.hpp" #include "patches.hpp" Assigner ic1; START_NAMESPACE_DISTRHO Chassis::Chassis() : Plugin(parameterCount, 128, 0), sampleRate(getSampleRate()) { // one parameter, no programs, no states } // Initialisation functions void Chassis::initAudioPort(bool input, uint32_t index, AudioPort &port) { port.groupId = kPortGroupStereo; Plugin::initAudioPort(input, index, port); if (!input && index == 0) port.name = "Left Out"; if (!input && index == 1) port.name = "Right Out"; } void Chassis::initProgramName(uint32_t index, String &programName) { programName = patchName[index & 0x7f].c_str(); } void Chassis::loadProgram(uint32_t index) { memmove(&s.patchRam, (uint8_t *)patchData + (index * 18), 18); } // Processing functions void Chassis::activate() { // calculate filter coefficients and stuff printf("called activate()\n"); for (uint8_t i = 0; i < 104; i++) { s.pitchCV[i] = (440.0f * powf(2, (i - 49) / 12.0f)) / sampleRate; } //ic1.ram[0xc6] = 0x42; //ic1.ram[0xb6] = 0; //ic1.resetVoices(); } void Chassis::deactivate() { // zero out the outputs, maybe printf("called deactivate()\n"); } void Chassis::noteOn(uint8_t note) { uint32_t i; //ic1.ram[0x4e] = 0x90; //ic1.ram[0x3e] = note; // ic1.noteOn(0x40); #ifdef D_MIDIBITMAP printf("note on -> "); for (uint8_t i = 0x40; i < 0x4f; i++) { printf("%02x ", ic1.ram[i]); } printf("\n"); #endif s.keyon = true; for (i = 0; i < NUM_VOICES; i++) { vPtr++; if (vPtr == NUM_VOICES) vPtr = 0; if (s.voice[vPtr].isFree()) { // if it's an existing note don't reset s.voice[vPtr].on(note, s.voice[i].note != note); break; } } if (i == NUM_VOICES) { // didn't find a free voice vPtr++; if (vPtr == NUM_VOICES) vPtr = 0; s.voice[vPtr].on(note, true); } } void Chassis::noteOff(uint8_t note) { //ic1.ram[0x4e] = 0x90; //ic1.ram[0x3e] = note; //ic1.noteOn(0); #ifdef D_MIDIBITMAP printf("note off -> "); for (uint8_t i = 0x40; i < 0x4f; i++) { printf("%02x ", ic1.ram[i]); } printf("\n"); #endif s.keyon = false; for (uint32_t i = 0; i < NUM_VOICES; i++) { if (s.voice[i].note == note && !s.voice[i].isFree()) { s.voice[i].off(); break; } } } void Chassis::doMidi(const MidiEvent *ev, uint32_t count, uint32_t timeLimit) { uint32_t i; if (count == 0) return; for (i = lastEvent; i < count; i++) { if (ev[i].frame > timeLimit) break; switch (ev[i].data[0]) { case 0x90: noteOn(ev[i].data[1]); break; case 0x80: noteOff(ev[i].data[1]); break; } } lastEvent = i; //ic1.voiceOn(); } void Chassis::run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) { uint32_t framePos = 0; uint32_t sizeThisTime; s.framesLeft = frames; // flatten the left channel to use as temporary storage, since // the synth engine only generates a mono channel bzero(outputs[0], sizeof(float) * frames); bzero(outputs[1], sizeof(float) * frames); // get any of the last block's worth of MIDI out of the way lastEvent = 0; doMidi(midiEvents, midiEventCount, s.blockLeft); while (framePos < frames) { if (s.blockLeft == 0) { s.blockLeft = sampleRate / 238; doMidi(midiEvents, midiEventCount, framePos + s.blockLeft); s.lfoDelay(); s.runLFO(); for (uint32_t i = 0; i < NUM_VOICES; i++) { s.voice[i].calcPitch(s); switch (s.patchRam.switch1 & 0x07) { case 0: s.voice[i].omega /= 2; break; case 2: s.voice[i].omega *= 2; default: break; } s.voice[i].envelope(s); s.voice[i].calcFilter(s); } } sizeThisTime = (s.framesLeft < s.blockLeft) ? s.framesLeft : s.blockLeft; // run each synth voice for (uint8_t i = 0; i < NUM_VOICES; i++) { s.voice[i].run(s, outputs[0] + framePos, sizeThisTime); } framePos += sizeThisTime; s.framesLeft -= sizeThisTime; s.blockLeft -= sizeThisTime; } // highpass filter float tmp = s.hptmp; float smp, flt = 0; float cut_tbl[4] = {0.91245, 0.97097, 1, 0.99088}; float cut = cut_tbl[(s.patchRam.switch2 & 0x18) >> 3]; float gain_tbl[4] = {-1, -1, 0, 4}; float gain = gain_tbl[(s.patchRam.switch2 & 0x18) >> 3]; // printf("hp= %02x\n", (s.patchRam.switch2 & 0x18)>>3); for (uint32_t i = 0; i < frames; i++) { smp = outputs[0][i]; flt = ((tmp - smp) * cut) + smp; tmp = flt; outputs[0][i] = ((flt * gain) + smp); } s.hptmp = flt; // copy left to right memmove(outputs[1], outputs[0], sizeof(float) * frames); // outputs[1][1]=1; } // create the plugin Plugin *createPlugin() { return new Chassis(); } END_NAMESPACE_DISTRHO