Branchwell

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

Raw 3.8 KB on 0e4fe45e403a1b6b5a57aa68addd681995d37722
1import Foundation
2import Metal
3
4/// A preset displays red only when its progress, frame counter and initialization
5/// FPS are valid. This catches timing corruption independently of bundled artwork.
6@main struct ProjectMTransitionSmoke {
7 static func main() throws {
8 let root = URL(fileURLWithPath: ".build/validation/projectm-transitions")
9 try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
10 guard let device = MTLCreateSystemDefaultDevice() else { fatalError("Metal unavailable") }
11 let pcm = (0..<1024).flatMap { sample -> [Float] in
12 let value = Float(sin(Double(sample) * 0.017) * 0.6)
13 return [value, value]
14 }
15 for fps in [24, 30, 60] {
16 let fixture = """
17 [preset00]
18 fDecay=0
19 fWaveAlpha=0
20 shapecode_0_enabled=1
21 shapecode_0_sides=4
22 shapecode_0_x=0.5
23 shapecode_0_y=0.5
24 shapecode_0_rad=1
25 shapecode_0_r=1
26 shapecode_0_g=0
27 shapecode_0_b=0
28 shapecode_0_a=1
29 shapecode_0_r2=1
30 shapecode_0_g2=0
31 shapecode_0_b2=0
32 shapecode_0_a2=1
33 per_frame_init_1=q3=fps;
34 per_frame_1=q1=progress;q2=frame;
35 shape_0_per_frame1=r=if(below(q1,0),0,1);g=if(above(q2,3),1,0);b=if(equal(q3,\(fps)),0,1);r2=r;g2=g;b2=b;
36 """
37 let first = root.appendingPathComponent("first-\(fps).milk")
38 let second = root.appendingPathComponent("second-\(fps).milk")
39 try fixture.write(to: first, atomically: true, encoding: .utf8)
40 try fixture.write(to: second, atomically: true, encoding: .utf8)
41 let engine = try ProjectMEngine()
42 func pixel(_ preset: URL, _ time: Double, chaos: Float = 1) throws -> [UInt8] {
43 let texture = try engine.frame(preset: preset, texturePaths: [], device: device, width: 64, height: 64,
44 time: time, sourceTime: time, pcm: pcm, chaos: chaos, fps: fps)
45 var pixels = [UInt8](repeating: 0, count: 64 * 64 * 4)
46 texture.getBytes(&pixels, bytesPerRow: 256, from: MTLRegionMake2D(0, 0, 64, 64), mipmapLevel: 0)
47 let offset = (32 * 64 + 32) * 4
48 return Array(pixels[offset..<offset + 4])
49 }
50 func requireRed(_ value: [UInt8], _ message: String) {
51 precondition(value[2] > 230 && value[1] < 5 && value[0] < 5, "\(message): BGRA \(value)")
52 }
53 for frame in 0...fps * 5 { _ = try pixel(first, Double(frame) / Double(fps)) }
54 let oldFrame = try pixel(first, 5 + 1 / Double(fps))
55 precondition(oldFrame[1] > 230, "The first preset must have accumulated frame history")
56 _ = try pixel(second, 5 + 2 / Double(fps))
57 let switched = try pixel(second, 5 + 3 / Double(fps))
58 requireRed(switched, "A preset switch must reset frame history and retain nonnegative progress and the chosen FPS")
59 let cached = try pixel(second, 5 + 3 / Double(fps))
60 precondition(cached == switched, "A paused/cached frame must not advance the engine")
61 _ = try pixel(second, 0)
62 requireRed(try pixel(second, 1 / Double(fps)), "A backward seek must restart cleanly")
63 _ = try pixel(second, 2 / Double(fps), chaos: 0)
64 requireRed(try pixel(second, 3 / Double(fps), chaos: 0), "Zero Chaos must restart cleanly without music history")
65 _ = try pixel(first, 10, chaos: 0)
66 requireRed(try pixel(first, 10 + 1 / Double(fps), chaos: 0), "A late switch at zero Chaos must remain valid")
67 }
68 print("PASS ProjectM transitions: fresh frame/audio timing, nonnegative progress, initialization FPS, cached pause, backward seek and zero Chaos at 24/30/60 fps")
69 }
70}