Module: MIDICommunications::Device::InstanceMethods

Included in:
Input, Output
Defined in:
lib/midi-communications/device.rb

Overview

Instance methods shared by both Input and Output instances.

Provides device lifecycle management (open, close) and access to device attributes (name, id, manufacturer, etc.).

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directionSymbol (readonly)

Returns the device direction (:input or :output).

Returns:

  • the device direction (:input or :output)



# File 'lib/midi-communications/device.rb', line 245


#display_nameString (readonly)

Returns the device display name.

Returns:

  • the device display name



# File 'lib/midi-communications/device.rb', line 263


#enabledBoolean (readonly)

Returns whether the device is currently open.

Returns:

  • whether the device is currently open



# File 'lib/midi-communications/device.rb', line 248


#idInteger (readonly)

Returns the device ID.

Returns:

  • the device ID



# File 'lib/midi-communications/device.rb', line 251


#manufacturerString (readonly)

Returns the device manufacturer name.

Returns:

  • the device manufacturer name



# File 'lib/midi-communications/device.rb', line 254


#modelString (readonly)

Returns the device model name.

Returns:

  • the device model name



# File 'lib/midi-communications/device.rb', line 257


#nameString (readonly)

Returns the device name.

Returns:

  • the device name



# File 'lib/midi-communications/device.rb', line 260


Instance Method Details

#close(*args) ⇒ Boolean

Closes the device.

Examples:

output.close

Parameters:

  • arguments passed to the underlying device

Returns:

  • true if the device was closed, false if already closed

API:

  • public



228
229
230
231
232
233
234
235
236
# File 'lib/midi-communications/device.rb', line 228

def close(*args)
  if @enabled
    @device.close(*args)
    @enabled = false
    true
  else
    false
  end
end

#closed?Boolean

Returns true if the device is closed (not enabled).

Returns:

  • true if device is closed

API:

  • public



241
242
243
# File 'lib/midi-communications/device.rb', line 241

def closed?
  !@enabled
end

#enabled?Boolean

Returns alias for #enabled.

Returns:



# File 'lib/midi-communications/device.rb', line 266


#initialize(device) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new device wrapper.

Parameters:

  • platform-specific device object

API:

  • private



177
178
179
180
181
182
# File 'lib/midi-communications/device.rb', line 177

def initialize(device)
  @device = device
  @enabled = false

  populate_from_device
end

#open(*args) {|device| ... } ⇒ Input, Output

Opens the device for use.

When a block is given, the device is automatically closed when the block exits. Otherwise, the device is closed at program exit.

Examples:

Open and close automatically with block

output.open do |o|
  o.puts(0x90, 60, 100)
end  # device closed here

Open manually (closed at program exit)

output.open
output.puts(0x90, 60, 100)

Parameters:

  • arguments passed to the underlying device

Yields:

  • (device)

    optional block to execute with the open device

Yield Parameters:

Returns:

  • self

API:

  • public



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/midi-communications/device.rb', line 202

def open(*args)
  unless @enabled
    @device.open(*args)
    @enabled = true
  end
  if block_given?
    begin
      yield(self)
    ensure
      close
    end
  else
    at_exit do
      close
    end
  end
  self
end

#typeSymbol

Returns alias for #direction.

Returns:



# File 'lib/midi-communications/device.rb', line 269