小智 7
首先,你需要找出你的键盘在pygame中有哪个设备ID.我写了这个小函数来找出:
import pygame.midi
def print_devices():
for n in range(pygame.midi.get_count()):
print (n,pygame.midi.get_device_info(n))
if __name__ == '__main__':
pygame.midi.init()
print_devices()
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
(0, ('MMSystem', 'Microsoft MIDI Mapper', 0, 1, 0))
(1, ('MMSystem', '6- Saffire 6USB', 1, 0, 0))
(2, ('MMSystem', 'MK-249C USB MIDI keyboard', 1, 0, 0))
(3, ('MMSystem', 'Microsoft GS Wavetable Synth', 0, 1, 0))
Run Code Online (Sandbox Code Playgroud)
从pygame手册中,您可以了解到此信息元组中的第一个将此设备确定为合适的输入设备.所以让我们在无限循环中读取它的一些数据:
def readInput(input_device):
while True:
if input_device.poll():
event = input_device.read(1)
print (event)
if __name__ == '__main__':
pygame.midi.init()
my_input = pygame.midi.Input(2) #only in my case the id is 2
readInput(my_input)
Run Code Online (Sandbox Code Playgroud)
这表明:
[[[144, 24, 120, 0], 1321]]
Run Code Online (Sandbox Code Playgroud)
我们有一个包含2个项目的列表列表:
第二个值是您感兴趣的值.因此我们将其打印为注释:
def number_to_note(number):
notes = ['c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'b']
return notes[number%12]
def readInput(input_device):
while True:
if input_device.poll():
event = input_device.read(1)[0]
data = event[0]
timestamp = event[1]
note_number = data[1]
velocity = data[2]
print (number_to_note(note_number), velocity)
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助.这是我的第一个答案,我希望它不会太久.:)
| 归档时间: |
|
| 查看次数: |
1621 次 |
| 最近记录: |