Simple test

Ensure your device works with this simple test.

examples/midi_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3# simple_test
 4import random
 5import time
 6
 7import usb_midi
 8
 9import adafruit_midi
10from adafruit_midi.control_change import ControlChange
11from adafruit_midi.note_off import NoteOff
12from adafruit_midi.note_on import NoteOn
13from adafruit_midi.pitch_bend import PitchBend
14
15print(usb_midi.ports)
16midi = adafruit_midi.MIDI(
17    midi_in=usb_midi.ports[0], in_channel=0, midi_out=usb_midi.ports[1], out_channel=0
18)
19print("Midi test")
20# Convert channel numbers at the presentation layer to the ones musicians use
21print("Default output channel:", midi.out_channel + 1)
22print("Listening on input channel:", midi.in_channel + 1)
23while True:
24    midi.send(NoteOn(44, 120))  # G sharp 2nd octave
25    time.sleep(0.25)
26    a_pitch_bend = PitchBend(random.randint(0, 16383))
27    midi.send(a_pitch_bend)
28    # note how a list of messages can be used
29    midi.send([NoteOff("G#2", 120), ControlChange(3, 44)])
30    time.sleep(0.5)
31    msg = midi.receive()
32    if msg is not None:
33        print("Received:", msg, "at", time.monotonic())