Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[osc] mysterious clock drifting #119

Closed
felixroos opened this issue May 28, 2022 · 8 comments
Closed

[osc] mysterious clock drifting #119

felixroos opened this issue May 28, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@felixroos
Copy link
Collaborator

Under some unknown circumstances, the osc messages will arrive late in supercollider.
On lowend machines, this seems to happen more easily, though it could be a separate problem.
There seems to be some clock drifting going on, most likely that osc uses the system clock and strudel uses Date.now (on page load) + audio time since since page load. So far, I was only able to get late messages after a longer time of inactivity without refreshing the repl. after a refresh, the clocks are aligned again.

The clock drifting could be avoided if .osc would use performance.now instead of audio time.

@felixroos felixroos added the bug Something isn't working label May 28, 2022
@bwagner
Copy link
Contributor

bwagner commented Feb 21, 2023

This happened on a MacBook Pro with chip Apple M1 Max, memory 32 GB running Ventura 13.2.1. Drifting started after around 45 seconds.

@felixroos
Copy link
Collaborator Author

same drifting seems to happen with midi too. the midi clock fell ~15ms behind after roughly a minute. Midi uses WebMidi.time which is based on performance.now() and the scheduler uses AudioContext.currentTime, so these clocks are probably also not running in sync

@felixroos
Copy link
Collaborator Author

the scheduler is clock agnostic, so it might make sense to test what happens when it uses performance.now(). My guess is that midi will run perfectly but web audio will get out of sync..

@felixroos
Copy link
Collaborator Author

this could be the solution for the clock drift: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/getOutputTimestamp

@felixroos felixroos changed the title [osc] mysterious clock drifting [osc] [midi] mysterious clock drifting Jun 29, 2023
@felixroos felixroos changed the title [osc] [midi] mysterious clock drifting [osc] mysterious clock drifting Jun 30, 2023
@felixroos
Copy link
Collaborator Author

fixed with #666

@felixroos felixroos reopened this Oct 2, 2023
@felixroos
Copy link
Collaborator Author

this is not fixed.. it seems to be related to accumulating float rounding errors. this seems to only happen for certain cps numbers: .5 1 2 is fine, while .75 is not.. The problematic bit is most likely:

const begin = this.lastEnd;
this.lastBegin = begin;
const end = round(begin + duration * this.cps);
this.lastEnd = end;
const haps = this.pattern.queryArc(begin, end);

the calculation duration*this.cps does not have perfect precision, so each tick the error is accumulating, creating a larger and larger drift as time passes. this should be fixable with fractions

@yaxu
Copy link
Member

yaxu commented Oct 2, 2023

It doesn't necessarily need fractions, you can avoid accumulating rounding errors with a calculation like:

begin = lastend
end = time_of_cps_change + (number_of_frames_since_change * time_per_frame)

@jarmitage
Copy link
Contributor

workaround

const loopAx = register('loopAx', 
  (l, pat) => pat.loopAt(l).chop(l*8).legato(1).mul(speed(0.99))
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants