Last active
August 29, 2015 14:05
-
-
Save robertely/3d57516d95e49f95012a to your computer and use it in GitHub Desktop.
Go app to print default sink and volume (int54)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"os/exec" | |
"strconv" | |
) | |
const vmax = 65635 | |
const vmin = 0 | |
func getdefault() (string, int64) { | |
var sink string | |
var volume int64 | |
out, err := exec.Command("pacmd", "dump").Output() | |
if err != nil { | |
fmt.Println(err) | |
} | |
lines := strings.Split(string(out), "\n") | |
// Get sink | |
for _, line := range lines { | |
if (strings.HasPrefix(line, "set-default-sink")) { | |
sink = string(strings.Replace(line, "set-default-sink ", "", 1)) | |
} | |
} | |
// Get volume | |
for _, line := range lines { | |
if (strings.HasPrefix(line, "set-sink-volume " + sink)) { | |
volume, _ = strconv.ParseInt(strings.Replace(line, "set-sink-volume " + sink + " ", "", 1), 0, 64) | |
} | |
} | |
return sink, volume | |
} | |
func setvolume(sink string, volume int64){ | |
if (volume > vmax){ | |
volume = vmax | |
} | |
if (volume <= vmin){ | |
volume = vmin | |
} | |
out, err := exec.Command("pactl", "set-sink-volume", sink, strconv.FormatInt(volume, 10)).Output() | |
if err != nil { | |
fmt.Println(err, out) | |
} | |
} | |
func main() { | |
sink, vol := getdefault() | |
setvolume(sink, vol - 1000) | |
fmt.Println(getdefault()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment