Skip to content

Instantly share code, notes, and snippets.

@stengland
Forked from uriel1998/volume.rb
Created April 26, 2012 09:15
Show Gist options
  • Save stengland/2497962 to your computer and use it in GitHub Desktop.
Save stengland/2497962 to your computer and use it in GitHub Desktop.
Set pulseaudio volume from the command line using ruby
#!/usr/bin/env ruby
# Changed bit of code toggling mute - was throwing errors for me.
# Added output, so it could be piped to a notify-osd or somesuch if desired.
# Pulseaudio volume control
class Pulse
attr_reader :volumes, :mutes
# Constructor
def initialize
dump = `pacmd dump`.lines
@volumes = {}
@mutes = {}
# Find the volume settings
dump.each do |line|
args = line.split
# Volume setting
if args[0] == "set-sink-volume" then
@volumes[args[1]] = args[2].hex
end
# Mute setting
if args[0] == "set-sink-mute" then
@mutes[args[1]] = args[2]
end
end
end
# Adjust the volume with the given increment for every sink
def volume_set_relative(increment)
@volumes.keys.each do |sink|
volume = @volumes[sink] + increment
volume = [[0, volume].max, 0x10000].min
@volumes[sink] = volume
`pacmd set-sink-volume #{sink} #{"0x%x" % volume}`
end
end
# Turn the music up!
def volume_up
puts "volume up"
volume_set_relative 0x1000
end
# ... and down again
def volume_down
puts "volume down"
volume_set_relative -0x1000
end
# Toggle the mute setting for every sink
# Was getting errors thrown here; I don't know enough ruby to know why
# This fixes it.
def mute_toggle
@mutes.keys.each do |sink|
if @mutes[sink] == "yes"
puts "unmuting #{sink}"
`pacmd set-sink-mute #{sink} no`
else
puts "muting #{sink}"
`pacmd set-sink-mute #{sink} yes`
end
end
end
end
# Control code
p = Pulse.new
if ARGV.first == "up" then
p.volume_up
end
if ARGV.first == "down" then
p.volume_down
end
if ARGV.first == "toggle" then
p.mute_toggle
end
@stengland
Copy link
Author

Just a little tweak. Changed shebang to work with rvm http://22ideastreet.com/debug/ruby-shebang-rvm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment