Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
6.3 KiB
Swift
141 lines
6.3 KiB
Swift
// 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?
|
|
///
|
|
/// Runs only when asked (not at launch), records each step in UserDefaults before
|
|
/// doing it, and routes AVAudioEngine calls through SPKTry, so a crash or an
|
|
/// Objective-C exception leaves a readable trail instead of a vanishing app.
|
|
@MainActor
|
|
final class SelfTest: ObservableObject {
|
|
@Published var lines: [String] = []
|
|
private let log = Logger(subsystem: "cc.tiredsysadmin.ios.sizzlespike", category: "selftest")
|
|
private let defaults = UserDefaults.standard
|
|
private static let stepKey = "selftest.step"
|
|
private var liveEngine: AVAudioEngine?
|
|
|
|
init() {
|
|
lines = ["iOS \(ProcessInfo.processInfo.operatingSystemVersionString)",
|
|
"App started OK."]
|
|
if let step = defaults.string(forKey: Self.stepKey) {
|
|
lines.append("!! The previous run stopped during: \(step)")
|
|
}
|
|
lines.append("Tap Run self-test.")
|
|
}
|
|
|
|
private func say(_ s: String) {
|
|
lines.append(s)
|
|
log.notice("\(s, privacy: .public)")
|
|
}
|
|
|
|
/// Remember what we're about to do; cleared when the run finishes.
|
|
private func step(_ s: String) {
|
|
defaults.set(s, forKey: Self.stepKey)
|
|
defaults.synchronize()
|
|
}
|
|
|
|
/// Runs an AVAudioEngine call, reporting an Objective-C exception as a failure.
|
|
private func objc(_ what: String, _ body: () -> Void) -> Bool {
|
|
step(what)
|
|
if let ex = SPKTry(body) { say(" \(what): EXCEPTION \(ex)"); return false }
|
|
return true
|
|
}
|
|
|
|
func run() async {
|
|
lines = ["iOS \(ProcessInfo.processInfo.operatingSystemVersionString)"]
|
|
|
|
// 1. Registration
|
|
step("[1] component lookup")
|
|
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)
|
|
step("[3] App Group probe")
|
|
say("[3] " + Spike.probeGroup(as: "app"))
|
|
|
|
// 2. Load out of process, render 1 s offline (silent), measure the peak
|
|
do {
|
|
step("[2] instantiate out of process")
|
|
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)!
|
|
var thrown: Error?
|
|
guard objc("[2] enable offline rendering", {
|
|
do { try engine.enableManualRenderingMode(.offline, format: format, maximumFrameCount: 4096) }
|
|
catch { thrown = error }
|
|
}), thrown == nil else { throw thrown ?? SpikeError.exception }
|
|
guard objc("[2] connect", {
|
|
engine.attach(unit)
|
|
engine.connect(unit, to: engine.mainMixerNode, format: format)
|
|
}) else { throw SpikeError.exception }
|
|
guard objc("[2] start engine", { do { try engine.start() } catch { thrown = error } }), thrown == nil
|
|
else { throw thrown ?? SpikeError.exception }
|
|
guard objc("[2] note on", { (unit as? AVAudioUnitMIDIInstrument)?.startNote(69, withVelocity: 100, onChannel: 0) })
|
|
else { throw SpikeError.exception }
|
|
let buf = AVAudioPCMBuffer(pcmFormat: engine.manualRenderingFormat, frameCapacity: 4096)!
|
|
var peak: Float = 0
|
|
for _ in 0..<11 {
|
|
guard objc("[2] render", {
|
|
do { _ = try engine.renderOffline(4096, to: buf) } catch { thrown = error }
|
|
}), thrown == nil else { throw thrown ?? SpikeError.exception }
|
|
if let ch = buf.floatChannelData?[0] {
|
|
for i in 0..<Int(buf.frameLength) { peak = max(peak, abs(ch[i])) }
|
|
}
|
|
}
|
|
_ = objc("[2] stop engine") { engine.stop() }
|
|
say(String(format: "[2] Rendered 1 s offline, peak %.3f: %@", peak, peak > 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
|
|
step("[3] App Group probe after AU load")
|
|
say("[3] after AU load: " + Spike.probeGroup(as: "app"))
|
|
defaults.removeObject(forKey: Self.stepKey)
|
|
say("Done.")
|
|
}
|
|
|
|
/// Audible check through the speaker, using the same out-of-process AU.
|
|
func playTone() async {
|
|
do {
|
|
step("play: audio session")
|
|
try AVAudioSession.sharedInstance().setCategory(.playback)
|
|
try AVAudioSession.sharedInstance().setActive(true)
|
|
step("play: instantiate")
|
|
let unit = try await AVAudioUnit.instantiate(with: Spike.desc, options: .loadOutOfProcess)
|
|
let engine = AVAudioEngine()
|
|
var thrown: Error?
|
|
guard objc("play: connect + start", {
|
|
engine.attach(unit)
|
|
engine.connect(unit, to: engine.mainMixerNode, format: nil)
|
|
do { try engine.start() } catch { thrown = error }
|
|
}), thrown == nil else { throw thrown ?? SpikeError.exception }
|
|
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
|
|
defaults.removeObject(forKey: Self.stepKey)
|
|
say("Played A4 for 1 s")
|
|
} catch {
|
|
say("Play FAIL: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
|
|
enum SpikeError: LocalizedError {
|
|
case exception
|
|
var errorDescription: String? { "Objective-C exception (see the line above)" }
|
|
}
|