-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolumeregulator.py
227 lines (200 loc) · 8.53 KB
/
volumeregulator.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from time import sleep
import hid
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
import configparser
import os
import pystray
import PIL.Image
from sys import exit
import sys
import _thread
import os
def resource_path(relative_path):
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
return os.path.join(application_path, relative_path)
class volumeregulator:
def __init__(self) -> None:
image = PIL.Image.open(resource_path("images/Soundmixer-Box-icon.png"))
self.icon = pystray.Icon("Soundmixer-Box", image, menu=pystray.Menu(
pystray.MenuItem("Config", self.openconfig),
pystray.MenuItem("Close", self.stop)
))
self.icon.run_detached()
self.startup(first=True)
def openconfig(self):
os.startfile(resource_path("configurator.exe"))
def stop(self):
self.icon.stop()
_thread.interrupt_main()
def startup(self, first=False):
config = configparser.ConfigParser()
try:
config.read(resource_path(f"config.ini"))
except:
with open(resource_path(f"config.ini", "w")) as f:
f.write("""
[device]
vendor_id = 9025
product_id = 32823
[1]
name = Spotify
process = Spotify.exe
[2]
name = Discord
process = Discord.exe
[3]
name = Game
process = fornite.exe,SuperHexagon.exe
[4]
name = Chrome
process = chrome.exe
""")
print("Created default config file")
config.read(resource_path(f"config.ini"))
try:
gamepad = hid.device()
try:
gamepad.open(int(config.get("device", "vendor_id")), int(config.get("device", "product_id")), config.get("device", "serial_number"))
except:
print("try without serial number")
gamepad.open(int(config.get("device", "vendor_id")), int(config.get("device", "product_id")))
print("No serial number used")
gamepad.set_nonblocking(True)
self.gamepad = gamepad
self.config = config
return
except:
print("Failed to open Device -exiting")
if not first: exit()
else: self.reconnect()
def convert(self,n):
if n >= 128:
conv = n - 128
else:
conv = n + 128
return round(conv*100 / 255)
def assign_buttons(self, report, device="main"):
if device == "main":
buttons = {
# "b01": bool(report[1] & 0b00000001),
# "b02": bool(report[1] & 0b00000010),
# "b03": bool(report[1] & 0b00000100),
# "b04": bool(report[1] & 0b00001000),
# "b05": bool(report[1] & 0b00010000),
# "b06": bool(report[1] & 0b00100000),
# "b07": bool(report[1] & 0b01000000),
# "b08": bool(report[1] & 0b10000000),
# "b09": bool(report[2] & 0b00000001),
# "b10": bool(report[2] & 0b00000010),
# "b11": bool(report[2] & 0b00000100),
# "b12": bool(report[2] & 0b00001000),
# "b13": bool(report[2] & 0b00010000),
# "b14": bool(report[2] & 0b00100000),
# "b15": bool(report[2] & 0b01000000),
# "b16": bool(report[2] & 0b10000000),
# "b17": bool(report[2] & 0b00000001),
# "b18": bool(report[3] & 0b00000010),
# "b19": bool(report[3] & 0b00000100),
# "b20": bool(report[3] & 0b00001000),
# "b21": bool(report[3] & 0b00010000),
# "b22": bool(report[3] & 0b00100000),
# "b23": bool(report[3] & 0b01000000),
# "b24": bool(report[3] & 0b10000000),
# "b25": bool(report[4] & 0b00000001),
# "b26": bool(report[4] & 0b00000010),
# "b27": bool(report[4] & 0b00000100),
# "b28": bool(report[4] & 0b00001000),
# "b29": bool(report[4] & 0b00010000),
# "b30": bool(report[4] & 0b00100000),
# "b31": bool(report[4] & 0b01000000),
# "b32": bool(report[4] & 0b10000000),
"x": self.convert(report[7]),
"y": round(int('{0:08b}'.format(report[8])[2:], 2)*100 / 62 - 2),
"z": self.convert(report[11]),
"t": self.convert(report[21])
}
return buttons
def setAudio(self,process, val):
if "," in process:
sessions = AudioUtilities.GetAllSessions()
processes = process.split(",")
for session in sessions:
if session.Process and session.Process.name() in processes:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
volume.SetMasterVolume(val, None)
else:
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
if session.Process and session.Process.name() == process:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
volume.SetMasterVolume(val, None)
def getAudio(self,process):
sessions = AudioUtilities.GetAllSessions()
if "," in process:
processes = process.split(",")
for session in sessions:
if session.Process and session.Process.name() in processes:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
return round(volume.GetMasterVolume() * 100)
else:
for session in sessions:
if session.Process and session.Process.name() == process:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
return round(volume.GetMasterVolume() * 100)
return 100
def run(self):
lastX = self.getAudio(self.config.get("1", "process"))
lastY = self.getAudio(self.config.get("2", "process"))
lastZ = self.getAudio(self.config.get("3", "process"))
lastT = self.getAudio(self.config.get("4", "process"))
print(f'{self.config.get("1", "name")}: {lastX}% {self.config.get("2", "name")}: {lastY}% {self.config.get("3", "name")}: {lastZ}% {self.config.get("4", "name")}: {lastT}%')
while True:
try:
report = self.gamepad.read(64)
except:
break
if report:
buttons = self.assign_buttons(report)
x = buttons['x']
if abs(lastX -x) >= 2:
if x >= 99: x = 100
elif x <= 1: x= 0
self.setAudio(self.config.get('1', 'process'), round(x/100, 2))
lastX = x
print(f"Set {self.config.get('1', 'name')} to {x}%")
y = buttons ['y']
if abs(lastY -y) >= 3: #lower toleranze
if y >= 98: y = 100 #lower toleranze
elif y <= 1: y= 0
self.setAudio(self.config.get('2', 'process'), round(y/100, 2))
lastY = y
print(f"Set {self.config.get('2', 'name')} to {y}%")
z = buttons['z']
if abs(lastZ -z) >= 2:
if z >= 99: z = 100
elif z <= 1: z= 0
self.setAudio(self.config.get('3', 'process'), round(z/100, 2))
lastZ = z
print(f"Set {self.config.get('3', 'name')} to {z}%")
t = buttons['t']
if abs(lastT -t) >= 2:
if t >= 99: t = 100
elif t <= 1: t= 0
self.setAudio(self.config.get('4', 'process'), round(t/100, 2))
lastT = t
print(f"Set {self.config.get('4', 'name')} to {t}%")
sleep(0.01)
self.reconnect()
def reconnect(self):
while True:
try:
self.startup()
break
except:
sleep(1)
self.run()
if __name__ == "__main__":
volumeregulator().run()