PlaybackClock.kt
src/jvmMain/kotlin/afield/graphics/texture/video/PlaybackClock.kt · 81 lines
Defines PlaybackClock, the time source VideoTexture reads to choose a frame, with two implementations: SystemClock (a monotonic clock with play, pause and seek) and AudioClock (time taken from an AudioPlayer).
package afield.graphics.texture.video
interface PlaybackClock
Time source consumed by VideoTexture when selecting the frame for the current update.
interface PlaybackClock {
fun getCurrentTime(): Double
Get the current playback time in seconds. @return Current time in seconds from the start of playback
fun isPlaying(): Boolean
Check if the clock is currently active/playing. @return true if the clock is running
class SystemClock : PlaybackClock
Monotonic process clock with explicit play, pause, seek, and reset state.
VideoTexture creates this implementation internally when no external PlaybackClock is given.
class SystemClock : PlaybackClock {
private var startTime: Long = 0
private var pausedTime: Double = 0.0
private var isActive: Boolean = false
override fun getCurrentTime(): Double
override fun getCurrentTime(): Double {
if (!isActive) return pausedTime
val elapsed = (System.nanoTime() - startTime) / 1_000_000_000.0
return pausedTime + elapsed
}
override fun isPlaying(): Boolean = isActive
fun play()
fun play() {
if (!isActive) {
startTime = System.nanoTime()
isActive = true
}
}
fun pause()
fun pause() {
if (isActive) {
pausedTime = getCurrentTime()
isActive = false
}
}
fun seek(timestamp: Double)
fun seek(timestamp: Double) {
pausedTime = timestamp
if (isActive) {
startTime = System.nanoTime()
}
}
fun reset()
fun reset() {
startTime = System.nanoTime()
pausedTime = 0.0
isActive = false
}
}
class AudioClock(private val audioPlayer: AudioPlayer) : PlaybackClock
Delegates time and playing state to AudioPlayer's processed-sample and OpenAL-offset clock.
override fun getCurrentTime(): Double
override fun getCurrentTime(): Double {
return audioPlayer.getCurrentTime()
}
override fun isPlaying(): Boolean
override fun isPlaying(): Boolean {
return audioPlayer.isCurrentlyPlaying()
}
}