public
Description: A customizable Ruby-based MIDI drum controller for the Wii remote and nunchuk on OS X
Homepage: http://studiobleep.com
Clone URL: git://github.com/jmileham/drumchuk.git
drumchuk / midi_interface.rb
100644 42 lines (35 sloc) 0.902 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'rubygems'
require 'midiator'
require 'midi_in'
 
module DrumChuk
  class MidiInterface
    MIDI_CHANNEL = 9
 
    def initialize
      # Grabs the first in available.
      @midi_in = MidiIn.new
      @midi_in.scan
      @midi_in.link(0)
 
      # Grabs the first out available.
      @midi_out = MIDIator::Interface.new
      @midi_out.autodetect_driver
 
      @listeners = []
    end
 
    def register_listener(l)
      @listeners << l
    end
 
    def run_triggers (params={})
      @midi_in.capture do |data|
        data.each do |d|
          @listeners.each { |l| l.process(d) }
        end
        if params[:if] and params[:if].respond_to?(:call)
          @listeners.each { |l| l.trigger(self) } if params[:if].call
        end
        @listeners.each { |l| l.update }
      end
    end
 
    def play(note,velocity)
      @midi_out.play(note, 0, MIDI_CHANNEL, velocity)
    end
  end
end