-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
volume_unix.go
67 lines (58 loc) · 1.36 KB
/
volume_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//go:build !windows
// +build !windows
package volume
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func execCmd(cmdArgs []string) ([]byte, error) {
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Env = append(os.Environ(), cmdEnv()...)
out, err := cmd.Output()
if err != nil {
err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(cmdArgs, " "), err)
}
return out, err
}
// GetVolume returns the current volume (0 to 100).
func GetVolume() (int, error) {
out, err := execCmd(getVolumeCmd())
if err != nil {
return 0, err
}
return parseVolume(string(out))
}
// SetVolume sets the sound volume to the specified value.
func SetVolume(volume int) error {
if volume < 0 || 100 < volume {
return errors.New("out of valid volume range")
}
_, err := execCmd(setVolumeCmd(volume))
return err
}
// IncreaseVolume increases (or decreases) the audio volume by the specified value.
func IncreaseVolume(diff int) error {
_, err := execCmd(increaseVolumeCmd(diff))
return err
}
// GetMuted returns the current muted status.
func GetMuted() (bool, error) {
out, err := execCmd(getMutedCmd())
if err != nil {
return false, err
}
return parseMuted(string(out))
}
// Mute mutes the audio.
func Mute() error {
_, err := execCmd(muteCmd())
return err
}
// Unmute unmutes the audio.
func Unmute() error {
_, err := execCmd(unmuteCmd())
return err
}