Class: Knj::Amixer::Mixer

Inherits:
Object show all
Defined in:
lib/knj/amixer.rb

Overview

This class controls each mixer.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Mixer

Returns a new instance of Mixer.



103
104
105
# File 'lib/knj/amixer.rb', line 103

def initialize(args)
  @args = args
end

Instance Method Details

#nameObject

Returns the name of the mixer (example: Master).



108
109
110
# File 'lib/knj/amixer.rb', line 108

def name
  return @args[:name]
end

#vol(args = {}) ⇒ Object

Returns the volume-value as an integer (or as the percent if => true if given in arguments).



123
124
125
126
127
128
129
130
131
132
# File 'lib/knj/amixer.rb', line 123

def vol(args = {})
  ret = %x[#{@args[:amixer].args[:amixer_bin]} -c #{@args[:device].id} sget "#{@args[:name]}"]
  raise "No content for mixer: '#{@args[:name]}'." if !ret
  
  match = ret.match(/(Capture|Playback) (\d+) \[(\d+%)\]/)
  raise "Couldnt figure out volume for '#{@args[:name]}' from:\n'#{ret}'\n" if !match
  
  return match[3].to_i if args[:percent]
  return match[2].to_i
end

#vol=(newvol) ⇒ Object

Sets a new value for the volume.



135
136
137
138
# File 'lib/knj/amixer.rb', line 135

def vol=(newvol)
  ret = %x[#{@args[:amixer].args[:amixer_bin]} -c #{@args[:device].id} sset "#{@args[:name]}" "#{newvol}"]
  #NOTE: Do some error handeling here?
end

#vol_add(add_vol) ⇒ Object

Adds a number to the volume.



141
142
143
144
145
146
# File 'lib/knj/amixer.rb', line 141

def vol_add(add_vol)
  vol = self.vol
  newvol = vol + add_vol.to_i
  newvol = 0 if newvol < 0
  self.vol=(newvol)
end

#volume?Boolean

Returns a bool. If the mixer supports volume-operations (some mixers are just switches and doenst support volume).

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
# File 'lib/knj/amixer.rb', line 113

def volume?
  ret = %x[#{@args[:amixer].args[:amixer_bin]} -c #{@args[:device].id} sget "#{@args[:name]}"]
  raise "No content for mixer: '#{@args[:name]}'." if !ret
  
  match = ret.match(/(Capture|Playback) (\d+) \[(\d+%)\]/)
  return false if !match
  return true
end