Module: Metronome::Command

Defined in:
lib/metronome.rb

Class Method Summary collapse

Class Method Details

.run(args) ⇒ Object

Parse option and start metronome



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/metronome.rb', line 10

def self.run( args )
  options = {}

  OptionParser.new do |opts|
    opts.on( '-t BPM', '--tempo BPM', /^[0-9]+/ ) do |bpm|
      options[:bpm] = bpm
    end
  end.parse!( args )

  start( options[:bpm] )
end

.start(bpm) ⇒ Object

Start metronome



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metronome.rb', line 23

def self.start( bpm )
  sound = Rubygame::Sound.load( File.expand_path("../../sounds/tap_short.wav", __FILE__) )

  t = Thread.new do
    puts 'press Ctrl+C to stop'
    # main loop
    loop do
      sound.play
      sleep( 60 / bpm.to_f ) 
    end
  end

  begin 
    t.join
  rescue Interrupt
    puts "\nmetronome off"
  end
end