Python Forum

Full Version: Extract data from large string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm building a project with Raspberry PI. Reading MIDI messages that come from a guitar pedal. The intent is to have the PI play a mp3 file when it receives a program change message on the appropriate MIDI chnl and Program Change number.

I am using the pygame module and I am recieving the messages.

The received message are large strings with various wrappers.

First, the entire messgae is wrapped in [ ]

Inside that ther can be several events. Each wrapped in <>

THen inside of that lives the data wrapped in ()

Here is an example,

[<Event(34-Unknown {'status': 194, 'data1': 0, 'data2': 0, 'data3': 0, 'timestamp': 4221, 'vice_id': 3})>, <Event(34-Unknown {'status': 178, 'data1': 80, 'data2': 0, 'data3': 0, 'timestamp': 4222, 'vice_id': 3})>, <Event(34-Unknown {'status': 178, 'data1': 81, 'data2': 0, 'data3': 0, 'timestamp': 4222, 'vice_id': 3})>]

THere are three events in this message.

I am interested in only messages with status 194 and then want to return the value of data1,

Status 194 is MIDI chnl 3 and data1 0 is Program change 1

So, I want to only act on events with status 194 and then set a variable "track" to the value of data1

I haven'y written any code for a decade or so and am happy to be at this point...

I'm guessing this is easy stuff for a Python veteran. Any and all help will be greatly appreciated.


Thank you,
you should take a look at https://pypi.org/project/py-midi/
(can be installed with pip install py-midi)
many more packages to take a look at: https://pypi.org/search/?q=%27midi+message%27&o=