iOS 0.2.0: instruments as AUv3, in-app host with keyboard, MIDI in and benchmark

Shared SizzleDeviceAU base for effects and instruments; sizzle::Instrument voice
pools; Android's factory presets generated into the specs header; the app hosts
every device in-process (no dependence on the system plugin registry).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-15 22:17:47 +01:00
co-authored by Claude Opus 5.5
parent a5b2f4c873
commit 7f231c8f79
35 changed files with 1989 additions and 368 deletions
+142
View File
@@ -0,0 +1,142 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#include "sizzle/Instrument.hpp"
#include <cstring>
#include <string>
#include "echofield/GrainCloud.hpp"
#include "frasil/FrasilVoice.hpp"
#include "sampler/SampleStore.hpp"
#include "sampler/SampleVoice.hpp"
#include "synth/SynthPatch.hpp"
#include "synth/SynthVoice.hpp"
#include "echofield/GrainSources.hpp"
#include "wavetable/WaveTables.hpp"
#include "wavetable/WaveVoice.hpp"
namespace sizzle {
namespace {
constexpr int N = Instrument::POLYPHONY;
/** Synth, Morph Pad, Wavetable: SynthVoice pool with the engine's patch. */
class SynthInstrument final : public Instrument {
public:
enum Kind { SYNTH, PAD, WAVE };
private:
Kind kind;
int sr;
frasil::FrasilClock clock;
frasil::FrasilSeeds seeds;
std::unique_ptr<synth::SynthVoice> voices[N];
synth::SynthPatch patch = synth::SynthPatch::defaults();
int alloc = 0;
public:
SynthInstrument(Kind k, int sampleRate) : kind(k), sr(sampleRate) {
for (auto &v : voices) v = std::make_unique<synth::SynthVoice>(sr, clock, seeds.next());
}
void update(const Params &p, float tempo) override {
switch (kind) {
case PAD: patch = synth::SynthPatch::forPad(frasil::FrasilPatch::from(&p)); break;
case WAVE: patch = synth::SynthPatch::forWave(wavetable::WavePatch::from(&p, tempo)); break;
default: patch = synth::SynthPatch::from(&p, tempo); break;
}
for (auto &v : voices) if (v->isActive()) v->setPatch(patch);
}
void noteOn(int midi, int vel) override {
for (auto &v : voices) if (!v->isActive()) { v->noteOn(midi, vel, patch); return; }
auto &v = voices[alloc];
alloc = (alloc + 1) % N;
v->kill();
v->noteOn(midi, vel, patch);
}
void noteOff(int midi) override {
for (auto &v : voices) if (v->isGated() && v->pitch() == midi) v->noteOff();
}
void allNotesOff() override { for (auto &v : voices) v->noteOff(); }
void kill() override { for (auto &v : voices) v->kill(); }
void blockStart(int frames) override { clock.advance(frames, sr); }
float render() override {
float s = 0.f;
for (auto &v : voices) if (v->isActive()) s += v->render();
return s;
}
};
/** Sampler pads and the SF2/XI loader: SampleVoice pool. */
class SampleInstrument final : public Instrument {
std::string type;
sampler::SampleStore *store;
std::unique_ptr<sampler::SampleVoice> voices[N];
Params params;
int alloc = 0;
public:
SampleInstrument(const char *t, int sr, sampler::SampleStore *s) : type(t), store(s) {
for (auto &v : voices) v = std::make_unique<sampler::SampleVoice>(sr);
}
void update(const Params &p, float) override { params = p; }
void noteOn(int midi, int vel) override {
if (!store) return;
sampler::VoiceSample vs;
if (!sampler::voiceSample(type.c_str(), params, *store, midi, vs)) return;
for (auto &v : voices) if (!v->isActive()) { v->noteOn(vs, midi, vel); return; }
auto &v = voices[alloc];
alloc = (alloc + 1) % N;
v->noteOn(vs, midi, vel); // declicks the hand-off itself
}
void noteOff(int midi) override {
for (auto &v : voices) if (v->isGated() && v->pitch == midi) v->noteOff();
}
void allNotesOff() override { for (auto &v : voices) v->noteOff(); }
void kill() override { for (auto &v : voices) v->kill(); }
void blockStart(int) override { if (store) store->quiescent(); }
float render() override {
float s = 0.f;
for (auto &v : voices) if (v->isActive()) s += v->render();
return s;
}
};
/** Grain Cloud: one polyphonic cloud. */
class GrainInstrument final : public Instrument {
echofield::GrainCloud cloud;
sampler::SampleStore *store;
static const Sample *lookup(void *ctx, const char *id) {
auto *s = static_cast<sampler::SampleStore *>(ctx);
return s ? s->get(id) : nullptr;
}
public:
GrainInstrument(int sr, sampler::SampleStore *s) : cloud(sr, echofield::GrainSeeds{}.next()), store(s) {
cloud.setSampleLookup(&GrainInstrument::lookup, store);
}
void update(const Params &p, float tempo) override { cloud.setPatch(echofield::GrainPatch::from(&p, tempo)); }
void noteOn(int midi, int vel) override { cloud.noteOn(midi, vel, echofield::GrainCloud::OWNER_LIVE); }
void noteOff(int midi) override { cloud.noteOff(midi); }
void allNotesOff() override { cloud.releaseAll(); }
void kill() override { cloud.kill(); }
void blockStart(int) override { if (store) store->quiescent(); }
float render() override { return cloud.render(); }
};
} // namespace
std::unique_ptr<Instrument> Instrument::create(const char *type, int sr, sampler::SampleStore *store) {
auto is = [&](const char *t) { return std::strcmp(type, t) == 0; };
if (is("NES_SYNTH")) return std::make_unique<SynthInstrument>(SynthInstrument::SYNTH, sr);
if (is("MORPH_PAD")) return std::make_unique<SynthInstrument>(SynthInstrument::PAD, sr);
// Wavetables and grain sources are generated once, off the audio thread; until
// they're ready those oscillators / clouds are silent (as on Android, where the
// app starts this at launch). prewarm() returns at once.
if (is("WAVETABLE")) { wavetable::WaveTables::prewarm(); return std::make_unique<SynthInstrument>(SynthInstrument::WAVE, sr); }
if (is("GRAIN_CLOUD")) { echofield::GrainSources::prewarm(); return std::make_unique<GrainInstrument>(sr, store); }
if (is("SAMPLER") || is("SOUNDFONT")) return std::make_unique<SampleInstrument>(type, sr, store);
return nullptr;
}
} // namespace sizzle