-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
212 lines (162 loc) · 6.23 KB
/
main.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
import json
import subprocess
import time
from typing import List
from pipewire_python import link
from pipewire_python.link import StereoInput, StereoOutput
def create_virtual_microphone(name="VirtualMicrophone"):
"""
Create a virtual microphone for PipeWire.
"""
try:
# Load the null sink (virtual audio sink)
sink_module = subprocess.run(
["pactl", "load-module", "module-null-sink", f"sink_name={name}"],
stdout=subprocess.PIPE,
check=True,
).stdout.decode("utf-8").strip()
print(f"Virtual sink '{name}' created with module ID: {sink_module}")
# Verify the sink creation using pactl list sinks
sinks_output = subprocess.run(
["pactl", "-f", "json", "list", "sinks"],
stdout=subprocess.PIPE,
check=True,
).stdout.decode("utf-8")
sinks = json.loads(sinks_output)
sink_node = None
for sink in sinks:
if sink["name"] == name:
sink_node = sink["index"]
break
if sink_node is None:
raise RuntimeError("Failed to find the virtual sink node.")
print(f"Sink node found with ID: {sink_node}")
# Link the virtual sink to a source (loopback)
# source_module = subprocess.run(
# [
# "pactl",
# "load-module",
# "module-loopback",
# f"source={name}.monitor",
# f"sink={name}",
# ],
# stdout=subprocess.PIPE,
# check=True,
# ).stdout.decode("utf-8").strip()
#
# print(f"Loopback source created with module ID: {source_module}")
print(f"Virtual microphone '{name}' setup successfully.")
except subprocess.CalledProcessError as e:
print(f"Error occurred while setting up virtual mic: {e}")
except Exception as ex:
print(f"Unexpected error: {ex}")
def delete_virtual_microphone(name="VirtualMicrophone"):
"""
Delete a virtual microphone created with PipeWire.
"""
try:
# List all loaded modules
modules_output = subprocess.run(
["pactl", "-f", "json", "list", "modules"],
stdout=subprocess.PIPE,
check=True,
).stdout.decode("utf-8")
modules = json.loads(modules_output)
modules_to_unload = []
# Find all modules related to the given name
for module in modules:
if module["argument"] is None:
continue
argument = module["argument"]
splittable_char_count = argument.count("=")
if splittable_char_count == 0:
continue
if splittable_char_count == 1:
split_argument = argument.split("=")
if split_argument[1] == name:
modules_to_unload.append(module["name"])
else:
split_arguments = argument.split(" ")
for split_argument_item in split_arguments:
split_argument = split_argument_item.split("=")
if len(split_argument) == 2 and split_argument[1] == name:
modules_to_unload.append(module["name"])
if not modules_to_unload:
print(f"No modules found for virtual microphone '{name}'.")
return
# Unload all related modules
for module_id in modules_to_unload:
subprocess.run(["pactl", "unload-module", str(module_id)], check=True)
print(f"Unloaded module ID: {module_id}")
print(f"Virtual microphone '{name}' successfully deleted.")
except subprocess.CalledProcessError as e:
print(f"Error occurred while deleting virtual mic: {e}")
except Exception as ex:
print(f"Unexpected error: {ex}")
def get_mic_list() -> List[StereoInput]:
inputs = link.list_inputs()
mic_list = []
for sink in inputs:
if not isinstance(sink, StereoInput):
continue
if sink.left.device == 'alsa_output.pci-0000_00_1f.3.analog-stereo':
continue
if sink.left.name != 'input_FL':
continue
mic_list.append(sink)
return mic_list
def pw_dump():
res = subprocess.run(
["pw-dump"],
stdout=subprocess.PIPE,
check=True,
).stdout.decode("utf-8").strip()
json_decoded = json.loads(res)
links_list = []
for item in json_decoded:
if item['type'] != 'PipeWire:Interface:Link':
continue
links_list.append({'input': item['info']['input-port-id'], 'output': item['info']['output-port-id']})
return links_list
def get_outputs_list(mic_names_list: List[str]) -> List[StereoOutput]:
outputs = link.list_outputs()
outputs_list = []
for source in outputs:
if not isinstance(source, StereoOutput):
continue
if source.left.device in mic_names_list:
continue
if source.left.device == 'alsa_output.pci-0000_00_1f.3.analog-stereo':
continue
if source.left.name != 'output_FL':
continue
outputs_list.append(source)
return outputs_list
def is_do_not_needed_link(mic: StereoInput, out: StereoOutput):
links_list = pw_dump()
for link_item in links_list:
if link_item['input'] == int(mic.left.id) and link_item['output'] == int(out.left.id):
return True
return False
def check_update():
mic_list = get_mic_list()
if len(mic_list) == 0:
print("[X] No microphone found")
return
mic_list_names = [mic.left.device for mic in mic_list]
outputs_list = get_outputs_list(mic_list_names)
for out in outputs_list:
for mic in mic_list:
try:
if is_do_not_needed_link(mic, out):
print(f"||> Skip connecting {out.left.device} to {mic.left.device}")
continue
out.connect(mic)
print(f"|> Connected {out.left.device} to {mic.left.device}")
except Exception as e:
print(f"[X] Failed to connect {out} to {mic}: {e}")
if __name__ == '__main__':
while True:
print("\n\n\n\n\n\n\n\n\n\nChecking for updates...")
check_update()
time.sleep(5)