I’m doing a music synchronization code but in my imagination all devices works on the same clock. Is this not true?
When I execute this code on three devices (mi laptop, cellphone, and Android Tablet) the all go in different phases.
Is this how devices work with the clock?
Do yo know any better solution?
let minuto,
minutoEqual = true,
primera = false;
function setup() {
createCanvas(windowWidth, windowHeight);
minuto = minute();
textSize(50);
textAlign(CENTER);
}
function draw() {
background(255);
//PREVIA
if (minutoEqual){
let segundo = 60-second();
text(segundo,windowWidth/2,windowHeight/2)
minutoChange = minute();
if(minuto != minutoChange){
minutoEqual = false;
print('cambio')
primera = true;
}
}
//PRIMERA
if(primera){
text('comenzamos',windowWidth/2,windowHeight/2)
}
//SEGUNDA a
//SEGUNDA b
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
You cell phone probably has the most accurate clock because that’s necessary for it to work with the cell phone networks. Computers usually set their clocks using NTP servers (network time protocol) where they ping some official time-keeping computer on the internet. I don’t think it will be as accurate as the cell phone network, however. Windows or Macs should have some settings interface for making sure that you have that enabled.
Computers, including cell phones and tablets, try to be as fast as they can, but they do not make any guarantees about real-time accuracy. All of them, these days, are running full multi-tasking operating systems which means that the processor is constantly switching between different activities. When that happens, your Processing sketch is paused while the OS goes off to check in with the cell phone tower, or update your desktop clock, or check if you have new email, and so on. This means you can never guarantee that your sketch is running at any given second (or at least any given microsecond). So when the clock ticks over from 59 seconds to 0 seconds, one device is likely to see that happen just a bit before another one does simply because it was distracted by some other program at that moment.
Poke around in the clock settings on each device and see if you can find the NTP server settings. time.apple.com is used for Mac and iOS tablets or possibly your cell phone provider. time.windows.com for Windows. I don’t know what android tablets use. Maybe time.google.com? One would hope that those time servers are all putting out the same values, but who knows?
The next question is how recently has your device synced up with a server. On my android tablet, I can turn off “automatic date and time”, set the time to a wrong value, turn it back on and see it immediately update the time to a “correct” value for some definition of correct. Try doing that on each of your devices and maybe, if you’re lucky, they’ll all sync up to a very similar time value.