From cd54c4cf39a1a0f7bf936dece083610cd66f2717 Mon Sep 17 00:00:00 2001 From: mar77i Date: Wed, 10 Jul 2024 02:10:12 +0200 Subject: [PATCH] clean up pressed_keys and disappearing of segments in key_vis. --- musicbox.py | 11 ++--------- piano_keys.py | 39 ++++++++++++++++++++++++++++++++------- visualization.py | 14 +++++++++----- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/musicbox.py b/musicbox.py index 0b9687e..bebad21 100755 --- a/musicbox.py +++ b/musicbox.py @@ -100,11 +100,9 @@ class MusicBox: PitchLookup(432), 2, ) - self.pressed_keys = set() self.fingers = {} self.key_vis = KeyVisualization( self.piano_keys, - self.pressed_keys, pygame.Rect((0, middle), (size[0], middle)) ) @@ -142,14 +140,9 @@ class MusicBox: pygame.display.update() def update(self): - self.pressed_keys.clear() - self.pressed_keys.update(( - key for key in ( - self.piano_keys.find_key(pos) for pos in self.fingers.values() - ) if key - )) + self.piano_keys.update(self.fingers.values()) self.channel_manager.update_tones( - {key.frequency for key in self.pressed_keys} + {key.frequency for key in self.piano_keys.pressed_keys} ) self.channel_manager.update() self.key_vis.update() diff --git a/piano_keys.py b/piano_keys.py index 8c07b23..ff6845b 100644 --- a/piano_keys.py +++ b/piano_keys.py @@ -4,10 +4,11 @@ import pygame class Key: - def __init__(self, frequency, color, polygon): + def __init__(self, spn, frequency, black_key, polygon): + self.spn = spn self.frequency = frequency - self.color = color self.polygon = polygon + self.pressed = False class PianoKeys: @@ -27,6 +28,7 @@ class PianoKeys: self.num_white = int(7 * num_octaves + 1) self.rect = rect self.keys = [] + self.pressed_keys = set() border_pixels = (self.num_white + 1) * self.STYLE["border"] white_width = (rect.width - border_pixels) / self.num_white x = self.STYLE["border"] @@ -34,10 +36,12 @@ class PianoKeys: r = pygame.Rect((int(x), 0), (int(white_width), rect.height)) key_name = self.WHITE_KEY_NAMES[n % len(self.WHITE_KEY_NAMES)] octave = start_octave + n // len(self.WHITE_KEY_NAMES) + spn = f"{key_name}{octave}" self.keys.append( Key( - pitch_lookup(f"{key_name}{octave}"), - "white", + spn, + pitch_lookup(spn), + False, [r.bottomleft, r.topleft, r.topright, r.bottomright], ) ) @@ -65,11 +69,13 @@ class PianoKeys: ) octave = start_octave + n // len(self.BLACK_KEY_NAMES) ins_off += 1 + spn = f"{key_name}{octave}" self.keys.insert( n + ins_off, Key( - pitch_lookup(f"{key_name}{octave}"), - "black", + spn, + pitch_lookup(spn), + True, [r.bottomleft, r.topleft, r.topright, r.bottomright], ) ) @@ -116,7 +122,26 @@ class PianoKeys: def draw(self, surf): for key in self.keys: - pygame.draw.polygon(surf, key.color, key.polygon) + if key.pressed: + color = "gray" + elif len(key.spn) > 2: + color = "black" + else: + color = "white" + pygame.draw.polygon(surf, color, key.polygon) + + def update(self, poss): + new_pressed_keys = { + key for key in ( + self.find_key(pos) for pos in poss + ) if key + } + for key in self.pressed_keys - new_pressed_keys: + key.pressed = False + self.pressed_keys.remove(key) + for key in new_pressed_keys - self.pressed_keys: + key.pressed = True + self.pressed_keys.add(key) def main(): diff --git a/visualization.py b/visualization.py index 1846511..0d09e6e 100644 --- a/visualization.py +++ b/visualization.py @@ -13,9 +13,8 @@ class KeyVisualization: self.start_time = start_time self.end_time = end_time - def __init__(self, piano_keys, pressed_keys, rect): + def __init__(self, piano_keys, rect): self.piano_keys = piano_keys - self.pressed_keys = pressed_keys self.rect = rect self.segments = [] @@ -24,10 +23,14 @@ class KeyVisualization: num_piano_keys = len(self.piano_keys.keys) width = self.rect.width // num_piano_keys top = self.rect.top + 15 - for s in self.segments: + for s in iter(self.segments): s_top = top if s.end_time: - s_top += (t - s.end_time) * self.PX_PER_SEC + y_offset = (t - s.end_time) * self.PX_PER_SEC + if y_offset > self.rect.height: + self.segments.remove(s) + continue + s_top += y_offset height = (s.end_time - s.start_time) * self.PX_PER_SEC else: height = (t - s.start_time) * self.PX_PER_SEC @@ -40,12 +43,13 @@ class KeyVisualization: for v in hsv_to_rgb(s.index / num_piano_keys, 1, 1) ) pygame.draw.rect(surf, rgb, rect) + def update(self): running_segments_per_index = { s.index: s for s in self.segments if s.end_time is None } t = time() - for pressed_key in self.pressed_keys: + for pressed_key in self.piano_keys.pressed_keys: index = self.piano_keys.keys.index(pressed_key) if index not in running_segments_per_index: self.segments.append(self.Segment(index, t)) -- 2.47.0