-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvolume_windows.go
120 lines (105 loc) · 2.95 KB
/
volume_windows.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
package volume
import (
"errors"
"math"
"github.com/go-ole/go-ole"
"github.com/moutend/go-wca/pkg/wca"
)
// GetVolume returns the current volume (0 to 100).
func GetVolume() (int, error) {
vol, err := invoke(func(aev *wca.IAudioEndpointVolume) (interface{}, error) {
var level float32
err := aev.GetMasterVolumeLevelScalar(&level)
vol := int(math.Floor(float64(level*100.0 + 0.5)))
return vol, err
})
if vol == nil {
return 0, err
}
return vol.(int), err
}
// 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 := invoke(func(aev *wca.IAudioEndpointVolume) (interface{}, error) {
err := aev.SetMasterVolumeLevelScalar(float32(volume)/100, nil)
return nil, err
})
return err
}
// IncreaseVolume increases (or decreases) the audio volume by the specified value.
func IncreaseVolume(diff int) error {
_, err := invoke(func(aev *wca.IAudioEndpointVolume) (interface{}, error) {
var level float32
err := aev.GetMasterVolumeLevelScalar(&level)
if err != nil {
return nil, err
}
vol := math.Min(math.Max(math.Floor(float64(level*100.0+0.5))+float64(diff), 0.0), 100.0)
err = aev.SetMasterVolumeLevelScalar(float32(vol)/100, nil)
return nil, err
})
return err
}
// GetMuted returns the current muted status.
func GetMuted() (bool, error) {
muted, err := invoke(func(aev *wca.IAudioEndpointVolume) (interface{}, error) {
var muted bool
err := aev.GetMute(&muted)
return muted, err
})
if muted == nil {
return false, err
}
return muted.(bool), err
}
// Mute mutes the audio.
func Mute() error {
_, err := invoke(func(aev *wca.IAudioEndpointVolume) (interface{}, error) {
err := aev.SetMute(true, nil)
return nil, err
})
return err
}
// Unmute unmutes the audio.
func Unmute() error {
_, err := invoke(func(aev *wca.IAudioEndpointVolume) (interface{}, error) {
err := aev.SetMute(false, nil)
return nil, err
})
return err
}
func invoke(f func(aev *wca.IAudioEndpointVolume) (interface{}, error)) (ret interface{}, err error) {
if err = ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED); err != nil {
return
}
defer ole.CoUninitialize()
var mmde *wca.IMMDeviceEnumerator
if err = wca.CoCreateInstance(wca.CLSID_MMDeviceEnumerator, 0, wca.CLSCTX_ALL, wca.IID_IMMDeviceEnumerator, &mmde); err != nil {
return
}
defer mmde.Release()
var mmd *wca.IMMDevice
if err = mmde.GetDefaultAudioEndpoint(wca.ERender, wca.EConsole, &mmd); err != nil {
return
}
defer mmd.Release()
var ps *wca.IPropertyStore
if err = mmd.OpenPropertyStore(wca.STGM_READ, &ps); err != nil {
return
}
defer ps.Release()
var pv wca.PROPVARIANT
if err = ps.GetValue(&wca.PKEY_Device_FriendlyName, &pv); err != nil {
return
}
var aev *wca.IAudioEndpointVolume
if err = mmd.Activate(wca.IID_IAudioEndpointVolume, wca.CLSCTX_ALL, nil, &aev); err != nil {
return
}
defer aev.Release()
ret, err = f(aev)
return
}