[sdiy] Beat timing calculations

Olivier Gillet ol.gillet at gmail.com
Wed Jul 27 10:41:11 CEST 2011


> 130 bpm= bps=60/130=0.46153846
>
> 1000ms per second * bps
> ms =461.53846  per beat division
>
> 16 beats=16*461.53846 =7,384.61536
> Milliseconds per loop

Assuming that each "loop" is for emitting (or not) a 16th step (4
ppqn), the duration of a step will be:

1 beat = 461.53846ms
1 beat = 4 16-th notes
461.53846 / 4 = 115.38ms per step

> but there probably has to be something to handle things that don't land on a proper digital boundary like

Why are you reasoning with ms in the first place? Not sure on which
platform you are working but:
- on desktop OS you have the possibility of timestamping every MIDI
message you emit - the OS does the timing with sub-ms accuracy.
- when generating audio you can use the sample rate as a time base.
- on a microcontroller, you'd rather have a timer fire an interrupt at
a subdivision of the clock (eg 16000000 / 256 = 62.5kHz for a chip
clocked at 16 MHz) and use this as a time basis. You'll get a better
resolution than any millisecond based delay or wait function provided
by some libraries, and you won't have to bother timing your code. At 4
ppqn (x0x style), a 62.5kHz base gives you a 0.02 BPM granularity. At
24 ppqn (enough for generating a MIDI clock), the 62.5kHz base gives a
granularity of about 0.1 BPM. You'd rather work with a 24ppqn base
since it makes adding MIDI sync easier. You can increase the
resolution by unevenly spacing clock ticks. Say the ideal number of
clocks ticks is 1400.5. You use a 1400 tick delay on odd steps and a
1401 tick delay on even steps. Not sure everybody is caring that much
about getting the exact BPM right at 0.00001% - many products I have
used would "drift" if you just started them together at 120BPM without
sync. If you want things to play in time you need to sync them to a
common clock, MIDI or other...

Olivier



More information about the Synth-diy mailing list