This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
metronome.rb
executable file
·77 lines (64 loc) · 1.88 KB
/
metronome.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
#!/usr/bin/env ruby
#
# A metronome that can run at any given tempo.
#
# == Synopsis
#
# $ ./metronome
# => starts a metronome running at 120bpm
#
# $ ./metronome 180
# => starts a metronome running at 180bpm
#
# == Authors
#
# * Ben Bleything <ben@bleything.net>
#
# == Copyright
#
# Copyright (c) 2008 Ben Bleything
#
# This code released under the terms of the MIT license.
#
########################################################################
### S A N I T Y C H E C K S
########################################################################
@tempo = 120.0
if input = ARGV[0]
begin
@tempo = Float( input )
rescue ArgumentError => e
$stderr.puts "'#{input}' is not a valid tempo.\n"
$stderr.puts "Please specify the tempo in beats per minute " +
"(bpm). Fractional values are allowed!"
exit 1
end
end
require 'rubygems'
########################################################################
### M I D I A T O R S E T U P
########################################################################
require 'midiator'
include MIDIator::Notes
@midi = MIDIator::Interface.new
@midi.autodetect_driver
@midi.instruct_user!
@midi.program_change 0, 115 # Wood block!
# trap interrupts to properly kill the timer
Signal.trap( "INT" ) { @timer.thread.exit! }
########################################################################
### T I M E R S E T U P
########################################################################
# 60 / tempo gives us the delay (in seconds) between each beat. Divide
# that again by 10 to give us our desired resolution.
@interval = 60.0 / @tempo
@timer = MIDIator::Timer.new( @interval / 10 )
def register_next_bang( time )
@timer.at( time ) do |yielded_time|
register_next_bang yielded_time + @interval
@midi.play MiddleC
end
end
puts "Starting metronome running at #{@tempo} bpm."
register_next_bang Time.now.to_f
@timer.thread.join