Skip to content

Instantly share code, notes, and snippets.

@anilgulecha
Created December 1, 2018 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anilgulecha/910f79267f512b0bc60cd4a0671043c5 to your computer and use it in GitHub Desktop.
Save anilgulecha/910f79267f512b0bc60cd4a0671043c5 to your computer and use it in GitHub Desktop.
THINK 320k mp3s are better than 96k mp3? TAKE THE BIT RATE CHALLENGE. On your commandline.
# THINK 320k mp3s are better than 96k mp3?
# TAKE THE BIT RATE CHALLENGE!
# Needs ruby, ffmpeg and mplayer on your commandline.
# Some sample flac files can be found at:
# http://www.eclassical.com/pages/24-bit-faq.html
# http://www.2l.no/hires/
# http://www.findhdmusic.com/high-res-audio/free-music/
# LICENSE: This scripe is released under Public Domain.
require 'io/console'
puts "-----\nanilg's bit-rate busting script!\n-----"
if ARGV.length == 0
puts "Usage: ruby abtester.rb <flacfile>"
puts " ruby abtester.rb <flacfile> <runs> <sampleseconds>"
puts " (needs ffmpeg and mplayer installed)"
puts "----"
exit
end
def randstr len = 8
key_space = [('a'..'z'), ('0'..'9')].map { |i| i.to_a }.flatten
pass = (0...len).map { key_space[rand(key_space.length)] }.join
return pass
end
runs = ARGV[1] && ARGV[1].to_i || 10 # 10 runs default
sample = ARGV[2] && ARGV[2].to_i || 10 # 10 seconds default
if runs <=4
puts "\n** WARNING: #{runs} runs is too few to be definitive. (minimum 8 recommended) **\n\n"
end
r1 = "320k"
r2 = "96k"
v1 = "tmp"+randstr(5)
v2 = "tmp"+randstr(5)
puts "Working with flac file #{ARGV[0]}"
puts " cleaning up older files"
`rm -f tmp* 2>&1 > /dev/null`
puts " building #{r1} version.."
`ffmpeg -loglevel panic -i '#{ARGV[0]}' -ab #{r1} -acodec libmp3lame #{v1}.mp3`
puts " ..done"
puts " building #{r2} version.."
`ffmpeg -loglevel panic -i '#{ARGV[0]}' -ab #{r2} -acodec libmp3lame #{v2}.mp3`
puts " ..done"
# play
puts "\n\nLet the games begin!\nWe will now play a random section from both bitrates, and you can choose which was 'better'.\nReady? Press key.."
STDIN.getch
# set random order for 10 runs, and a random point to play music from
data = {}
runs.times.map{|i| data[i] = [v1, v2].shuffle + [(rand*90).to_i]}
currentrun = 0
while currentrun < runs do
puts "\n-----\nRUN #{currentrun+1}/#{runs}"
puts " playing 'a'"; sleep 1
`mplayer -ss 00:00:#{data[currentrun][2]} -endpos 00:00:#{sample} #{data[currentrun][0]}.mp3 2>&1 >/dev/null`
puts " playing 'b'"; sleep 1
`mplayer -ss 00:00:#{data[currentrun][2]} -endpos 00:00:#{sample} #{data[currentrun][1]}.mp3 2>&1 >/dev/null`
puts " which was better? [a] , [b] , [r]eplay"
k = STDIN.getch
if k == "a"
data[currentrun].push("a")
puts "..marked " + k
currentrun += 1
elsif k == "b"
data[currentrun].push("b")
puts " ..marked " + k
currentrun += 1
end
end
# Build report
puts "\n#{'='*15}\nREPORT\n#{'='*15}"
puts "#{r1} is #{v1}.mp3"
puts "#{r2} is #{v2}.mp3"
report = data.keys.map{|i|
r = data[i]
reverse = (r[0] == v2)
s1 = reverse ? "#{r1}, #{r2}": "#{r2}, #{r1}"
if r[3] == "a"
s2 = (reverse ? "#{r2}": "#{r1}")
else
s2 = (reverse ? "#{r1}": "#{r2}")
end
"[RUN #{i+1}/#{runs}] Presented: " + s1 + ". You picked " + s2
}
puts "-----"
puts report.join("\n") + "\n#{'='*15}"
puts "So how close were you? ;) Spread the word, and this gist!\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment