Skip to content

Commit 98949c8

Browse files
authored
Merge pull request #2 from adafruit/update_parser_api
update parser api
2 parents fab5655 + 0aa3d94 commit 98949c8

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

README.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Usage Example
9494
9595
import adafruit_midi_parser
9696
97+
parser = adafruit_midi_parser.MIDIParser()
98+
9799
midi_file = "/song.mid"
98100
99101
print("MIDI File Analyzer")
@@ -102,9 +104,8 @@ Usage Example
102104
file_list = os.listdir("/")
103105
if midi_file[1:] in file_list:
104106
print(f"\nFound MIDI file {midi_file}")
105-
parser = adafruit_midi_parser.MIDIParser(midi_file)
106107
print("\nParsing MIDI file...")
107-
parser.parse()
108+
parser.parse(midi_file)
108109
print("\nMIDI File Information:")
109110
print("=====================")
110111
print(f"Format Type: {parser.format_type}")

adafruit_midi_parser.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ class MIDIParser:
4646
:param str filename: Path to the MIDI file
4747
"""
4848

49-
def __init__(self, filename: str) -> None:
49+
def __init__(self) -> None:
5050
"""
5151
Initialize the MIDI parser.
5252
5353
:param str filename: Path to the MIDI file
5454
"""
55-
self._filename: str = filename
55+
self._filename: Optional[str] = None
5656
self._events: List[Dict[str, Any]] = []
5757
self._tempo: int = 500000 # Default tempo (microseconds per quarter note)
5858
self._ticks_per_beat: int = 480 # Default time division
@@ -188,7 +188,21 @@ def _read_variable_length(data: bytes, offset: int) -> Tuple[int, int]:
188188
break
189189
return value, offset
190190

191-
def parse(self, debug: bool = False) -> bool: # noqa: PLR0912 PLR0915 PLR0914
191+
def clear(self) -> None:
192+
"""
193+
Clear all parsed data and reset the parser state.
194+
"""
195+
self._filename = None
196+
self._events = []
197+
self._tempo = 500000 # Default tempo
198+
self._ticks_per_beat = 480 # Default division
199+
self._current_event_index = 0
200+
self._format_type = 0
201+
self._num_tracks = 0
202+
self._parsed = False
203+
self._last_absolute_time = 0
204+
205+
def parse(self, filename: str, debug: bool = False) -> bool: # noqa: PLR0912 PLR0915 PLR0914
192206
"""
193207
Parse the MIDI file and extract events.
194208
@@ -197,6 +211,8 @@ def parse(self, debug: bool = False) -> bool: # noqa: PLR0912 PLR0915 PLR0914
197211
:rtype: bool
198212
:raises MIDIParseError: If the file doesn't exist or is not a valid MIDI file
199213
"""
214+
self.clear()
215+
self._filename = filename
200216
try: # noqa: PLR1702
201217
with open(self._filename, "rb") as file:
202218
data = file.read()

examples/midi_parser_player_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ def on_playback_complete(self): # noqa: PLR6301
5454
print(f"Found MIDI file {midi_file}")
5555

5656
# Create a MIDIParser instance
57-
parser = adafruit_midi_parser.MIDIParser(midi_file)
57+
parser = adafruit_midi_parser.MIDIParser()
5858

5959
# Parse the file
60-
parser.parse()
60+
parser.parse(midi_file)
6161
print(f"Successfully parsed! Found {len(parser.events)} events.")
6262
print(f"BPM: {parser.bpm:.1f}")
6363
print(f"Note Count: {parser.note_count}")

examples/midi_parser_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
# Check if the file exists
2222
if midi_file[1:] in file_list:
2323
print(f"\nFound MIDI file {midi_file}")
24-
parser = adafruit_midi_parser.MIDIParser(midi_file)
24+
parser = adafruit_midi_parser.MIDIParser()
2525
print("\nParsing MIDI file...")
26-
parser.parse()
26+
parser.parse(midi_file)
2727
print("\nMIDI File Information:")
2828
print("=====================")
2929
print(f"Format Type: {parser.format_type}")

0 commit comments

Comments
 (0)