Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
5.8 KiB
Swift
129 lines
5.8 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import AVFoundation
|
|
|
|
/// Lists Sizzle's registered plugins and, on request, loads each one out of
|
|
/// process (as a host would) and renders a test signal through it offline.
|
|
/// PASS = loads, output is finite, and not silent.
|
|
@MainActor
|
|
final class PluginCheck: ObservableObject {
|
|
@Published var lines: [String] = []
|
|
@Published var running = false
|
|
|
|
private static let manufacturer: FourCharCode = "Tsys".utf8.reduce(0) { ($0 << 8) | FourCharCode($1) }
|
|
|
|
private func installed() -> [AVAudioUnitComponent] {
|
|
let any = AudioComponentDescription(componentType: 0, componentSubType: 0,
|
|
componentManufacturer: Self.manufacturer,
|
|
componentFlags: 0, componentFlagsMask: 0)
|
|
return AVAudioUnitComponentManager.shared().components(matching: any)
|
|
.filter { $0.audioComponentDescription.componentType == kAudioUnitType_Effect }
|
|
}
|
|
|
|
func listInstalled() {
|
|
let comps = installed()
|
|
lines = ["Registered effects: \(comps.count)"] + comps.map { " \($0.name)" }
|
|
}
|
|
|
|
func run() async {
|
|
running = true
|
|
defer { running = false }
|
|
let comps = installed()
|
|
lines = ["Checking \(comps.count) effects…"]
|
|
var ok = 0
|
|
for c in comps {
|
|
let result = await check(c)
|
|
if result.hasPrefix("PASS") { ok += 1 }
|
|
lines.append("\(result) \(c.name.replacingOccurrences(of: "Tired Sysadmin: ", with: ""))")
|
|
}
|
|
lines.append("\(ok)/\(comps.count) passed")
|
|
}
|
|
|
|
/// Loads a plugin out of process without ever crashing the app. Right after an
|
|
/// update, iOS may still list the old registration while the new one isn't
|
|
/// resolvable yet; AVAudioUnit.instantiate then raises an Objective-C
|
|
/// exception ("comp != NULL") instead of returning an error.
|
|
private func instantiate(_ desc: AudioComponentDescription) async throws -> AVAudioUnit {
|
|
var d = desc
|
|
guard AudioComponentFindNext(nil, &d) != nil else { throw CheckError.notRegistered }
|
|
return try await withCheckedThrowingContinuation { cont in
|
|
var resumed = false
|
|
let ex = SPKTry {
|
|
AVAudioUnit.instantiate(with: d, options: .loadOutOfProcess) { unit, error in
|
|
if let unit = unit { cont.resume(returning: unit) }
|
|
else { cont.resume(throwing: error ?? CheckError.notRegistered) }
|
|
}
|
|
resumed = true
|
|
}
|
|
if let ex = ex, !resumed { cont.resume(throwing: CheckError.exception(ex)) }
|
|
}
|
|
}
|
|
|
|
/// 1 s at 44.1 kHz stereo: a decaying saw burst, then noise, then silence.
|
|
private func testBuffer(format: AVAudioFormat) -> AVAudioPCMBuffer {
|
|
let n = AVAudioFrameCount(format.sampleRate)
|
|
let buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: n)!
|
|
buf.frameLength = n
|
|
var seed: UInt32 = 0x1234567
|
|
for i in 0..<Int(n) {
|
|
let t = Double(i) / format.sampleRate
|
|
var x: Float = 0
|
|
if t < 0.3 { x = Float(0.6 * (2 * (t * 110).truncatingRemainder(dividingBy: 1) - 1) * exp(-t * 6)) }
|
|
else if t < 0.6 { seed = seed &* 1103515245 &+ 12345; x = Float((seed >> 8) & 0xFFFF) / 65535 * 0.6 - 0.3 }
|
|
for ch in 0..<Int(format.channelCount) { buf.floatChannelData![ch][i] = x }
|
|
}
|
|
return buf
|
|
}
|
|
|
|
private func check(_ comp: AVAudioUnitComponent) async -> String {
|
|
do {
|
|
let unit = try await instantiate(comp.audioComponentDescription)
|
|
let engine = AVAudioEngine()
|
|
let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 2)!
|
|
let player = AVAudioPlayerNode()
|
|
var thrown: Error?
|
|
let ex = SPKTry {
|
|
do {
|
|
try engine.enableManualRenderingMode(.offline, format: format, maximumFrameCount: 4096)
|
|
engine.attach(player)
|
|
engine.attach(unit)
|
|
engine.connect(player, to: unit, format: format)
|
|
engine.connect(unit, to: engine.mainMixerNode, format: format)
|
|
try engine.start()
|
|
player.scheduleBuffer(self.testBuffer(format: format), completionHandler: nil)
|
|
player.play()
|
|
} catch { thrown = error }
|
|
}
|
|
if let ex = ex { return "FAIL exception \(ex)" }
|
|
if let e = thrown { return "FAIL \(e.localizedDescription)" }
|
|
let out = AVAudioPCMBuffer(pcmFormat: engine.manualRenderingFormat, frameCapacity: 4096)!
|
|
var peak: Float = 0, finite = true
|
|
for _ in 0..<11 {
|
|
let status = try engine.renderOffline(4096, to: out)
|
|
if status != .success { engine.stop(); return "FAIL render status \(status.rawValue)" }
|
|
for ch in 0..<Int(out.format.channelCount) {
|
|
let d = out.floatChannelData![ch]
|
|
for i in 0..<Int(out.frameLength) { let v = d[i]; if !v.isFinite { finite = false }; peak = max(peak, abs(v)) }
|
|
}
|
|
}
|
|
engine.stop()
|
|
if !finite { return "FAIL non-finite output" }
|
|
return peak > 0.01 ? String(format: "PASS peak %.2f", peak) : String(format: "FAIL silent (peak %.4f)", peak)
|
|
} catch {
|
|
return "FAIL \(error.localizedDescription)"
|
|
}
|
|
}
|
|
}
|
|
|
|
enum CheckError: LocalizedError {
|
|
case notRegistered
|
|
case exception(String)
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .notRegistered: return "not registered (yet): reopen the app, or restart the iPad after an update"
|
|
case .exception(let e): return "exception \(e)"
|
|
}
|
|
}
|
|
}
|