-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.go
129 lines (104 loc) · 2.42 KB
/
audio.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
package main
/**
* Audio status
*/
import (
"log"
ui "github.com/gizak/termui"
"github.com/sqp/pulseaudio"
)
////////////////////////////////////////////
// Widget: Audio
////////////////////////////////////////////
type AudioWidget struct {
widget *ui.Gauge
pulse *pulseaudio.Client
volumePercent uint32
isMuted bool
}
func NewAudioWidget() *AudioWidget {
// Create base element
e := ui.NewGauge()
e.Height = 3
e.Border = true
e.BorderLabel = "Audio"
// Connect to pulseaudio daemon
pulse, err := pulseaudio.New()
if err != nil {
log.Printf("Error connecting to pulse daemon: %v", err)
pulse = nil
}
// Create widget
w := &AudioWidget{
widget: e,
pulse: pulse,
volumePercent: 0,
isMuted: false,
}
// Register listener
if pulse != nil {
pulse.Register(w)
}
w.update()
w.resize()
return w
}
func (w *AudioWidget) getGridWidget() ui.GridBufferer {
return w.widget
}
func (w *AudioWidget) update() {
if w.pulse == nil {
w.widget.BorderLabel = "Audio"
w.widget.Percent = 0
w.widget.Label = "UNSUPPORTED"
w.widget.LabelAlign = ui.AlignCenter
w.widget.PercentColor = ui.ColorMagenta | ui.AttrBold
} else {
// Just query status
sink := w.getBestSink()
if sink != nil {
// Load information about this sink
muted, mutedErr := sink.Bool("Mute")
if mutedErr == nil {
w.isMuted = muted
} else {
w.isMuted = false
}
volume, volErr := sink.ListUint32("Volume")
if volErr == nil {
// Convert to a percent (with shitty rounding)
volPercent := (volume[0] * 1000) / 65536
volPercent = (volPercent + 5) / 10
w.volumePercent = volPercent
} else {
w.volumePercent = 0
}
}
w.widget.Percent = int(w.volumePercent)
w.widget.Label = "{{percent}}%"
w.widget.LabelAlign = ui.AlignRight
w.widget.PercentColor = ui.ColorWhite | ui.AttrBold
w.widget.PercentColorHighlighted = w.widget.PercentColor
if w.isMuted {
w.widget.BarColor = ui.ColorRed
} else {
w.widget.BarColor = ui.ColorGreen
}
}
}
func (w *AudioWidget) getBestSink() *pulseaudio.Object {
fallbackSink, fallbackErr := w.pulse.Core().ObjectPath("FallbackSink")
if fallbackErr == nil {
return w.pulse.Device(fallbackSink)
} else {
sinks, sinkErr := w.pulse.Core().ListPath("Sinks")
if sinkErr == nil {
// Take the first one
return w.pulse.Device(sinks[0])
}
}
return nil
}
func (w *AudioWidget) resize() {
// Do nothing
}