Online music jam/Global Metronome/Mathematical Aspects

Workflow to create an Global Metronome
The following steps are based on basic mathematical principles and show the underlying arithmetic. Basically a global metronom refers to a global zero of time (not the Big Bang - it is January 1st 1970, 0:00am). We assume that all metronoms start with the first beat on the January 1st 1970 and dependent on the beats per minute and the beats per measure a global metronom has the same reference point in time (see https://niebert.github.io/globalmetronome for testing).

Miliseconds, Seconds, Minutes and Beats Per Minute
At first we define global time stamps for the beats with a global zero as time stamp:
 * (Global Zero Time Stamp) Define a initial joined time stamp $$t_0=0$$ is in general a number, representing the number of milliseconds since midnight January 1, 1970. This is the global reference point.
 * (Beats Per Minute - BPM) The bpm beats per minutes define the beat. First of all we calculate the milliseconds between to beats. It is important to keep the duration in milliseconds as integer values, so we round all calculations from float to integer values. Lets do the basic calculations.
 * (Second) 1000 milliseconds $$=$$ 1 second
 * (Minute) 60 seconds $$=$$ 1 minute $$=$$ 60000 milliseconds
 * (Beats Per Minute - BPM) if the tempo is defined by 120bpm then a single beat has the duration of $$\frac{60000}{120}=500$$ milliseconds. Let us perform another calculation with 111bpm an then the duration of a single beat is $$\frac{60000}{120}\approx 540.540540...$$. We will round the value always to an integer value, i.e. with 111bpm we use as beat duration 541 milliseconds.


 * (Global Beat Time) we want to have a global time stamp of beat, so we start the metronome theoretically at all location at $$t_0=0$$ and the first beat (independent from the defined bpm value) will alway be at $$t_0=0$$. The next beats for 120bpm will be at $$t_1=500$$, $$t_2=1000$$, $$t_3=1500$$, .... The next beats for 111bpm will be at $$t_1=541$$, $$t_2=1082$$, $$t_3=1623$$, ....

Current Time in Milliseconds
Even in a browser you can determine the current time in milliseconds e.g. with Javascript var n = Date.now; On the 3rd of June 2021 the value of $$n$$ was $$now=1622727306012$$. For 120bpm you could add 500 milliseconds is the duration of the beat and we could add 500 until we exceed the current timestamp. So the next global beat would be at $$t_k=1622727306500$$, $$t_{k+1}=1622727307000$$, ...

For the beat duration of 500 milliseconds we could easily determine the next beat by looking at the last 3 digits in the number $$now=1622727306012$$ (i.e. "...012" and the next beat will be at "...500", "...000", "...500", ...

For other beat durations in milliseconds we cannot use the last 3 digits approach. We need an efficient iteration.

Efficient Iteration
Of course the iteration with the basic beat duration provides mathematically the correct time stamp for the next beat. Nevertheless looking at the correct mathematical approach from the computer science angle then the iteration is time consuming. As a consequence we would like to perform the iteration with larger time steps to calculate the next beat and also determine the next "1" in a measure/bar. We will calculate the timesteps in milliseconds in integer values. The following calculations are performed for "111bpm" and a beat duration of 541 milliseconds per beat.

Meter of the Measure/Bar
We also have to consider the meter in which the online Jam is performed because a metronome could have an accent on the 1 and 2,3,4 in a measure does not have an accent in the measure. The meter is e.g. 3 for a waltz and e.g. 5 for the Jazz standard "Take Five". Now we calculate in meter steps to identify also the next "1" in the measure/bar,
 * The next 1 in measure/bar $$541 \cdot 4 = 2164 $$ milliseconds for 4 beats, measure duration is $$2164 $$ milliseconds.

Now we add to $$t_0=0$$ an integer value of the type $$ 10^m \cdot 2164 $$ until we exceed the current time stamp $$now=1622727306012$$. $$ without exceeding $$now=1622727306012$$ $$ without exceeding $$now=1622727306012$$ $$ without exceeding $$now=1622727306012$$ $$ without exceeding $$now=1622727306012$$ $$ without exceeding $$now=1622727306012$$ $$ without exceeding $$now=1622727306012$$
 * we can add $$7 \cdot 2164\cdot 10^8 = 1514800000000 $$ without exceeding $$now=1622727306012$$
 * we can add $$4 \cdot 2164\cdot 10^7 + 1514800000000 = 1601360000000
 * we can add $$9 \cdot 2164\cdot 10^6 + 1601360000000 = 1620836000000$$ without exceeding $$now=1622727306012$$
 * we can add $$8 \cdot 2164\cdot 10^5 + 1620836000000 = 1622567200000$$ without exceeding $$now=1622727306012$$
 * we can add $$7 \cdot 2164\cdot 10^4 + 1622567200000 = 1622718680000
 * we can add $$3 \cdot 2164\cdot 10^3 + 1622718680000 = 1622725172000
 * we can add $$9 \cdot 2164\cdot 10^2 + 1622725172000 = 1622727119600
 * we can add $$8 \cdot 2164\cdot 10^1 + 1622727119600 = 1622727292720
 * we can add $$6 \cdot 2164 + 1622727292720 = 1622727305704

Now we add 2164 to 1622727305704 and we get with 1622727307868 the next global "1" in the next measure/bar after $$now=1622727306012$$.