Sha256: 203bfc4ad1c48e55d413132a431317cc090d9c1994aeb58e34aba371699bbbd5

Contents?: true

Size: 938 Bytes

Versions: 2

Compression:

Stored size: 938 Bytes

Contents

require 'midiops/handler'
require 'unimidi'

module MIDIOps
  class Observer
    def initialize(worker_number = 1)
      @handler = Handler.new worker_number
    end

    def on event, &handler
      @handler.add event, handler
    end

    def listen input
      loop do
        input.gets.each do |events|
          events[:data].each do |ev|
            @handler.handle ev
          end
        end
      end
    end

    def listen_device device_name
      if !(dev = UniMIDI::Input.find_by_name device_name).nil?
        listen dev.open
      elsif !(dev = UniMIDI::Input.find { |input| input.name.include? device_name }).nil?
        listen dev.open
      else
        raise RuntimeError,
              "Specified device \"#{device_name}\" not available; available device(s): #{UniMIDI::Input.map{|i| '"' + i.name + '"' }.join(', ')}"
      end
    end

    def listen_first
      listen UniMIDI::Input.first.open
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
midiops-0.1.1 lib/midiops/observer.rb
midiops-0.1.0 lib/midiops/observer.rb