Class: AlsaRawMIDI::Input

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

Overview

Input device class

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

included, #initialize

Methods included from Device::ClassMethods

#all, #all_by_type, #first, #last

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



8
9
10
# File 'lib/alsa-rawmidi/input.rb', line 8

def buffer
  @buffer
end

Class Method Details

.allArray<Input>

All available inputs

Returns:



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

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

.clearObject



108
109
110
111
# File 'lib/alsa-rawmidi/input.rb', line 108

def @buffer.clear
  super
  @pointer = 0
end

.firstInput

The first input available

Returns:



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

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

.lastInput

The last input available

Returns:



91
92
93
# File 'lib/alsa-rawmidi/input.rb', line 91

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

Instance Method Details

#closeBoolean

Close this input

Returns:

  • (Boolean)


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

def close
  if @enabled
    Thread.kill(@listener)
    API::Device.close(@resource)
    @enabled = false
    true
  else
    false
  end
end

#enable(_options = {}) ⇒ Input Also known as: open, start

Enable this the input for use; yields

Parameters:

  • options (Hash)
  • block (Proc)

Returns:



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

def enable(_options = {})
  unless @enabled
    @start_time = Time.now.to_f
    @resource = API::Input.open(@system_id)
    @enabled = true
    initialize_buffer
    spawn_listener
  end
  if block_given?
    begin
      yield(self)
    ensure
      close
    end
  end
  self
end

#getsArray<Hash> Also known as: read

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

Returns:

  • (Array<Hash>)


22
23
24
25
26
27
# File 'lib/alsa-rawmidi/input.rb', line 22

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

#gets_sArray<Hash> Also known as: gets_bytestr, gets_hex

Like Input#gets but returns message data as string of hex digits as such:

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

Returns:

  • (Array<Hash>)


38
39
40
41
42
# File 'lib/alsa-rawmidi/input.rb', line 38

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