Experimental Tempo Sync Calculator
One of my favorite music-production techniques is applying tempo-synchronized effects to musical phrases. A generic percussion pattern really perks up when you add a dotted-eighth-note echo. A subtle eighth- or quarter-note echo imparts a spaciousness to vocal lines that reverb would just blur. And a four-bar filter sweep adds ear-catching movement to a phrase.
You can hear some examples of tempo sync in my article “Sync and Grow Rich,” on O’Reilly’s Digital Audio site. Because I also enjoy messing around with JavaScript, I created a Web-based calculator for that article; it calculates the duration of a quarter-note at whatever tempo you input. (The magic formula is duration in milliseconds = 60,000 / tempo in bpm.)
As I was developing the calculator script, I noticed that it often spat out an annoyingly large number of digits to the right of the decimal point. Searching for JavaScript rounding techniques, I discovered that some people are converting numbers to characters and truncating them with string operations. That was the technique I used in the sync article:
// STRING METHOD FOR ROUNDING
var DivideIt = eval(6000000 / thebpm); // Quarter-note with 2 extra zeroes for rounding
var TheDuration = "" + Math.round(DivideIt); // Round, then convert to a string
var dec_point=TheDuration.length-2; // Shift decimal point two spaces left
var first_part=TheDuration.substring(0,dec_point); // Grab characters to left of decimal
var second_part=TheDuration.substring(dec_point,TheDuration.length); // Grab char to right
var result=first_part+"."+second_part; // Join L and R strings with a period between
var sixteenth = result - 0 // Convert string back to number
After publishing the article, I realized it would be useful if the calculator could display multiple note values at once. Deriving them was a simple matter of multiplying the basic quarter-note duration by different factors (0.5 for an eighth-note, 8 for two bars, etc.). But I wanted a more efficient way to do the rounding. On the handy Internet Related Technologies site, I came across a pure arithmetic rounding method, which I implemented as a function:
function roundIt(bignum,decplaces) {
roundNum = Math.round(bignum * Math.pow(10,decplaces))/Math.pow(10,decplaces);
return roundNum;
}
I then added a selection menu so I could specify the number of decimal places. For the single-note durations (expressed in milliseconds), I typically specify zero decimal places, whereas for the multibar durations (expressed in seconds), I’ll specify one to three.
Try it: Enter a tempo and click the button to see the duration at various note values.
To see the complete JavaScript with comments, view this page’s source code. I’m just a JavaScript enthusiast, not a trained programmer, so I’m sure there are better ways to do these calculations. (For instance, I haven’t been able to get the script to work in Netscape yet. Maybe it’s the doctype?) But the process of writing it gave me some insight into the math behind music, and provided a handy reference for those numerous times I want to set up a tempo-synced effect.
—David Battino
How can I improve this script or solve other musical problems with JavaScript?
Categories
AudioComments (2)
Read More Entries by David Battino.

The calculator should now be fixed.
NOTE: Due to a change in the way our blogging system handles JavaScript, the calculator above currently doesn’t run from this page. The code will work if you copy it to another page, though.