Sha256: 5958624bc7ef9831b2e6823ae4548abcc619f4f599c85ddff62bf74d4f7c40a2

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

require 'open3'

class Mpg321
  attr_reader :volume

  def initialize
    @volume = 50
    @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 }
    send_volume
  end

  def pause
    @music_input.puts "P"
  end

  def stop
    @music_input.puts "S"
  end

  def play song_list
    songs = song_list.respond_to?(:join) ? song_list.join(' ') : song_list
    @music_input.puts "L #{songs}"
  end

  def volume_up volume
    @volume += volume
    @volume = [@volume, 100].min
    send_volume
  end

  def volume_down volume
    @volume -= volume
    @volume = [@volume, 0].max
    send_volume
  end

  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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mpg321-0.0.3 lib/mpg321.rb