// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #import "SizzleInstrumentAU.h" #import "SizzleDeviceAU+Private.h" #import #include #include #include "sizzle/Instrument.hpp" using namespace sizzle; namespace { struct InstrumentKernel final : DeviceKernel { std::unique_ptr inst; std::vector outBuf; // for hosts that pass null output buffers void apply(const Params &p, float tempoBpm) override { if (inst) inst->update(p, tempoBpm); } /** One MIDI message (any channel). */ void midi(const uint8_t *d, int len) { if (!inst || len < 1) return; uint8_t status = d[0] & 0xF0; if (status == 0x90 && len >= 3 && d[2] > 0) inst->noteOn(d[1], d[2]); else if ((status == 0x80 || status == 0x90) && len >= 3) inst->noteOff(d[1]); else if (status == 0xB0 && len >= 3) { if (d[1] == 123) inst->allNotesOff(); // all notes off else if (d[1] == 120) inst->kill(); // all sound off } } }; } // namespace @implementation SizzleInstrumentAU { AUAudioUnitBusArray *_outputs; AUAudioUnitBus *_outBus; } - (std::unique_ptr)makeKernel { return std::make_unique(); } - (InstrumentKernel *)instKernel { return static_cast(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]; _outBus = [[AUAudioUnitBus alloc] initWithFormat:fmt error:outError]; if (!_outBus) return nil; _outBus.maximumChannelCount = 2; _outputs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeOutput busses:@[_outBus]]; return self; } - (AUAudioUnitBusArray *)outputBusses { return _outputs; } - (NSArray *)channelCapabilities { return @[@0, @1, @0, @2]; } - (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError { if (![super allocateRenderResourcesAndReturnError:outError]) return NO; InstrumentKernel *k = self.instKernel; k->inst = Instrument::create(k->type.c_str(), (int)std::lround(_outBus.format.sampleRate), static_cast(self.sampleStore)); k->outBuf.assign(self.maximumFramesToRender, 0.f); if (!k->inst) { if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil]; [super deallocateRenderResources]; return NO; } return YES; } - (void)deallocateRenderResources { self.instKernel->inst.reset(); [super deallocateRenderResources]; } - (AUInternalRenderBlock)internalRenderBlock { InstrumentKernel *k = self.instKernel; return ^AUAudioUnitStatus(AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *timestamp, AVAudioFrameCount frameCount, NSInteger outputBusNumber, AudioBufferList *outputData, const AURenderEvent *events, AURenderPullInputBlock pullInputBlock) { Instrument *inst = k->inst.get(); if (!inst) return kAudioUnitErr_Uninitialized; UInt32 nb = outputData->mNumberBuffers; if (nb < 1) return noErr; if (frameCount > k->outBuf.size()) return kAudioUnitErr_TooManyFramesToProcess; // Hosts may pass null buffers: then the AU supplies its own. if (!outputData->mBuffers[0].mData) outputData->mBuffers[0].mData = k->outBuf.data(); float *out0 = (float *)outputData->mBuffers[0].mData; k->refresh(k->pollTempo()); inst->blockStart((int)frameCount); // Render between events so each note starts on its own sample. const AUEventSampleTime start = (AUEventSampleTime)timestamp->mSampleTime; const AURenderEvent *e = events; AVAudioFrameCount frame = 0; while (frame < frameCount) { while (e && e->head.eventSampleTime - start <= (AUEventSampleTime)frame) { switch (e->head.eventType) { case AURenderEventMIDI: k->midi(e->MIDI.data, e->MIDI.length); break; case AURenderEventParameter: case AURenderEventParameterRamp: k->setValue(e->parameter.parameterAddress, e->parameter.value); k->refresh(false); break; default: break; } e = e->head.next; } AVAudioFrameCount end = frameCount; if (e) { AUEventSampleTime t = e->head.eventSampleTime - start; if (t < (AUEventSampleTime)end) end = (AVAudioFrameCount)std::max(t, frame + 1); } for (AVAudioFrameCount i = frame; i < end; ++i) { float y = inst->render(); out0[i] = std::isfinite(y) ? y : 0.f; } frame = end; } outputData->mBuffers[0].mDataByteSize = frameCount * sizeof(float); for (UInt32 c = 1; c < nb; ++c) { if (!outputData->mBuffers[c].mData) outputData->mBuffers[c].mData = out0; else std::memcpy(outputData->mBuffers[c].mData, out0, frameCount * sizeof(float)); outputData->mBuffers[c].mDataByteSize = frameCount * sizeof(float); } (void)actionFlags; (void)outputBusNumber; (void)pullInputBlock; return noErr; }; } @end