Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 425
cym13 Avatar asked Apr 07 '13 14:04

cym13


People also ask

Does Pygame support midi?

New in pygame 1.9. 0. The midi module can send output to midi devices and get input from midi devices. It can also list midi devices on the system.

How do I use midi input in python?

Input is used to get midi input from midi devices. Input(device_id) Input(device_id, buffer_size) Input. close - closes a midi stream, flushing any pending buffers. closes a midi stream, flushing any pending buffers.


2 Answers

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()
like image 198
Cito Avatar answered Sep 22 '22 06:09

Cito


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)

like image 35
jose Avatar answered Sep 22 '22 06:09

jose