From 0aa3d941004acaade4d1d54d47c7c218272628e3 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 13 May 2025 13:56:40 -0400 Subject: [PATCH] update parser api Update parser API so that a MIDI file is passed to parse() rather than during the init. This way you can dynamically pass a midi file to the parser in user code --- README.rst | 5 +++-- adafruit_midi_parser.py | 22 +++++++++++++++++++--- examples/midi_parser_player_example.py | 4 ++-- examples/midi_parser_simpletest.py | 4 ++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 26b3f04..36da81b 100644 --- a/README.rst +++ b/README.rst @@ -94,6 +94,8 @@ Usage Example import adafruit_midi_parser + parser = adafruit_midi_parser.MIDIParser() + midi_file = "/song.mid" print("MIDI File Analyzer") @@ -102,9 +104,8 @@ Usage Example file_list = os.listdir("/") if midi_file[1:] in file_list: print(f"\nFound MIDI file {midi_file}") - parser = adafruit_midi_parser.MIDIParser(midi_file) print("\nParsing MIDI file...") - parser.parse() + parser.parse(midi_file) print("\nMIDI File Information:") print("=====================") print(f"Format Type: {parser.format_type}") diff --git a/adafruit_midi_parser.py b/adafruit_midi_parser.py index fc0eea6..4e7e1bd 100644 --- a/adafruit_midi_parser.py +++ b/adafruit_midi_parser.py @@ -46,13 +46,13 @@ class MIDIParser: :param str filename: Path to the MIDI file """ - def __init__(self, filename: str) -> None: + def __init__(self) -> None: """ Initialize the MIDI parser. :param str filename: Path to the MIDI file """ - self._filename: str = filename + self._filename: Optional[str] = None self._events: List[Dict[str, Any]] = [] self._tempo: int = 500000 # Default tempo (microseconds per quarter note) self._ticks_per_beat: int = 480 # Default time division @@ -188,7 +188,21 @@ def _read_variable_length(data: bytes, offset: int) -> Tuple[int, int]: break return value, offset - def parse(self, debug: bool = False) -> bool: # noqa: PLR0912 PLR0915 PLR0914 + def clear(self) -> None: + """ + Clear all parsed data and reset the parser state. + """ + self._filename = None + self._events = [] + self._tempo = 500000 # Default tempo + self._ticks_per_beat = 480 # Default division + self._current_event_index = 0 + self._format_type = 0 + self._num_tracks = 0 + self._parsed = False + self._last_absolute_time = 0 + + def parse(self, filename: str, debug: bool = False) -> bool: # noqa: PLR0912 PLR0915 PLR0914 """ Parse the MIDI file and extract events. @@ -197,6 +211,8 @@ def parse(self, debug: bool = False) -> bool: # noqa: PLR0912 PLR0915 PLR0914 :rtype: bool :raises MIDIParseError: If the file doesn't exist or is not a valid MIDI file """ + self.clear() + self._filename = filename try: # noqa: PLR1702 with open(self._filename, "rb") as file: data = file.read() diff --git a/examples/midi_parser_player_example.py b/examples/midi_parser_player_example.py index 14a7290..e6a91f5 100644 --- a/examples/midi_parser_player_example.py +++ b/examples/midi_parser_player_example.py @@ -54,10 +54,10 @@ def on_playback_complete(self): # noqa: PLR6301 print(f"Found MIDI file {midi_file}") # Create a MIDIParser instance - parser = adafruit_midi_parser.MIDIParser(midi_file) + parser = adafruit_midi_parser.MIDIParser() # Parse the file - parser.parse() + parser.parse(midi_file) print(f"Successfully parsed! Found {len(parser.events)} events.") print(f"BPM: {parser.bpm:.1f}") print(f"Note Count: {parser.note_count}") diff --git a/examples/midi_parser_simpletest.py b/examples/midi_parser_simpletest.py index 919c6b7..64736b0 100644 --- a/examples/midi_parser_simpletest.py +++ b/examples/midi_parser_simpletest.py @@ -21,9 +21,9 @@ # Check if the file exists if midi_file[1:] in file_list: print(f"\nFound MIDI file {midi_file}") - parser = adafruit_midi_parser.MIDIParser(midi_file) + parser = adafruit_midi_parser.MIDIParser() print("\nParsing MIDI file...") - parser.parse() + parser.parse(midi_file) print("\nMIDI File Information:") print("=====================") print(f"Format Type: {parser.format_type}")