// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import AVFoundation import os /// Answers the spike's questions on the device itself: /// 1. did iOS register the extension's Audio Unit (what hosts will list)? /// 2. does it load out of process and render sound? /// 3. can the app and the extension share files through the App Group? @MainActor final class SelfTest: ObservableObject { @Published var lines: [String] = [] private let log = Logger(subsystem: "cc.tiredsysadmin.ios.sizzlespike", category: "selftest") private var liveEngine: AVAudioEngine? private func say(_ s: String) { lines.append(s) log.notice("\(s, privacy: .public)") } func run() async { lines = [] let os = ProcessInfo.processInfo.operatingSystemVersionString say("iOS \(os)") // 1. Registration let all = AVAudioUnitComponentManager.shared().components(matching: Spike.desc) say("[1] Registered components matching Tsys/sSpk: \(all.count) \(all.isEmpty ? "FAIL" : "PASS")") for c in all { say(" \(c.name) v\(c.versionString), type \(c.typeName)") } // 3a. App Group from the app side (before the AU writes) say("[3] " + Spike.probeGroup(as: "app")) // 2. Load out of process, render 1 s offline (silent), measure the peak do { let unit = try await AVAudioUnit.instantiate(with: Spike.desc, options: .loadOutOfProcess) say("[2] Instantiated \"\(unit.auAudioUnit.audioUnitName ?? "?")\" out of process: PASS") let engine = AVAudioEngine() let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 2)! try engine.enableManualRenderingMode(.offline, format: format, maximumFrameCount: 4096) engine.attach(unit) engine.connect(unit, to: engine.mainMixerNode, format: format) try engine.start() (unit as? AVAudioUnitMIDIInstrument)?.startNote(69, withVelocity: 100, onChannel: 0) let buf = AVAudioPCMBuffer(pcmFormat: engine.manualRenderingFormat, frameCapacity: 4096)! var peak: Float = 0 for _ in 0..<11 { _ = try engine.renderOffline(4096, to: buf) let ch = buf.floatChannelData![0] for i in 0.. 0.05 ? "PASS" : "FAIL")) } catch { say("[2] FAIL: \(error.localizedDescription) (\((error as NSError).code))") } // 3b. The AU wrote au.txt when it was created; read it back from the app side say("[3] after AU load: " + Spike.probeGroup(as: "app")) } /// Audible check through the speaker, using the same out-of-process AU. func playTone() async { do { try AVAudioSession.sharedInstance().setCategory(.playback) try AVAudioSession.sharedInstance().setActive(true) let unit = try await AVAudioUnit.instantiate(with: Spike.desc, options: .loadOutOfProcess) let engine = AVAudioEngine() engine.attach(unit) engine.connect(unit, to: engine.mainMixerNode, format: nil) try engine.start() liveEngine = engine let inst = unit as? AVAudioUnitMIDIInstrument inst?.startNote(69, withVelocity: 100, onChannel: 0) try await Task.sleep(nanoseconds: 1_000_000_000) inst?.stopNote(69, onChannel: 0) try await Task.sleep(nanoseconds: 300_000_000) engine.stop() liveEngine = nil say("Played A4 for 1 s") } catch { say("Play FAIL: \(error.localizedDescription)") } } }