From f2756c355a254d68008831a730efe202dcb2c8fe Mon Sep 17 00:00:00 2001 From: mar77i Date: Tue, 20 Aug 2024 01:47:22 +0200 Subject: [PATCH] add some debug output, optimize rects --- musicbox.py | 20 +++++++++++++++++++- visualization.py | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/musicbox.py b/musicbox.py index bebad21..a04787c 100755 --- a/musicbox.py +++ b/musicbox.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 +# todo: setup_venv.sh + +from time import time + import numpy import pygame @@ -24,6 +28,7 @@ class ChannelManager: self.envelope = ADSR.Envelope(s(.2), s(.4), .75, s(.5)) self.tones = {} self.counter = 0 + self.eta = time() + 1 / chunks_per_second @staticmethod def duplicate_channel(g): @@ -47,11 +52,16 @@ class ChannelManager: if self.channel: self.channel = None self.counter = 0 + self.eta = 0 return if self.channel is None: self.channel = pygame.mixer.Channel(True) if self.channel.get_busy() and self.channel.get_queue() is not None: return + t = time() + print("gening", t, self.eta) + if self.eta is not None and self.eta < t: + print("buffer underrun") sample_slice = slice(self.counter, self.counter + self.chunk_size) self.queue( self.channel, @@ -65,6 +75,7 @@ class ChannelManager: self.sample_type ) self.counter = sample_slice.stop + self.eta = time() + self.chunk_size / self.sample_rate def update_tones(self, frequencies): have_keys = {frequency for frequency, tone in self.tones.items() if tone.signal} @@ -86,9 +97,12 @@ class ChannelManager: class MusicBox: def __init__(self): + pygame.mixer.pre_init(buffer=2048) pygame.init() assert hasattr(pygame.constants, "FINGERMOTION") - self.surf = pygame.display.set_mode((2200, 1400)) + self.surf = pygame.display.set_mode(flags=pygame.FULLSCREEN) + pygame.freetype.init() + self.font = pygame.freetype.Font(None, size=48) self.running = True self.dirty = False self.clock = pygame.time.Clock() @@ -116,6 +130,8 @@ class MusicBox: elif ev.type == pygame.KEYDOWN: if ev.key == pygame.K_ESCAPE: self.running = False + elif ev.key == pygame.K_r: + self.fingers.clear() elif ev.type == pygame.WINDOWEXPOSED: self.dirty = True elif ev.type == pygame.MOUSEBUTTONDOWN: @@ -137,6 +153,8 @@ class MusicBox: self.surf.fill("darkgray") self.piano_keys.draw(self.surf) self.key_vis.draw(self.surf) + fs, r = self.font.render(str(len(self.channel_manager.tones)), "black") + self.surf.blit(fs, (self.surf.get_width() - r.width - 30, 30)) pygame.display.update() def update(self): diff --git a/visualization.py b/visualization.py index 0d09e6e..5994e0c 100644 --- a/visualization.py +++ b/visualization.py @@ -34,6 +34,9 @@ class KeyVisualization: height = (s.end_time - s.start_time) * self.PX_PER_SEC else: height = (t - s.start_time) * self.PX_PER_SEC + max_height = surf.get_height() - s_top + if height > max_height: + height = max_height rect = pygame.Rect( (s.index * width + 5, s_top), (width - 10, height) -- 2.47.0