Branchwell

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

Raw 3.0 KB on 32796b3b2cff314d20e7c78bdc17d74e2eace8c1
1import AppKit
2import Foundation
3@preconcurrency import Metal
4
5// The harness runs outside the checkout and loads only the built app's resources.
6extension Bundle {
7 static var module: Bundle {
8 Bundle(path: ProcessInfo.processInfo.environment["PULSECANVAS_TEST_APP"]!)!
9 }
10}
11@main struct OriginalBundleSmoke {
12 static func require(_ value: Bool, _ message: String) throws {
13 if !value { throw NSError(domain: message, code: 1) }
14 }
15 static func pixels(_ texture: MTLTexture) -> [UInt8] {
16 var data = [UInt8](repeating: 0, count: texture.width * texture.height * 4)
17 data.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: texture.width * 4,
18 from: MTLRegionMake2D(0, 0, texture.width, texture.height), mipmapLevel: 0) }
19 return data
20 }
21 @MainActor static func main() throws {
22 NSApplication.shared.setActivationPolicy(.accessory)
23 let audio = Bundle.module.url(forResource: "PulseCanvas", withExtension: "mp3", subdirectory: "Resources/Audio")!
24 let analyzer = try SoundtrackAnalyzer(url: audio)
25 let state = RenderState(), renderer = VisualizerRenderer(state: state)
26 var comparisons = 0
27 for (width,height) in [(320,180),(180,320)] {
28 try renderer.prepareOffscreen(width: width, height: height)
29 for scene in NativeEffects.activeScenes {
30 state.scene = scene; VisualPreset.defaults(for: scene).apply(to: state)
31 state.chaos = 0; state.strobeEnabled = true; state.motionBlur = 0.4
32 for zoom: Float in [0,1.5,3] {
33 state.zoomSpeed = zoom
34 for time in [0.301,2.3,12.3] {
35 let feature = try analyzer.frame(at: time, clipStart: 0, clipEnd: 20)
36 let quiet = pixels(try renderer.renderOffscreen(time: time, audioBands: Array(repeating: 0, count: 32)))
37 let loud = pixels(try renderer.renderOffscreen(time: time, audioBands: feature.bands,
38 audioPulse: feature.pulse, audioMomentum: feature.momentum,
39 audioBeatMotionTime: feature.beatMotionTime, audioTreblePulse: 1, audioTrebleAttackTime: time,
40 audioEnvelopes: feature.envelopes, audioSwell: feature.swell))
41 try require(quiet == loud, "Bundled zero-Chaos output differs: \(scene), \(time), \(zoom)")
42 let rgb = stride(from: 0, to: quiet.count, by: 4).reduce(0.0) { $0 + Double(quiet[$1]) + Double(quiet[$1+1]) + Double(quiet[$1+2]) }
43 try require(rgb / Double(width*height*3) > 1, "Bundled frame is black: \(scene)")
44 comparisons += 1
45 }
46 }
47 }
48 }
49 print("PASS actual app resources: \(NativeEffects.activeCount) effects, \(comparisons) music/silence GPU comparisons, two orientations, three zoom speeds and three song positions; no checkout-resource fallback")
50 }
51}