USB-midi-device crashes when a lot of pitchbend messages are received [MicroPython v1.29.0]

Open
#1,158 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
embedded-iot

Research direction

Start with micropython/usb/usb-device-midi/usb/device/midi.py, especially on_midi_event, and reproduce the issue using the pitch-bend handler shown in micropython/usb/examples/device/midi_example.py. Trace what happens when rapid pitch-bend messages arrive and determine whether the device hangs or recovers normally under that input rate. Done means sustained pitch-bend traffic no longer hangs the Pico or requires restarting the backend.

Written by the indexing model from the issue text.

Description

bug

As I wanted to test receiving MIDI pitchbend messages, I added the following lines to micropython/usb/usb-device-midi/usb/device/midi.py:

    def on_midi_event(self, cin, midi0, midi1, midi2):
        ch = midi0 & 0x0F
        if cin == _CIN_NOTE_ON:
            self.on_note_on(ch, midi1, midi2)
        elif cin == _CIN_NOTE_OFF:
            self.on_note_off(ch, midi1, midi2)
        elif cin == _CIN_CONTROL_CHANGE:
            self.on_control_change(ch, midi1, midi2)
        elif cin == _CIN_PITCH_BEND:              # <------------ Added these 4 lines
            # Pitch bend combines two 7-bit values into a 14-bit unsigned integer
            value = midi1 | (midi2 << 7)
            self.on_pitch_bend(ch, value)             

and

    def on_pitch_bend(self, channel, value):
        pass 

To micropython/usb/examples/device/midi_example.py I added these lines:

    def on_pitch_bend(self, ch, value):
        """Called when Pitch Bend is received. Value is an integer from 0 to 16383 (8192 is center)."""
        normalized = value - 8192
        print(f"PitchBend| Ch: {ch:<2} Raw: {value:<5} Relative to center: {normalized}")

Now when I move the pitchbend on my Arturia MiniLab MKII, it spits out a lot of pitchbend messages (every ~1ms). On a serial monitor, I see this:

Image

After this, the Raspberry Pi Pico 2 hangs and I have to hit Thonny's "Stop/Restart backend" button twice to return/reset the Pico to the REPL.
It seems the rapid messages overflow a queue?

Paul

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from micropython/micropython-lib

All issues in micropython/micropython-lib

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.