xxxxxxxxxx
52
// Add a snare drum sound
// Exercise: Play a kick/snare pattern
// SOUNDS
// Create a Players object and load the "kick.mp3" and "snare.mp3" files
let kit = new Tone.Players({
"kick": "samples/505/kick.mp3",
"snare":"samples/505/snare.mp3"
});
// Connect the player output to the computer's audio output
kit.toMaster();
Tone.Transport.bpm.value = 120;
// Create a loop: call playBeat every quarter note
Tone.Transport.scheduleRepeat(playBeat, "4n");
// Audio playback loop
function playBeat(time) {
// Tone's position gives us a string:
// bar:beat:sixteenth
// Slice the string by ":" and get the number in the second position (the beat)
let beat = Tone.Transport.position.split(":")[1];
console.log(beat);
let kick = kit.get("kick");
if(beat == 0){
kick.volume.value = 0;
}
else{
kick.volume.value = -10;
}
kick.start(time);
}
// Once all audio files have been loaded, start the Tone playhead
Tone.Buffer.on('load', start);
function start() {
Tone.Transport.start();
}
function setup() {
}
function draw() {
}