-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolumeTest.py
147 lines (127 loc) · 5.63 KB
/
volumeTest.py
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
import ctypes
import comtypes
from ctypes import wintypes
MMDeviceApiLib = comtypes.GUID(
'{2FDAAFA3-7523-4F66-9957-9D5E7FE698F6}')
IID_IMMDevice = comtypes.GUID(
'{D666063F-1587-4E43-81F1-B948E807363F}')
IID_IMMDeviceCollection = comtypes.GUID(
'{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}')
IID_IMMDeviceEnumerator = comtypes.GUID(
'{A95664D2-9614-4F35-A746-DE8DB63617E6}')
IID_IAudioEndpointVolume = comtypes.GUID(
'{5CDF2C82-841E-4546-9722-0CF74078229A}')
CLSID_MMDeviceEnumerator = comtypes.GUID(
'{BCDE0395-E52F-467C-8E3D-C4579291692E}')
# EDataFlow
eRender = 0 # audio rendering stream
# ERole
eMultimedia = 1 # music, movies, narration
LPCGUID = REFIID = ctypes.POINTER(comtypes.GUID)
LPFLOAT = ctypes.POINTER(ctypes.c_float)
LPDWORD = ctypes.POINTER(wintypes.DWORD)
LPUINT = ctypes.POINTER(wintypes.UINT)
LPBOOL = ctypes.POINTER(wintypes.BOOL)
PIUnknown = ctypes.POINTER(comtypes.IUnknown)
class IMMDevice(comtypes.IUnknown):
_iid_ = IID_IMMDevice
_methods_ = (
comtypes.COMMETHOD([], ctypes.HRESULT, 'Activate',
(['in'], REFIID, 'iid'),
(['in'], wintypes.DWORD, 'dwClsCtx'),
(['in'], LPDWORD, 'pActivationParams', None),
(['out','retval'], ctypes.POINTER(PIUnknown), 'ppInterface')),
comtypes.STDMETHOD(ctypes.HRESULT, 'OpenPropertyStore', []),
comtypes.STDMETHOD(ctypes.HRESULT, 'GetId', []),
comtypes.STDMETHOD(ctypes.HRESULT, 'GetState', []))
PIMMDevice = ctypes.POINTER(IMMDevice)
class IMMDeviceCollection(comtypes.IUnknown):
_iid_ = IID_IMMDeviceCollection
PIMMDeviceCollection = ctypes.POINTER(IMMDeviceCollection)
class IMMDeviceEnumerator(comtypes.IUnknown):
_iid_ = IID_IMMDeviceEnumerator
_methods_ = (
comtypes.COMMETHOD([], ctypes.HRESULT, 'EnumAudioEndpoints',
(['in'], wintypes.DWORD, 'dataFlow'),
(['in'], wintypes.DWORD, 'dwStateMask'),
(['out','retval'], ctypes.POINTER(PIMMDeviceCollection),
'ppDevices')),
comtypes.COMMETHOD([], ctypes.HRESULT, 'GetDefaultAudioEndpoint',
(['in'], wintypes.DWORD, 'dataFlow'),
(['in'], wintypes.DWORD, 'role'),
(['out','retval'], ctypes.POINTER(PIMMDevice), 'ppDevices')))
@classmethod
def get_default(cls, dataFlow, role):
enumerator = comtypes.CoCreateInstance(
CLSID_MMDeviceEnumerator, cls, comtypes.CLSCTX_INPROC_SERVER)
return enumerator.GetDefaultAudioEndpoint(dataFlow, role)
class IAudioEndpointVolume(comtypes.IUnknown):
_iid_ = IID_IAudioEndpointVolume
_methods_ = (
comtypes.COMMETHOD([], ctypes.HRESULT, 'SetMasterVolumeLevel',
(['in'], ctypes.c_float, 'fLevelDB'),
(['in'], LPCGUID, 'pguidEventContext', None)),
comtypes.COMMETHOD([], ctypes.HRESULT, 'SetMasterVolumeLevelScalar',
(['in'], ctypes.c_float, 'fLevel'),
(['in'], LPCGUID, 'pguidEventContext', None)),
comtypes.COMMETHOD([], ctypes.HRESULT, 'GetMasterVolumeLevel',
(['out','retval'], LPFLOAT, 'pfLevelDB')),
comtypes.COMMETHOD([], ctypes.HRESULT, 'GetMasterVolumeLevelScalar',
(['out','retval'], LPFLOAT, 'pfLevel')),
comtypes.COMMETHOD([], ctypes.HRESULT, 'SetChannelVolumeLevelScalar',
(['in'], wintypes.UINT, 'nChannel'),
(['in'], ctypes.c_float, 'fLevel'),
(['in'], LPCGUID, 'pguidEventContext', None)),
comtypes.COMMETHOD([], ctypes.HRESULT, 'SetMute',
(['in'], wintypes.BOOL, 'bMute'),
(['in'], LPCGUID, 'pguidEventContext', None)),
comtypes.COMMETHOD([], ctypes.HRESULT, 'GetMute',
(['out','retval'], LPBOOL, 'pbMute')),
comtypes.COMMETHOD([], ctypes.HRESULT, 'GetVolumeStepInfo',
(['out','retval'], LPUINT, 'pnStep'),
(['out','retval'], LPUINT, 'pnStepCount')),
comtypes.COMMETHOD([], ctypes.HRESULT, 'VolumeStepUp',
(['in'], LPCGUID, 'pguidEventContext', None)),
comtypes.COMMETHOD([], ctypes.HRESULT, 'VolumeStepDown',
(['in'], LPCGUID, 'pguidEventContext', None)))
@classmethod
def get_default(cls):
endpoint = IMMDeviceEnumerator.get_default(eRender, eMultimedia)
interface = endpoint.Activate(cls._iid_, comtypes.CLSCTX_INPROC_SERVER)
return ctypes.cast(interface, ctypes.POINTER(cls))
if __name__ == '__main__':
def show_vol(ev):
voldb = ev.GetMasterVolumeLevel()
volsc = ev.GetMasterVolumeLevelScalar()
volst, nstep = ev.GetVolumeStepInfo()
print('Master Volume (dB): %0.4f' % voldb)
print('Master Volume (scalar): %0.4f' % volsc)
print('Master Volume (step): %d / %d' % (volst, nstep))
def test():
ev = IAudioEndpointVolume.get_default()
vol = ev.GetMasterVolumeLevelScalar()
vmin, vmax, vinc = ev.GetVolumeRange()
print('Volume Range (min, max, step) (dB): '
'%0.4f, %0.4f, %0.4f' % (vmin, vmax, vinc))
show_vol(ev)
try:
print('\nIncrement the master volume')
ev.VolumeStepUp()
show_vol(ev)
print('\nDecrement the master volume twice')
ev.VolumeStepDown()
ev.VolumeStepDown()
show_vol(ev)
print('\nSet the master volume to 0.75 scalar')
ev.SetMasterVolumeLevelScalar(0.75)
show_vol(ev)
print('\nSet the master volume to 0.25 scalar')
ev.SetMasterVolumeLevelScalar(0.25)
show_vol(ev)
finally:
ev.SetMasterVolumeLevelScalar(vol)
comtypes.CoInitialize()
try:
test()
finally:
comtypes.CoUninitialize()