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>
This commit is contained in:
Reactorcoremeltdown
2026-11-14 21:07:05 +01:00
co-authored by Claude Opus 5.5
parent dcf85ba915
commit 7d8311a77f
6 changed files with 95 additions and 10 deletions
+64 -3
View File
@@ -8,7 +8,11 @@
// 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).
// 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>
@@ -77,14 +81,57 @@ static void renderAU(AVAudioUnit *unit, const std::vector<float> &in, std::vecto
[engine stop];
}
static std::vector<float> renderCore(const char *type, const Params &p, const std::vector<float> &in) {
auto fx = Effect::create(type, 48000);
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]));
@@ -152,6 +199,20 @@ int main(int argc, char **argv) {
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);
}