Created
September 14, 2012 20:37
-
-
Save trinary/3724631 to your computer and use it in GitHub Desktop.
a teeny drum machine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DrumMachine | |
constructor: (@midi) -> | |
@bpm = 120 | |
@playing = false | |
@currentStep = 0 | |
measure: -> | |
60000 / @bpm * 4 | |
beat: -> | |
60000 / @bpm / 4 | |
instruments: | |
kick: 36 | |
hihat: 42 | |
clap: 39 | |
snare: 40 | |
cowbell: 56 | |
crash: 49 | |
playLoop: (pattern) -> | |
@playing = true | |
setInterval(=> | |
thisBeat = pattern[@currentStep % 16] | |
if thisBeat.length > 0 | |
@playNote(inst) for inst in thisBeat | |
@currentStep += 1 | |
, @beat()) | |
stop: -> | |
@playing = false | |
playNote: (note) -> | |
@midi.sendMessage [144,@instruments[note], 100] | |
midi = require 'midi' | |
out = new midi.output | |
try | |
out.openPort 0 | |
catch error | |
out.openVirtualPort '' | |
dm = new DrumMachine out | |
console.log "Playing at #{dm.bpm} bpm, each beat is #{dm.beat()} ms" | |
myLoop = [['kick'],[],[],[], | |
['kick'],[],['hihat'],[], | |
['kick'],[],[],[], | |
['kick'],[],[],[]] | |
dm.playLoop(myLoop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment