-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume_control.py
78 lines (65 loc) · 2.54 KB
/
volume_control.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
from ctypes import POINTER, cast
# import comtypes
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume, CLSID_MMDeviceEnumerator, IMMDeviceEnumerator, EDataFlow, ERole
# class MyAudioUtilities(AudioUtilities):
# @staticmethod
# def GetSpeaker(id=None):
# device_enumerator = comtypes.CoCreateInstance(
# CLSID_MMDeviceEnumerator,
# IMMDeviceEnumerator,
# comtypes.CLSCTX_INPROC_SERVER)
# if id is not None:
# speakers = device_enumerator.GetDevice(id)
# else:
# speakers = device_enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender.value, ERole.eMultimedia.value)
# return speakers
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
def setVolume(targetVolume, outputDevice=None):
volume.SetMasterVolumeLevelScalar(targetVolume/100, None)
# print("volume.GetMasterVolumeLevel(): %s" % volume.GetMasterVolumeLevelScalar())
def getVolume(outputDevice=None):
# print("volume.GetMasterVolumeLevel(): %s" % round(volume.GetMasterVolumeLevelScalar()*100))
return volume.GetMasterVolumeLevelScalar()*100
def getName():
return str(devices)
# async def printAllDevices():
# mixer_output = None
#
# devicelist = MyAudioUtilities.GetAllDevices()
#
# for device in devicelist:
# if "Speaker" in str(device):
# print(device)
#
# def main():
# mixer_output = None
#
# devicelist = MyAudioUtilities.GetAllDevices()
#
# for device in devicelist:
# if "Speakers (Realtek(R) Audio)" in str(device) and "Speaker" in str(device):
# mixer_output = device
#
# devices = MyAudioUtilities.GetSpeaker(mixer_output.id)
# print(devices)
#
# interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
# volume = cast(interface, POINTER(IAudioEndpointVolume))
# print("volume.GetMute(): %s" % volume.GetMute())
# print("volume.GetMasterVolumeLevel(): %s" % volume.GetMasterVolumeLevel())
# print("volume.GetVolumeRange(): (%s, %s, %s)" % volume.GetVolumeRange())
# print("volume.SetMasterVolumeLevel()")
# volume.SetMasterVolumeLevelScalar(0.3, None)
# print("volume.GetMasterVolumeLevel(): %s" % volume.GetMasterVolumeLevelScalar())
#
#
#
# if __name__ == "__main__":
# #main()
# #setVolume(100, "Speakers (Realtek(R) Audio)")
# getVolume("Speakers (Realtek(R) Audio)")
# #printAllDevices()