|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | + |
| 5 | +import audiopwmio |
| 6 | +import board |
| 7 | +import synthio |
| 8 | +import adafruit_midi |
| 9 | +import adafruit_usb_host_midi |
| 10 | +import usb.core |
| 11 | +from adafruit_midi.note_on import NoteOn |
| 12 | +from adafruit_midi.note_off import NoteOff |
| 13 | + |
| 14 | +print("Looking for midi device") |
| 15 | +raw_midi = None |
| 16 | +while raw_midi is None: |
| 17 | + for device in usb.core.find(find_all=True): |
| 18 | + try: |
| 19 | + raw_midi = adafruit_usb_host_midi.MIDI(device) |
| 20 | + print("Found", hex(device.idVendor), hex(device.idProduct)) |
| 21 | + except ValueError: |
| 22 | + continue |
| 23 | + |
| 24 | +# This setup is for pin D4 on Feather RP2040 with USB Type A Host |
| 25 | +# You must wire this up yourself to an RC filter and headphones or line in of an amp |
| 26 | +audio = audiopwmio.PWMAudioOut(board.D4) |
| 27 | +synth = synthio.Synthesizer(sample_rate=22050) |
| 28 | +audio.play(synth) |
| 29 | + |
| 30 | +midi = adafruit_midi.MIDI(midi_in=raw_midi, in_channel=0) |
| 31 | + |
| 32 | +pressed = {} |
| 33 | + |
| 34 | +while True: |
| 35 | + msg = midi.receive() |
| 36 | + if isinstance(msg, NoteOn) and msg.velocity != 0: |
| 37 | + note = synthio.Note(synthio.midi_to_hz(msg.note)) |
| 38 | + print("noteOn: ", msg.note, "vel:", msg.velocity) |
| 39 | + synth.press(note) |
| 40 | + pressed[msg.note] = note |
| 41 | + elif (isinstance(msg,NoteOff) or (isinstance(msg,NoteOn) and msg.velocity==0)) and msg.note in pressed: |
| 42 | + print("noteOff:", msg.note, "vel:", msg.velocity) |
| 43 | + note = pressed[msg.note] |
| 44 | + synth.release(note) |
| 45 | + del pressed[msg.note] |
0 commit comments