Last active
December 19, 2015 18:18
-
-
Save paigeruten/5997423 to your computer and use it in GitHub Desktop.
play an audio file at a particular speed
This file contains 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
#!/usr/bin/env ruby | |
# rit.rb - play an audio file at a particular speed | |
# | |
# usage: ruby rit.rb <speed> <path_to_audio_file> | |
# | |
# <speed> is a float which acts as a multiplier. 1.0 is normal speed, 2.0 | |
# is double speed, 0.5 is half speed, and so on. | |
# | |
# put 0 for <speed> for a fun easter egg. | |
# | |
# dependencies are mplayer and the open4 gem. | |
require "open4" | |
if ARGV.length != 2 | |
puts "Usage: #$0 <speed> <path_to_audio_file>" | |
exit | |
end | |
speed, path = *ARGV | |
speed = speed.to_f | |
raise "file '#{path}' doesn't exist" unless File.exists? path | |
cmd = ["mplayer", "-slave", "-quiet", path] | |
pid, stdin, stdout, stderr = Open4.popen4(*cmd) | |
begin | |
line = stdout.gets | |
raise "file '#{path}' isn't a valid audio file" if line.nil? | |
end until line.chomp == "Starting playback..." | |
Thread.new { Process.wait(pid); exit } | |
if speed == 0.0 | |
speed = 1.0 | |
loop do | |
stdin.puts "speed_set #{speed}" | |
speed += (rand(3) - 1) / 100.0 | |
sleep 0.5 | |
end | |
else | |
stdin.puts "speed_set #{speed}" | |
loop { sleep 0.5 } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment