Open source CapCut alternative.
TypeScript
feat(ios): the prompter draws as many passes as the screen holds
David Leeebad108519e
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swiftindex 21a34d5e..0ecb3c09 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift@@ -102,25 +102,57 @@ nonisolated public struct TeleprompterState: Equatable, Sendable {/// of the prompter's height.public static let leadShare = 0.5- /// Vertical offset of the script at `elapsed` seconds in. The script- /// starts halfway down the prompter and rises at the reading pace — its- /// own rendered height per read duration, so the words pass the reader at- /// the set words per minute, paragraph gaps included as natural pauses.+ /// Where the script sits at `elapsed` seconds in, and how many copies of+ /// it the screen needs to stay covered.///- /// The travel wraps at the script's height plus `gap`, which is the copy- /// the screen draws twice: the next pass comes up from below as the last- /// one leaves, so the loop never runs through an empty screen.- public func scrollOffset(+ /// The script starts halfway down the prompter and rises at the reading+ /// pace — its own rendered height per read duration, so the words pass the+ /// reader at the set words per minute, paragraph gaps included as natural+ /// pauses. `nudge` is the reader's own hand on the script; it moves with+ /// the pacing rather than fighting it.+ ///+ /// One pass is drawn every `textHeight + gap`, above the window as well as+ /// below it, and the offset it starts from is always within one pass of the+ /// top. However long the run goes and wherever the loop has reached, the+ /// screen is covered top to bottom: a reader never watches the words leave+ /// and nothing take their place.+ public func prompterPass(elapsed: TimeInterval,overlayHeight: Double,textHeight: Double,- gap: Double- ) -> Double {+ gap: Double,+ nudge: Double = 0+ ) -> PrompterPass {let lead = overlayHeight * Self.leadSharelet total = duration- guard total > 0, textHeight > 0 else { return lead }- let speed = textHeight / totallet cycle = textHeight + gap- return lead - (speed * elapsed).truncatingRemainder(dividingBy: cycle)+ guard total > 0, textHeight > 0, cycle > 0 else {+ return PrompterPass(offset: lead + nudge, copies: 1)+ }+ let travelled = textHeight / total * elapsed+ // The first copy's top, carried back up to within one pass of the+ // window's own top: the passes before it are drawn, so the words that+ // have already gone by are still there to run off the screen.+ let raw = lead - travelled + nudge+ let wrapped = raw.truncatingRemainder(dividingBy: cycle)+ let offset = wrapped > 0 ? wrapped - cycle : wrapped+ // Enough passes to reach the foot of the window from there, plus one+ // waiting below it so the next is always on its way up.+ let copies = Int(((overlayHeight - offset) / cycle).rounded(.up)) + 1+ return PrompterPass(offset: offset, copies: max(2, copies))+ }+}++/// One frame of the prompter's loop: where the first copy of the script sits+/// and how many copies follow it down the screen.+nonisolated public struct PrompterPass: Equatable, Sendable {+ /// The top of the first copy, in the prompter's own space.+ public var offset: Double+ /// Copies to draw, spaced a gap apart.+ public var copies: Int++ public init(offset: Double, copies: Int) {+ self.offset = offset+ self.copies = copies}}diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swiftindex 95f295a0..af0f1bfd 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift@@ -611,21 +611,29 @@ struct TeleprompterOverlay: View {let lines = script(room: geometry.size.width - Self.sidePadding * 2)TimelineView(.animation) { context inlet elapsed = elapsed(at: context.date)- // Two passes, one behind the other: the script runs without- // ever leaving the screen empty, in a take as in a preview.+ // As many passes as the screen holds, above it and below it:+ // the loop comes round with the words already in place, in a+ // take as in a preview.+ let pass = camera.teleprompter.prompterPass(+ elapsed: elapsed,+ overlayHeight: height,+ textHeight: textHeight,+ gap: gap,+ nudge: dragged + dragging+ )VStack(alignment: .leading, spacing: gap) {- scriptText(lines)- .onGeometryChange(for: Double.self, of: { $0.size.height }) { textHeight = $0 }- scriptText(lines)+ ForEach(0..<pass.copies, id: \.self) { copy in+ if copy == 0 {+ scriptText(lines)+ .onGeometryChange(for: Double.self, of: { $0.size.height }) {+ textHeight = $0+ }+ } else {+ scriptText(lines)+ }+ }}- .offset(- y: camera.teleprompter.scrollOffset(- elapsed: elapsed,- overlayHeight: height,- textHeight: textHeight,- gap: gap- ) + dragged + dragging- )+ .offset(y: pass.offset).padding(.horizontal, Self.sidePadding)}.frame(height: height, alignment: .top)diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swiftindex 747d39cc..1b38a233 100644--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift@@ -61,35 +61,87 @@ import Testing}@Suite struct TeleprompterTests {- @Test func scrollStartsLowAndMovesUp() {+ /// A prompter of `words` words read at 120 wpm.+ func prompter(words: Int) -> TeleprompterState {var state = TeleprompterState()state.settings.wordsPerMinute = 120- state.script = Array(repeating: "word", count: 120).joined(separator: " ")+ state.script = Array(repeating: "word", count: words).joined(separator: " ")+ return state+ }++ /// The lowest point the drawn copies reach without a break above it: the+ /// window is covered from its top down to here.+ func covered(_ pass: PrompterPass, textHeight: Double, gap: Double) -> Double {+ var reach = pass.offset <= 0 ? pass.offset + textHeight : 0+ for copy in 1..<max(1, pass.copies) {+ let top = pass.offset + Double(copy) * (textHeight + gap)+ // The gap is breathing room the reader expects; what must never+ // happen is a copy starting below where the drawn ones ran out.+ if top > reach + gap + 0.000_001 { break }+ reach = max(reach, top + textHeight)+ }+ return reach+ }++ @Test func theScriptStartsLowAndRisesAtTheReadingPace() {+ let state = prompter(words: 120)// 120 words at 120 wpm = 60s, so 420pt of script passes in a minute.- // The script starts halfway down the prompter and rises at that pace.#expect(state.duration == 60)- let start = state.scrollOffset(elapsed: 0, overlayHeight: 300, textHeight: 420, gap: 100)- #expect(start == 150)- let mid = state.scrollOffset(elapsed: 30, overlayHeight: 300, textHeight: 420, gap: 100)- #expect(mid == 150 - 210)- let end = state.scrollOffset(elapsed: 60, overlayHeight: 300, textHeight: 420, gap: 100)- #expect(end == 150 - 420)+ let start = state.prompterPass(elapsed: 0, overlayHeight: 300, textHeight: 420, gap: 100)+ #expect(start.offset == 150 - 520) // one pass up, so the words above are drawn too+ let mid = state.prompterPass(elapsed: 30, overlayHeight: 300, textHeight: 420, gap: 100)+ #expect(mid.offset == 150 - 210)+ // A pass on from the start sits exactly where the start did.+ let cycle = 520.0 / 7 // 7pt a second+ let round = state.prompterPass(elapsed: cycle, overlayHeight: 300, textHeight: 420, gap: 100)+ #expect(abs(round.offset - start.offset) < 0.000_001)+ }++ @Test func theWindowIsCoveredAtEveryMomentOfTheLoop() {+ // A long script and a short one, on a tall window and a short one:+ // whatever the shape, the words reach the foot of the screen.+ for words in [8, 40, 400] {+ for overlayHeight in [400.0, 900.0] {+ let state = prompter(words: words)+ let textHeight = Double(words) * 3.5+ let gap = overlayHeight * 0.3+ let cycle = (textHeight + gap) / (textHeight / state.duration)+ // Three loops, sampled finely enough to catch a single frame.+ for step in 0...600 {+ let elapsed = cycle * 3 * Double(step) / 600+ let pass = state.prompterPass(+ elapsed: elapsed,+ overlayHeight: overlayHeight,+ textHeight: textHeight,+ gap: gap+ )+ #expect(pass.offset <= 0.000_001)+ #expect(pass.offset > -(textHeight + gap) - 0.000_001)+ #expect(covered(pass, textHeight: textHeight, gap: gap) >= overlayHeight)+ }+ }+ }}- @Test func scrollWrapsAtTheGapWithoutAnEmptyScreen() {- var state = TeleprompterState()- state.settings.wordsPerMinute = 120- state.script = Array(repeating: "word", count: 120).joined(separator: " ")- // 7pt a second: the second copy of the script reaches the first one's- // starting place exactly when the travel wraps.- let cycle = (420.0 + 100.0) / 7- let wrapped = state.scrollOffset(elapsed: cycle, overlayHeight: 300, textHeight: 420, gap: 100)- #expect(abs(wrapped - 150) < 0.000_001)+ @Test func aReadersNudgeMovesTheWordsAndKeepsTheScreenCovered() {+ let state = prompter(words: 40)+ for nudge in [-800.0, -120.0, 0.0, 120.0, 800.0] {+ let pass = state.prompterPass(+ elapsed: 12,+ overlayHeight: 800,+ textHeight: 140,+ gap: 240,+ nudge: nudge+ )+ #expect(covered(pass, textHeight: 140, gap: 240) >= 800)+ }}@Test func emptyScriptHoldsAtLead() {let state = TeleprompterState()- #expect(state.scrollOffset(elapsed: 5, overlayHeight: 300, textHeight: 0, gap: 100) == 150)+ let pass = state.prompterPass(elapsed: 5, overlayHeight: 300, textHeight: 0, gap: 100)+ #expect(pass.offset == 150)+ #expect(pass.copies == 1)}@Test func hasScriptIgnoresWhitespace() {