breaks up into 4.3ms blocks correctly

This commit is contained in:
Gordon JC Pearce 2024-10-13 21:07:01 +01:00
parent c758a0a493
commit 0fe57ff017
5 changed files with 63 additions and 26 deletions

View File

@ -48,7 +48,7 @@ void Assigner::handleMidi(const MidiEvent *ev) {
noteOn(ev->data[1]);
break;
default:
printf("handle event, status %02x value %02x\n", ev->data[0], ev->data[1]);
d_debug("unhandled MIDI event, status %02x value %02x\n", ev->data[0], ev->data[1]);
break;
}
}

View File

@ -18,23 +18,21 @@
#include "ic29.hpp"
Synth s;
Synth::Synth() {
printf("initialising synth\n");
d_debug("initialising synth\n");
envAtk = 0x007f;
envDcy = envRls = 0xfe90;
envStn = 0x1fff;
}
void Synth::run() {
//printf("called synth::run()\n");
//printf("%d\n", voices[0].env.phase);
// handle a "loop" worth of envelopes, pitch calculations, etc
// callled once every 4.3ms block of samples
for (uint8_t i = 0; i < NUM_VOICES; i++) {
s.voices[i].env.run();
}
}
@ -42,6 +40,9 @@ void Synth::voiceOn(uint8_t voice, uint8_t note) {
// enable synth voice, start it all running
voice &= 0x7f;
s.voices[voice].env.on();
// FIXME determine if we need to reset the counter
s.voices[voice].note = note;
}
void Synth::voiceOff(uint8_t voice) {

View File

@ -20,8 +20,6 @@
#include "peacock.hpp"
class Envelope {
public:
Envelope();

View File

@ -17,6 +17,7 @@
*/
#include "peacock.hpp"
#include "ic1.hpp"
#include "ic29.hpp"
@ -28,20 +29,56 @@ Peacock::Peacock() : Plugin(paramCount, 0, 0), sampleRate(getSampleRate()) {
s.run();
}
void Peacock::runMidi(const MidiEvent *ev, uint32_t count, uint32_t timeLimit) {
// handle MIDI events, starting at lastEvent and continuing until timeLimit
if (count == 0) return; // no events to do, at all
for (uint32_t i = lastEvent; i < count; i++) {
if (ev[i].frame > timeLimit) break; // exceeded the time limit
ic1.handleMidi((const MidiEvent *)&ev[i]);
}
}
void Peacock::run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) {
// no content yet
// dispose of parameters
(void)outputs;
(void)frames;
//printf("got %d MIDI events\n", midiEventCount);
for(uint32_t i=0; i<midiEventCount; i++) {
ic1.handleMidi((const MidiEvent *) &midiEvents[i]);
// 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);
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);
s.run();
}
sizeThisTime = (framesLeft < blockLeft) ? framesLeft : blockLeft;
// run every synth voice into the buffer here FIXME
framePos += sizeThisTime; // move along the frame
framesLeft -= sizeThisTime;
blockLeft -= sizeThisTime;
}
}
Plugin *createPlugin() { return new Peacock(); }

View File

@ -25,7 +25,6 @@
START_NAMESPACE_DISTRHO
class Peacock : public Plugin {
public:
enum Parameters {
@ -35,7 +34,6 @@ class Peacock : public Plugin {
Peacock();
protected:
float sampleRate;
const char *getLabel() const override { return "peacock-8"; }
@ -59,11 +57,14 @@ class Peacock : public Plugin {
// void deactivate() override;
void run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) override;
private:
uint32_t framesLeft = 0;
uint32_t blockLeft = 0;
uint32_t lastEvent = 0;
void runMidi(const MidiEvent *ev, uint32_t count, uint32_t timeLimit);
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Peacock);
};
END_NAMESPACE_DISTRHO