class MIDIJRuby::Input

Input device class

Attributes

buffer[R]

Public Class Methods

all() click to toggle source

All inputs @return [Array<Input>]

# File lib/midi-jruby/input.rb, line 90
def self.all
  Device.all_by_type[:input]
end
first() click to toggle source

Select the first input @return [Input]

# File lib/midi-jruby/input.rb, line 78
def self.first
  Device.first(:input)  
end
last() click to toggle source

Select the last input @return [Input]

# File lib/midi-jruby/input.rb, line 84
def self.last
  Device.last(:input) 
end

Public Instance Methods

close() click to toggle source

Close this input @return [Boolean]

# File lib/midi-jruby/input.rb, line 70
def close
  @listener.kill
  API.close_input(@device)
  @enabled = false
end
enable(options = {}) { |self| ... } click to toggle source

Enable this the input for use; can be passed a block @param [Hash] options @param [Proc] block @return [Input] self

# File lib/midi-jruby/input.rb, line 50
def enable(options = {}, &block)
  unless @enabled
    initialize_input
    @enabled = true
  end
  if block_given?
    begin
      yield(self)
    ensure
      close
    end
  else
    self
  end
end
Also aliased as: open, start
gets() click to toggle source

An array of MIDI event hashes as such: [

{ :data => [144, 60, 100], :timestamp => 1024 },
{ :data => [128, 60, 100], :timestamp => 1100 },
{ :data => [144, 40, 120], :timestamp => 1200 }

]

The data is an array of numeric bytes The timestamp is the number of millis since this input was enabled

@return [Array<Hash>]

# File lib/midi-jruby/input.rb, line 22
def gets
  loop until queued_messages?
  messages = queued_messages
  @pointer = @buffer.length
  messages
end
Also aliased as: read
gets_bytestr()
Alias for: gets_s
gets_hex()
Alias for: gets_s
gets_s() click to toggle source

Same as Input#gets but returns message data as string of hex digits: [

{ :data => "904060", :timestamp => 904 },
{ :data => "804060", :timestamp => 1150 },
{ :data => "90447F", :timestamp => 1300 }

]

@return [Array<Hash>]

# File lib/midi-jruby/input.rb, line 38
def gets_s
  messages = gets
  messages.each { |message| message[:data] = numeric_bytes_to_hex_string(message[:data]) }
  messages
end
Also aliased as: gets_bytestr, gets_hex
open(options = {}, &block)
Alias for: enable
read()
Alias for: gets
start(options = {}, &block)
Alias for: enable

Private Instance Methods

add_to_buffer(messages) click to toggle source

@param [Array<Array<Fixnum>>] @return [Array<Array<Fixnum>>]

# File lib/midi-jruby/input.rb, line 168
def add_to_buffer(messages)
  @buffer += messages.compact.map do |message| 
    get_message_formatted(message, now)
  end
end
get_message_formatted(message, timestamp) click to toggle source

A hash of a MIDI message and corresponding timestamp @param [Array<Fixnum>] message @param [Fixnum] timestamp @return [Hash]

# File lib/midi-jruby/input.rb, line 128
def get_message_formatted(message, timestamp) 
  { 
    :data => message, 
    :timestamp => timestamp
  }
end
initialize_buffer() click to toggle source

Initialize the input buffer @return [Boolean]

# File lib/midi-jruby/input.rb, line 108
def initialize_buffer
  @buffer = []
  @pointer = 0
  def @buffer.clear
    @pointer = 0
    super        
  end
end
initialize_input() click to toggle source

Initialize the input components @return [Boolean]

# File lib/midi-jruby/input.rb, line 98
def initialize_input
  initialize_buffer
  API.enable_input(@device)
  @start_time = Time.now.to_f
  initialize_listener
  true
end
initialize_listener() click to toggle source

Launch a background thread that collects messages @return [Thread]

# File lib/midi-jruby/input.rb, line 149
def initialize_listener
  @listener = Thread.new do
    begin
      loop do        
        while (messages = API.read_input(@device)).empty?
          sleep(1.0/1000)
        end
        add_to_buffer(messages) unless messages.empty?
      end
    rescue Exception => exception
      Thread.main.raise(exception)
    end
  end
  @listener.abort_on_exception = true
  @listener
end
now() click to toggle source

Get a timestamp for the current time @return [Fixnum]

# File lib/midi-jruby/input.rb, line 119
def now
  now = Time.now.to_f - @start_time
  now * 1000
end
numeric_bytes_to_hex_string(bytes) click to toggle source

Convert an array of numeric bytes to a hex string (eg [0x90, 0x40, 0x40] -> “904040”) @param [Array<Fixnum>] bytes @return [String]

# File lib/midi-jruby/input.rb, line 177
def numeric_bytes_to_hex_string(bytes)
  string_bytes = bytes.map do |byte| 
    string = byte.to_s(16).upcase
    string = "0#{string}" if byte < 0x10
    string
  end
  string_bytes.join
end
queued_messages() click to toggle source

Messages in the buffer @return [Array<Array<Fixnum>>]

# File lib/midi-jruby/input.rb, line 137
def queued_messages
  @buffer.slice(@pointer, @buffer.length - @pointer)
end
queued_messages?() click to toggle source

Are there any new messages in the buffer? @return [Boolean]

# File lib/midi-jruby/input.rb, line 143
def queued_messages?
  @pointer < @buffer.length
end