Python Forum
rtmidi problem after running the code x times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rtmidi problem after running the code x times
#1
def midi_input():

    class Collector(threading.Thread):
        def __init__(self, device, port):
            threading.Thread.__init__(self)
            self.setDaemon(True)
            self.port = port
            self.portName = device.getPortName(port)
            self.device = device
            self.quit = False

        def run(self):
            self.device.openPort(self.port)
            self.device.ignoreTypes(True, False, True)
            while True:
                if self.quit:
                    return
                msg = self.device.getMessage(2500)
                if msg:
                    if msg.isNoteOn():
                        if midi_record_toggle == 1:
                            note = msg.getNoteNumber() - 20
                            textw.insert(textw.index(INSERT), number2pitch[note])
                            render('q', papercolor)

    dev = rtmidi.RtMidiIn()
    for i in range(dev.getPortCount()):
        device = rtmidi.RtMidiIn()
        print('OPENING',dev.getPortName(i))
        collector = Collector(device, i)
        collector.start()

    sys.stdin.read(1)
    for c in collectors:
        c.quit = True


threading.Thread(target=midi_input).start()
I am running this using threading. I don't know what the risks are... The code runs fine but if I run it more than 16 times it gives an Error: 'ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Cannot allocate memory. I think it doesn't close the midi port/process but I don't know how to close this correctly. I noticed that Sunvox(a really cool synth) gives the same error on midi input after I messed up with my code. Is there anything suspicious about my code? how am I supposed to use threading; I used this to make a while loop run while other functions are still working. I took the code from a GitHub page and modified it a little. Thanks!
Reply
#2
I found the solution to this problem. I made a variable 'midiswitch' that is standard set to 1 and when the program closes it is set to 0. The while loop checks if the variable is 0 and closes the loop if so.
def midi_input():

    class Collector(threading.Thread):
        def __init__(self, device, port):
            threading.Thread.__init__(self)
            self.setDaemon(True)
            self.port = port
            self.portName = device.getPortName(port)
            self.device = device
            self.quit = False

        def run(self):
            self.device.openPort(self.port)
            self.device.ignoreTypes(True, False, True)
            while True:
                if midiswitch == 0:
                    return
                msg = self.device.getMessage(2500)
                if msg:
                    if msg.isNoteOn():
                        if midi_record_toggle == 1:
                            note = msg.getNoteNumber() - 20
                            if shiftkey == 0:
                                textw.insert(textw.index(INSERT), number2pitch[note])
                            elif shiftkey == 1:
                                textw.insert(textw.index(INSERT), '_'+number2pitch[note])
                            renderkey()

    dev = rtmidi.RtMidiIn()
    for i in range(dev.getPortCount()):
        device = rtmidi.RtMidiIn()
        print('OPENING',dev.getPortName(i))
        collector = Collector(device, i)
        collector.start()

threading.Thread(target=midi_input).start()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 665 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 396 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 535 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Different code execution times Wirbelwind94 4 764 Oct-06-2023, 12:30 PM
Last Post: snippsat
Question Running an action only between certain times alexbca 9 1,734 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Code running many times nad not just one? korenron 4 1,376 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,776 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,515 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Why is this Python code running twice? mcva 5 5,296 Feb-02-2022, 10:21 AM
Last Post: mcva
  Python keeps running the old version of the code quest 2 3,789 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020