peacock/plugin/peacock.cpp

104 lines
3.9 KiB
C++
Raw Normal View History

2024-10-09 21:10:51 +00:00
/*
Peacock-8 VA polysynth
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 "peacock.hpp"
2024-10-13 20:07:01 +00:00
2024-10-09 21:10:51 +00:00
#include "ic1.hpp"
2024-10-11 23:15:00 +00:00
#include "ic29.hpp"
2024-10-09 21:10:51 +00:00
START_NAMESPACE_DISTRHO
Peacock::Peacock() : Plugin(paramCount, 0, 0), sampleRate(getSampleRate()) {
2024-10-13 22:27:54 +00:00
printf("peacock constructor\n");
ic29.buildTables(getSampleRate());
2024-10-18 23:19:35 +00:00
ic29.bufferSize = getBufferSize();
2024-10-09 21:10:51 +00:00
}
2024-10-13 20:07:01 +00:00
void Peacock::runMidi(const MidiEvent *ev, uint32_t count, uint32_t timeLimit) {
// handle MIDI events, starting at lastEvent and continuing until timeLimit
2024-10-13 22:27:54 +00:00
uint32_t i;
2024-10-13 20:07:01 +00:00
if (count == 0) return; // no events to do, at all
2024-10-13 22:27:54 +00:00
for (i = lastEvent; i < count; i++) {
2024-10-13 20:07:01 +00:00
if (ev[i].frame > timeLimit) break; // exceeded the time limit
ic1.handleMidi((const MidiEvent *)&ev[i]);
}
2024-10-13 22:27:54 +00:00
lastEvent = i;
2024-10-13 20:07:01 +00:00
}
2024-10-17 22:24:53 +00:00
void Peacock::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";
}
2024-10-09 21:10:51 +00:00
void Peacock::run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) {
2024-10-13 20:07:01 +00:00
// calculate an entire jack period's worth of samples
// harder than it sounds because for short jack periods there may be many
// such calls between 4.3ms control updates, or there may be many updates
// within a single update
2024-10-09 21:10:51 +00:00
2024-10-13 20:07:01 +00:00
uint32_t framePos = 0; // sample position within current frame
2024-10-09 21:10:51 +00:00
2024-10-13 20:07:01 +00:00
// blockLeft is how many samples are left within this 4.3ms block
// framesleft is how many samples are left within this jack period
// the names are awful, I should FIXME this for legibility
uint32_t sizeThisTime = (framesLeft < blockLeft) ? framesLeft : blockLeft;
framesLeft = frames; // number of frames left to calculate
// now we'll handle MIDI events up to however long is left in this block
lastEvent = 0;
runMidi(midiEvents, midiEventCount, blockLeft);
2024-10-13 22:27:54 +00:00
// generate a buffer's worth of samples
2024-10-19 18:49:24 +00:00
memset(outputs[1], 0, sizeof(float) * frames);
2024-10-13 22:27:54 +00:00
2024-10-13 20:07:01 +00:00
while (framePos < frames) {
if (blockLeft == 0) {
blockLeft = (int)(getSampleRate() * 0.0043); // how many samples per 4.3ms block?
2024-10-09 21:10:51 +00:00
2024-10-13 20:07:01 +00:00
// handle all MIDI events for this block, up to the end of the block and frame
runMidi(midiEvents, midiEventCount, framePos + blockLeft);
2024-10-13 22:27:54 +00:00
ic29.run();
2024-10-13 20:07:01 +00:00
}
sizeThisTime = (framesLeft < blockLeft) ? framesLeft : blockLeft;
// run every synth voice into the buffer here FIXME
2024-10-13 22:27:54 +00:00
for (uint8_t i = 0; i < NUM_VOICES; i++) {
2024-10-18 23:19:35 +00:00
ic29.voices[i].run(outputs[0], framePos, sizeThisTime);
2024-10-19 18:49:24 +00:00
ic29.voices[i].filter(outputs[0], framePos, sizeThisTime);
for (uint8_t j=0; j<sizeThisTime; j++) {
outputs[1][framePos+j] += outputs[0][framePos+j] * ic29.voices[i].env.level/16384.0;
}
2024-10-13 22:27:54 +00:00
}
2024-10-13 20:07:01 +00:00
framePos += sizeThisTime; // move along the frame
framesLeft -= sizeThisTime;
blockLeft -= sizeThisTime;
}
2024-10-13 22:27:54 +00:00
// output processing goes here
2024-10-19 18:49:24 +00:00
memcpy(outputs[0], outputs[1], sizeof(float) * frames);
2024-10-09 21:10:51 +00:00
}
Plugin *createPlugin() { return new Peacock(); }
END_NAMESPACE_DISTRHO