]> git.mar77i.info Git - musicbox/commitdiff
add some debug output, optimize rects master
authormar77i <mar77i@protonmail.ch>
Mon, 19 Aug 2024 23:47:22 +0000 (01:47 +0200)
committermar77i <mar77i@protonmail.ch>
Mon, 19 Aug 2024 23:47:22 +0000 (01:47 +0200)
musicbox.py
visualization.py

index bebad217d3b69a112eae1b4f9e63cc102929008e..a04787c6b6f3febf9d814a40f5239505ce6aa8f2 100755 (executable)
@@ -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):
index 0d09e6e9f4fbae7478cb2440a149937980f7451f..5994e0c4c46b2a1cadcf163c3320e40f3baecee5 100644 (file)
@@ -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)