简体   繁体   中英

Playing note with pygame.midi

I'm trying to play a sound with the pygame.midi module. Here is the code I use :

#!/usr/bin/env python

import pygame.midi
import time

pygame.midi.init()

print pygame.midi.get_default_output_id()
print pygame.midi.get_device_info(0)

player = pygame.midi.Output(0)

player.set_instrument(0)

print 'Playing...'

player.note_on(64)
time.sleep(1)
player.note_off(64)

print 'Played'

pygame.midi.quit()

I've found similar codes while searching for exemples, here is the output :

0

('ALSA', 'Midi Through Port-0', 0, 1, 0)

Playing...

Played

PortMidi call failed...

PortMidi: `Bad pointer'

type ENTER...

No sound is played, and I didn't find any info about the PortMidi error which occurs surprisingly after pygame.midi quits.

Do you have any idea? I'm running an debian-based linux distribution if that can help.

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()

thanks for your code, helped me to start with midi and python.

It seems to me you forgot the velocity (sort of volume) information in the note_on, note_off events. The default value is 0, so the note would 'play', but would not be audible.

About the quit error message you get... I can't help, i dont know about Linux and ALSA. For reference, this worked fine for me in a Win Vista box using the default midi mapper. This simply plays either a note, an arpeggio or a chord, using a base note and a major chord structure.

import pygame
import time
import pygame.midi

pygame.midi.init()
player= pygame.midi.Output(0)
player.set_instrument(48,1)

major=[0,4,7,12]

def go(note):
    player.note_on(note, 127,1)
    time.sleep(1)
    player.note_off(note,127,1)

def arp(base,ints):
    for n in ints:
        go(base+n)

def chord(base, ints):
    player.note_on(base,127,1)
    player.note_on(base+ints[1],127,1)
    player.note_on(base+ints[2],127,1)
    player.note_on(base+ints[3],127,1)
    time.sleep(1)
    player.note_off(base,127,1)
    player.note_off(base+ints[1],127,1)
    player.note_off(base+ints[2],127,1)
    player.note_off(base+ints[3],127,1)
def end():
       pygame.quit()

To use it, just import the module and, for example, type a command like go(60), chord (60, major) or arp(60, major)

The error message shows that your output device is a "MIDI Through Port" - which isn't capable of making sounds on it's own. You would have to connect it (eg using qjackctl or any other tool letting you connect ALSA MIDI ports) to a software synthesizer like qsynth.

Try importing the entire pygame module:

import pygame

not

import pygame.midi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM