I'm trying to use pygame.midi in Anaconda Notebook (MacOS Mojave) to get sounds from virtual sampler. Sometimes it works fine, sometimes with no obvious reason the same script next time gives me the MIDI output error (see below).
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-22-59fb23bd9d1f> in <module>
5 note = [44, 46, 49, 51, 53, 56, 58, 61]
6 pygame.midi.init()
----> 7 player = pygame.midi.Output(1)
8 player.set_instrument(0)
9 for i in range (24):
~/.local/lib/python3.7/site-packages/pygame/midi.py in __init__(self, device_id, latency, buffer_size)
420 if output:
421 try:
--> 422 self._output = _pypm.Output(device_id, latency)
423 except TypeError:
424 raise TypeError("an integer is required")
pypm.pyx in pypm.Output.__init__()
Exception: b"PortMidi: `Invalid device ID'"
Meanwhile, pygame.midi.get_device_info(1) returns (b'CoreMIDI', b'IAC Driver Bus 1', 0, 1, 1)
My code looks like this:
import pygame.midi
import time
import random
note = [44, 46, 49, 51, 53, 56, 58, 61]
pygame.midi.init()
player = pygame.midi.Output(1)
player.set_instrument(0)
for i in range (24):
random.shuffle(note)
player.note_on(note[0], 127)
time.sleep(0.5)
player.note_off(note[0], 127)
del player
pygame.midi.quit()
What can be the problem? Thank you!