-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvolume_linux.go
152 lines (132 loc) · 4.2 KB
/
volume_linux.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//go:build !windows && !darwin
// +build !windows,!darwin
package volume
import (
"errors"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
var (
useAmixer bool
)
const (
// outputdevice string = "Master"
// outputdevice string = "Speaker" // Modified from Master to Speaker for talkkonnect for raspberry pi
)
func init() {
if _, err := exec.LookPath("pactl"); err != nil {
useAmixer = true
}
}
func cmdEnv() []string {
return []string{"LANG=C", "LC_ALL=C"}
}
func getVolumeCmd(outputdevice string) []string {
if useAmixer {
return []string{"amixer", "-M", "get", outputdevice}
}
return []string{"pactl", "list", "sinks"}
}
var volumePattern = regexp.MustCompile(`\d+%`)
func parseRXVolume(out string) (int, error) {
lines := strings.Split(out, "\t")
for _, line := range lines {
s := strings.TrimLeft(line, " \t")
if useAmixer && (strings.Contains(s, "Playback") || strings.Contains(s, "Front Left:")) && strings.Contains(s, "%") || !useAmixer && strings.HasPrefix(s, "Volume:") {
volumeStr := volumePattern.FindString(s)
return strconv.Atoi(volumeStr[:len(volumeStr)-1])
}
}
return 0, errors.New("no volume found")
}
func parseTXVolume(in string) (int, error) {
lines := strings.Split(in, "\t")
for _, line := range lines {
s := strings.TrimLeft(line, " \t")
if useAmixer && (strings.Contains(s, "Capture")) {
volumeStr := volumePattern.FindString(s)
return strconv.Atoi(volumeStr[:len(volumeStr)-1])
}
}
return 0, errors.New("no volume found")
}
func setVolumeCmd(volume int, outputdevice string) []string {
if useAmixer {
return []string{"amixer", "set", outputdevice, strconv.Itoa(volume) + "%"}
} else if _, err := strconv.Atoi(outputdevice); err == nil {
return []string{"pactl", "set-sink-volume", outputdevice, strconv.Itoa(volume) + "%"}
}
return []string{"pactl", "set-sink-volume", "0", strconv.Itoa(volume) + "%"}
}
func increaseVolumeCmd(diff int, outputdevice string) []string {
var OrigVolume int
var err error
OrigVolume, err = GetVolume(outputdevice)
if err != nil {
log.Println("error: Cannot Get Volume of Current Output Device")
return nil
}
if diff > 0 {
log.Printf("debug: Changing Volume From %v increase by %v dB step on %v\n", OrigVolume, diff, outputdevice)
return []string{"amixer", "sset", "-q", outputdevice, strconv.Itoa(abs(diff)) + "db+"}
}
if diff < 0 {
log.Printf("debug: Changing Volume From %v decrease by %v dB step on %v\n", OrigVolume, diff, outputdevice)
return []string{"amixer", "sset", "-q", outputdevice, strconv.Itoa(abs(diff)) + "db-"}
}
return nil
}
func getMutedCmd(outputdevice string) []string {
if useAmixer {
return []string{"amixer", "sget", outputdevice}
}
return []string{"pactl", "list", "sinks"}
}
func parseRXMuted(out string) (bool, error) {
lines := strings.Split(out, "\t")
for _, line := range lines {
s := strings.TrimLeft(line, "\t")
if (useAmixer && strings.Contains(s, "Playback")) && (strings.Contains(s, "on") || strings.Contains(s, "off")) {
if strings.Contains(s, "[off]") || strings.Contains(s, "off") || strings.Contains(s, "yes") {
return true, nil
}
if strings.Contains(s, "[on]") || strings.Contains(s, "on") || strings.Contains(s, "no") {
return false, nil
}
}
if !useAmixer && strings.HasPrefix(s, "Mute: ") {
if strings.Contains(s, "[off]") || strings.Contains(s, "off") || strings.Contains(s, "yes") {
return true, nil
}
if strings.Contains(s, "[on]") || strings.Contains(s, "on") || strings.Contains(s, "no") {
return false, nil
}
}
}
return false, errors.New("no muted information found")
}
func muteCmd(outputdevice string) []string {
if useAmixer {
return []string{"amixer", "set", outputdevice, "mute"}
} else if _, err := strconv.Atoi(outputdevice); err == nil {
return []string{"pactl", "set-sink-mute", outputdevice, "1"}
}
return []string{"pactl", "set-sink-mute", "0", "1"}
}
func unmuteCmd(outputdevice string) []string {
if useAmixer {
return []string{"amixer", "set", outputdevice, "unmute"}
} else if _, err := strconv.Atoi(outputdevice); err == nil {
return []string{"pactl", "set-sink-mute", outputdevice, "0"}
}
return []string{"pactl", "set-sink-mute", "0", "0"}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}