Skip to content

Instantly share code, notes, and snippets.

@bawNg
Created October 3, 2012 14:46
Show Gist options
  • Save bawNg/3827311 to your computer and use it in GitHub Desktop.
Save bawNg/3827311 to your computer and use it in GitHub Desktop.
Refactored ruby krad client
def get_image_dim(filename)
splitter = filename.downcase.include?('.png') ? 'PNG' : 'JPEG'
dims = `identify "#{filename}"`.chomp.split(splitter)[1].split(' ')[0].split('x')
width, height = dims.first.to_i, dims.last.to_i
puts "got #{width} #{height} for #{filename} - #{dims}"
return width, height
end
def center_or_zero(max, item)
item > max ? 0 : (max - item) / 2
end
def place_image(filename, width, height)
image_width, image_height = get_image_dim(filename)
center_width = center_or_zero(width, image_width)
center_height = center_or_zero(height, image_height)
puts "got center of #{center_width}-#{center_height} for image #{image_width}x#{image_height} for max of #{width}x#{height}"
return center_width, center_height
end
class KradStation
def self.define_cmds(*names)
names.each do |name|
define_method(name) {|*args| cmd(name, *args) }
end
end
def initialize(name_or_url)
@name = name_or_url
@comp_width = 0
@comp_height = 0
Kernel.srand
launch_daemon
end
def cmd(*args)
command = ['krad_radio', @name, *args]
puts "command: #{command.inspect}"
output = IO.popen(command).read.chomp
sleep 0.1
output
end
define_cmds :launch, :info, :uptime, :play, :lsv
alias_method :launch_daemon, :launch
alias_method :get_v4l2_devices, :lsv
alias_method :jack?, :jacked?
alias_method :jack_running?, :jacked?
def jacked?
cmd('jacked').downcase == 'yes'
end
def get_v4l2_device(num=0)
get_v4l2_devices.lines.to_a[num] || ''
end
def sprite(filename, x=0, y=0, rate=5, scale=1, opacity=1.0, rotation=0.0)
cmd :addsprite, filename, x, y, rate, scale, opacity, rotation
end
def set_sprite(num=0, x=0, y=0, rate=5, scale=1, opacity=1.0, rotation=0.0)
cmd :setsprite, num, x, y, rate, scale, opacity, rotation
end
def rm_sprite(num=0)
cmd :rmsprite, num
end
def throw_sprite(num=0)
if rand(100) % 100 > 50
if rand(100) % 100 > 50
if rand(100) % 100 > 50
set_sprite(num, 0, 2333, 5, 0.3, 0, 545)
else
set_sprite(num, 0, -1333, 5, 3.3, 0, -545)
end
else
if rand(100) % 100 > 50
set_sprite(num, 1333, 0, 5, 2.3, 0, 45)
else
set_sprite(num, -1333, 0, 5, 0.1, 0, -45)
end
end
else
if rand(100) % 100 > 50
if rand(100) % 100 > 50
set_sprite(num, 1333, 1333, 5, 0.3, 0, 45)
else
set_sprite(num, -1333, -1333, 5, 0.8, 0, -45)
end
else
if rand(100) % 100 > 50
set_sprite(num, -1333, 0, 45, 0.5, 0, 15)
else
set_sprite(num, 0, -1333, 15, 0.5, 0, -45)
end
end
end
end
def text(msg, x=0, y=0, rate=5, scale=32, opacity=1.0, rotation=0.0, red=0, green=0, blue=0, font='sans')
cmd :addtext, msg, x, y, rate, scale, opacity, rotation, red, green, blue, font
end
def set_text(num=0, x=0, y=0, rate=5, scale=32, opacity=1.0, rotation=0.0, red=0, green=0, blue=0)
cmd :settext, num, x, y, rate, scale, opacity, rotation, red, green, blue
end
def rm_text(num=0)
cmd :rmtext, num
end
def record(filename, options={})
options[:codec] ||= 'default'
bitrate, audio_bitrate = options[:bitrate], options[:audiobitrate]
cmd :record, :audiovideo, filename, options[:codec], options[:width], options[:height], bitrate, audio_bitrate
end
def record_audio(filename, options={})
options[:codec] ||= 'default'
cmd :record, :audio, filename, options[:codec], options[:audiobitrate] || options[:bitrate]
end
def record_video(filename, options={})
options[:codec] ||= 'vp8vorbis'
cmd :record, :video, filename, options[:codec], options[:width], options[:height], options[:bitrate]
end
def set_resolution(width, height)
@comp_width, @comp_height = width, height
cmd :res, width, height
end
def slideshow(pictures_dir, timeper=3, label=true)
Kernel.srand
playlist = []
count = 0
Dir.foreach(pictures_dir) do |item|
next if item == '.' || item == '..'
if %w[ .png .jpg ].include? File.extname(item).downcase
playlist << item
end
end
playlist.shuffle!
puts "Slideshow of #{playlist.length} pictures"
playlist.each do |item|
filename = pictures_dir + '/' + item
xy = place_image(filename, @comp_width, @comp_height)
x = xy.first
y = xy.last
self.sprite(filename, x, y)
if item.include? '_frames_'
case count
when 0, 1
set_sprite(1, 955, 255, 4, 3.3, 0.85, 25)
when 2
set_sprite(0, 450, 335, 4, 3.3, 1, -25)
end
end
throw_sprite(0) if count == 1
throw_sprite(1) if count == 2
if label
text(item, 35, 155, 5, 44, 1, 0, 255, 255, 55, "DroidSans")
sleep 1.5
set_text(0, 35, 155, 5, 44, 0, 0, 255, 255, 2)
sleep 1.5
rm_text(0)
sleep timeper - 1
else
sleep timeper
end
if count == 1
rm_sprite(0)
elsif count == 2
rm_sprite(1)
count = 0
end
sleep 0.2
count = count + 1
rm_sprite(2)
rm_sprite(3)
sleep 0.2
end
rm_sprite(0)
rm_sprite(1)
end
def play_dir(playlist_dir)
loop do
Kernel.srand
playlist = []
total_duration = 0
Dir.foreach(playlist_dir) do |item|
next if item == '.' || item == '..'
if File.extname(item) == '.webm'
playlist << item
info = `mkvinfo "#{playlist_dir}/#{item}" | grep Dura`.chomp
total_duration += info.split(' ')[3].to_i
end
end
total_duration_minutes = total_duration / 60
playlist.shuffle!
puts "Playlist of #{playlist.length} items, duration of #{total_duration_minutes} minutes"
playlist.each do |item|
info = `mkvinfo "#{playlist_dir}/#{item}" | grep Dura`.chomp
duration = info.split(' ')[3].to_i
cmd ' rm 2'
sleep 0.3
play(playlist_dir + "/" + item)
puts "PLAYING (duration #{duration}) #{playlist_dir}/#{item}"
puts
sleep duration
end
cmd ' rm 2'
end
end
def play_dir_single(playlist_dir)
Kernel.srand
playlist = []
total_duration = 0
Dir.foreach(playlist_dir) do |item|
next if item == '.' || item == '..'
if File.extname(item) == '.webm'
playlist << item
info = `mkvinfo "#{playlist_dir}/#{item}" | grep Dura`.chomp
total_duration += info.split(" ")[3].to_i
end
end
total_duration_minutes = total_duration / 60
playlist.shuffle!
puts "Playlist of #{playlist.length} items, duration of #{total_duration_minutes} minutes"
playlist.each do |item|
info = `mkvinfo "#{playlist_dir}/#{item}" | grep Dura`.chomp
duration = info.split(' ')[3].to_i
cmd ' rm 1'
sleep 0.3
play(playlist_dir + "/" + item)
puts "PLAYING (duration #{duration}) #{playlist_dir}/#{item}"
puts
sleep duration
break
end
cmd ' rm 1'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment