Class: JSound::Midi::Devices::JDevice

Inherits:
JSound::Midi::Device show all
Defined in:
lib/jsound/midi/devices/jdevice.rb

Overview

A Java-provided MIDI device (wraps javax.sound.midi.MidiDevice objects)

Direct Known Subclasses

InputDevice, OutputDevice

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JSound::Midi::Device

#<=, #>>, #message, #open?, #output, #output=

Methods included from TypeFromClassName

included

Constructor Details

#initialize(java_device, type) ⇒ JDevice

Returns a new instance of JDevice.



29
30
31
32
33
34
# File 'lib/jsound/midi/devices/jdevice.rb', line 29

def initialize(java_device, type)
  @java_device = java_device
  @info = @java_device.deviceInfo
  @description = @info.description
  @type = type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/jsound/midi/devices/jdevice.rb', line 72

def method_missing(sym, *args, &block)
  if @java_device.respond_to? sym
    @java_device.send(sym, *args, &block)
  else
    @info.send(sym, *args, &block)
  end
end

Instance Attribute Details

#descriptionString (readonly)

the description of this device

Returns:

  • (String)


13
14
15
# File 'lib/jsound/midi/devices/jdevice.rb', line 13

def description
  @description
end

#infoObject (readonly)

the javax.sound.midi.MidiDevice.Info object for this java device



9
10
11
# File 'lib/jsound/midi/devices/jdevice.rb', line 9

def info
  @info
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/jsound/midi/devices/jdevice.rb', line 15

def type
  @type
end

Class Method Details

.from_java(java_device) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jsound/midi/devices/jdevice.rb', line 36

def self.from_java(java_device)
  case java_device
  when javax.sound.midi.Sequencer then type = :sequencer
  when javax.sound.midi.Synthesizer then type = :synthesizer
  else
    # This assumes a single device cannot be both an input and an output:
    if java_device.maxTransmitters != 0
      return InputDevice.new(java_device)
    elsif java_device.maxReceivers != 0
      return OutputDevice.new(java_device)
    else
      type = :unknown
    end
  end
  new java_device, type
end

.open_devicesObject

Note:

All open devices will be automatically closed at_exit

All open JDevices



19
20
21
# File 'lib/jsound/midi/devices/jdevice.rb', line 19

def self.open_devices
  @@open_devices ||= []
end

Instance Method Details

#[](field) ⇒ Object



84
85
86
# File 'lib/jsound/midi/devices/jdevice.rb', line 84

def [](field)
  send field
end

#closeObject



64
65
66
67
68
69
70
# File 'lib/jsound/midi/devices/jdevice.rb', line 64

def close
  if @java_device.open?
    puts "Closing #{to_s}"
    @java_device.close
    self.class.open_devices.delete(self)
  end
end

#inspectObject



92
93
94
# File 'lib/jsound/midi/devices/jdevice.rb', line 92

def inspect
  to_s
end

#openObject

Note:

All open devices will be automatically closed at_exit



55
56
57
58
59
60
61
# File 'lib/jsound/midi/devices/jdevice.rb', line 55

def open
  unless @java_device.open?
    puts "Opening #{to_s}"
    @java_device.open
    self.class.open_devices << self
  end
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/jsound/midi/devices/jdevice.rb', line 80

def respond_to?(sym)
  super or @java_device.respond_to? sym or info.respond_to? sym
end

#to_sObject



88
89
90
# File 'lib/jsound/midi/devices/jdevice.rb', line 88

def to_s
  "#{super}: #{info.description}"
end