Playing note with pygame.midi

There are two small problems. The sound is not played because you don’t set the velocity of the note. Try setting it to 127 (maximum) to hear the sound. The other problem is that you don’t delete the midi output object at the end before quitting. This leads to the “PortMidi: `Bad pointer'” error at the end. So here is the corrected code that should work properly:

import pygame.midi
import time

pygame.midi.init()
player = pygame.midi.Output(0)
player.set_instrument(0)
player.note_on(64, 127)
time.sleep(1)
player.note_off(64, 127)
del player
pygame.midi.quit()

Leave a Comment