Branchwell

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

Raw 6.4 KB on 23ffa4e102ea595ec83f2b18683b75332fc815c3
1import AppKit
2@preconcurrency import AVFoundation
3@preconcurrency import Metal
4
5/// Verify audible attacks actually move geometry, independent of musical clock/brightness.
6@main struct MusicResponseSmoke {
7 static func require(_ value: Bool, _ message: String) throws {
8 if !value { throw ExportError(message) }
9 }
10 static func pixels(_ texture: MTLTexture) -> [UInt8] {
11 var result = [UInt8](repeating: 0, count: texture.width * texture.height * 4)
12 result.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: texture.width * 4,
13 from: MTLRegionMake2D(0, 0, texture.width, texture.height), mipmapLevel: 0) }
14 return result
15 }
16 static func difference(_ a: [UInt8], _ b: [UInt8]) -> Double {
17 var sum = 0
18 for p in stride(from: 0, to: a.count, by: 4) {
19 for c in 0..<3 { sum += abs(Int(a[p+c]) - Int(b[p+c])) }
20 }
21 return Double(sum) / Double(a.count / 4 * 3)
22 }
23 /// Luma edges measure movement of shapes rather than a uniform brightness increase.
24 static func edges(_ a: [UInt8], width: Int) -> [UInt8] {
25 var output = a
26 let height = a.count / (width * 4)
27 for y in 0..<height-1 { for x in 0..<width-1 {
28 let p = (y * width + x) * 4
29 for c in 0..<3 {
30 output[p+c] = UInt8(min(255, abs(Int(a[p+c]) - Int(a[p+4+c])) + abs(Int(a[p+c]) - Int(a[p+width*4+c]))))
31 }
32 }}
33 return output
34 }
35 @MainActor static func main() async throws {
36 _ = NSApplication.shared
37 let root = URL(fileURLWithPath: ".build/music-response")
38 try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
39 let source = URL(fileURLWithPath: "Sources/PulseCanvas/Resources/Audio/PulseCanvas.mp3")
40 let state = RenderState(), renderer = VisualizerRenderer(state: state)
41 try renderer.prepareOffscreen(width: 320, height: 180)
42 let zero = [Float](repeating: 0, count: 32)
43 func render(_ time: Double, _ audio: AudioFeatures) throws -> [UInt8] {
44 pixels(try renderer.renderOffscreen(time: time, audioBands: audio.bands, audioPulse: audio.pulse,
45 audioMomentum: audio.momentum, audioBeatMotionTime: audio.beatMotionTime,
46 audioTreblePulse: audio.treblePulse, audioTrebleAttackTime: audio.trebleAttackTime,
47 audioEnvelopes: audio.envelopes, audioSwell: audio.swell))
48 }
49 var minimumAttack = 255.0, minimumEdge = 255.0
50 // Integrated designs share the expansion prelude's beat motion (Coaster versions of the
51 // original tunnels 100–133 use the original prelude, like the designs they come from).
52 let integrated = NativeEffects.activeIDs.filter {
53 let file = NativeEffects.all[$0].file
54 return file?.hasPrefix("NativeExpansion") == true && file != NativeEffects.atmospheric
55 }
56 for scene in integrated {
57 state.scene = scene; VisualPreset.defaults(for: scene).apply(to: state)
58 state.motionBlur = 0; state.strobeEnabled = false
59 let idle = try render(99.2, .silence)
60 let hit = try render(99.2, AudioFeatures(bands: zero, pulse: 1))
61 let delta = difference(idle, hit), edgeDelta = difference(edges(idle, width: 320), edges(hit, width: 320))
62 minimumAttack = min(minimumAttack, delta); minimumEdge = min(minimumEdge, edgeDelta)
63 try require(delta > 2 && edgeDelta > 1, "Weak geometric attack: \(scene) \(NativeEffects.all[scene].name) rgb \(delta), edges \(edgeDelta)")
64 let musicClock = try render(99.2, AudioFeatures(bands: zero, pulse: 0, beatMotionTime: 1))
65 try require(difference(idle, musicClock) > 2, "Disconnected music clock: \(scene)")
66 for band in 0..<4 {
67 var levels = SIMD4<Float>(repeating: 0); levels[band] = 1
68 let active = try render(99.2, AudioFeatures(bands: zero, pulse: 0, envelopes: levels))
69 try require(difference(idle, active) > 0.75, "Disconnected band \(band): \(scene)")
70 }
71 }
72 print(String(format: "PASS %d integrated native shaders: direct attacks move shapes (minimum RGB %.2f/255, edge movement %.2f/255); integrated percussion clock and all four bands connected", integrated.count, minimumAttack, minimumEdge))
73 // These designs use attacks in local highlights and keep forward camera travel steady.
74 for scene in NativeEffects.activeIDs where NativeEffects.all[scene].file == NativeEffects.atmospheric {
75 state.scene = scene; VisualPreset.defaults(for: scene).apply(to: state)
76 state.motionBlur = 0; state.strobeEnabled = false
77 let idle = try render(99.2, .silence)
78 let accent = AudioFeatures(bands: zero, pulse: 1, swell: 1)
79 try require(idle != render(99.2, accent), "Disconnected local accent: \(scene)")
80 state.chaos = 0
81 try require(render(99.2, .silence) == render(99.2, accent), "Atmospheric effect responds at zero Chaos: \(scene)")
82 }
83 let analyzer = try SoundtrackAnalyzer(url: source)
84 var features: [(Double, AudioFeatures)] = []
85 for i in 0..<240 { let t = 99 + Double(i) / 60; features.append((t, try analyzer.frame(at: t, clipStart: 0, clipEnd: 10_000))) }
86 for scene in [201, 205, 252, 273, 285, 290] {
87 state.scene = scene; VisualPreset.defaults(for: scene).apply(to: state)
88 state.motionBlur = 0; state.strobeEnabled = false
89 var on: [Double] = [], between: [Double] = []
90 for (time, audio) in features {
91 let measured = try render(time, audio)
92 var withoutAttack = audio; withoutAttack.pulse = 0
93 let movement = difference(measured, try render(time, withoutAttack))
94 if audio.pulse > 0.25 { on.append(movement) }
95 if audio.pulse < 0.02 { between.append(movement) }
96 }
97 let strong = on.reduce(0,+) / Double(on.count), quiet = between.reduce(0,+) / Double(between.count)
98 try require(on.count > 10 && between.count > 10 && strong > 2 && strong > quiet * 3,
99 "Movement does not follow measured beats: \(scene) \(strong)/\(quiet)")
100 print(String(format: "PASS source percussion %@: attack displacement %.2f, between attacks %.2f/255 (%d/%d frames)", NativeEffects.all[scene].name, strong, quiet, on.count, between.count))
101 }
102 }
103}