Sha256: b258b16a7a84fe87926b6e1a58d122c87901f2eddc95ae64437bd42ba240284a

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 KB

Contents

# Volay module
module Volay
  # Mixer module
  module Mixer
    # Alsa class for playing with amixer
    class Alsa < Default
      DEFAULT_VALUE = 2

      def up(value = DEFAULT_VALUE)
        command("-q set Master #{value}%+")
      end

      def down(value = DEFAULT_VALUE)
        command("-q set Master #{value}%-")
      end

      def value=(value)
        command("-q set Master #{value}%")
      end

      def toggle
        command('-q set Master toggle')
      end

      def current
        result = command('get Master')
        max_value = result.match(/ 0 - (\d+)/)[1].to_i
        current_percent = result.match(/\[([0-9]+)%\]\s+\[o(?:n|ff)\]/)[1].to_i
        current_value = ((current_percent * max_value) / 100).to_i
        current_state = result.match(/\[([a-z]+)\]/)[1].to_s

        { value: current_value,
          max_value: max_value,
          percent: current_percent,
          muted: current_state == 'on' ? false : true }
      end

      private

      def command(cmd)
        result = Mixlib::ShellOut.new("amixer #{cmd}")
        result.run_command
        Volay::Config.logger.error(result.stderr) unless result.stderr.empty?
        result.stdout
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
volay-1.1.0 lib/volay/mixer/alsa.rb
volay-1.0.1 lib/volay/mixer/alsa.rb
volay-1.0.0 lib/volay/mixer/alsa.rb
volay-0.7.0 lib/volay/mixer/alsa.rb
volay-0.6.0 lib/volay/mixer/alsa.rb
volay-0.5.0 lib/volay/mixer/alsa.rb
volay-0.4.0 lib/volay/mixer/alsa.rb
volay-0.3.0 lib/volay/mixer/alsa.rb
volay-0.2.0 lib/volay/mixer/alsa.rb
volay-0.1.0 lib/volay/mixer/alsa.rb
volay-0.0.1 lib/volay/mixer/alsa.rb