PulseCanvas / app / visionOS / VisionProject.swift
| 1 | import Foundation |
| 2 | import SwiftUI |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | struct VisionLook: Codable, Equatable { |
| 6 | var zoom: Float |
| 7 | var speed: Float |
| 8 | var zoomSpeed: Float |
| 9 | var chaos: Float |
| 10 | var brightness: Float |
| 11 | var size: Float |
| 12 | var detail: Float |
| 13 | var fog: Float |
| 14 | var palette: Int |
| 15 | var rotation: Float = 0 |
| 16 | var motionBlur: Float = 0 |
| 17 | var haloCount = 6 |
| 18 | var strobeEnabled = false |
| 19 | var strobeSensitivity: Float = 1 |
| 20 | var strobeStrength: Float = 1 |
| 21 | init(scene: Int) { |
| 22 | let look = NativeEffects.all[NativeEffects.resolved(scene)].look |
| 23 | zoom = look.zoom |
| 24 | speed = look.speed; zoomSpeed = look.zoomSpeed; chaos = look.chaos |
| 25 | brightness = look.brightness; size = look.size; detail = look.detail; fog = look.fog; palette = look.palette |
| 26 | } |
| 27 | var isValid: Bool { |
| 28 | [zoom, speed, zoomSpeed, chaos, brightness, size, detail, fog, rotation, motionBlur, strobeSensitivity, strobeStrength].allSatisfy(\.isFinite) |
| 29 | && (0.1...3).contains(zoom) && (0...3).contains(speed) && (0...3).contains(zoomSpeed) && (0...1).contains(chaos) |
| 30 | && (0.1...3).contains(brightness) && (0.1...6).contains(size) |
| 31 | && (0...1).contains(detail) && (0...1).contains(fog) && (0...4).contains(palette) |
| 32 | && (0.25...4).contains(strobeSensitivity) && (0.1...1).contains(strobeStrength) |
| 33 | && (1...12).contains(haloCount) && (0...1).contains(motionBlur) |
| 34 | } |
| 35 | |
| 36 | private enum CodingKeys: String, CodingKey { |
| 37 | case zoom, speed, zoomSpeed, chaos, brightness, size, detail, fog, palette |
| 38 | case strobeEnabled, strobeSensitivity, strobeStrength |
| 39 | case rotation, motionBlur, haloCount |
| 40 | } |
| 41 | init(from decoder: Decoder) throws { |
| 42 | let v = try decoder.container(keyedBy: CodingKeys.self) |
| 43 | zoom = try v.decode(Float.self, forKey: .zoom); speed = try v.decode(Float.self, forKey: .speed) |
| 44 | zoomSpeed = try v.decode(Float.self, forKey: .zoomSpeed); chaos = try v.decode(Float.self, forKey: .chaos) |
| 45 | brightness = try v.decode(Float.self, forKey: .brightness); size = try v.decode(Float.self, forKey: .size) |
| 46 | detail = try v.decode(Float.self, forKey: .detail); fog = try v.decode(Float.self, forKey: .fog) |
| 47 | palette = try v.decode(Int.self, forKey: .palette) |
| 48 | strobeEnabled = try v.decodeIfPresent(Bool.self, forKey: .strobeEnabled) ?? false |
| 49 | strobeSensitivity = try v.decodeIfPresent(Float.self, forKey: .strobeSensitivity) ?? 1 |
| 50 | strobeStrength = try v.decodeIfPresent(Float.self, forKey: .strobeStrength) ?? 1 |
| 51 | rotation = try v.decodeIfPresent(Float.self, forKey: .rotation) ?? 0 |
| 52 | motionBlur = try v.decodeIfPresent(Float.self, forKey: .motionBlur) ?? 0 |
| 53 | haloCount = try v.decodeIfPresent(Int.self, forKey: .haloCount) ?? 6 |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | struct VisionClip: Codable, Identifiable, Equatable { |
| 58 | var id = UUID() |
| 59 | var scene: Int |
| 60 | var bars = 4 |
| 61 | } |
| 62 | |
| 63 | struct VisionProject: Codable { |
| 64 | var version = 1 |
| 65 | var clips: [VisionClip] = [VisionClip(scene: NativeEffects.defaultScene)] |
| 66 | /// The same native ID shares one appearance across every clip. |
| 67 | var looks: [Int: VisionLook] = [:] |
| 68 | var audioFile: String? = nil |
| 69 | var audioTitle = "Demo soundtrack" |
| 70 | var bpm = 120.0 |
| 71 | var beatBoundaries: [Double]? = nil |
| 72 | /// Same four-beat bar grid used by Mac Studio; old beat grids remain decodable. |
| 73 | var barBoundaries: [Double]? = nil |
| 74 | var externalAudioURL: URL? = nil |
| 75 | /// Preserve exact Mac cuts and fields not exposed in Vision's editor. |
| 76 | var importedTimeline: [StudioSegment]? = nil |
| 77 | var macProject: Data? = nil |
| 78 | var exportAspect: String? = nil |
| 79 | var exportResolution: Int? = nil |
| 80 | var exportFPS: Int? = nil |
| 81 | var loopPlayback = true |
| 82 | var cloudProjectID: String? = nil |
| 83 | |
| 84 | init() {} |
| 85 | |
| 86 | private enum CodingKeys: String, CodingKey { |
| 87 | case version, clips, looks, audioFile, audioTitle, bpm, beatBoundaries, barBoundaries, loopPlayback |
| 88 | case externalAudioURL, importedTimeline, macProject |
| 89 | case exportAspect, exportResolution, exportFPS, cloudProjectID |
| 90 | } |
| 91 | |
| 92 | init(from decoder: Decoder) throws { |
| 93 | let values = try decoder.container(keyedBy: CodingKeys.self) |
| 94 | version = try values.decode(Int.self, forKey: .version) |
| 95 | clips = try values.decode([VisionClip].self, forKey: .clips) |
| 96 | looks = try values.decode([Int: VisionLook].self, forKey: .looks) |
| 97 | audioFile = try values.decodeIfPresent(String.self, forKey: .audioFile) |
| 98 | audioTitle = try values.decode(String.self, forKey: .audioTitle) |
| 99 | bpm = try values.decode(Double.self, forKey: .bpm) |
| 100 | beatBoundaries = try values.decodeIfPresent([Double].self, forKey: .beatBoundaries) |
| 101 | barBoundaries = try values.decodeIfPresent([Double].self, forKey: .barBoundaries) |
| 102 | externalAudioURL = try values.decodeIfPresent(URL.self, forKey: .externalAudioURL) |
| 103 | importedTimeline = try values.decodeIfPresent([StudioSegment].self, forKey: .importedTimeline) |
| 104 | macProject = try values.decodeIfPresent(Data.self, forKey: .macProject) |
| 105 | exportAspect = try values.decodeIfPresent(String.self, forKey: .exportAspect) |
| 106 | exportResolution = try values.decodeIfPresent(Int.self, forKey: .exportResolution) |
| 107 | exportFPS = try values.decodeIfPresent(Int.self, forKey: .exportFPS) |
| 108 | // Existing Studio projects gain looping without losing their scenes or looks. |
| 109 | loopPlayback = try values.decodeIfPresent(Bool.self, forKey: .loopPlayback) ?? true |
| 110 | cloudProjectID = try values.decodeIfPresent(String.self, forKey: .cloudProjectID) |
| 111 | } |
| 112 | |
| 113 | func validated() throws -> Self { |
| 114 | guard version == 1, cloudProjectID.map({ !$0.isEmpty && $0.count <= 100 }) ?? true, |
| 115 | (1...10_000).contains(clips.count), Set(clips.map(\.id)).count == clips.count, |
| 116 | bpm.isFinite, (20...300).contains(bpm), |
| 117 | clips.allSatisfy({ NativeEffects.all.indices.contains($0.scene) && (1...64).contains($0.bars) }), |
| 118 | looks.allSatisfy({ NativeEffects.all.indices.contains($0.key) && $0.value.isValid }), |
| 119 | importedTimeline.map({ $0.count == clips.count && StudioTimeline.valid($0, sceneCount: NativeEffects.all.count) }) ?? true, |
| 120 | externalAudioURL.map(\.isFileURL) ?? true, |
| 121 | exportResolution.map({ [720, 1080, 2160].contains($0) }) ?? true, |
| 122 | exportFPS.map({ [24, 30, 60, 120].contains($0) }) ?? true, |
| 123 | audioFile.map({ !$0.contains("/") && !$0.contains("\\") && $0 != "." && $0 != ".." }) ?? true, |
| 124 | [beatBoundaries, barBoundaries].allSatisfy({ $0.map({ beats in |
| 125 | beats.count >= 2 && beats.count <= 100_000 && beats.first == 0 |
| 126 | && beats.allSatisfy { $0.isFinite && (0...86_400).contains($0) } |
| 127 | && zip(beats, beats.dropFirst()).allSatisfy { $0 < $1 } |
| 128 | }) ?? true }) else { |
| 129 | throw CocoaError(.fileReadCorruptFile) |
| 130 | } |
| 131 | var result = self |
| 132 | result.clips = clips.map { var clip = $0; clip.scene = NativeEffects.resolved(clip.scene); return clip } |
| 133 | return result |
| 134 | } |
| 135 | |
| 136 | func segments(duration: Double) -> [StudioSegment] { |
| 137 | guard duration.isFinite, duration > 0 else { return [] } |
| 138 | if let importedTimeline { |
| 139 | return zip(importedTimeline, clips).compactMap { cut, clip in |
| 140 | guard cut.start < duration else { return nil } |
| 141 | return StudioSegment(start: cut.start, end: min(duration, cut.end), scene: clip.scene, bars: clip.bars) |
| 142 | } |
| 143 | } |
| 144 | var start = 0.0 |
| 145 | var bar = 0 |
| 146 | return clips.compactMap { clip in |
| 147 | guard start < duration else { return nil } |
| 148 | bar += clip.bars |
| 149 | let end = sectionEnd(bar: bar, start: start, bars: clip.bars, duration: duration) |
| 150 | defer { start = end } |
| 151 | return StudioSegment(start: start, end: end, scene: clip.scene, bars: clip.bars) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /// Fill the song after bar edits, retaining existing cards and IDs in order. |
| 156 | mutating func fitTimeline(duration: Double) { |
| 157 | guard duration.isFinite, duration > 0, !clips.isEmpty else { return } |
| 158 | let previous = clips |
| 159 | var result: [VisionClip] = [], cursor = 0, time = 0.0 |
| 160 | while time < duration, result.count < 10_000 { |
| 161 | let i = result.count |
| 162 | let clip = i < previous.count ? previous[i] |
| 163 | : VisionClip(scene: previous[i % previous.count].scene, bars: previous.last!.bars) |
| 164 | cursor += clip.bars |
| 165 | let end = sectionEnd(bar: cursor, start: time, bars: clip.bars, duration: duration) |
| 166 | guard end > time else { break } |
| 167 | result.append(clip); time = end |
| 168 | } |
| 169 | if time == duration { clips = result } |
| 170 | } |
| 171 | /// Rebuild every cut on the bar grid, retaining card identities and visualizer order where possible. |
| 172 | /// Work in a temporary array so an invalid length or capacity failure cannot leave a partial edit. |
| 173 | @discardableResult mutating func randomizeBars(duration: Double, chooseLength: () -> Int) -> Bool { |
| 174 | guard duration.isFinite, duration > 0, !clips.isEmpty else { return false } |
| 175 | let previous = clips |
| 176 | var result: [VisionClip] = [], cursor = 0, time = 0.0 |
| 177 | while time < duration, result.count < 10_000 { |
| 178 | let index = result.count |
| 179 | let bars = chooseLength() |
| 180 | guard (1...8).contains(bars) else { return false } |
| 181 | var clip = index < previous.count ? previous[index] : VisionClip(scene: previous[index % previous.count].scene) |
| 182 | clip.bars = bars |
| 183 | cursor += bars |
| 184 | let end = sectionEnd(bar: cursor, start: time, bars: bars, duration: duration) |
| 185 | guard end > time else { return false } |
| 186 | result.append(clip); time = end |
| 187 | } |
| 188 | guard time == duration else { return false } |
| 189 | clips = result; importedTimeline = nil |
| 190 | return true |
| 191 | } |
| 192 | private func sectionEnd(bar: Int, start: Double, bars: Int, duration: Double) -> Double { |
| 193 | // The audio player and analyzer can disagree slightly about decoder padding. |
| 194 | // The terminal cut always uses the player's duration, without zero-length tail cards. |
| 195 | if let grid = barBoundaries { return bar >= grid.count - 1 ? duration : min(duration, grid[bar]) } |
| 196 | if let grid = beatBoundaries { return bar * 4 >= grid.count - 1 ? duration : min(duration, grid[bar * 4]) } |
| 197 | return min(duration, start + Double(bars) * 240 / bpm) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | struct VisionCommonSettings { |
| 202 | var bars: Int? = nil |
| 203 | var chaos: Float? = nil |
| 204 | var zoomSpeed: Float? = nil |
| 205 | var motionSpeed: Float? = nil |
| 206 | var strobeEnabled: Bool? = nil |
| 207 | var strobeSensitivity: Float? = nil |
| 208 | var strobeStrength: Float? = nil |
| 209 | var isValid: Bool { |
| 210 | (bars.map { (1...64).contains($0) } ?? true) |
| 211 | && (chaos.map { $0.isFinite && (0...1).contains($0) } ?? true) |
| 212 | && ([zoomSpeed, motionSpeed].allSatisfy { $0.map { $0.isFinite && (0...3).contains($0) } ?? true }) |
| 213 | && (strobeSensitivity.map { $0.isFinite && (0.25...4).contains($0) } ?? true) |
| 214 | && (strobeStrength.map { $0.isFinite && (0.1...1).contains($0) } ?? true) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | extension UTType { |
| 219 | static let pulseVision = UTType(exportedAs: "com.protomota.pulsecanvas.vision-project", conformingTo: .json) |
| 220 | static let pulseCanvas = UTType(exportedAs: "com.protomota.pulsecanvas.project", conformingTo: .json) |
| 221 | } |
| 222 | |
| 223 | struct VisionProjectDocument: FileDocument { |
| 224 | static var readableContentTypes: [UTType] { [.pulseCanvas, .pulseVision, .json] } |
| 225 | var data: Data |
| 226 | init(data: Data) { self.data = data } |
| 227 | init(configuration: ReadConfiguration) throws { |
| 228 | guard let data = configuration.file.regularFileContents, data.count < 4_000_000 else { throw CocoaError(.fileReadCorruptFile) } |
| 229 | self.data = data |
| 230 | } |
| 231 | func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { |
| 232 | FileWrapper(regularFileWithContents: data) |
| 233 | } |
| 234 | } |