Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending MIDI signals #8

Open
sammlapp opened this issue Dec 12, 2024 · 2 comments
Open

Sending MIDI signals #8

sammlapp opened this issue Dec 12, 2024 · 2 comments

Comments

@sammlapp
Copy link

Hi, cool project! I'm wondering if you could point me in the right direction if I wanted to use this library to play MIDI files and send the MIDI information over USB or to a MIDI output pinout from RPI Pico. For example, this project shows how to send midi signals with the Rpi Pico to a midi pin out. I think it might be straightforward to combine this library with some other midi library to send midi out, but I'm not sure how to do it. Thanks for any tips!

@bixb922
Copy link
Owner

bixb922 commented Dec 12, 2024

Yes, that should be quite simple. The page you link shows the hardware part for serial MIDI. The software part is about as follows (not tested).

from machine import UART, Pin
from umidiparser import MidiFile
uart = UART( 1, baudrate=31250, tx=Pin(4), rx=Pin(5))
for event in MidiFile("myfile.mid").play():
    if event.is_channel():
        message = bytearray(1)
        message[0] = event.status + event.channel
        message.extend( event.data )
        uart.write( message )

Here is how this should work: UART is a serial port. On the RPI Pico UART1 normally uses pins 4 and 5. Baud rate 31250 is standard for MIDI. See https://docs.micropython.org/en/latest/rp2/quickref.html#uart-serial-bus

MidiFile("myfile.mid").play() will return one event after the other, in the correct order, merging tracks and waiting until the event is due.

message rebuilds the MIDI message that's inside the MIDI file. And then the message is sent over the UART.

MIDI over USB seems to be possible with a recent version of MicroPython, but I haven't researched that.

If you want your software to do other stuff while playing the MIDI file, use the async for version of MidiFile.play() and have other tasks running in parallel with asyncio.create_task()

This code snippet can be optimized for MicroPython by allocating the needed 2-byte and 3-byte bytearreays before the loop.

(edit) Please post whatever results. I am very interested if this works, and how you made it work if not. If there is a problem, please post here also, I'll try to help.

@sammlapp
Copy link
Author

Awesome thanks, this is super helpful! I'm planning to try this out this week and will share the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants