#!/usr/bin/env python3
+# todo: setup_venv.sh
+
+from time import time
+
import numpy
import pygame
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):
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,
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}
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()
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:
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):
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)