用 pygame.midi 演奏音符 [英] Playing note with pygame.midi

查看:53
本文介绍了用 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 通过 Port-0', 0, 1, 0)

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

正在播放...

玩过

PortMidi 调用失败...

PortMidi call failed...

PortMidi:`坏指针'

PortMidi: `Bad pointer'

输入回车...

没有播放声音,而且我没有找到有关 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: `Bad pointer'"错误.所以这里是应该可以正常工作的更正后的代码:

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天全站免登陆