Class: Knj::Amixer::Device

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Device

Returns a new instance of Device.



35
36
37
38
# File 'lib/knj/amixer.rb', line 35

def initialize(args)
  @args = args
  @mixers = {}
end

Instance Method Details

#active?(args = {}) ⇒ Boolean

Returns true if the device is active by looking in ‘/proc/asounc/card*/pcm*/sub*/status’.

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/knj/amixer.rb', line 57

def active?(args = {})
  proc_path = "/proc/asound/#{@args[:code]}"
  
  Dir.foreach(proc_path) do |file|
    next if file == "." or file == ".." or !file.match(/^pcm(\d+)[a-z]+$/)
    sub_path = "#{proc_path}/#{file}"
    info_path = "#{sub_path}/info"
    info_cont = File.read(info_path)
    
    if stream_match = info_cont.match(/stream: (.+?)\s+/)
      next if args.key?(:stream) and stream_match[1] != args[:stream]
    end
    
    Dir.foreach(sub_path) do |file_sub|
      next if file_sub == "." or file_sub == ".." or !file_sub.match(/^sub(\d+)$/)
      status_path = "#{sub_path}/#{file_sub}/status"
      cont = File.read(status_path)
      return true if cont.strip != "closed"
    end
  end
  
  return false
end

#amixerObject



52
53
54
# File 'lib/knj/amixer.rb', line 52

def amixer
  return @args[:amixer]
end

#codeObject



48
49
50
# File 'lib/knj/amixer.rb', line 48

def code
  return @args[:code]
end

#idObject



40
41
42
# File 'lib/knj/amixer.rb', line 40

def id
  return @args[:id]
end

#mixersObject

Returns a hash of the various mixers.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/knj/amixer.rb', line 82

def mixers
  ret = %x[#{@args[:amixer].args[:amixer_bin]} -c #{@args[:id]} scontrols]
  
  ret.scan(/Simple mixer control '(.+)',0/) do |match|
    name = match[0]
    
    if !@mixers.key?(name)
      @mixers[name] = Knj::Amixer::Mixer.new(
        :amixer => @args[:amixer],
        :device => self,
        :name => name
      )
    end
  end
  
  return @mixers
end

#nameObject



44
45
46
# File 'lib/knj/amixer.rb', line 44

def name
  return @args[:name]
end