Simple test

Ensure your MIDI file can be parsed with this simple test.

examples/midi_parser_simpletest.py
 1# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5"""
 6Simple example showing how to use the adafruit_midi_parser library
 7to open a MIDI file and display information about it.
 8"""
 9
10import os
11
12import adafruit_midi_parser
13
14midi_file = "/song.mid"  # Your MIDI file name
15
16print("MIDI File Analyzer")
17print("=================")
18print(f"Looking for: {midi_file}")
19file_list = os.listdir("/")
20
21# Check if the file exists
22if midi_file[1:] in file_list:
23    print(f"\nFound MIDI file {midi_file}")
24    parser = adafruit_midi_parser.MIDIParser()
25    print("\nParsing MIDI file...")
26    parser.parse(midi_file)
27    print("\nMIDI File Information:")
28    print("=====================")
29    print(f"Format Type: {parser.format_type}")
30    print(f"Number of Tracks: {parser.num_tracks}")
31    print(f"Ticks per Beat: {parser.ticks_per_beat}")
32    print(f"Tempo: {parser.tempo} microseconds per quarter note")
33    print(f"BPM: {parser.bpm:.1f}")
34    print(f"Total Events: {len(parser.events)}")
35    print(f"Note Count: {parser.note_count}")
36else:
37    print(f"MIDI file {midi_file} not found!")
38print("\nDone!")

MIDI Player test

Playback a MIDI file.

examples/midi_parser_player_example.py
 1# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4"""
 5Simple example showing how to use the adafruit_midi_parser library
 6to play a MIDI file with the built-in LED blinking on notes.
 7"""
 8
 9import os
10import time
11
12import board
13import digitalio
14
15import adafruit_midi_parser
16
17# Setup the built-in LED
18led = digitalio.DigitalInOut(board.LED)
19led.direction = digitalio.Direction.OUTPUT
20
21# Path to your MIDI file
22midi_file = "/song.mid"
23
24
25# Create a custom player class
26class Custom_Player(adafruit_midi_parser.MIDIPlayer):
27    def on_note_on(self, note, velocity, channel):  # noqa: PLR6301
28        print(f"Note On: {note}, velocity: {velocity}")
29        led.value = True
30
31    def on_note_off(self, note, velocity, channel):  # noqa: PLR6301
32        print(f"Note Off: {note}")
33        led.value = False
34
35    def on_end_of_track(self, track):  # noqa: PLR6301
36        print(f"End of track {track}")
37        time.sleep(5)
38
39    def on_playback_complete(self):  # noqa: PLR6301
40        print("Playback complete, restarting...")
41        # Flash LED to indicate end of sequence
42        for _ in range(3):
43            led.value = True
44            time.sleep(0.05)
45            led.value = False
46            time.sleep(0.05)
47
48
49print("MIDI File Player")
50print("===============")
51
52# Check if the file exists
53if midi_file[1:] in os.listdir("/"):
54    print(f"Found MIDI file {midi_file}")
55
56    # Create a MIDIParser instance
57    parser = adafruit_midi_parser.MIDIParser()
58
59    # Parse the file
60    parser.parse(midi_file)
61    print(f"Successfully parsed! Found {len(parser.events)} events.")
62    print(f"BPM: {parser.bpm:.1f}")
63    print(f"Note Count: {parser.note_count}")
64
65    # Create our player and enable looping
66    player = Custom_Player(parser)
67
68    # Start playback
69    print("Starting playback...")
70
71    # Main loop
72    while True:
73        # Update the player (process events)
74        player.play(loop=True)
75else:
76    print(f"MIDI file {midi_file} not found")