Limit visualization update rate

This commit is contained in:
Correl Roush 2020-09-03 21:04:22 -04:00
parent 82bc2418e9
commit 57cdbf8ed3
2 changed files with 14 additions and 7 deletions

View file

@ -70,6 +70,7 @@ def run() -> "Iterator[Queue[Event]]":
try: try:
yield events yield events
except: except:
logging.exception("Terminating")
for process in processes: for process in processes:
if process.is_alive(): if process.is_alive():
process.terminate() process.terminate()

View file

@ -8,7 +8,7 @@ import pyglet # type: ignore
import pyglet.clock # type: ignore import pyglet.clock # type: ignore
import scipy.signal # type: ignore import scipy.signal # type: ignore
from turntable import application, turntable from turntable import application, models, turntable
class Plot: class Plot:
@ -32,9 +32,13 @@ class Plot:
self.color = color self.color = color
self.batch = batch or pyglet.graphics.Batch() self.batch = batch or pyglet.graphics.Batch()
self.lines: List[pyglet.shapes.Line] = [] self.lines: List[pyglet.shapes.Line] = []
self.audio = b""
def update(self, data): def update(self):
heights = scipy.signal.resample(data, self.bars) * self.height / 2 ** 16 data = np.fromstring(self.audio, dtype=np.int16)
fft = abs(np.fft.fft(data).real)
fft = fft[: len(fft) // 2]
heights = scipy.signal.resample(fft, self.bars) * self.height / 2 ** 16
self.lines = [ self.lines = [
pyglet.shapes.Line( pyglet.shapes.Line(
self.x + x / self.bars * self.width, self.x + x / self.bars * self.width,
@ -55,6 +59,7 @@ class Plot:
def main(): def main():
window = pyglet.window.Window(fullscreen=True) window = pyglet.window.Window(fullscreen=True)
with application.run() as events: with application.run() as events:
audio = b""
label = pyglet.text.Label( label = pyglet.text.Label(
"<Idle>", "<Idle>",
font_name="Noto Sans", font_name="Noto Sans",
@ -92,12 +97,13 @@ def main():
elif isinstance(event, turntable.NewMetadata): elif isinstance(event, turntable.NewMetadata):
label.text = event.title label.text = event.title
elif isinstance(event, turntable.Audio): elif isinstance(event, turntable.Audio):
data = np.fromstring(event.pcm.raw, dtype=np.int16) plot.audio = event.pcm.raw
fft = abs(np.fft.fft(data).real)
fft = fft[: len(fft) // 2]
plot.update(fft)
except queue.Empty: except queue.Empty:
... ...
def update_vis(dt):
plot.update()
pyglet.clock.schedule(check_events) pyglet.clock.schedule(check_events)
pyglet.clock.schedule_interval(update_vis, 0.03)
pyglet.app.run() pyglet.app.run()