USB MIDI Simple Read

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Tavda3172
 
Posts: 19
Joined: Sat Nov 26, 2016 8:08 pm

USB MIDI Simple Read

Post by Tavda3172 »

Hello,
I've read through all of the MIDI examples that Adafruit has and tried my best to understand the "read the docs" information on midi, but I'm still not sure how to read a specific midi message over usb. I'm coming over to CircuitPython from Arduino, and trying to keep it as simple as possible here, but I'm having a hard time and am not finding it to be intuitive. In a nutshell, I just want to light up the neopixel on a KB2040 with a MIDI Control Change message and value. I've tried MANY different ways but need some help to bring this home. Hopefully someone can get the gist of what I'm trying to do from this bit of code I'm attempting to make work.

Code: Select all

import board
import busio
import usb_midi
import adafruit_midi
from adafruit_midi.control_change import ControlChange
import time
import neopixel

midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], midi_out=usb_midi.ports[1], out_channel=0)

uart = busio.UART(board.TX, board.RX, baudrate=31250, timeout=0)  # initialize UART

pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)



while True:

    msg = midi.receive()

    if msg is not None:
        if isinstance(msg, ControlChange):
            if msg.control = 41:
                if msg.value = 127:
                   pixels.fill((255, 0, 0))
Any help would be greatly appreciated. Thank you,
Kevin

User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Re: USB MIDI Simple Read

Post by blnkjns »

I miss at least the pixels.show() command. Don't know much about CP, but a quick lookup says you need that command to push the "screen buffer" to the actual pixels.

User avatar
Tavda3172
 
Posts: 19
Joined: Sat Nov 26, 2016 8:08 pm

Re: USB MIDI Simple Read

Post by Tavda3172 »

Oh interesting! Thanks blnkjns.

I was getting this to work without pixels.show()

Code: Select all

while True:
    pixels.fill((255, 0, 0))
and the Adafruit example Neopixel Blink doesn't have pixels.show() either. Curious.

Code: Select all

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""CircuitPython NeoPixel Blink example - blinking the built-in NeoPixels."""
import time
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 4)

while True:
    pixels.fill((255, 0, 0))
    time.sleep(0.5)
    pixels.fill((0, 0, 0))
    time.sleep(0.5)
I will look into that for future reference. Right now I think my MIDI code is the one that doesn't want to behave. I don't think I'm understanding the syntax so I might look for an "unfrozen" version of the MIDI library to see if I can figure out how I'm supposed to reference the messages from midi.receive().

User avatar
Tavda3172
 
Posts: 19
Joined: Sat Nov 26, 2016 8:08 pm

Re: USB MIDI Simple Read

Post by Tavda3172 »

Just curious if anyone can explain how to read a specific midi message over usb? I'm still stumped trying to read a control change message and value?

User avatar
fredrabelo
 
Posts: 6
Joined: Mon Nov 28, 2022 4:53 am

Re: USB MIDI Simple Read

Post by fredrabelo »

I am also very confused about the use of the MIDI library.

What i am doing now to read a Control Message is:

Code: Select all

msg = midi.receive()
if isinstance(msg, ControlChange):              # check if msg is control change
	value = msg.value				 # save controller value

But this is kind of very complicated if the you are trying to do more than the real MIDI Basic.

For example, trying to reverse engineer my DAW to build a controller and still havent figured out how to read a msg.

with the code:

Code: Select all

print(midi.receive())
i get for example this reply:

<ControlChange object at 0x200212b0>

ok, so i know this message was a controlchange message
as far as i understood a ControlChange MIDI message should be:
0x Bn cc vv ( n = channel n°, 0-f) ( cc = controller number, 0-7f) (vv = controlle value, 0-7f)

How to interpret the above message without having to do this for every message type?

Code: Select all

if isinstance(msg, MessageType):
	 print(MessageType, 
	 	"channel:", msg.channel, 
	 	"controller number:", msg.????, 
	 	"controller value:", msg.value)	
I still dont get how to read Sysex message.
Since i am not a Python professional i could figure the whole the documentation out (https://docs.circuitpython.org/projects/midi/en/latest/)


I also noted that it takes around 2 millliseconds to send/receive a midi message.
did the testing with the following code:

Code: Select all

last_midi_rec = time.monotonic_ns()
msg = midi.receive()
print("midi receive took:", (time.monotonic_ns() - last_midi_rec))

last_midi_send = time.monotonic_ns()
midi.send(ControlChange(0, 0), 0)  
print("midi.send took:", (time.monotonic_ns() - last_midi_send))
Is this reliable way to test the time?
and is 2 ms not a little bit slow?

Any help would be very appreciated!

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”