简体   繁体   中英

Count number of Midi inputs received using Python

I am using Mido ( https://mido.readthedocs.io/en/latest/ ) for receiving Midi messages in Python. I would like to be able to keep a counter of how many messages are being receiving every time there is a new input.

Questions:

How can I wrap all inputs in a list?

How do I then append new input items to the list?

How can I then count the number of items in that list as that list is continually changing?

class MyThread(threading.Thread):
def run(self):
    for msg in inport:
        print msg       
m = MyThread()
m.start()

This produces an output like this:

control_change channel=0 control=16 value=1 time=0
control_change channel=0 control=16 value=2 time=0
control_change channel=0 control=16 value=3 time=0
control_change channel=0 control=16 value=4 time=0
control_change channel=0 control=16 value=5 time=0
control_change channel=0 control=16 value=6 time=0
control_change channel=0 control=16 value=7 time=0

You can just modify your thread class to do operations on the inport list, like this:

class MyThread(threading.Thread):
    inport = []

    def add(ele):
        inport.append(ele)
    def count(): 
        return len(inport)
    def run(self):
        for msg in self.inport:
            print msg       
    m = MyThread()
    m.start() 

If you're running multithreads on the list, you can use a synchronized structure like Queue . Learn more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM