chassis/plugin/chassis.cpp

189 lines
5.5 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
2024-09-04 20:08:27 +00:00
Chassis::Chassis() : Plugin(kParameterCount, 0, 0), sampleRate(getSampleRate()) { // one parameter, no programs, no states
2024-09-02 21:11:57 +00:00
}
// Initialisation functions
2024-09-03 22:30:08 +00:00
void Chassis::initParameter(uint32_t index, Parameter &parameter) {
switch (index) {
2024-09-04 20:08:27 +00:00
case k_saw:
2024-09-03 22:30:08 +00:00
parameter.hints = kParameterIsAutomatable | kParameterIsBoolean;
parameter.name = "Saw";
parameter.symbol = "saw";
parameter.ranges.min = 0.0f;
parameter.ranges.max = 127.0f;
2024-09-03 22:41:01 +00:00
parameter.ranges.def = 0.0f;
2024-09-03 23:40:40 +00:00
break;
2024-09-04 20:08:27 +00:00
case k_sqr:
2024-09-03 23:40:40 +00:00
parameter.hints = kParameterIsAutomatable | kParameterIsBoolean;
parameter.name = "Square";
parameter.symbol = "sqr";
parameter.ranges.min = 0.0f;
parameter.ranges.max = 127.0f;
parameter.ranges.def = 0.0f;
break;
2024-09-04 20:08:27 +00:00
case k_sub:
parameter.hints = kParameterIsAutomatable | kParameterIsBoolean;
parameter.name = "Sub Osc";
parameter.symbol = "sub";
parameter.ranges.min = 0.0f;
parameter.ranges.max = 127.0f;
parameter.ranges.def = 0.0f;
break;
2024-09-03 22:30:08 +00:00
}
}
2024-09-02 21:11:57 +00:00
void Chassis::setParameterValue(uint32_t index, float value) {
2024-09-03 22:30:08 +00:00
switch (index) {
case k_saw:
s.p.saw = value / 127.0f;
2024-09-03 23:40:40 +00:00
break;
2024-09-04 20:08:27 +00:00
case k_sqr:
2024-09-03 23:40:40 +00:00
s.p.sqr = value / 127.0f;
break;
2024-09-04 20:08:27 +00:00
case k_sub:
s.p.sub = value / 127.0f;
break;
2024-09-03 22:30:08 +00:00
}
2024-09-02 21:11:57 +00:00
}
2024-09-03 22:30:08 +00:00
/*
2024-09-02 21:11:57 +00:00
float Chassis::getParameterValue(uint32_t index) const {
return 0;
}
*/
2024-09-02 21:11:57 +00:00
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() {
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");
}
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;
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-03 13:15:02 +00:00
// 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-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) {
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-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-02 21:52:53 +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
if (s.framesLeft == 0) s.framesLeft = frames;
2024-09-04 20:08:27 +00:00
while ( s.framesLeft != 0) {
uint32_t sizeThisTime = (frames <= s.blockLeft)?frames:s.blockLeft;
2024-09-04 20:08:27 +00:00
printf("sL = %d bL = %d, calculating %d frames\n", s.framesLeft, s.blockLeft, sizeThisTime);
2024-09-04 20:08:27 +00:00
s.framesLeft -= sizeThisTime;
s.blockLeft -= sizeThisTime;
if (s.blockLeft == 0) {
s.blockLeft = 48000/238;
printf("compute params and reset block size\n");
}
if (s.framesLeft <= s.blockLeft) {
s.blockLeft -= s.framesLeft;
printf("ended with %d frames, %d left in block\n", s.framesLeft, s.blockLeft);
s.framesLeft = 0;
}
};
2024-09-04 20:08:27 +00:00
//printf("done with all that, %d\n", s.samplesLeft);
2024-09-04 20:08:27 +00:00
printf("and now run the rest of the process for %d frames\n\n", frames);
2024-09-04 20:08:27 +00:00
2024-09-02 21:52:53 +00:00
for (uint32_t i = 0; i < midiEventCount; i++) {
if (MidiEvents[i].data[0] == 0x90) {
noteOn(MidiEvents[i].data[1]);
}
2024-09-03 11:42:16 +00:00
if (MidiEvents[i].data[0] == 0x80) {
noteOff(MidiEvents[i].data[1]);
}
2024-09-02 21:52:53 +00:00
}
2024-09-03 12:50:46 +00:00
// run each synth voice
2024-09-03 13:15:02 +00:00
bzero(outputs[0], sizeof(float) * frames);
2024-09-03 12:50:46 +00:00
for (uint8_t i = 0; i < NUM_VOICES; i++) {
2024-09-03 22:41:01 +00:00
s.voice[i].run(s, outputs[0], frames);
2024-09-03 12:50:46 +00:00
}
2024-09-04 20:08:27 +00:00
s.lastpw = s.lfo;
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-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