用pygame.midi播放音符 [英] Playing note with pygame.midi

查看:434
本文介绍了用pygame.midi播放音符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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)

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

正在播放...

玩过

PortMidi呼叫失败...

PortMidi call failed...

PortMidi:错误指针"

PortMidi: `Bad pointer'

输入ENTER ...

type ENTER...

没有声音播放,我没有找到有关PortMidi错误的任何信息 pygame.midi退出后意外发生.

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

你有什么主意吗?我正在运行基于debian的linux发行版 可以帮忙.

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

推荐答案

有两个小问题.由于未设置音符的力度,因此无法播放声音.尝试将其设置为127(最大)以听到声音.另一个问题是您在退出前不删除midi输出对象.最后导致"PortMidi:错误指针""错误.因此,这是应该正确运行的更正代码:

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

这篇关于用pygame.midi播放音符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆