Effect visualisation through the C++ effect, engine and AU; custom wavetables and grain sources load and persist with the song; the app's slot editor hosts the file picker for bespoke views. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
6.8 KiB
Plaintext
167 lines
6.8 KiB
Plaintext
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#import "SizzleEffectAU.h"
|
|
#import "SizzleDeviceAU+Private.h"
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#include <cstring>
|
|
#include <mutex>
|
|
#include <vector>
|
|
#include "sizzle/Effect.hpp"
|
|
|
|
using namespace sizzle;
|
|
|
|
namespace {
|
|
|
|
struct EffectKernel final : DeviceKernel {
|
|
std::unique_ptr<Effect> fx[2];
|
|
int channels = 0;
|
|
std::atomic<bool> bypass{false};
|
|
std::vector<float> inBuf[2];
|
|
/// Held by the editor's scope reads and by (de)allocation, which replace fx[]:
|
|
/// never by the render thread, so the audio path stays lock-free.
|
|
std::mutex vizLock;
|
|
|
|
void apply(const Params &p, float tempoBpm) override {
|
|
for (int c = 0; c < channels; ++c) fx[c]->update(p, tempoBpm);
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
@implementation SizzleEffectAU {
|
|
AUAudioUnitBusArray *_inputs;
|
|
AUAudioUnitBusArray *_outputs;
|
|
AUAudioUnitBus *_inBus;
|
|
AUAudioUnitBus *_outBus;
|
|
}
|
|
|
|
- (std::unique_ptr<DeviceKernel>)makeKernel { return std::make_unique<EffectKernel>(); }
|
|
- (EffectKernel *)fxKernel { return static_cast<EffectKernel *>(self.kernel); }
|
|
|
|
- (instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
|
options:(AudioComponentInstantiationOptions)options
|
|
error:(NSError **)outError {
|
|
self = [super initWithComponentDescription:desc options:options error:outError];
|
|
if (!self) return nil;
|
|
AVAudioFormat *fmt = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:2];
|
|
_inBus = [[AUAudioUnitBus alloc] initWithFormat:fmt error:outError];
|
|
_outBus = [[AUAudioUnitBus alloc] initWithFormat:fmt error:outError];
|
|
if (!_inBus || !_outBus) return nil;
|
|
_inBus.maximumChannelCount = 2;
|
|
_outBus.maximumChannelCount = 2;
|
|
_inputs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeInput busses:@[_inBus]];
|
|
_outputs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeOutput busses:@[_outBus]];
|
|
return self;
|
|
}
|
|
|
|
- (AUAudioUnitBusArray *)inputBusses { return _inputs; }
|
|
- (AUAudioUnitBusArray *)outputBusses { return _outputs; }
|
|
- (NSArray<NSNumber *> *)channelCapabilities { return @[@1, @1, @2, @2]; }
|
|
- (BOOL)canProcessInPlace { return YES; }
|
|
|
|
- (void)setShouldBypassEffect:(BOOL)bypass {
|
|
[super setShouldBypassEffect:bypass];
|
|
self.fxKernel->bypass.store(bypass);
|
|
}
|
|
|
|
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError {
|
|
AVAudioChannelCount chIn = _inBus.format.channelCount, chOut = _outBus.format.channelCount;
|
|
if (chIn != chOut || chOut < 1 || chOut > 2) {
|
|
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil];
|
|
return NO;
|
|
}
|
|
if (![super allocateRenderResourcesAndReturnError:outError]) return NO;
|
|
EffectKernel *k = self.fxKernel;
|
|
int sr = (int)std::lround(_outBus.format.sampleRate);
|
|
k->channels = (int)chOut;
|
|
{
|
|
std::lock_guard<std::mutex> g(k->vizLock);
|
|
for (int c = 0; c < 2; ++c) {
|
|
k->fx[c] = c < k->channels ? Effect::create(k->type.c_str(), sr) : nullptr;
|
|
k->inBuf[c].assign(self.maximumFramesToRender, 0.f);
|
|
}
|
|
}
|
|
if (!k->fx[0]) {
|
|
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil];
|
|
[super deallocateRenderResources];
|
|
return NO;
|
|
}
|
|
return YES;
|
|
}
|
|
|
|
- (void)deallocateRenderResources {
|
|
{
|
|
std::lock_guard<std::mutex> g(self.fxKernel->vizLock);
|
|
for (auto &f : self.fxKernel->fx) f.reset();
|
|
}
|
|
[super deallocateRenderResources];
|
|
}
|
|
|
|
- (NSInteger)copyEffectScope:(float *)out capacity:(NSInteger)capacity {
|
|
EffectKernel *k = self.fxKernel;
|
|
std::lock_guard<std::mutex> g(k->vizLock);
|
|
Effect *fx = k->fx[0].get();
|
|
return fx && fx->hasScope() ? fx->copyScope(out, (int)capacity) : -1;
|
|
}
|
|
|
|
- (float)effectPlayhead {
|
|
EffectKernel *k = self.fxKernel;
|
|
std::lock_guard<std::mutex> g(k->vizLock);
|
|
Effect *fx = k->fx[0].get();
|
|
return fx && fx->hasScope() ? fx->playhead() : -1.f;
|
|
}
|
|
|
|
- (AUInternalRenderBlock)internalRenderBlock {
|
|
EffectKernel *k = self.fxKernel;
|
|
return ^AUAudioUnitStatus(AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *timestamp,
|
|
AVAudioFrameCount frameCount, NSInteger outputBusNumber,
|
|
AudioBufferList *outputData, const AURenderEvent *events,
|
|
AURenderPullInputBlock pullInputBlock) {
|
|
if (frameCount > k->inBuf[0].size() || !pullInputBlock) return kAudioUnitErr_TooManyFramesToProcess;
|
|
|
|
// Pull the input, offering our own buffers. The upstream may instead point
|
|
// mData at its own memory (it does out of process, i.e. in every real host),
|
|
// so the samples are wherever in.list points afterwards, not necessarily inBuf.
|
|
struct { AudioBufferList list; AudioBuffer extra; } in;
|
|
in.list.mNumberBuffers = (UInt32)k->channels;
|
|
for (int c = 0; c < k->channels; ++c) {
|
|
in.list.mBuffers[c].mNumberChannels = 1;
|
|
in.list.mBuffers[c].mDataByteSize = frameCount * sizeof(float);
|
|
in.list.mBuffers[c].mData = k->inBuf[c].data();
|
|
}
|
|
AudioUnitRenderActionFlags pullFlags = 0;
|
|
AUAudioUnitStatus err = pullInputBlock(&pullFlags, timestamp, frameCount, 0, &in.list);
|
|
if (err != noErr) return err;
|
|
|
|
// Host automation: applied at block start.
|
|
for (const AURenderEvent *e = events; e; e = e->head.next)
|
|
if (e->head.eventType == AURenderEventParameter || e->head.eventType == AURenderEventParameterRamp)
|
|
k->setValue(e->parameter.parameterAddress, e->parameter.value);
|
|
k->refresh(k->pollTempo());
|
|
|
|
bool bypass = k->bypass.load(std::memory_order_relaxed);
|
|
for (int c = 0; c < k->channels && c < (int)outputData->mNumberBuffers; ++c) {
|
|
float *src = (float *)in.list.mBuffers[c].mData;
|
|
if (!src) return kAudioUnitErr_NoConnection;
|
|
if (!outputData->mBuffers[c].mData) outputData->mBuffers[c].mData = src; // in place
|
|
float *dst = (float *)outputData->mBuffers[c].mData;
|
|
outputData->mBuffers[c].mDataByteSize = frameCount * sizeof(float);
|
|
if (bypass) {
|
|
if (dst != src) std::memcpy(dst, src, frameCount * sizeof(float));
|
|
continue;
|
|
}
|
|
Effect *fx = k->fx[c].get();
|
|
for (AVAudioFrameCount i = 0; i < frameCount; ++i) {
|
|
float y = fx->process(src[i]);
|
|
dst[i] = std::isfinite(y) ? y : 0.f;
|
|
}
|
|
}
|
|
(void)actionFlags; (void)outputBusNumber;
|
|
return noErr;
|
|
};
|
|
}
|
|
|
|
@end
|