Class: AlsaRawMIDI::Input

Inherits:
Object
  • Object
show all
Includes:
Device
Defined in:
lib/alsa-rawmidi/input.rb

Overview

Input device class

Constant Summary collapse

BufferSize =
256

Instance Attribute Summary collapse

Attributes included from Device

#enabled, #id, #name, #subname, #system_id, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Device

all_by_type, #initialize

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



14
15
16
# File 'lib/alsa-rawmidi/input.rb', line 14

def buffer
  @buffer
end

Class Method Details

.allObject



89
90
91
# File 'lib/alsa-rawmidi/input.rb', line 89

def self.all
  Device.all_by_type[:input]
end

.clearObject



98
99
100
101
# File 'lib/alsa-rawmidi/input.rb', line 98

def @buffer.clear          
  super
  @pointer = 0
end

.firstObject



81
82
83
# File 'lib/alsa-rawmidi/input.rb', line 81

def self.first
  Device.first(:input)	
end

.lastObject



85
86
87
# File 'lib/alsa-rawmidi/input.rb', line 85

def self.last
  Device.last(:input)	
end

Instance Method Details

#closeObject

close this input



74
75
76
77
78
79
# File 'lib/alsa-rawmidi/input.rb', line 74

def close
  Thread.kill(@listener)
  Map.snd_rawmidi_drain(@handle)
  Map.snd_rawmidi_close(@handle)
  @enabled = false
end

#enable(options = {}, &block) ⇒ Object Also known as: open, start

enable this the input for use; can be passed a block



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/alsa-rawmidi/input.rb', line 52

def enable(options = {}, &block)
  handle_ptr = FFI::MemoryPointer.new(FFI.type_size(:int))
  Map.snd_rawmidi_open(handle_ptr, nil, @system_id, Map::Constants[:SND_RAWMIDI_NONBLOCK])
  @handle = handle_ptr.read_int
  @enabled = true
  @start_time = Time.now.to_f
  initialize_buffer
  spawn_listener!
  unless block.nil?
    begin
      yield(self)
    ensure
      close
    end
  else
    self
  end
end

#getsObject Also known as: read

returns 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



27
28
29
30
31
32
33
# File 'lib/alsa-rawmidi/input.rb', line 27

def gets
  until queued_messages?
  end
  msgs = queued_messages
  @pointer = @buffer.length
  msgs
end

#gets_sObject Also known as: gets_bytestr, gets_hex

same as gets but returns message data as string of hex digits as such:

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


43
44
45
46
47
# File 'lib/alsa-rawmidi/input.rb', line 43

def gets_s
  msgs = gets
  msgs.each { |m| m[:data] = numeric_bytes_to_hex_string(m[:data]) }
  msgs
end