Branchwell

PulseCanvas / app / Sources / PulseCanvas / StudioTimeline.swift

1import Foundation
2
3/// Timing follows the beat grid; each card can request its own bar length.
4struct StudioSegment: Codable, Identifiable, Sendable {
5 let start: Double
6 let end: Double
7 var scene: Int
8 var bars: Int? = nil
9 var id: Double { start }
10}
11
12enum StudioTimeline {
13 static func sections(boundaries: [Double], bars: Int, scene: (_ index: Int, _ start: Double) -> Int) -> [StudioSegment] {
14 zip(boundaries, boundaries.dropFirst()).enumerated().map { index, pair in
15 StudioSegment(start: pair.0, end: pair.1, scene: scene(index, pair.0), bars: bars)
16 }
17 }
18
19 /// Each scene is dealt once. Favorites lead; unused library scenes fill the tail.
20 /// Return nil when uniqueness is impossible without changing the clip timing.
21 static func randomizedScenes(current: [Int], favorites: [Int], available: [Int], allowRepeats: Bool = false) -> [Int]? {
22 let availableSet = Set(available)
23 let preferred = Set(favorites).intersection(availableSet)
24 var choices = preferred.shuffled() + availableSet.subtracting(preferred).shuffled()
25 guard !choices.isEmpty else { return nil }
26 if allowRepeats && current.count > choices.count {
27 var result: [Int] = [], bag = [Int]()
28 for previous in current {
29 if bag.isEmpty { bag = choices.shuffled() }
30 let candidate = bag.firstIndex { $0 != result.last && $0 != previous }
31 ?? bag.firstIndex { $0 != result.last } ?? bag.startIndex
32 result.append(bag.remove(at: candidate))
33 }
34 return result
35 }
36 guard current.count <= choices.count else { return nil }
37 var result: [Int] = []
38 for (index, previous) in current.enumerated() {
39 let favoritesLeft = choices.prefix(while: { preferred.contains($0) }).count
40 // Reserve enough remaining slots to include every preferred scene.
41 let requiredFavorites = favoritesLeft >= current.count - index
42 let eligible = requiredFavorites ? 0..<favoritesLeft : choices.indices
43 let candidate = eligible.first(where: { choices[$0] != previous }) ?? eligible.lowerBound
44 result.append(choices.remove(at: candidate))
45 // With an exactly full pool the final slot may retain its old scene.
46 // Swap earlier assignments where both cards can still change.
47 if result[index] == previous,
48 let swap = (0..<index).first(where: { result[$0] != previous && current[$0] != previous }) {
49 result.swapAt(swap, index)
50 }
51 }
52 return result
53 }
54
55 static func index(at time: Double, in segments: [StudioSegment]) -> Int? {
56 guard !segments.isEmpty else { return nil }
57 var low = 0, high = segments.count
58 while low < high { let mid = (low + high) / 2; if segments[mid].start <= time { low = mid + 1 } else { high = mid } }
59 return max(0, min(segments.count - 1, low - 1))
60 }
61 static func valid(_ segments: [StudioSegment], sceneCount: Int) -> Bool {
62 guard segments.count <= 10_000 else { return false }
63 for (index, segment) in segments.enumerated() {
64 guard segment.start.isFinite, segment.end.isFinite, segment.start >= 0, segment.end > segment.start,
65 segment.bars.map({ $0 > 0 && $0 <= Int.max / 4 }) ?? true,
66 (0..<sceneCount).contains(segment.scene), index == 0 ? segment.start == 0 : abs(segments[index-1].end - segment.start) < 0.001 else { return false }
67 }
68 return true
69 }
70}