chassis/plugin/chassis.cpp

171 lines
5.4 KiB
C++
Raw Normal View History

2024-09-02 21:11:57 +00:00
/*
2024-09-03 15:17:32 +00:00
Chassis polysynth framework
2024-09-02 21:11:57 +00:00
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 "chassis.hpp"
START_NAMESPACE_DISTRHO
Chassis::Chassis() : Plugin(parameterCount, 0, 0), sampleRate(getSampleRate()) { // one parameter, no programs, no states
2024-09-02 21:11:57 +00:00
}
// 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";
}
// Processing functions
void Chassis::activate() {
2024-09-04 20:08:27 +00:00
// calculate filter coefficients and stuff
2024-09-02 21:11:57 +00:00
printf("called activate()\n");
2024-09-09 12:34:02 +00:00
for (uint8_t i=0; i<104; i++) {
s.pitchCV[i] = (261.63 * powf(2, (i-24) / 12.0f)) / sampleRate;
}
2024-09-02 21:11:57 +00:00
}
void Chassis::deactivate() {
// zero out the outputs, maybe
printf("called deactivate()\n");
2024-09-03 11:42:16 +00:00
// printf("%02x", assign[1]);
2024-09-02 21:11:57 +00:00
}
2024-09-02 21:52:53 +00:00
void Chassis::noteOn(uint8_t note) {
2024-09-03 11:42:16 +00:00
uint32_t i;
2024-09-09 10:34:13 +00:00
s.keyon = true;
2024-09-03 11:42:16 +00:00
for (i = 0; i < NUM_VOICES; i++) {
2024-09-03 13:15:02 +00:00
vPtr++;
if (vPtr == NUM_VOICES) vPtr = 0;
2024-09-03 22:30:08 +00:00
if (s.voice[vPtr].isFree()) {
2024-09-07 19:46:57 +00:00
// printf("voice %d is free, existing note = %d, note = %d\n", vPtr, s.voice[i].note, note);
// if it's an existing note don't reset
2024-09-03 22:30:08 +00:00
s.voice[vPtr].on(note, s.voice[i].note != note);
2024-09-07 19:46:57 +00:00
// printf("note on %d for voice %d\n", note, vPtr);
2024-09-02 21:52:53 +00:00
break;
}
}
2024-09-04 20:08:27 +00:00
2024-09-03 11:42:16 +00:00
if (i == NUM_VOICES) { // didn't find a free voice
2024-09-03 13:15:02 +00:00
vPtr++;
if (vPtr == NUM_VOICES) vPtr = 0;
2024-09-03 22:30:08 +00:00
s.voice[vPtr].on(note, true);
2024-09-02 21:52:53 +00:00
}
}
2024-09-02 21:11:57 +00:00
2024-09-02 21:52:53 +00:00
void Chassis::noteOff(uint8_t note) {
// printf("noteoff %d\n", note);
2024-09-09 10:34:13 +00:00
s.keyon = false;
2024-09-02 21:52:53 +00:00
for (uint32_t i = 0; i < NUM_VOICES; i++) {
2024-09-03 22:30:08 +00:00
if (s.voice[i].note == note && !s.voice[i].isFree()) {
s.voice[i].off();
2024-09-07 19:46:57 +00:00
// printf("note off %d for voice %d\n", note, i);
2024-09-03 11:42:16 +00:00
break;
}
}
2024-09-02 21:11:57 +00:00
}
2024-09-03 11:42:16 +00:00
2024-09-05 22:18:41 +00:00
void Chassis::doMidi(const MidiEvent *ev, uint32_t count, uint32_t timeLimit) {
2024-09-07 19:46:57 +00:00
// printf("doMidi() handling events from %d to %d\n", lastEvent, timeLimit);
2024-09-05 22:18:41 +00:00
uint32_t i;
if (count == 0) return;
for (i = lastEvent; i < count; i++) {
2024-09-07 19:46:57 +00:00
// printf("doMidi event number %d of %d %02x %02x\n", i, count, ev[i].data[0], ev[i].data[1]);
2024-09-05 22:18:41 +00:00
if (ev[i].frame > timeLimit) break;
2024-09-07 19:46:57 +00:00
switch (ev[i].data[0]) {
case 0x90:
noteOn(ev[i].data[1]);
break;
case 0x80:
noteOff(ev[i].data[1]);
break;
2024-09-05 13:30:16 +00:00
}
}
2024-09-05 22:18:41 +00:00
lastEvent = i;
2024-09-05 13:30:16 +00:00
}
void Chassis::run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) {
2024-09-03 11:42:16 +00:00
// if (midiEventCount > 0) printf("\n--------------------\n");
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
uint32_t framePos = 0;
uint32_t sizeThisTime;
2024-09-05 13:30:16 +00:00
s.framesLeft = frames;
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
// printf("\n------------\ncalled run() for %d frames\n",frames);
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
// flatten the left channel to use as temporary storage, since
// the synth engine only generates a mono channel
bzero(outputs[0], sizeof(float) * frames);
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
// get any of the last block's worth of MIDI out of the way
lastEvent = 0;
doMidi(midiEvents, midiEventCount, s.blockLeft);
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
while (framePos < frames) {
if (s.blockLeft == 0) {
2024-09-05 22:18:41 +00:00
s.blockLeft = sampleRate / 238;
doMidi(midiEvents, midiEventCount, framePos + s.blockLeft);
// printf("compute params and reset block size\n");
2024-09-09 10:34:13 +00:00
s.lfoDelay();
s.runLFO();
2024-09-05 22:18:41 +00:00
for (uint32_t i = 0; i < NUM_VOICES; i++) {
2024-09-09 12:34:02 +00:00
s.voice[i].calcPitch(s);
switch(s.patchRam.switch1 & 0x03) {
case 1: s.voice[i].omega /= 4; break;
case 2: s.voice[i].omega /= 2;
default: break;
}
//printf("voice %d note = %02x ff71 = %04x\n",i, s.voice[i].note, s.voice[i].ff71 );
2024-09-08 00:08:00 +00:00
s.voice[i].gate(s);
2024-09-05 22:18:41 +00:00
}
}
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
sizeThisTime = (s.framesLeft < s.blockLeft) ? s.framesLeft : s.blockLeft;
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
// printf("sL = %d bL = %d, calculating %d frames at %d\n", s.framesLeft, s.blockLeft, sizeThisTime, framePos);
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
// run each synth voice
2024-09-03 12:50:46 +00:00
2024-09-05 22:18:41 +00:00
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;
2024-09-03 12:50:46 +00:00
}
2024-09-05 22:18:41 +00:00
// printf("and now run the rest of the process for %d frames\n\n", frames);
2024-09-03 12:50:46 +00:00
// copy left to right
2024-09-03 13:15:02 +00:00
memmove(outputs[1], outputs[0], sizeof(float) * frames);
2024-09-05 22:18:41 +00:00
// outputs[1][1]=1;
2024-09-02 21:52:53 +00:00
}
2024-09-02 21:11:57 +00:00
// create the plugin
Plugin *createPlugin() { return new Chassis(); }
END_NAMESPACE_DISTRHO