mlove-au / Metronome

A web-worker-based ticker for applications that require tempo-based routines.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Metronome

A web-worker-based ticker for applications that require tempo-based routines (falls back to using setInterval when web worker is not available).

Installation

<script src="metronome-worker.js"></script>
<script src="metronome.js"></script>

or...

<script src="metronome_compact.js"></script>

Usage

var metronome = new Metronome({
	debug: true, // See metronome logs in the browser console (default is false).
	tempo: 120,
	subticks: 4, // How each tick is to be subdivided
	callback: function () {
		console.log('Tick!'); // This will be invoked on each subtick.
	}
});
metronome.start();
setTimeout(function () {
	metronome.pause();
	setTimeout(function () {
		metronome.start();
		setTimeout(function () {
			metronome.removeCallbacks().stop();
		}, 2000);
	}, 2000);
}, 2000);

Most methods can be chained except for metronome.getCurrentTicks() (also metronome.tempo() and metronome.subticks() when no arguments are passed).

var metronome = new Metronome(), current_ticks;
function callback() {
	console.log('Tick!'); // This will be invoked on each subtick.
}
current_ticks = metronome
	.tempo(120)
	.subticks(4)
	.addCallback(callback)
	.start()
	.getCurrentTicks();
console.log('Current number of ticks: ' + current_ticks);
setTimeout(function () {
	metronome.removeCallback(callback).stop();
}, 2000);

Query existing settings.

var metronome = new Metronome({
	tempo: 120,
	subticks: 4
});
console.log('TEMPO: ' + metronome.tempo());
console.log('SUBTICKS: ' + metronome.subticks());

About

A web-worker-based ticker for applications that require tempo-based routines.

License:Apache License 2.0


Languages

Language:JavaScript 74.8%Language:HTML 25.2%