Skip to content

Instantly share code, notes, and snippets.

@GregPK
Created January 16, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GregPK/dbf088c5d05c2848a26103d056b42298 to your computer and use it in GitHub Desktop.
Save GregPK/dbf088c5d05c2848a26103d056b42298 to your computer and use it in GitHub Desktop.
pulse-audio-switch.rb
#!/usr/bin/env ruby
sink_output = `pacmd list-sinks`
sinks_index_lines = sink_output.each_line.select{|line| line =~ /index/}
sinks_indexes = sinks_index_lines.map{|index_line| Integer(index_line.strip.gsub(/[^\d]/,'')) }
# optional - for more descriptive notification message
card_names = sink_output.each_line.select{|line| line =~ /alsa.card_name/}.map{ |l| l.gsub(/.+ = "(.+)"/,'\1').strip }
card_names_dict = Hash[sinks_indexes.zip(card_names)]
# this should probably be customized by you
# I have a HDMI sink that I never use, hence I've only used the two I switch, do
# puts card_names_dict
# to check which sinkds you want to leave in
interesting_sinks = sinks_indexes.last(2)
active_sink_index = Integer(sinks_index_lines.select{|line| line =~ /\*/}.first.gsub(/[^\d]/,''))
next_sink_index = interesting_sinks.reject{|sink| sink == active_sink_index}.first
#change the default sink
`pacmd "set-default-sink #{next_sink_index}"`
# move current sinks to new one
sink_input_ids = `pacmd list-sink-inputs`.each_line.select{|line| line =~ /index/}.map{|line| line.gsub(/[^\d]/,'')}
sink_input_ids.each do |sink_id|
`pacmd 'move-sink-input #{sink_id} #{next_sink_index}'`
end
`notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: 'Sound output switched to #{card_names_dict[next_sink_index]}'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment