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)


107
108
109
110
111
112
# File 'lib/midiator/interface.rb', line 107

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
# File 'lib/midiator/interface.rb', line 28

def autodetect_driver
  driver = case RUBY_PLATFORM
  when /darwin/
    :dls_synth
  when /cygwin/, /mingw/
    :winmm
  when /linux/
    :alsa
  when /java/
    :mmj if Java::java.lang.System.get_property('os.name') == 'Mac OS X'
  else
    raise "No driver is available."
  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.



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

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.



97
98
99
# File 'lib/midiator/interface.rb', line 97

def rest( duration = 0.1 )
  sleep duration
end

#use(driver_name) ⇒ Object

Attempts to load the MIDI system driver called driver_name.



47
48
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
# File 'lib/midiator/interface.rb', line 47

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