简体   繁体   English

用pygame.midi播放音符

[英]Playing note with pygame.midi

I'm trying to play a sound with the pygame.midi module. 我正在尝试使用pygame.midi模块播放声音。 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 0

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

Playing... 打...

Played 玩过

PortMidi call failed... PortMidi呼叫失败......

PortMidi: `Bad pointer' PortMidi:“糟糕的指针”

type ENTER... 输入ENTER ...

No sound is played, and I didn't find any info about the PortMidi error which occurs surprisingly after pygame.midi quits. 没有播放声音,我没有找到任何有关Portgidi错误的信息,这个信息在pygame.midi退出后出乎意料地发生。

Do you have any idea? 你有什么主意吗? I'm running an debian-based linux distribution if that can help. 我正在运行基于debian的linux发行版,如果这可以帮助的话。

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. 尝试将其设置为127(最大)以听到声音。 The other problem is that you don't delete the midi output object at the end before quitting. 另一个问题是你在退出之前不要删除midi输出对象。 This leads to the "PortMidi: `Bad pointer'" error at the end. 这导致最后出现“PortMidi:'Bad pointer'”错误。 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. 感谢您的代码,帮助我开始使用midi和python。

It seems to me you forgot the velocity (sort of volume) information in the note_on, note_off events. 在我看来,你忘记了note_on,note_off事件中的速度(音量类型)信息。 The default value is 0, so the note would 'play', but would not be audible. 默认值为0,因此音符将“播放”,但不会发出声音。

About the quit error message you get... I can't help, i dont know about Linux and ALSA. 关于你得到的退出错误消息...我无法帮助,我不知道Linux和ALSA。 For reference, this worked fine for me in a Win Vista box using the default midi mapper. 作为参考,这在使用默认midi映射器的Win Vista盒子中对我来说很好。 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) 要使用它,只需导入模块,例如,键入命令,如go(60),chord(60,major)或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. 错误消息显示您的输出设备是“MIDI直通端口” - 它无法自行发出声音。 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. 您必须连接它(例如使用qjackctl或任何其他工具让您连接ALSA MIDI端口)到qsynth等软件合成器。

Try importing the entire pygame module: 尝试导入整个pygame模块:

import pygame

not

import pygame.midi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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