lib/mpg321.rb in mpg321-0.0.2 vs lib/mpg321.rb in mpg321-0.0.3

- old
+ new

@@ -3,40 +3,51 @@ class Mpg321 attr_reader :volume def initialize @volume = 50 - @mus, _stdout, _stderr, _thread = Open3.popen3("mpg321 -R mpg321_ruby") + @music_input, _stdout, _stderr, _thread = Open3.popen3("mpg321 -R mpg321_ruby") Thread.new { loop do _stderr.readline end } Thread.new { loop do _stdout.readline end } - set_volume + send_volume end def pause - @mus.puts "P" + @music_input.puts "P" end def stop - @mus.puts "S" + @music_input.puts "S" end def play song_list songs = song_list.respond_to?(:join) ? song_list.join(' ') : song_list - @mus.puts "L #{songs}" + @music_input.puts "L #{songs}" end - def volume_up num - @volume += num + def volume_up volume + @volume += volume @volume = [@volume, 100].min - set_volume + send_volume end - def volume_down num - @volume -= num + def volume_down volume + @volume -= volume @volume = [@volume, 0].max - set_volume + send_volume end - private def set_volume - @mus.puts "G #{@volume}" + def volume= volume + if volume < 0 + @volume = 0 + elsif volume > 100 + @volume = 100 + else + @volume = volume + end + send_volume + end + + private def send_volume + @music_input.puts "G #{@volume}" end end