'Fatal Python error: (pygame parachute) Segmentation Fault using pygame.midi

I have been working on playing midi files within pygame and keep getting faced with this error.

Fatal Python error: (pygame parachute) Segmentation Fault

Python runtime state: initialized

This is my code:

import pygame.midi
pygame.midi.init()
device = pygame.midi.get_default_output_id()
player = pygame.midi.Output(0)

the fault is highlighted to be an issue with the final line, but I'm not entirely sure what is wrong with it.

Thank you!



Solution 1:[1]

I think this is a bug in Pygame, but there was nothing in the tracker seeming to match. I can replicate the issue with PyGame 1.9.4.post1.

The cause of the issue is calling pygame.midi.Output(), I played around with a few different calls to this, specifying different parameters gives different errors - sometimes on call, but other times on shutdown.

It's a workaround, not a solution - but do you actually need to call this function? Basic MIDI functionality seems to work without it. The code below plays a MIDI file, then exits cleanly.

import sys
import pygame
import pygame.midi

pygame.init()
pygame.midi.init()
pygame.mixer.init()

midi_file = "popcorn.mid"
pygame.mixer.music.load( midi_file )
pygame.mixer.music.play()
device = pygame.midi.get_default_output_id()
device_info = pygame.midi.get_device_info( device )
print( "MIDI Device: " + str( device ) )
print( "Device Info: " + str( device_info ) )
#player = pygame.midi.Output( device, buffer_size=4096 )

clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
    pygame.time.delay( 1500 )
    print("?", end='')
    sys.stdout.flush()   # just ensure the tick-mark is printed in-time

pygame.mixer.music.stop()
pygame.quit()

Maybe a later version of PyGame - perhaps even the (upcoming?) SDL2 version.

Solution 2:[2]

It should be an error related to C implementation. Please see if the following command works:

apt-get install -y xserver-xorg

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kingsley
Solution 2 caocao