-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.pyw
391 lines (362 loc) · 15.7 KB
/
main.pyw
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from pynput import keyboard
from configparser import ConfigParser
from popup import showPopup, popupRoot
import json
import websocket
import time
import os
import threading
import logging
# Change working directory to script location
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Classes
class Mixer:
# These should not change.
Local = "com.elgato.mix.local"
Stream = "com.elgato.mix.stream"
class Input:
# If you have more inputs, like an additional microphone, you should add and label them here
System = "PCM_OUT_01_V_00_SD2"
Music = "PCM_OUT_01_V_02_SD3"
Browser = "PCM_OUT_01_V_04_SD4"
VoiceChat = "PCM_OUT_01_V_06_SD5"
SFX = "PCM_OUT_01_V_08_SD6"
Game = "PCM_OUT_01_V_10_SD7"
Aux1 = "PCM_OUT_01_V_12_SD8"
Aux2 = "PCM_OUT_01_V_14_SD9"
class Output:
# You can find your identifiers and their names by running the script and checking the logs for "available outputs" or "selected output"
Speakers = {
"identifier": "HDAUDIO#FUNC_01&VEN_10EC&DEV_1168&SUBSYS_104387C5&REV_1001#5&32F1D1AA&0&0001#{6994AD04-93EF-11D0-A3CC-00A0C9223196}\\ELINEOUTWAVE",
"name": "Speakers (High Definition Audio Device)"
}
Headphones = {
"identifier": "PCM_OUT_01_C_00_SD1",
"name": "Headphones (Elgato Wave:3)"
}
# Variables
current_volume = None
is_muted = None
config = ConfigParser()
# Read config file
config.read("config.ini")
# Set variables from config
step = int(config.get("main", "step"))
websocket_url = str(config.get("main", "websocket_url"))
# Initialize logging
logging.basicConfig(
filename="latest.log",
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
filemode="w"
)
# WebSocket connection handler
def on_message(ws, message):
global current_volume, is_muted
response = json.loads(message)
# Operations without method key
if "result" in response:
# For initial volume
if "localMixer" in response["result"]:
local_mixer = response["result"]["localMixer"]
is_muted = local_mixer[0]
current_volume = local_mixer[1]
if current_volume is not None:
logging.debug(f"Connection received, current volume: {current_volume}")
elif "outputs" in response["result"]:
available_outputs = response["result"]["outputs"]
selected_output = response["result"]["selectedOutput"]
logging.debug(f"Available outputs: {json.dumps(available_outputs)}")
logging.debug(f"Selected output: {json.dumps(selected_output)}")
# Mute Status
elif "method" in response and response["method"] == "outputMuteChanged":
if response["params"]["mixerID"] == Mixer.Local:
is_muted = response["params"]["value"]
logging.debug(f"Mute changed to: {is_muted}")
if is_muted:
showPopup(f" Muted ({current_volume})", "Output Volume")
else:
showPopup(f" Unmuted ({current_volume})", "Output Volume")
else:
logging.debug(f"Mute changed on mixer: {response['params']['mixerID']} - {response['params']['value']}")
# Input Mute Status
elif "method" in response and response["method"] == "inputMuteChanged":
logging.debug(f"Input mute changed on mixer: {response['params']['mixerID']}, identifier: {response['params']['identifier']} - {response['params']['value']}")
# Input Volumes
elif "method" in response and response["method"] == "inputVolumeChanged":
# We only want to show the popup for one mixer, in this case the local mixer.
# This is so we don't get multiple popups for the same volume change - notibly when faders are linked.
if response["params"]["mixerID"] == Mixer.Local:
match response["params"]["identifier"]:
case Input.System:
logging.debug(f"System volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "System Volume")
case Input.Music:
logging.debug(f"Music volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "Music Volume")
case Input.Browser:
logging.debug(f"Browser volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "Browser Volume")
case Input.VoiceChat:
logging.debug(f"Voice Chat volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "Voice Chat Volume")
case Input.SFX:
logging.debug(f"SFX volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "SFX Volume")
case Input.Game:
logging.debug(f"Game volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "Game Volume")
case Input.Aux1:
logging.debug(f"Aux 1 volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "Aux 1 Volume")
case Input.Aux2:
logging.debug(f"Aux 2 volume changed to: {response['params']['value']}")
showPopup(f" {response['params']['value']}", "Aux 2 Volume")
# Output Volumes
elif "method" in response and response["method"] == "outputVolumeChanged":
if response["params"]["mixerID"] == Mixer.Local:
current_volume = response["params"]["value"]
logging.debug(f"Volume changed to: {current_volume}")
showPopup(f" {current_volume}", "Output Volume")
else:
logging.debug(f"Volume changed on mixer: {response['params']['mixerID']} - {response['params']['value']}")
# Output Changes
elif "method" in response and response["method"] == "selectedOutputChanged":
logging.debug(f"Output on mixer {response["params"]["mixerID"]} changed to identifier: {response['params']["identifier"]}")
# For my specific setup these are the identifiers for my speakers and headphones
# You can find your identifiers by running the script and checking the logs for "available outputs" or "selected output"
if response["params"]["mixerID"] == Mixer.Local:
if response["params"]["identifier"] == Output.Speakers["identifier"]:
showPopup(" Speakers", "Output Changed")
elif response["params"]["identifier"] == Output.Headphones["identifier"]:
showPopup(" Headphones", "Output Changed")
# WebSocket error handler
def on_error(ws, error):
logging.error(f"WebSocket error: {error}")
# WebSocket close handler
def on_close(ws, close_status_code, close_msg):
logging.info(f"WebSocket connection was closed: {close_status_code} - {close_msg}")
reconnect()
# WebSocket open handler
def on_open(ws):
logging.info("WebSocket connection opened")
get_output_config()
get_output_devices()
# Function to get output configuration
def get_output_config():
volume_message = {
"id": 1,
"jsonrpc": "2.0",
"method": "getOutputConfig"
}
ws.send(json.dumps(volume_message))
# Function to get available output devices
def get_output_devices():
message = {
"id": 1,
"jsonrpc": "2.0",
"method": "getOutputs",
}
ws.send(json.dumps(message))
# Function to change output device.
# This is not used in my use case, but can be used to set the current output device to a specific device.
# You can get your available output devices by running the script and checking the logs for "available outputs" or "selected output"
def change_output_device(device: Output):
# Only run if the socket is connected
if ws.sock.connected:
try:
logging.debug(f"Change output device was called with identifier: {device['identifier']} and name: {device['name']}")
message = {
"id": 1,
"jsonrpc": "2.0",
"method": "setSelectedOutput",
"params": {
"identifier": device["identifier"],
"name": device["name"]
}
}
ws.send(json.dumps(message))
except Exception as e:
logging.error(f"Error changing output device: {e}")
else:
logging.error("Change output device was called, but the socket is closed")
# Function to increase output volume by configured step
def increase_output_volume(mixer: Mixer):
# Only run if the socket is connected
if ws.sock.connected:
try:
logging.debug(f"Increase output volume was called on mixer: {mixer}")
if current_volume is not None:
# Increase current volume by step
new_volume = current_volume + step
# Set the volume to the new value on provided mixer
set_output_volume(mixer, new_volume)
except Exception as e:
logging.error(f"Error increasing output volume: {e}")
else:
logging.error("Increase volume was called, but the socket is closed.")
# Function to decrease output volume by configured step
def decrease_output_volume(mixer: Mixer):
# Only run if the socket is connected
if ws.sock.connected:
try:
logging.debug(f"Decrease output volume was called on mixer: {mixer}")
if current_volume is not None:
# Decrease current volume by step
new_volume = current_volume - step
# Set the volume to the new value on provided mixer
set_output_volume(mixer, new_volume)
except Exception as e:
logging.error(f"Error decreasing output volume: {e}")
else:
logging.error("Decrease output volume was called, but the socket is closed.")
# Function to set output volume to a specific value
def set_output_volume(mixer: Mixer, volume: int):
# Only run if the socket
if ws.sock.connected:
try:
logging.debug(f"Set output volume was called on mixer: {mixer}")
volume_message = {
"id": 1,
"jsonrpc": "2.0",
"method": "setOutputConfig",
"params": {
"property": "Output Level",
"mixerID": mixer,
"value": volume,
"forceLink": False
}
}
ws.send(json.dumps(volume_message))
except Exception as e:
logging.error(f"Error setting output volume: {e}")
else:
logging.error("Set output volume was called, but the socket is closed")
# Function to set toggle the output mute state
def toggle_output_mute(mixer: Mixer):
# Only run if the socket is connected
if ws.sock.connected:
try:
# New mute state is the opposite of the current mute state
new_mute_state = not is_muted
# Set the new mute state on the provided mixer
set_mute_output(mixer, new_mute_state)
except Exception as e:
logging.error(f"Error toggling output mute: {e}")
else:
logging.error("Toggle mute was called, but the socket is closed")
# Function to mute output
def set_mute_output(mixer: Mixer, value: bool):
# Only run if the socket is connected
if ws.sock.connected:
try:
logging.debug(f"Mute was called on mixer: {mixer}")
mute_message = {
"id": 1,
"jsonrpc": "2.0",
"method": "setOutputConfig",
"params": {
"property": "Output Mute",
"mixerID": mixer,
"value": value,
"forceLink": False
}
}
ws.send(json.dumps(mute_message))
except Exception as e:
logging.error(f"Error muting output: {e}")
else:
logging.error("Mute was called, but the socket is closed")
# Function to set input volume to a specific value
# This is not used in my use case, but can be used to set the volume of specific inputs.
# You can get the current volume of inputs by listening to the inputVolumeChanged method in the on_message function.
def set_input_volume(mixer: Mixer, identifier: Input, volume: int):
# Only run if the socket is connected
if ws.sock.connected:
try:
logging.debug(f"Set input volume was called on mixer: {mixer} and identifier: {identifier}")
volume_message = {
"id": 1,
"jsonrpc": "2.0",
"method": "setInputConfig",
"params": {
"property": "Volume",
"identifier": identifier,
"mixerID": mixer,
"forceLink": False,
"value": volume
}
}
ws.send(json.dumps(volume_message))
except Exception as e:
logging.error(f"Error setting input volume: {e}")
else:
logging.error("Set input volume was called, but the socket is closed")
# Same as function above, this also is not used in my use case, but can be used to mute specific inputs.
# You can get the mute state of inputs by listening to the inputMuteChanged method in the on_message function.
def set_input_mute(mixer: Mixer, identifier: Input, value: bool):
# Only run if the socket is connected and when a mixer is provided
if ws.sock.connected:
try:
logging.debug(f"Set input mute was called on mixer: {mixer} and identifier: {identifier}")
mute_message = {
"id": 1,
"jsonrpc": "2.0",
"method": "setInputConfig",
"params": {
"property": "Mute",
"identifier": identifier,
"mixerID": mixer,
"forceLink": False,
"value": value
}
}
ws.send(json.dumps(mute_message))
except Exception as e:
logging.error(f"Error setting input mute: {e}")
else:
logging.error("Set input mute was called, but the socket is closed")
# WebSocket setup
ws = websocket.WebSocketApp(
websocket_url,
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open
)
# Function to reconnect WebSocket
def reconnect():
while True:
try:
logging.info("Attempting to reconnect to WebSocket")
ws.run_forever()
logging.info("Successfully reconnected to WebSocket")
break
except Exception as e:
logging.error(f"Failed to reconnect to WebSocket: {e}")
time.sleep(5) # Wait before attempting to reconnect again
# Start WebSocket on separate thread
def run_ws():
ws.run_forever()
ws.thread = threading.Thread(target=run_ws)
ws.thread.daemon = True
ws.thread.start()
# Handle keyboard presses through pynput
def on_press(key):
try:
if key == keyboard.Key.f13:
increase_output_volume(Mixer.Local)
elif key == keyboard.Key.f14:
decrease_output_volume(Mixer.Local)
elif key == keyboard.Key.f15:
toggle_output_mute(Mixer.Local)
except Exception as e:
logging.error(f"Error handling key press: {e}")
# Start listening for keyboard events
listener = keyboard.Listener(on_press=on_press)
listener.start()
# Start WebSocket listener
if __name__ == "__main__":
logging.info("Script started")
# Start the Tkinter main loop for the volume popups
popupRoot.mainloop()