2

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!

vanyazhuk
  • 21
  • 2
  • I suspect your problem is that you're hardcoding the device index in `pygame.midi.Output(1)`. If the list of devices changes, you may be attempting to select a device that's only useful as an input. What if you use `pygame.midi.get_default_output_id()` to get the device id, rather than hardcoding `1`? – larsks May 28 '20 at 15:24
  • thank you @larsks! I tried that (got the id, saved to variable, and used for output) but still it doesn't work. – vanyazhuk May 28 '20 at 17:02
  • Darn. Not sure what to suggest, then; I don't have access to a platform on which I can reproduce this behavior. – larsks May 28 '20 at 17:03
  • How do you choose the MIDI device? Shouldn't you get the list and check the names? – CL. May 30 '20 at 08:55
  • thank you @CL. ! I did that, it has clarified things for me a bit – vanyazhuk Jun 03 '20 at 09:15

0 Answers0