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
+103 -6
View File
@@ -9,6 +9,9 @@
// 2. parameters: moving every parameter through the AU's tree must change the
// output to exactly what the core gives for those values;
// 3. presets (Mix Tightener) and saved state (fullState round trip);
// 5. instruments: MIDI notes at given sample offsets through the AU's render
// block must give exactly what the C++ Instrument gives for the same notes;
// the Sampler / SF2 get a test sample through fullState;
// 4. zero-copy input: calling the render block directly with an upstream that
// points the input buffers at its own memory instead of filling ours. Real
// hosts do that out of process; AVAudioEngine in-process copies, which is why
@@ -20,7 +23,12 @@
#include <fstream>
#include <vector>
#import "Components.gen.h"
#import "SizzleEffectAU.h"
#import "SizzleDeviceAU.h"
#import "SizzleInstrumentAU.h"
#include "sizzle/Instrument.hpp"
#include "sampler/SampleStore.hpp"
#include "echofield/GrainSources.hpp"
#include "wavetable/WaveTables.hpp"
#include "sizzle/DeviceSpecs.gen.hpp"
#include "sizzle/Effect.hpp"
@@ -138,15 +146,101 @@ static double maxDiff(const std::vector<float> &a, const std::vector<float> &b)
return m;
}
static sampler::SampleStore gStore;
/// Instruments: render the AU's render block directly with MIDI events at sample
/// offsets inside 512-frame blocks, against the C++ Instrument with the same notes.
static void checkInstrument(AudioComponentDescription d, const SizzleComponent &sc) {
const int sr = 44100, n = 512, blocks = 120; // ~1.4 s
struct Note { int frame, status, midi, vel; };
const Note notes[] = {{100, 0x90, 60, 100}, {9000, 0x90, 64, 90}, {9000, 0x90, 67, 70},
{26000, 0x80, 60, 0}, {30001, 0x80, 64, 0}, {30001, 0x80, 67, 0},
{40000, 0x90, 48, 127}, {52000, 0x80, 48, 0}};
const bool sampled = !strcmp(sc.toolboxType, "SAMPLER") || !strcmp(sc.toolboxType, "SOUNDFONT");
char msg[200];
AVAudioUnit *unit = instantiate(d);
if (!unit) { check(false, "instantiate", sc.name); return; }
SizzleInstrumentAU *au = (SizzleInstrumentAU *)unit.AUAudioUnit;
au.sampleStore = &gStore;
const DeviceSpec *spec = findDevice(sc.toolboxType);
Params p; p.fill(*spec);
if (sampled) { // point it at the test sample through saved state
NSMutableDictionary *values = [NSMutableDictionary dictionary];
for (int i = 0; i < p.size(); ++i) values[@(p.keyAt(i))] = @(p.valueAt(i));
if (!strcmp(sc.toolboxType, "SAMPLER")) { values[@"pad12"] = @"t"; values[@"pad24"] = @"t"; p.set("pad12", "t"); p.set("pad24", "t"); }
else { values[@"sfSample"] = @"t"; p.set("sfSample", "t"); }
au.fullState = @{@"sizzleValues": values};
}
au.maximumFramesToRender = 4096;
NSError *err = nil;
if (![au allocateRenderResourcesAndReturnError:&err]) { check(false, "allocate", sc.name); return; }
auto inst = Instrument::create(sc.toolboxType, sr, &gStore);
inst->update(p, 120.f);
std::vector<float> want, got;
AURenderBlock render = au.renderBlock;
AUScheduleMIDIEventBlock midi = au.scheduleMIDIEventBlock;
size_t next = 0;
for (int b = 0; b < blocks; ++b) {
const int base = b * n;
// reference: same notes on the same frames
inst->blockStart(n);
size_t k = next;
for (int i = 0; i < n; ++i) {
while (k < sizeof notes / sizeof notes[0] && notes[k].frame == base + i) {
if (notes[k].status == 0x90) inst->noteOn(notes[k].midi, notes[k].vel); else inst->noteOff(notes[k].midi);
++k;
}
want.push_back(inst->render());
}
// AU: schedule this block's notes at their sample times, then render
for (; next < k; ++next) {
uint8_t bytes[3] = {(uint8_t)notes[next].status, (uint8_t)notes[next].midi, (uint8_t)notes[next].vel};
midi((AUEventSampleTime)notes[next].frame, 0, 3, bytes);
}
float bufL[512], bufR[512];
struct { AudioBufferList list; AudioBuffer extra; } out;
out.list.mNumberBuffers = 2;
out.list.mBuffers[0] = {1, (UInt32)(n * 4), bufL};
out.list.mBuffers[1] = {1, (UInt32)(n * 4), bufR};
AudioTimeStamp ts = {};
ts.mSampleTime = base;
ts.mFlags = kAudioTimeStampSampleTimeValid;
AudioUnitRenderActionFlags flags = 0;
if (render(&flags, &ts, n, 0, &out.list, nil) != noErr) { check(false, "render", sc.name); return; }
for (int i = 0; i < n; ++i) {
if (bufL[i] != bufR[i]) { check(false, "left != right", sc.name); return; }
got.push_back(bufL[i]);
}
}
[au deallocateRenderResources];
double peak = 0;
for (float v : want) peak = std::max(peak, (double)std::fabs(v));
double dl = maxDiff(got, want);
snprintf(msg, sizeof msg, "MIDI at sample offsets: AU == Instrument (%.2g, peak %.3f)", dl, peak);
check(dl < 1e-6 && peak > 0.01, msg, sc.name);
}
int main(int argc, char **argv) {
@autoreleasepool {
if (argc < 2) { std::fprintf(stderr, "usage: au_test <golden/input.f32>\n"); return 2; }
std::vector<float> in = readF32(argv[1]);
wavetable::WaveTables::prewarmBlocking();
echofield::GrainSources::prewarmBlocking();
{ // the Sampler / SF2 test sample: 0.5 s decaying saw at 44.1 kHz, looped
Sample s;
s.sampleRate = 44100;
for (int i = 0; i < 22050; ++i) s.data.push_back(0.6f * (2.f * fmodf(i * 220.f / 44100.f, 1.f) - 1.f) * expf(-i / 20000.f));
s.loopStart = 4000; s.loopEnd = 16000;
gStore.put("t", std::move(s));
}
in.resize(48000); // 1 s is plenty
for (int c = 0; c < kSizzleComponentCount; ++c) {
const SizzleComponent &sc = kSizzleComponents[c];
AudioComponentDescription d = {kAudioUnitType_Effect, sc.subtype, 'Tsys', 0, 0};
[AUAudioUnit registerSubclass:SizzleEffectAU.class asComponentDescription:d name:@(sc.name) version:1];
AudioComponentDescription d = {sc.type, sc.subtype, 'Tsys', 0, 0};
[AUAudioUnit registerSubclass:[SizzleDeviceAU classForComponentType:sc.type] asComponentDescription:d name:@(sc.name) version:1];
if (sc.type == kAudioUnitType_MusicDevice) { checkInstrument(d, sc); continue; }
AVAudioUnit *unit = instantiate(d);
if (!unit) { check(false, "instantiate", sc.name); continue; }
const DeviceSpec *spec = findDevice(sc.toolboxType);
@@ -180,12 +274,15 @@ int main(int argc, char **argv) {
if (au.factoryPresets.count > 0) {
au.currentPreset = au.factoryPresets[1];
Params pp; pp.fill(*spec);
const char *const *kv = gen::mixTightenerPresets[1];
for (int i = 1; kv[i] && kv[i + 1]; i += 2) pp.set(kv[i], kv[i + 1]);
const FactoryPreset *fp = nullptr;
for (int i = 0, n = 0; i < gen::factoryPresetCount; ++i)
if (!strcmp(gen::factoryPresets[i].type, sc.toolboxType) && n++ == 1) fp = &gen::factoryPresets[i];
const char *const *kv = fp->values;
for (int i = 0; kv[i] && kv[i + 1]; i += 2) pp.set(kv[i], kv[i + 1]);
want = renderCore(sc.toolboxType, pp, in);
renderAU(unit, in, L, R);
dl = maxDiff(L, want);
snprintf(msg, sizeof msg, "factory preset \"%s\": AU == core (%.2g)", kv[0], dl);
snprintf(msg, sizeof msg, "factory preset \"%s\": AU == core (%.2g)", fp->name, dl);
check(dl < 1e-6, msg, sc.name);
}