From b325aabc3f74567d6445f2bd23f63a68fc237583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Sat, 5 Feb 2022 12:54:32 +0100 Subject: [PATCH] init --- go.mod | 5 +++ go.sum | 2 ++ main.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..de44fb5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module puvol + +go 1.17 + +require mrogalski.eu/go/pulseaudio v0.0.0-20200511091429-8449222912dd diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b6a4047 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +mrogalski.eu/go/pulseaudio v0.0.0-20200511091429-8449222912dd h1:yEN6CTPZV70rUFqpZ5SqJn15JK5iKiSL4Gdw2gJ98ug= +mrogalski.eu/go/pulseaudio v0.0.0-20200511091429-8449222912dd/go.mod h1:C3V0v+gsiHHbMtFJvwozjNVdPJZw9oUxlRTVc619wSU= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6a09875 --- /dev/null +++ b/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "strconv" + + "mrogalski.eu/go/pulseaudio" +) + +func main() { + flag.Parse() + + c, e := pulseaudio.NewClient() + if e != nil { + log.Println(e) + os.Exit(1) + } + + if os.Args[0] == "puvol-cont" { + cont(c) + } else { + single(c) + } +} + +func printvol(c *pulseaudio.Client) float32 { + muted, e := c.Mute() + if muted { + fmt.Println("🔇") + return 0 + } + + v, e := c.Volume() + if e != nil { + fmt.Println(e) + } else { + fmt.Printf("%d%%\n", int(100*v)) + } + return v +} + +func cont(c *pulseaudio.Client) { + + ch, e := c.Updates() + if e != nil { + log.Println(e) + os.Exit(2) + } + + for { + printvol(c) + _ = <-ch + } +} + +func single(c *pulseaudio.Client) { + if len(os.Args) == 1 { + printvol(c) + return + } + + switch a := os.Args[1]; a { + case "toggle": + c.ToggleMute() + printvol(c) + case "mute", "unmute": + c.SetMute(a == "mute") + printvol(c) + case "inc", "dec": + v, e := c.Volume() + if e != nil { + log.Println(e) + os.Exit(2) + } + + if a == "inc" { + v += 0.1 + } else if a == "dec" { + v -= 0.1 + } + + if v <= 0 { + v = 0 + } + + c.SetVolume(float32(v)) + printvol(c) + + case "set": + if len(os.Args) < 3 { + log.Println("missing volume") + os.Exit(3) + } + v, e := strconv.ParseFloat(os.Args[2], 32) + if e != nil { + log.Println(e) + os.Exit(4) + } + c.SetVolume(float32(v)) + printvol(c) + default: + printvol(c) + } +}