// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import AudioToolbox import AVFoundation import Foundation /// State the render block touches. Allocated before rendering starts; the render /// block only reads and writes these fields (no allocation, no locks). final class SineKernel { var sampleRate: Double = 44_100 var phase: Double = 0 var freq: Double = 440 var env: Float = 0 var gate = false var gain: Float = 0.3 /// Our own output memory for hosts that pass buffers with nil mData. var scratch = UnsafeMutablePointer.allocate(capacity: 2 * 4096) deinit { scratch.deallocate() } } /// The spike's instrument: one sine voice, MIDI note on/off, one Gain parameter. public final class SpikeAudioUnit: AUAudioUnit { private let kernel = SineKernel() private var outBus: AUAudioUnitBus! private var busses: AUAudioUnitBusArray! private var tree: AUParameterTree! public override init(componentDescription: AudioComponentDescription, options: AudioComponentInstantiationOptions = []) throws { try super.init(componentDescription: componentDescription, options: options) let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 2)! outBus = try AUAudioUnitBus(format: format) outBus.maximumChannelCount = 2 busses = AUAudioUnitBusArray(audioUnit: self, busType: .output, busses: [outBus]) let gain = AUParameterTree.createParameter( withIdentifier: "gain", name: "Gain", address: 0, min: 0, max: 1, unit: .linearGain, unitName: nil, flags: [.flag_IsReadable, .flag_IsWritable], valueStrings: nil, dependentParameters: nil) tree = AUParameterTree.createTree(withChildren: [gain]) let k = kernel tree.implementorValueObserver = { p, v in if p.address == 0 { k.gain = v } } tree.implementorValueProvider = { p in p.address == 0 ? k.gain : 0 } maximumFramesToRender = 4096 // Prove the extension side can write the App Group too. _ = Spike.probeGroup(as: "au") } public override var outputBusses: AUAudioUnitBusArray { busses } public override var parameterTree: AUParameterTree? { get { tree } set { } } public override func allocateRenderResources() throws { try super.allocateRenderResources() kernel.sampleRate = outBus.format.sampleRate if maximumFramesToRender > 4096 { kernel.scratch.deallocate() kernel.scratch = .allocate(capacity: 2 * Int(maximumFramesToRender)) } } public override var internalRenderBlock: AUInternalRenderBlock { let k = kernel return { _, _, frameCount, _, outputData, eventList, _ in // MIDI (applied at the start of the block; good enough for a spike) var ev = eventList while let e = ev { if e.pointee.head.eventType == .MIDI { let m = e.pointee.MIDI let status = m.data.0 & 0xF0, note = m.data.1, vel = m.data.2 if status == 0x90 && vel > 0 { k.freq = 440 * pow(2, (Double(note) - 69) / 12); k.gate = true } else if status == 0x80 || status == 0x90 { k.gate = false } } ev = UnsafePointer(e.pointee.head.next) } let abl = UnsafeMutableAudioBufferListPointer(outputData) for j in 0.. 2 * Double.pi { k.phase -= 2 * Double.pi } for j in 0..