Files
sizzletracker-android/ios/app/tests/au_test.mm
T
ReactorcoremeltdownandClaude Opus 5.5 7d8311a77f iOS 0.1.1: effects read the input where the host puts it (were silent out of process)
Out of process the pulled input arrives by pointer, not in the buffers the
plugin offered. au_test now drives the render block with such an upstream;
it fails on 0.1.0 and passes now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 21:07:05 +01:00

221 lines
11 KiB
Plaintext

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Mac-side check of the AUv3 wrapper (SizzleEffectAU), run by `make -C ios au-test`.
// AUAudioUnit exists on macOS too, so every effect in components.txt is registered
// in-process and driven through AVAudioEngine (offline), like a host would:
// 1. audio: the reference input through the AU must equal the C++ core run
// directly (both channels), so the wrapper adds nothing and drops nothing;
// 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);
// 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
// 0.1.0 passed here and was silent on the iPad.
#import <AVFoundation/AVFoundation.h>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <vector>
#import "Components.gen.h"
#import "SizzleEffectAU.h"
#include "sizzle/DeviceSpecs.gen.hpp"
#include "sizzle/Effect.hpp"
using namespace sizzle;
static int failures = 0;
static void check(bool ok, const char *what, const char *name) {
std::printf("%s %-18s %s\n", ok ? "PASS" : "FAIL", name, what);
if (!ok) ++failures;
}
static std::vector<float> readF32(const char *path) {
std::ifstream f(path, std::ios::binary);
std::vector<char> b((std::istreambuf_iterator<char>(f)), {});
std::vector<float> v(b.size() / 4);
memcpy(v.data(), b.data(), v.size() * 4);
return v;
}
static AVAudioUnit *instantiate(AudioComponentDescription d) {
__block AVAudioUnit *unit = nil;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[AVAudioUnit instantiateWithComponentDescription:d options:0 completionHandler:^(AVAudioUnit *u, NSError *e) {
unit = u;
if (e) std::printf("instantiate error: %s\n", e.localizedDescription.UTF8String);
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return unit;
}
/// Render [in] (mono, both channels) through [unit]; returns the left and right outputs.
static void renderAU(AVAudioUnit *unit, const std::vector<float> &in, std::vector<float> &L, std::vector<float> &R) {
AVAudioEngine *engine = [AVAudioEngine new];
AVAudioFormat *fmt = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:48000 channels:2];
NSError *err = nil;
[engine enableManualRenderingMode:AVAudioEngineManualRenderingModeOffline format:fmt maximumFrameCount:1024 error:&err];
AVAudioPlayerNode *player = [AVAudioPlayerNode new];
[engine attachNode:player];
[engine attachNode:unit];
[engine connect:player to:unit format:fmt];
[engine connect:unit to:engine.mainMixerNode format:fmt];
[engine startAndReturnError:&err];
AVAudioPCMBuffer *src = [[AVAudioPCMBuffer alloc] initWithPCMFormat:fmt frameCapacity:(AVAudioFrameCount)in.size()];
src.frameLength = (AVAudioFrameCount)in.size();
memcpy(src.floatChannelData[0], in.data(), in.size() * 4);
memcpy(src.floatChannelData[1], in.data(), in.size() * 4);
[player scheduleBuffer:src completionHandler:nil];
[player play];
AVAudioPCMBuffer *out = [[AVAudioPCMBuffer alloc] initWithPCMFormat:engine.manualRenderingFormat frameCapacity:1024];
L.clear(); R.clear();
while (L.size() < in.size()) {
[engine renderOffline:1024 toBuffer:out error:&err];
L.insert(L.end(), out.floatChannelData[0], out.floatChannelData[0] + out.frameLength);
R.insert(R.end(), out.floatChannelData[1], out.floatChannelData[1] + out.frameLength);
}
L.resize(in.size()); R.resize(in.size());
[engine stop];
}
static std::vector<float> renderCore(const char *type, const Params &p, const std::vector<float> &in, int sr = 48000) {
auto fx = Effect::create(type, sr);
fx->update(p, 120.f);
std::vector<float> y(in.size());
for (size_t i = 0; i < in.size(); ++i) { float v = fx->process(in[i]); y[i] = std::isfinite(v) ? v : 0.f; }
return y;
}
/// Render [in] through the AU's render block at 44.1 kHz in 512-frame blocks. The
/// upstream hands over its own buffers (mData replaced, ours untouched); with
/// [inPlace] the output buffers are null too, as hosts may pass them.
static void renderZeroCopy(AUAudioUnit *au, const std::vector<float> &in, bool inPlace, std::vector<float> &L, std::vector<float> &R) {
au.maximumFramesToRender = 4096;
NSError *err = nil;
if (![au allocateRenderResourcesAndReturnError:&err]) { std::printf("allocate: %s\n", err.localizedDescription.UTF8String); return; }
AURenderBlock render = au.renderBlock;
const AVAudioFrameCount n = 512;
std::vector<float> upL(n), upR(n), outL(n), outR(n);
L.clear(); R.clear();
for (size_t pos = 0; pos + n <= in.size(); pos += n) {
memcpy(upL.data(), &in[pos], n * 4);
memcpy(upR.data(), &in[pos], n * 4);
float *upL0 = upL.data(), *upR0 = upR.data();
AURenderPullInputBlock pull = ^AUAudioUnitStatus(AudioUnitRenderActionFlags *f, const AudioTimeStamp *t,
AUAudioFrameCount frames, NSInteger bus, AudioBufferList *data) {
for (UInt32 c = 0; c < data->mNumberBuffers; ++c) {
data->mBuffers[c].mData = c ? upR0 : upL0;
data->mBuffers[c].mDataByteSize = frames * 4;
}
return noErr;
};
struct { AudioBufferList list; AudioBuffer extra; } out;
out.list.mNumberBuffers = 2;
for (int c = 0; c < 2; ++c) {
out.list.mBuffers[c].mNumberChannels = 1;
out.list.mBuffers[c].mDataByteSize = n * 4;
out.list.mBuffers[c].mData = inPlace ? nullptr : (c ? outR.data() : outL.data());
}
AudioTimeStamp ts = {};
ts.mSampleTime = (Float64)pos;
ts.mFlags = kAudioTimeStampSampleTimeValid;
AudioUnitRenderActionFlags flags = 0;
AUAudioUnitStatus st = render(&flags, &ts, n, 0, &out.list, pull);
if (st != noErr) { std::printf("render status %d\n", (int)st); break; }
const float *l = (const float *)out.list.mBuffers[0].mData, *r = (const float *)out.list.mBuffers[1].mData;
L.insert(L.end(), l, l + n);
R.insert(R.end(), r, r + n);
}
[au deallocateRenderResources];
}
static double maxDiff(const std::vector<float> &a, const std::vector<float> &b) {
double m = 0;
for (size_t i = 0; i < a.size() && i < b.size(); ++i) m = std::max(m, std::fabs((double)a[i] - b[i]));
return m;
}
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]);
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];
AVAudioUnit *unit = instantiate(d);
if (!unit) { check(false, "instantiate", sc.name); continue; }
const DeviceSpec *spec = findDevice(sc.toolboxType);
// 1. defaults
Params p; p.fill(*spec);
std::vector<float> L, R, want = renderCore(sc.toolboxType, p, in);
renderAU(unit, in, L, R);
char msg[160];
double dl = maxDiff(L, want), dr = maxDiff(R, want);
snprintf(msg, sizeof msg, "defaults: AU == core (L %.2g, R %.2g)", dl, dr);
check(dl < 1e-6 && dr < 1e-6, msg, sc.name);
// 2. move every parameter to 70% (enums: last choice) through the AU tree
AUAudioUnit *au = unit.AUAudioUnit;
for (int i = 0; i < spec->paramCount; ++i) {
const ParamSpec &s = spec->params[i];
AUParameter *param = [au.parameterTree parameterWithAddress:(AUParameterAddress)i];
AUValue v = s.isEnum() ? (AUValue)(s.choiceCount() - 1) : s.min + 0.7f * (s.max - s.min);
if (s.isInt) v = std::round(v);
param.value = v;
if (s.isEnum()) p.set(s.key, s.choices[s.choiceCount() - 1]); else p.set(s.key, v);
}
want = renderCore(sc.toolboxType, p, in);
renderAU(unit, in, L, R);
dl = maxDiff(L, want);
snprintf(msg, sizeof msg, "parameters at 70%%: AU == core (%.2g)", dl);
check(dl < 1e-6, msg, sc.name);
// 3a. factory presets
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]);
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);
check(dl < 1e-6, msg, sc.name);
}
// 3b. fullState round trip into a fresh instance
NSDictionary *state = au.fullState;
AVAudioUnit *other = instantiate(d);
other.AUAudioUnit.fullState = state;
std::vector<float> L2, R2;
renderAU(unit, in, L, R);
renderAU(other, in, L2, R2);
dl = maxDiff(L, L2);
snprintf(msg, sizeof msg, "fullState round trip renders the same (%.2g)", dl);
check(dl < 1e-6, msg, sc.name);
// 4. zero-copy input, with and without host output buffers
for (int inPlace = 0; inPlace < 2; ++inPlace) {
AVAudioUnit *fresh = instantiate(d);
Params dp; dp.fill(*spec);
std::vector<float> zin(in.begin(), in.begin() + 44032); // 86 blocks of 512
std::vector<float> zw = renderCore(sc.toolboxType, dp, zin, 44100), zL, zR;
renderZeroCopy(fresh.AUAudioUnit, zin, inPlace, zL, zR);
double zl = zL.size() == zin.size() ? maxDiff(zL, zw) : 1e9;
double zr = zR.size() == zin.size() ? maxDiff(zR, zw) : 1e9;
snprintf(msg, sizeof msg, "zero-copy input%s: AU == core (L %.2g, R %.2g)",
inPlace ? ", in-place output" : "", zl, zr);
check(zl < 1e-6 && zr < 1e-6, msg, sc.name);
}
}
std::printf("\n%s (%d failures)\n", failures ? "FAILED" : "all passed", failures);
}
return failures ? 1 : 0;
}