Skip to content

Commit a05f57c

Browse files
committed
cavalcad optimization for multi-screen
1 parent 25b00bd commit a05f57c

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

modules/cavalcade.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,21 @@ class Cava:
4141
RESTARTING = 2
4242
CLOSING = 3
4343

44-
def __init__(self, mainapp):
44+
def data_handler(self, *a, **kw):
45+
"""Call all registered handlers with the provided arguments."""
46+
for h in self._handlers:
47+
h(*a, **kw)
48+
49+
def register_handler(self, handler):
50+
self._handlers.append(handler)
51+
52+
def __init__(self):
4553
self.bars = bars
4654
self.path = "/tmp/cava.fifo"
4755

4856
self.cava_config_file = CAVA_CONFIG
49-
self.data_handler = mainapp.draw.update
57+
self._handlers = []
58+
self._started = False
5059
self.command = ["cava", "-p", self.cava_config_file]
5160
self.state = self.NONE
5261
self.process = None
@@ -127,8 +136,11 @@ def _on_stop(self):
127136

128137
def start(self):
129138
"""Launch cava"""
139+
if self._started:
140+
return
130141
self._start_io_reader()
131142
self._run_process()
143+
self._started = True
132144

133145
def restart(self):
134146
"""Restart cava process"""
@@ -309,13 +321,25 @@ def color_update(self):
309321
blue = int(color[5:7], 16) / 255
310322
self.color = Gdk.RGBA(red=red, green=green, blue=blue, alpha=1.0)
311323

324+
325+
_instances = {}
326+
327+
328+
def getCava() -> Cava:
329+
if "cava" not in _instances:
330+
_instances["cava"] = Cava()
331+
return _instances["cava"]
332+
333+
312334
class SpectrumRender:
313335
def __init__(self, mode=None, **kwargs):
314336
super().__init__(**kwargs)
315337
self.mode = mode
316338

317339
self.draw = Spectrum()
318-
self.cava = Cava(self)
340+
self.cava = getCava()
341+
self.cava.register_handler(self.draw.update)
342+
319343
self.cava.start()
320344

321345
def get_spectrum_box(self):

0 commit comments

Comments
 (0)