Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / RendererOfflineSmoke.swift

Raw 2.4 KB on 32796b3b2cff314d20e7c78bdc17d74e2eace8c1
1import AppKit
2import MetalKit
3import Foundation
4@main struct OfflineSmoke {
5 @MainActor static func main() throws {
6 func pixels(_ t: MTLTexture) -> [UInt8] {
7 var p = [UInt8](repeating: 0, count: t.width * t.height * 4)
8 p.withUnsafeMutableBytes { t.getBytes($0.baseAddress!, bytesPerRow: t.width * 4, from: MTLRegionMake2D(0, 0, t.width, t.height), mipmapLevel: 0) }
9 return p
10 }
11 func mean(_ p: [UInt8]) -> Double {
12 var sum: Double = 0
13 for i in stride(from: 0, to: p.count, by: 4) { sum += Double(p[i]) + Double(p[i+1]) + Double(p[i+2]) }
14 return sum / Double(p.count / 4 * 3)
15 }
16 _ = NSApplication.shared
17 let state = RenderState(), renderer = VisualizerRenderer(state: RenderState())
18 renderer.state = state
19 let zero = [Float](repeating: 0, count: 32)
20 let spectrum = (0..<32).map { Float($0 % 7) / 10 }
21 var hats = zero; for i in 24..<32 { hats[i] = 0.9 }
22 var bass = zero; for i in 0..<6 { bass[i] = 1 }
23 var checks = 0
24 for (width, height) in [(320, 180), (90, 160)] {
25 try renderer.prepareOffscreen(width: width, height: height)
26 for scene in NativeEffects.activeIDs {
27 state.scene = scene; state.motionBlur = 0.55; state.strobeEnabled = false
28 let a = pixels(try renderer.renderOffscreen(time: 4.2, audioBands: spectrum))
29 let b = pixels(try renderer.renderOffscreen(time: 4.2, audioBands: spectrum))
30 assert(a == b, "Repeated timestamp mismatch scene \(scene)")
31 assert(a.count == width * height * 4 && mean(a) > 0.5)
32 state.strobeEnabled = true
33 _ = try renderer.renderOffscreen(time: 0, audioBands: zero)
34 let white = pixels(try renderer.renderOffscreen(time: 0.01, audioBands: hats))
35 assert(mean(white) > 254.5, "Flash not full-frame white scene \(scene)")
36 _ = try renderer.renderOffscreen(time: 0, audioBands: zero)
37 let low = pixels(try renderer.renderOffscreen(time: 0.01, audioBands: bass))
38 assert(mean(low) < 250, "Bass incorrectly triggered flash scene \(scene)")
39 checks += 3
40 }
41 }
42 print("PASS: \(checks) offline checks: all modes, landscape/portrait exact pixels, deterministic timestamps, post-blur white flash and bass rejection")
43 }
44}