export class Metronome { constructor() { this.context = new (window.AudioContext || window.webkitAudioContext)(); this.loadBuffer(); } loadBuffer(url="pulse.ogg") { var request = new XMLHttpRequest(); request.open("GET", url, true); request.responseType = "arraybuffer"; request.onload = () => this.context.decodeAudioData(request.response, (buffer) => this.buffer = buffer); request.send(); } play(beats) { this.stop(); this.src = this.context.createBufferSource(); this.src.buffer = this.extendBuffer(beats, this.buffer); this.src.loop = true; this.src.connect(this.context.destination); this.src.start(); } stop() { ("src" in this)?this.src.stop():""; } extendBuffer(beats, buffer) { var total = 60/beats * buffer.sampleRate; var ext = this.context.createBuffer(buffer.numberOfChannels, total, buffer.sampleRate); for(let i=0; i