Skip to content

Commit

Permalink
fix: osc animation should rely on monotonic time
Browse files Browse the repository at this point in the history
  • Loading branch information
mint-dewit committed Jan 25, 2023
1 parent e313198 commit 7989c9d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/timeline-state-resolver/src/integrations/osc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class OscDevice extends EventEmitter implements Device<OSCOptions, OscDev

this.transitions[command.path] = {
// push the tween
started: Date.now(), // todo - safe to use?
started: this.getMonotonicTime(),
...command,
}
this._oscSender({
Expand Down Expand Up @@ -208,9 +208,10 @@ export class OscDevice extends EventEmitter implements Device<OSCOptions, OscDev
this._oscClient.send(msg, address, port)
}
private runAnimation() {
const t = this.getMonotonicTime()
for (const addr in this.transitions) {
// delete old tweens
if (this.transitions[addr].started + this.transitions[addr].transition!.duration < Date.now()) {
if (this.transitions[addr].started + this.transitions[addr].transition!.duration < t) {
delete this.transitions[addr]
}
}
Expand All @@ -222,7 +223,7 @@ export class OscDevice extends EventEmitter implements Device<OSCOptions, OscDev
const easing = (easingType || {})[tween.transition!.direction]
if (easing) {
// scale time in range 0...1, then calculate progress in range 0..1
const deltaTime = Date.now() - tween.started
const deltaTime = t - tween.started
const progress = deltaTime / tween.transition!.duration
const fraction = easing(progress)
// calculate individual values:
Expand Down Expand Up @@ -267,4 +268,9 @@ export class OscDevice extends EventEmitter implements Device<OSCOptions, OscDev
this.transitionInterval = undefined
}
}

private getMonotonicTime() {
const hrTime = process.hrtime()
return hrTime[0] * 1000 + hrTime[1] / 1000000
}
}

0 comments on commit 7989c9d

Please sign in to comment.