Class: AlsaRawMIDI::Output

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

Overview

Output device class

Instance Attribute Summary

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

Class Method Details

.allObject



79
80
81
# File 'lib/alsa-rawmidi/output.rb', line 79

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

.firstObject



71
72
73
# File 'lib/alsa-rawmidi/output.rb', line 71

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

.lastObject



75
76
77
# File 'lib/alsa-rawmidi/output.rb', line 75

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

Instance Method Details

#closeObject

close this output



13
14
15
16
17
# File 'lib/alsa-rawmidi/output.rb', line 13

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

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

enable this device; also takes a block



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

def enable(options = {}, &block)
  handle_ptr = FFI::MemoryPointer.new(FFI.type_size(:int))
  Map.snd_rawmidi_open(nil, handle_ptr, @system_id, 0)
  @handle = handle_ptr.read_int
  @enabled = true
  unless block.nil?
  	begin
  		yield(self)
  	ensure
  		close
  	end
  else
    self
  end
end

#puts(*a) ⇒ Object Also known as: write

send a MIDI message of an indeterminant type



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

def puts(*a)
 case a.first
    when Array then puts_bytes(*a.first)
	  when Numeric then puts_bytes(*a)
	  when String then puts_s(*a)
  end
end

#puts_bytes(*data) ⇒ Object

sends a MIDI messages comprised of Numeric bytes



32
33
34
35
36
37
38
39
40
# File 'lib/alsa-rawmidi/output.rb', line 32

def puts_bytes(*data)

  format = "C" * data.size
  bytes = FFI::MemoryPointer.new(data.size).put_bytes(0, data.pack(format))

  Map.snd_rawmidi_write(@handle, bytes.to_i, data.size)
  Map.snd_rawmidi_drain(@handle)
  
end

#puts_s(data) ⇒ Object Also known as: puts_bytestr, puts_hex

sends a MIDI message comprised of a String of hex digits



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

def puts_s(data)
  data = data.dup
 output = []
  until (str = data.slice!(0,2)).eql?("")
  	output << str.hex
  end
  puts_bytes(*output)
end