-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_routing_visualiser.py
More file actions
77 lines (61 loc) · 2.94 KB
/
audio_routing_visualiser.py
File metadata and controls
77 lines (61 loc) · 2.94 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
import argparse
import json
import os
from datetime import datetime
import matplotlib.pyplot as plt
import pulsectl
from routing import get_audio_routing, generate_audio_state_json
from graph import create_audio_routing_graph, update_graph, save_graph_figure
import time
import sys
import signal
def signal_handler(sig, frame):
print('You pressed Ctrl-C! Exiting gracefully...')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def save_audio_state_to_file(state, directory):
if not os.path.exists(directory):
os.makedirs(directory)
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
file_path = os.path.join(directory, f'state_{timestamp}.json')
with open(file_path, 'w') as f:
json.dump(state, f, indent=4)
return file_path
def main():
parser = argparse.ArgumentParser( description="Visualize PulseAudio routing.")
parser.add_argument('--text_wrap', type=int, default=30,
help='Number of characters after which to wrap text labels.')
parser.add_argument('--hide', nargs='+', default=[],
help='List of strings to hide from graph labels.')
parser.add_argument('--ignore', nargs='+', default=[],
help='List of strings to ignore from audio sources and sinks.')
parser.add_argument('--active', action='store_true',
help='Only show active elements in the graph.')
parser.add_argument('--alpha', action='store_true',
help='Sort nodes alphabetically, rather than with minimised edge crossings.', default=False)
args = parser.parse_args()
print(f"Ignoring applications containing: {args.ignore}")
print(f"Hiding strings: {args.hide}")
plt.ion()
# Double the size of the initial window
fig, ax = plt.subplots(figsize=(20, 10))
pos = None
last_update_time = datetime.now()
previous_state = None
with pulsectl.Pulse('pulseaudio-routing-visualizer') as pulse:
while True:
current_state = generate_audio_state_json(pulse, previous_state=previous_state)
# Only update the graph if the audio configuration changes
if current_state["has_changed"]:
# Save the state to a file
state_file_path = save_audio_state_to_file(current_state, './graphs')
print(f"Saved state to {state_file_path}")
G = create_audio_routing_graph(current_state, text_wrap=args.text_wrap, hide_list=args.hide, ignore_list=args.ignore, only_active=args.active)
pos = update_graph( G, ax, fig, pos, datetime.now(), only_active=args.active, spring_layout=not(args.alpha))
save_graph_figure(G, pos, './graphs')
previous_state = current_state
last_update_time = datetime.now()
# Update every second without bringing the window to the front
fig.canvas.start_event_loop(1)
if __name__ == "__main__":
main()