-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_volume.py
More file actions
84 lines (70 loc) · 3.76 KB
/
control_volume.py
File metadata and controls
84 lines (70 loc) · 3.76 KB
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
import json
import platform
import subprocess
PLATFORM = platform.system().lower()
async def func(args):
if PLATFORM == "windows":
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
VOLUME_INTERFACE = cast(interface, POINTER(IAudioEndpointVolume))
try:
if 'set' in args:
target = max(0, min(100, int(args['set'])))
if PLATFORM == "darwin":
subprocess.run(["osascript", "-e", f"set volume output volume {target}"])
return json.dumps({"message": f"Volume set to {target}%"})
elif PLATFORM == "linux":
subprocess.run(["amixer", "-q", "sset", "Master", f"{target}%"])
return json.dumps({"message": f"Volume set to {target}%"})
elif PLATFORM == "windows":
if VOLUME_INTERFACE:
VOLUME_INTERFACE.SetMasterVolumeLevelScalar(target / 100.0, None)
return json.dumps({"message": f"Volume set to {target}%"})
return json.dumps({"error": "Windows volume control requires pycaw package"})
return json.dumps({"error": f"Unsupported operating system: {PLATFORM}"})
elif 'adjust' in args:
change = max(-100, min(100, int(args['adjust'])))
if PLATFORM == "darwin":
current = subprocess.getoutput("osascript -e 'output volume of (get volume settings)'")
new_vol = max(0, min(100, int(current) + change))
subprocess.run(["osascript", "-e", f"set volume output volume {new_vol}"])
return json.dumps({"message": f"Volume adjusted by {change}% to {new_vol}%"})
elif PLATFORM == "linux":
sign = "+" if change > 0 else "-"
subprocess.run(["amixer", "-q", "sset", "Master", f"{abs(change)}%{sign}"])
return json.dumps({"message": f"Volume adjusted by {change}%"})
elif PLATFORM == "windows":
if VOLUME_INTERFACE:
current_vol = VOLUME_INTERFACE.GetMasterVolumeLevelScalar()
new_vol = max(0.0, min(1.0, current_vol + (change / 100.0)))
VOLUME_INTERFACE.SetMasterVolumeLevelScalar(new_vol, None)
return json.dumps({"message": f"Volume adjusted by {change}% to {int(new_vol * 100)}%"})
return json.dumps({"error": "Windows volume control requires pycaw package"})
return json.dumps({"error": f"Unsupported operating system: {PLATFORM}"})
return json.dumps({"error": "Please specify either 'set' or 'adjust' in the arguments"})
except Exception as e:
return json.dumps({"error": str(e)})
object = {
"name": "controlVolume",
"description": f"Control system volume on {PLATFORM.capitalize()}. Required format: For setting volume use {{\"set\": number}} where number is 0-100, or for adjusting volume use {{\"adjust\": number}} where number is -100 to +100.",
"parameters": {
"type": "object",
"properties": {
"set": {
"type": "integer",
"description": "Set volume to specific level (0-100)"
},
"adjust": {
"type": "integer",
"description": "Adjust volume by relative amount (-100 to +100)"
}
},
"oneOf": [
{"required": ["set"]},
{"required": ["adjust"]}
]
}
}