PulseCanvas / app · pulsecanvas.ai / scripts / ChaosSmoke.swift
| 1 | import AppKit |
| 2 | import AVFoundation |
| 3 | import MetalKit |
| 4 | import Foundation |
| 5 | @main struct ChaosSmoke { |
| 6 | static func require(_ condition: Bool, _ message: String) throws { if !condition { throw AudioAnalysisError(message) } } |
| 7 | static func pixels(_ texture: MTLTexture) -> [UInt8] { |
| 8 | var bytes = [UInt8](repeating: 0, count: texture.width * texture.height * 4) |
| 9 | bytes.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: texture.width*4, from: MTLRegionMake2D(0,0,texture.width,texture.height), mipmapLevel: 0) } |
| 10 | return bytes |
| 11 | } |
| 12 | @MainActor static func main() throws { |
| 13 | _ = NSApplication.shared |
| 14 | let root = URL(fileURLWithPath: ".build/chaos-validation") |
| 15 | try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) |
| 16 | let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 1)! |
| 17 | let pcm = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 176_400)! |
| 18 | pcm.frameLength = pcm.frameCapacity |
| 19 | for i in 0..<Int(pcm.frameLength) { |
| 20 | let time = Double(i)/44_100 |
| 21 | var value = 0.0 |
| 22 | for attack in [0.3,0.8] { |
| 23 | let age = time - attack |
| 24 | if age >= 0 && age < 0.12 { value += 0.65 * exp(-age * 30) * sin(age * 2 * .pi * 65) } |
| 25 | } |
| 26 | pcm.floatChannelData![0][i] = Float(value) |
| 27 | } |
| 28 | let url = root.appendingPathComponent("two-beats.wav") |
| 29 | var settings = format.settings; settings[AVLinearPCMIsNonInterleaved] = false |
| 30 | try AVAudioFile(forWriting: url, settings: settings).write(from: pcm) |
| 31 | let analyzer = try SoundtrackAnalyzer(url: url) |
| 32 | var previous = 0.0 |
| 33 | for i in 0..<400 { |
| 34 | let feature = try analyzer.frame(at: Double(i)/100, clipStart: 0, clipEnd: 4) |
| 35 | try require(feature.beatMotionTime >= previous, "Beat acceleration clock cannot rewind") |
| 36 | if i < 29 { try require(feature.beatMotionTime == 0, "No beat acceleration before percussion") } |
| 37 | previous = feature.beatMotionTime |
| 38 | } |
| 39 | let quiet = try analyzer.frame(at: 3.8, clipStart: 0, clipEnd: 4) |
| 40 | let later = try analyzer.frame(at: 3.9, clipStart: 0, clipEnd: 4) |
| 41 | try require((later.beatMotionTime-quiet.beatMotionTime)/0.1 < 0.01, "Acceleration must settle in quiet passages") |
| 42 | let hit = try analyzer.frame(at: 0.83, clipStart: 0, clipEnd: 4) |
| 43 | let repeated = try analyzer.frame(at: 0.83, clipStart: 0.8, clipEnd: 1.1, frameInterval: 1.0/24) |
| 44 | try require(hit.beatMotionTime > 0.05 && hit.beatMotionTime == repeated.beatMotionTime, "Beat clock must remain source-locked when seeking, trimming, and changing fps") |
| 45 | print("PASS: percussion-only acceleration, monotonic source clock, quiet release, deterministic seeks/trims/frame rates") |
| 46 | let state = RenderState(), renderer = VisualizerRenderer(state: state) |
| 47 | try renderer.prepareOffscreen(width: 320, height: 180) |
| 48 | let bands = [Float](repeating: 0.3, count: 32) |
| 49 | let silence = [Float](repeating: 0, count: 32) |
| 50 | for scene in NativeEffects.activeIDs { |
| 51 | state.scene = scene; state.particleSize = 1.6 |
| 52 | state.chaos = 0 |
| 53 | let calm = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: silence)) |
| 54 | state.chaos = 1 |
| 55 | let noBeats = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: silence)) |
| 56 | try require(calm == noBeats, "Chaos must add no activity without beats, scene \(scene)") |
| 57 | state.chaos = 0 |
| 58 | let restrained = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: bands, audioPulse: 0.8, audioMomentum: 0.9, audioBeatMotionTime: hit.beatMotionTime, audioEnvelopes: SIMD4(repeating: 0.6), audioSwell: 0.8)) |
| 59 | try require(calm == restrained, "Zero Chaos must ignore all music inputs, scene \(scene)") |
| 60 | state.chaos = 1 |
| 61 | let active = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: bands, audioPulse: 0.8, audioMomentum: 0.9, audioBeatMotionTime: hit.beatMotionTime, audioEnvelopes: SIMD4(repeating: 0.6), audioSwell: 0.8)) |
| 62 | let difference = zip(restrained,active).reduce(0.0) { $0 + Double(abs(Int($1.0)-Int($1.1))) } / Double(active.count) |
| 63 | try require(difference > 0.05, "Chaos must visibly change beat activity in scene \(scene): \(difference)") |
| 64 | _ = try renderer.renderOffscreen(time: 0.1, audioBands: bands) |
| 65 | let revisit = pixels(try renderer.renderOffscreen(time: 2.3, audioBands: bands, audioPulse: 0.8, audioMomentum: 0.9, audioBeatMotionTime: hit.beatMotionTime, audioEnvelopes: SIMD4(repeating: 0.6), audioSwell: 0.8)) |
| 66 | try require(active == revisit, "Chaotic visuals must remain deterministic after backwards seeks") |
| 67 | } |
| 68 | print("PASS: all \(NativeEffects.activeIDs.count) GPU visualizers respond to Chaos on beats, remain unchanged without beats, and reproduce exact pixels after seeking") |
| 69 | // Exercise the shared path with zoom/blur/strobe and sharply changing |
| 70 | // audio features. Zero Chaos must be identical to a silent soundtrack |
| 71 | // at every source timestamp, without stopping procedural animation. |
| 72 | for (width, height) in [(320,180),(180,320)] { |
| 73 | try renderer.prepareOffscreen(width: width, height: height) |
| 74 | for scene in NativeEffects.activeIDs { |
| 75 | state.scene = scene; state.chaos = 0; state.strobeEnabled = true |
| 76 | state.motionBlur = 0.4 |
| 77 | for zoom in [Float(0),1.5,3] { |
| 78 | state.zoomSpeed = zoom |
| 79 | var first: [UInt8]? |
| 80 | var last: [UInt8] = [] |
| 81 | for (index, time) in [0.299,0.301,0.799,0.801,2.3].enumerated() { |
| 82 | let clean = pixels(try renderer.renderOffscreen(time: time, audioBands: silence)) |
| 83 | let loud = pixels(try renderer.renderOffscreen(time: time, |
| 84 | audioBands: (0..<32).map { Float(($0+index)%3)/2 }, audioPulse: 1, |
| 85 | audioMomentum: 1, audioBeatMotionTime: Double(index+1), |
| 86 | audioTreblePulse: 1, audioTrebleAttackTime: time, |
| 87 | audioEnvelopes: SIMD4(Float(index % 2), 1, 0.5, 1), audioSwell: 1)) |
| 88 | try require(clean == loud, "Zero Chaos leaked music at \(time): \(RenderState.sceneNames[scene]), zoom \(zoom), \(width)x\(height)") |
| 89 | if first == nil { first = clean }; last = clean |
| 90 | } |
| 91 | try require(first != last, "Zero Chaos must retain visualization motion: \(RenderState.sceneNames[scene]), zoom \(zoom)") |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | print("PASS: every style at zero Chaos ignores pulse, spectrum, momentum, beat clock and strobe in wide/portrait at three zoom speeds, while visualization motion continues") |
| 96 | } |
| 97 | } |