chassis/plugin/chassis.cpp
Gordon JC Pearce 284ce7193c notes
2024-09-02 22:52:53 +01:00

194 lines
5.6 KiB
C++

/*
Chassis reverb plugin
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"
SVF::SVF(float cutoff = 0, float q = 0, float samplerate = 0) {
z1 = z2 = 0;
setFreq(cutoff, q, samplerate);
}
void SVF::setFreq(float cutoff, float q, float samplerate) {
z1 = z2 = 0;
printf("called with %f %f %f\n", cutoff, q, samplerate);
w = 2 * tan(3.14159 * (cutoff / samplerate));
a = w / q;
b = w * w;
// corrected SVF params, per Fons Adriaensen
c1 = (a + b) / (1 + a / 2 + b / 4);
c2 = b / (a + b);
d0 = c1 * c2 / 4;
printf("c1 %f c2 %f d0 %f\n", c1, c2, d0);
}
inline float SVF::lpStep(float in) {
x = in - z1 - z2;
z2 += c2 * z1;
z1 += c1 * x;
return d0 * x + z2;
}
START_NAMESPACE_DISTRHO
Chassis::Chassis() : Plugin(kParameterCount, 0, 0) { // one parameter, no programs, no states
// lowpass = new float[getBufferSize()];
// ram = new int16_t[16384];
// bzero(lowpass, sizeof(float) * getBufferSize());
// bzero(ram, sizeof(int16_t) * 16384);
// f1.setFreq(5916, .6572, getSampleRate());
// f2.setFreq(9458, 2.536, getSampleRate());
}
// Initialisation functions
void Chassis::initParameter(uint32_t index, Parameter &parameter) {
if (index == p_saw) {
parameter.hints = kParameterIsAutomatable | kParameterIsInteger;
parameter.name = "Saw";
parameter.symbol = "saw";
parameter.ranges.max = 1.0f;
}
if (index == p_sqr) {
parameter.hints = kParameterIsAutomatable | kParameterIsInteger;
parameter.name = "Square";
parameter.symbol = "sqr";
parameter.ranges.max = 4.0f;
}
if (index == p_sublev) {
parameter.hints = kParameterIsAutomatable;
parameter.name = "Sub";
parameter.symbol = "sub";
parameter.ranges.def = 20.0f;
parameter.ranges.min = 1.0f;
parameter.ranges.max = 64.0f;
}
if (index == paramProgram) {
parameter.hints = kParameterIsAutomatable;
parameter.name = "prog";
parameter.symbol = "prog";
parameter.ranges.def = 1.0f;
parameter.ranges.min = 0.0f;
parameter.ranges.max = 1.0f;
}
}
void Chassis::setParameterValue(uint32_t index, float value) {
}
float Chassis::getParameterValue(uint32_t index) const {
return 0;
}
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 = "init";
}
void Chassis::loadProgram(uint32_t index) {
prog_offset = (index & 0x3f) << 7;
program = index + 1;
}
*/
// Processing functions
void Chassis::activate() {
// calculate filter coefficients
printf("called activate()\n");
for (uint32_t i = 0; i< NUM_VOICES; i++ ) {
assign[i] = 0x80 | i;
}
}
void Chassis::deactivate() {
// zero out the outputs, maybe
printf("called deactivate()\n");
printf("%02x", assign[1]);
}
void Chassis::noteOn(uint8_t note) {
for (uint32_t i = 0; i < NUM_VOICES; i++) {
printf("note on, finding free voice in %02x for %d\n", assign[i], note);
if (assign[i] & 0x80) { // top bit is voice free flag
assign[i] &= 0x7f; // enable
printf("voice %d would be set to note %d\n", assign[i], note);
break;
}
if (i == NUM_VOICES) { // didn't find a free voice
uint32_t v = assign[0];
memmove(assign, assign + 1, NUM_VOICES - 1); // shunt all voices down by one
assign[NUM_VOICES - 1] = v; // oldest voice goes at the top of the queue
printf("no free voices, reused %d for note %d\n", v, note);
}
}
printf("note table is now ");
for (uint32_t i=0; i < NUM_VOICES; i++ ) {
printf("%02x = ", assign[i]);
}
printf("----------------------------\n");
}
/*
void Chassis::noteOff(uint8_t note) {
uint32_t v;
printf("note off called for %3d\n", note);
for (uint32_t i = 0; i < NUM_VOICES; i++) {
v = assign[i];
printf("trying voice %02x\n", v);
if (v < 0x80) {
printf("note %02x is playing\n", v);
}
}
}
*/
void Chassis::run(const float **, float **outputs, uint32_t frames, const MidiEvent *MidiEvents, uint32_t midiEventCount) {
if (midiEventCount > 0) printf("\n--------------------\n");
for (uint32_t i = 0; i < midiEventCount; i++) {
printf("%4d %02x %02x\n", MidiEvents[i].frame, MidiEvents[i].data[0], MidiEvents[i].data[1]);
if (MidiEvents[i].data[0] == 0x90) {
noteOn(MidiEvents[i].data[1]);
}
}
}
// create the plugin
Plugin *createPlugin() { return new Chassis(); }
END_NAMESPACE_DISTRHO