通过覆盆子皮上的pygame播放midi的声音

时间:2013-10-01 10:21:03

标签: python raspberry-pi midi

我正试图在我的raspberrypi上使用pygame播放midi声音(没有外部midi设备),但是我无法使用任何东西。

我尝试了很多例子: 以下给出。

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

给出以下错误

PortMidi call failed...
  PortMidi: 'Bad pointer'
type ENTER...

下一个示例(与大多数其他示例一样)会显示“设备ID无效,超出范围”。错误:

import pygame
import pygame.midi
from time import sleep

instrument = 0
note = 74
volume = 127

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

for id in range(pygame.midi.get_count()):
  print pygame.midi.get_device_info(id)

port = 2
midiOutput = pygame.midi.Output(port, 1)
midiOutput.set_instrument(instrument)
for note in range(0,127):
  midiOutput.note_on(note,volume)
  sleep(.25)
  midiOutput.note_off(note,volume)
del midiOutput
pygame.midi.quit()

给出此错误

('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
Traceback (most recent call last):
  File "midi-test2.py", line 16, in <module>
    midiOutput = pygame.midi.Output(port, 1)
  File "/usr/lib/python2.7/dist-packages/pygame/midi.py", line 414, in __init__
    raise MidiException("Device id invalid, out of range.")
pygame.midi.MidiException: 'Device id invalid, out of range.'

我没有找到任何设置RaspberryPi播放midi声音的指南,有什么建议吗?

1 个答案:

答案 0 :(得分:0)

port = 2 midiOutput = pygame.midi.Output(port,1)

我不知道“pygame.midi”我不知道你是否可以这样做,但是更改参数,代码运行没有错误。

midiOutput = pygame.midi.Output(0,0)