Class: RtMidi::In

Inherits:
Object
  • Object
show all
Defined in:
lib/rtmidi/in.rb

Overview

Object for handling MIDI input. The Ruby representation of a RtMidiIn C++ object..

See Also:

Instance Method Summary collapse

Constructor Details

#initializeIn

Create a new instance.



9
10
11
12
# File 'lib/rtmidi/in.rb', line 9

def initialize
  @midiin = Interface::midiin_new()
  at_exit{ Interface::midiin_delete @midiin }
end

Instance Method Details

#close_portObject

Close the port, if opened.

See Also:



44
45
46
# File 'lib/rtmidi/in.rb', line 44

def close_port()
  Interface::midiin_close_port(@midiin)
end

#close_portsObject

Deprecated.


49
50
51
52
# File 'lib/rtmidi/in.rb', line 49

def close_ports
  puts "Deprecated, use #close_port instead"
  close_port
end

#open_port(index) ⇒ Object

Note:

only one port may be open per RtMidi::In instance

Open the MIDI input port at the given index.

See Also:



38
39
40
# File 'lib/rtmidi/in.rb', line 38

def open_port(index)
  Interface::midiin_open_port(@midiin, index)
end

#port_countObject

The number of MIDI input ports available.



15
16
17
# File 'lib/rtmidi/in.rb', line 15

def port_count      
  @port_count ||= Interface::midiin_port_count(@midiin)
end

#port_name(index) ⇒ Object

The name of the MIDI input port at the given index.

See Also:



21
22
23
# File 'lib/rtmidi/in.rb', line 21

def port_name(index)
  port_names[index]
end

#port_namesObject

The list of all MIDI input port names.

The index of a port in this list is the index to be passed to #port_name and #open_port.



31
32
33
# File 'lib/rtmidi/in.rb', line 31

def port_names
  @port_names ||= (0...port_count).map{|port_index| Interface::midiin_port_name(@midiin, port_index) }
end

#receive_channel_message {|Fixnum, Fixnum, Fixnum| ... } ⇒ Object

Note:

Only one receive callback may be active per RtMidi::In instance. Calling this method or #receive_message multiple times will replace the previous receive callback.

Setup a callback block to handle incoming MIDI channel messages from opened ports. The block should receive 3 bytes (Ruby Fixnum integers)

All messages are assumed to have 3 bytes. SysEx messages will be ignored. Some channel messages only have 2 bytes in which case the 3rd byte is 0.

This is more efficient than #receive_message and should be used when handling only MIDI channel messages.

Examples:

midiin.receive_channel_message do |byte1, byte2, byte3|
  puts "#{byte1} #{byte2} #{byte3}"
end

Yields:

  • (Fixnum, Fixnum, Fixnum)

    MIDI channel message as individual bytes

See Also:



75
76
77
78
79
80
# File 'lib/rtmidi/in.rb', line 75

def receive_channel_message &callback
  cancel_callback # otherwise first callback wins. I think last wins is more intuitive
  Interface::midiin_ignore_types(@midiin, true, true, true) # Ignore sysex, timing, or active sensing messages.
  Interface::midiin_set_callback(@midiin, callback)
  @callback_set = true
end

#receive_message {|*Fixnum| ... } ⇒ Object Also known as: set_callback

Note:

Only one receive callback may be active per RtMidi::In instance. Calling this method or #receive_message multiple times will replace the previous receive callback.

Setup a callback block to handle all incoming MIDI messages from opened ports. The block should receive a variable number of Ruby Fixnum integers. SysEx messages will be passed to the callback.

This is less efficient than #receive_channel_message and should only be used when handling SysEx, timing, or active sensing messages.

Examples:

midiin.receive_message do |*bytes|
  puts bytes.inspect
end

Yields:

  • (*Fixnum)

    MIDI channel message as individual bytes

See Also:



103
104
105
106
107
108
# File 'lib/rtmidi/in.rb', line 103

def receive_message &callback
  cancel_callback # otherwise first callback wins. I think last wins is more intuitive
  Interface::midiin_ignore_types(@midiin, false, false, false) #  Don't ignore sysex, timing, or active sensing messages.
  Interface::midiin_set_varargs_callback(@midiin, ->(bytes,size){ yield *bytes.read_array_of_uchar(size) })
  @callback_set = true
end

#stop_receivingObject Also known as: cancel_callback

Cancel the current receive callback, if any.



116
117
118
119
120
121
# File 'lib/rtmidi/in.rb', line 116

def stop_receiving
  if @callback_set
    Interface::midiin_cancel_callback(@midiin)
    @callback_set = false
  end
end