Class: MIDIEye::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/midi-eye/source.rb

Overview

Retrieves new messages from a unimidi input buffer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Source



16
17
18
19
20
# File 'lib/midi-eye/source.rb', line 16

def initialize(input)
  @parser = Nibbler.new
  @pointer = 0
  @device = input
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



6
7
8
# File 'lib/midi-eye/source.rb', line 6

def device
  @device
end

#pointerObject (readonly)

Returns the value of attribute pointer.



6
7
8
# File 'lib/midi-eye/source.rb', line 6

def pointer
  @pointer
end

Class Method Details

.compatible?(input) ⇒ Boolean

Whether the given object is a UniMIDI input



11
12
13
# File 'lib/midi-eye/source.rb', line 11

def self.compatible?(input)
  input.respond_to?(:gets) && input.respond_to?(:buffer)
end

Instance Method Details

#poll(&block) ⇒ Object

Grabs new messages from the input buffer



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/midi-eye/source.rb', line 23

def poll(&block)
  messages = @device.buffer.slice(@pointer, @device.buffer.length - @pointer)
  @pointer = @device.buffer.length
  messages.compact.each do |raw_message|
    parsed_messages = begin
      @parser.parse(raw_message[:data], :timestamp => raw_message[:timestamp])
    rescue
      nil
    end
    objects = [parsed_messages].flatten.compact
    yield(objects)
  end
end

#uses?(input) ⇒ Boolean

If this source was created from the given input



40
41
42
# File 'lib/midi-eye/source.rb', line 40

def uses?(input)
  @device == input
end