Tone.js/examples/stepSequencer.html
Yotam Mann 1e1842c28b step sequencer example
thanks @therewasaguy
2014-06-24 12:12:56 -04:00

132 lines
No EOL
3.3 KiB
HTML

<html>
<head>
<title>STEP SEQUENCER</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="../Tone.js"></script>
<link rel="stylesheet" type="text/css" href="exampleStyle.css">
</head>
<body>
<div>16 Step Sequencer. Each column is a 16th note and each row is a different note.</div>
<br>
<button>start</button>
<br><br>
<div id="checkboxes"></div>
<div id="output"></div>
<div id="loading">loading...</div>
<script type="text/javascript">
//which beat in the 16 beats
var stepNumber = 0;
//the number of loaded players
var loadedCount = 0;
//2d array of checkboxes
var checkboxes = [];
//array of radio buttons
var radios = [];
//all of the audio players
var players = [
new Tone.Player("./audio/casio/A1.mp3", playerLoaded),
new Tone.Player("./audio/casio/Cs2.mp3", playerLoaded),
new Tone.Player("./audio/casio/E2.mp3", playerLoaded),
new Tone.Player("./audio/casio/Fs2.mp3", playerLoaded)
];
//player onload callback
function playerLoaded(player){
//able to be retriggered before the file is done playing
player.retrigger = true;
player.toMaster();
loadedCount++;
if (loadedCount === players.length){
$("#loading").remove();
}
}
//make the checkboxes
//1 row for each player
//16 beats in each row
function makeCheckboxes(){
for (var row = 0; row < players.length; row++){
checkboxes[row] = [];
for (var beat = 0; beat < 16; beat++){
var checkbox = $("<input type='checkbox'>").appendTo("#checkboxes");
checkboxes[row].push(checkbox);
//randomly set some as checked initially
if (Math.random() < 0.25){
checkbox.prop("checked", true);
}
}
$("#checkboxes").append("<br>")
}
}
//make the radio buttons to show which beat we're on
function makeRadios(){
for (var beat = 0; beat < 16; beat++){
var radio = $("<input type='radio' name='step'>").appendTo("#checkboxes");
radios.push(radio);
}
$("#checkboxes").append("<br>");
}
function startTransport(){
if (Tone.Transport.state === "started"){
Tone.Transport.stop();
$("button").text("start");
$("#output").text("");
} else {
stepNumber = 0;
$("button").text("stop");
Tone.Transport.setInterval(onStep, "16n");
Tone.Transport.start();
}
}
//called every sixteenth note
function onStep(time){
//set the right radio
selectRadio(stepNumber);
//for each of the rows
for (var row = 0; row < players.length; row++){
//check each of the checkbox values
var box = checkboxes[row][stepNumber];
if (box.prop("checked")){
players[row].start(time);
}
}
stepNumber++;
stepNumber = stepNumber % 16;
}
function selectRadio(stepNumber){
//unselect the previous one
var prev = stepNumber - 1;
if (prev === -1){
prev = 15;
}
radios[prev].prop("checked", false);
//select the current one
radios[stepNumber].prop("checked", true);
}
//initialization
$(function(){
//the start button
$("button").click(startTransport);
//the interface
makeRadios();
makeCheckboxes();
//set the loop points
Tone.Transport.loop = true;
Tone.Transport.setLoopStart("0:0");
Tone.Transport.setLoopEnd("1:0");
Tone.Transport.setBpm(120);
});
</script>
</body>
</html>