Skip to content

Instantly share code, notes, and snippets.

@kyontan

kyontan/pamove

Created Jun 2, 2020
Embed
What would you like to do?
PulseAudio sink-output changer
#!/usr/bin/env ruby
class String
def to_i_first
self.match(/\d+/)[0].to_i
end
def unquote_str
self.match(/"(.*)"/)[1]
end
end
def get_sinks
sinks_str = `pacmd list-sinks`.split("\n").slice_before(/index:/).drop(1)
sinks_str.map do |lines|
name_raw = lines.grep(/device.product.name/).first
[
lines[0].to_i_first,
{
name: name_raw.unquote_str,
is_active: lines[0].include?("*"),
}
]
end.to_h
end
def get_sink_inputs
sink_inputs_str = `pacmd list-sink-inputs`.split("\n").slice_before(/index:/).drop(1)
sink_inputs_str.map do |lines|
[
lines[0].to_i_first,
{
sink_id: lines.grep(/sink: /).first.to_i_first,
name: lines.grep(/application.name/).first.unquote_str,
}
]
end.to_h
end
if ARGV.size != 0 && ARGV.size != 2
puts "#{$0} [sink_input_id sink_id]"
exit 0
end
sinks = get_sinks
sink_inputs = get_sink_inputs
if ARGV.size == 2
sink_input_id = ARGV[0].to_i
sink_id = ARGV[1].to_i
unless sink_inputs[sink_input_id]
puts "Invalid sink_input_id: #{ARGV[0]}"
end
unless sinks[sink_id]
puts "Invalid sink_id: #{ARGV[1]}"
end
`pacmd set-default-sink #{sink_id}`
puts "Set default sink: #{sinks[sink_id][:name]} (ID: #{sink_id})"
`pacmd move-sink-input #{sink_input_id} #{sink_id}`
puts "Moved sink_input #{sink_inputs[sink_input_id][:name]} (ID: #{sink_input_id}) to default sink"
puts
sinks = get_sinks
sink_inputs = get_sink_inputs
end
puts "Sink inputs:"
sink_inputs.each{|k, v| puts " #{k}: #{v[:name]} => #{sinks[v[:sink_id]][:name]} (ID: #{v[:sink_id]})" }
puts
puts "Sinks:"
sinks.each{|k, v| puts "#{v[:is_active] ? "*" : " "} #{k}: #{v[:name]}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.