Created
June 8, 2017 21:56
-
-
Save MaxPleaner/1f9962a3e8fa4e0a418eba3b5532cd6f to your computer and use it in GitHub Desktop.
metronome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Tempo = 120.to_f | |
$Sounds = %w{ 2.mp3 1.mp3 1.mp3 1.mp3 } | |
$SoundsIdx = 0 | |
def rest_time_from_tempo | |
60.0 / $Tempo | |
end | |
$RestTime = rest_time_from_tempo | |
$Threads = { | |
playing_sounds: Thread.new do | |
loop do | |
Thread.new { `aplay #{$Sounds[$SoundsIdx]}` } | |
$SoundsIdx += 1 | |
$SoundsIdx = 0 if $SoundsIdx >= $Sounds.length | |
sleep $RestTime | |
end | |
end, | |
changing_tempo: Thread.new do | |
loop do | |
puts "tempo is #{$Tempo}" | |
puts "enter new tempo between 10 and 1000" | |
$Tempo = [[gets.chomp.to_f, 10.0].max, 1000.0].min | |
$RestTime = rest_time_from_tempo | |
end | |
end | |
} | |
$Threads.values.each &:join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment