Skip to content

Instantly share code, notes, and snippets.

@SonOfLilit
Last active May 30, 2022 21:56
Show Gist options
  • Save SonOfLilit/5e11768489fb1d1e2cd63bfddd1ffaef to your computer and use it in GitHub Desktop.
Save SonOfLilit/5e11768489fb1d1e2cd63bfddd1ffaef to your computer and use it in GitHub Desktop.
Official solution to refactoring challenge from Clean Code on Toilet #1: Naming Things.
# Sequencer is the music production term of art for what this does
class Sequencer:
def toggle_record(self, value):
self.is_recording = value
if not value:
for note in self.held_notes.values():
self.record_midi(midi.NoteOff(note.pitch, note.velocity))
self.held_notes = {}
def handle_note(self, note):
self.engine.send_midi(note)
if self.is_playing and self.is_recording:
self.record_midi(note)
def record_midi(self, note):
if isinstance(note, midi.NoteOn):
self.held_notes[note.pitch] = note
elif isinstance(note, midi.NoteOff):
self.held_notes.pop(note.pitch)
# `ticks` is the programming term of art for an int time
# (expressed in "frames" of some kind)
bisect.insort(self.sequence, (self.ticks, note))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment