admin管理员组文章数量:1403180
I have a site with a js visual metronome (like a conductor) that can be triggered via a websocket on multiple machines. How can I make the start of the metronomes as sync as possible?
Thanks.
I have a site with a js visual metronome (like a conductor) that can be triggered via a websocket on multiple machines. How can I make the start of the metronomes as sync as possible?
Thanks.
Share Improve this question edited Dec 23, 2012 at 1:58 Kieran Klaassen asked Dec 23, 2012 at 1:31 Kieran KlaassenKieran Klaassen 2,2124 gold badges22 silver badges25 bronze badges 8- What specifically are you trying to synchronize? How accurate do you need this to be? You tagged this with audio... depending on what you are trying to do, you won't be able to get these remotely close enough. – Brad Commented Dec 23, 2012 at 1:35
- I try it to be as tight as possible. It is a metronome for mucisians. So around 100 ms is perfect. – Kieran Klaassen Commented Dec 23, 2012 at 1:38
- As a musician, I'd suggest that any jitter above 10ms or 20ms will be very noticeable. That's going to be very hard to do over the web. Can you elaborate a bit more? Do you just need to ensure the same tempo on each side with a similar (~100ms?) starting point? – Brad Commented Dec 23, 2012 at 1:41
- Yes, the metronome has to be musical sync. The players get a score from the puter and just need a time indication that works sort of in sync. th emore sync the better. I dont run audio from the browser. – Kieran Klaassen Commented Dec 23, 2012 at 1:43
- Google has some way of doing it at jamwithchrome.. There is a latency set and logged in the console. – Kieran Klaassen Commented Dec 23, 2012 at 1:45
3 Answers
Reset to default 5What I would do is measure latency, and pensate accordingly.
For each connection to the server, the server should regularly send ping messages (not a real ICMP ping... just some message over your established websocket channel), and measure the amount of time it takes to get a response back from the client. Divide this latency by 2, and you have a decent guess as to the time it takes for your messages to get from the server to the client.
Once you know this, you can estimate the timing difference between the two clients, and adjust accordingly.
Note that this isn't perfect by any means. I would maintain your metronome clock entirely client-side, and only use the munication with the server to adjust. This will give smoother performance for the user.
This is a clock recovery problem.
The problem you will face is that wall-time is likely to be different on each system, and furthermore, will tend to drift apart over the longer term. In addition to this, audio word-clock is not driven from wall-time either and will have a drift of its own from it. This means you have no chance of achieving sample or phase accuracy. Both IEEE1394 audio streaming and the MPEG Transport stream layers pull this trick off by sending a time-stamp embedded with bit-accuracy into the data stream. You clearly don't get this luxury with the bination of Ethernet, a network stack, The Nagel algorithm and any transmit queue in webSockets.
10s to 100ms might be more realistic.
You might like to look at the techniques the Network Time Protocol uses to solve an essentially similar problem.
Get the time from all the clients (UTC). Find client's offset. Schedule a start time according to each client offset.
本文标签: nodejsHow can I sync two javascript timers via websocketsStack Overflow
版权声明:本文标题:node.js - How can I sync two javascript timers via websockets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744365277a2602745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论