include/pulseaudio_sink.rb in pulseaudio-0.0.3 vs include/pulseaudio_sink.rb in pulseaudio-0.0.4

- old
+ new

@@ -8,10 +8,19 @@ #The arguments-hash. Contains various data for the sink. attr_reader :args @@sinks = Wref_map.new + #Used to look up IDs from names (like when getting default sink). + @@sink_name_to_id_ref = {} + + #Autoloader for subclasses. + def self.const_missing(name) + require "#{File.realpath(File.dirname(__FILE__))}/pulseaudio_sink_#{name.to_s.downcase}.rb" + return PulseAudio::Sink.const_get(name) + end + #Returns a list of sinks on the system. It also reloads information for all sinks if the information has been changed. #===Examples # sinks = PulseAudio::Sink.list # sinks.each do |sink| # sink.vol_decr if sink.active? @@ -31,10 +40,11 @@ sink = @@sinks.get!(sink_id) if !sink sink = PulseAudio::Sink.new @@sinks[sink_id] = sink + @@sink_name_to_id_ref[props["name"]] = sink_id end sink.update(args) if block_given? @@ -47,9 +57,36 @@ if block_given? return nil else return sinks end + end + + #Returns the default sink by doing a smart lookup and using the 'name-to-id-ref'-cache. + def self.by_default + def_str = %x[pacmd info | grep "Default sink name"] + raise "Could not match default sink." if !match = def_str.match(/^Default sink name: (.+?)\s*$/) + sink_id = @@sink_name_to_id_ref[match[1]] + raise "Could not figure out sink-ID." if !sink_id + return PulseAudio::Sink.by_id(sink_id.to_i) + end + + #Returns a sink by its sink-ID. + #===Examples + # sink = PulseAudio::Sink.by_id(3) + def self.by_id(id) + #Return it from the weak-reference-map, if it already exists there. + if sink = @@sinks.get!(id) + return sink + end + + #Read the sinks one-by-one and return it when found. + PulseAudio::Sink.list do |sink| + return sink if sink.sink_id == id + end + + #Sink could not be found by the given ID - raise error. + raise NameError, "No sink by that ID: '#{id}' (#{id.class.name})." end #Updates the data on the object. This should not be called. def update(args) @args = args \ No newline at end of file