peacock/plugin/peacock.cpp
Gordon JC Pearce 202ddac184 noise gen
2024-10-19 00:19:35 +01:00

103 lines
3.7 KiB
C++

/*
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"
#include "ic1.hpp"
#include "ic29.hpp"
START_NAMESPACE_DISTRHO
Peacock::Peacock() : Plugin(paramCount, 0, 0), sampleRate(getSampleRate()) {
printf("peacock constructor\n");
ic29.buildTables(getSampleRate());
ic29.bufferSize = getBufferSize();
}
void Peacock::runMidi(const MidiEvent *ev, uint32_t count, uint32_t timeLimit) {
// handle MIDI events, starting at lastEvent and continuing until timeLimit
uint32_t i;
if (count == 0) return; // no events to do, at all
for (i = lastEvent; i < count; i++) {
if (ev[i].frame > timeLimit) break; // exceeded the time limit
ic1.handleMidi((const MidiEvent *)&ev[i]);
}
lastEvent = i;
}
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";
}
void Peacock::run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) {
// 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
uint32_t framePos = 0; // sample position within current frame
// 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);
// generate a buffer's worth of samples
memset(outputs[0], 0, sizeof(float) * frames);
while (framePos < frames) {
if (blockLeft == 0) {
blockLeft = (int)(getSampleRate() * 0.0043); // how many samples per 4.3ms block?
// handle all MIDI events for this block, up to the end of the block and frame
runMidi(midiEvents, midiEventCount, framePos + blockLeft);
ic29.run();
}
sizeThisTime = (framesLeft < blockLeft) ? framesLeft : blockLeft;
// run every synth voice into the buffer here FIXME
for (uint8_t i = 0; i < NUM_VOICES; i++) {
ic29.voices[i].run(outputs[0], framePos, sizeThisTime);
}
framePos += sizeThisTime; // move along the frame
framesLeft -= sizeThisTime;
blockLeft -= sizeThisTime;
}
// output processing goes here
memcpy(outputs[1], outputs[0], sizeof(float) * frames);
}
Plugin *createPlugin() { return new Peacock(); }
END_NAMESPACE_DISTRHO