Class: MIDIator::Interface

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

Checks to see if the currently-loaded driver knows how to do method and passes the message on if so. Raises an exception (as normal) if not.

Raises:

  • (NoMethodError)


109
110
111
112
113
114
# File 'lib/midiator/interface.rb', line 109

def method_missing( method, *args )
  raise NoMethodError, "Neither MIDIator::Interface nor #{@driver.class} " +
    "has a '#{method}' method." unless @driver.respond_to? method

  return @driver.send( method, *args )
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



25
26
27
# File 'lib/midiator/interface.rb', line 25

def driver
  @driver
end

Instance Method Details

#autodetect_driverObject

Automatically select a driver to use



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/midiator/interface.rb', line 28

def autodetect_driver
  driver = case Platform::IMPL
  when :macosx
    :core_midi
  when :mswin, :cygwin
    :winmm
  when :linux
    :alsa
  else
    if defined?( Java ) && Java::java.lang.System.get_property('os.name') == 'Mac OS X'
      :mmj
    else
      raise "No driver is available."
    end
  end
  
  self.use(driver)
end

#play(note, duration = 0.1, channel = 0, velocity = 100) ⇒ Object

A little shortcut method for playing the given note for the specified duration. If note is an array, all notes in it are played as a chord.



87
88
89
90
91
92
93
94
95
# File 'lib/midiator/interface.rb', line 87

def play( note, duration = 0.1, channel = 0, velocity = 100 )
  [note].flatten.each do |n|
    @driver.note_on( n, channel, velocity )
  end
  sleep duration
  [note].flatten.each do |n|
    @driver.note_off( n, channel, velocity )
  end 
end

#rest(duration = 0.1) ⇒ Object

Does nothing for duration seconds.



99
100
101
# File 'lib/midiator/interface.rb', line 99

def rest( duration = 0.1 )
  sleep duration
end

#use(driver_name) ⇒ Object

Attempts to load the MIDI system driver called driver_name.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/midiator/interface.rb', line 49

def use( driver_name )
  driver_path = "midiator/drivers/#{driver_name.to_s}"

  begin
    require driver_path
  rescue LoadError => e
    raise LoadError,
      "Could not load driver '#{driver_name}'."
  end

  # Fix two side-effects of the camelization process... first, change
  # instances of Midi to MIDI.  This fixes the acronym form but doesn't
  # change, for instance, 'timidity'.
  #
  # Second, the require path is midiator/drivers/foo, but the module
  # name is Driver singular, so fix that.
  driver_class = driver_path.camelize.
    gsub( /Midi/, 'MIDI' ).
    sub( /::Drivers::/, '::Driver::')

  # special case for the ALSA driver
  driver_class.sub!( /Alsa/, 'ALSA' )

  # special case for the WinMM driver
  driver_class.sub!( /Winmm/, 'WinMM' )

  # special case for the DLSSynth driver
  driver_class.sub!( /Dls/, 'DLS' )

  # this little trick stolen from ActiveSupport.  It looks for a top-
  # level module with the given name.
  @driver = Object.module_eval( "::#{driver_class}" ).new
end