Skip to content

Instantly share code, notes, and snippets.

@trinary
Created September 14, 2012 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trinary/3724631 to your computer and use it in GitHub Desktop.
Save trinary/3724631 to your computer and use it in GitHub Desktop.
a teeny drum machine.
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