pygame midi 读取PC(程序更改)消息 [英] pygame midi read PC (program change) messages

查看:96
本文介绍了pygame midi 读取PC(程序更改)消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是聆听连接到我的吉他踏板切换器的 MIDI 转 USB 适配器.当收到程序更改,(PC)消息时,播放相应的 mp2 文件.

我尝试了许多不同的 Python MIDI 模块,而 pygame 是我最接近的.

下面是我一直在修改的代码,当我为踏板切换器发送程​​序消息时,它正在打印消息.我只是不知道如何提取和解释我需要的东西.

例如,踏板切换器发送 PC 消息 MIDI Chnl 3, Program Change 1

我的代码,

print(转换)

退货,

,<事件(34-未知{'状态':61,'数据1':247,'数据2':0,'数据3':0,'时间戳':4674,'vice_id':3})>]

或者,MIDI Chnl 3,Program Change 2

代码返回,

[[, <Event(34-Unknown {'status': 0, 'data1': 0, 'data2': 20, 'data3': 18, 'timestamp': 2248, 'vice_id': 3})>, <Event(34-Unknown {'status': 127, 'data1': 1, 'data2': 0, 'data3': 68, 'timestamp': 2248, 'vice_id': 3})>, <Event(34-Unknown {'status': 60, 'data1': 247, 'data2': 0, 'data3': 0, 'timestamp': 2248, 'vice_id': 3})>]

绞尽脑汁想知道如何将其转换为 Channel 3, PC 1 和/或 Channel 3, PC 2

有什么指导吗?

整个测试脚本,

导入pygame、pygame.midipygame.midi.init()pygame.init()计数 = pygame.midi.get_count()打印(有",计数,MIDI设备")dev1 = pygame.midi.get_device_info(1)打印(1=",dev1)dev2 = pygame.midi.get_device_info(2)打印(2=",dev2)dev3 = pygame.midi.get_device_info(3)打印(3=",dev3)inp = pygame.midi.Input(3,100)打印(输入)为真:如果 inp.poll():# 无法找到队列中的消息数# 所以我们只指定一个高最大值#print (inp.read(1000))阅读 = inp.read(100)#打印(读取)转换 = pygame.midi.midis2events(read,3)打印(转换)# wait 10ms - 这是任意的,但 wait(0) 仍然产生# 在 100% cpu 利用率pygame.time.wait(10)

好的,所以继续挖掘......

我发现我的踏板发出了大量与我正在做的事情无关的 MIDI 信息.现在,我已经安顿好了踏板,我收到了有意义的消息.

当我发送 Chnl 3 PC 1 程序更改消息时,

[<事件(34-未知{'状态':196,'数据1':0,'数据2':0,'数据3':0,'时间戳':3942,'vice_id':3})>;, <Event(34-Unknown {'status': 180, 'data1': 80, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>,<Event(34-Unknown {'status': 180, 'data1': 81, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>]

当我发送 Chnl 3 PC 1 2 get 时,

[<事件(34-未知{'状态':196,'数据1':1,'数据2':0,'数据3':0,'时间戳':3942,'vice_id':3})>;, <Event(34-Unknown {'status': 180, 'data1': 80, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>,<Event(34-Unknown {'status': 180, 'data1': 81, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>]

所以,三个事件"我只对 staus: 196 的事件感兴趣,因为那是 chnl 3.

我发现了这个,它揭示了这些 MIDI 消息,

[url]https://www.midimountain.com/midi/midi_status.htm[/url]

所以,现在我需要扫描这个长字符串中的'status': 196";如果找到,则获取 data1 的值

如果有人有一种优雅的方式来做到这一点,我将不胜感激...

解决方案

midis2events() 函数毫无意义,如果你不使用事件循环.

只需从您读取的数据中提取值:

<代码>...阅读 = inp.read(100)对于阅读中的事件:((status,data1,data2,data3),timestamp) = 事件如果状态 == 196:打印(f更改为程序 {data1}")

My goal is to listen to a MIDI to USB adapter that is connected to my guitar pedalboard switcher. When a program change, (PC) message is received, play the appropriate mp2 file.

I've tried a number of different Python MIDI modules and pygame has got me the closest.

Below is the code I have been tinkering with and it is printing messages when I send program messages for the pedal switcher. I just do not know how to extract and interpret what I need.

For instance, the pedal switcher sends a PC message MIDI Chnl 3, Program Change 1

My code,

print (convert)

Returns,

<Event(34-Unknown {'status': 240, 'data1': 65, 'data2': 0, 'data3': 0, 'timestamp': 4674, 'vice_id': 3})>, <Event(34-Unknown {'status': 0, 'data1': 0, 'data2': 20, 'data3': 18, 'timestamp': 4674, 'vice_id': 3})>, <Event(34-Unknown {'status': 127, 'data1': 1, 'data2': 0, 'data3': 67, 'timestamp': 4674, 'vice_id': 3})>, <Event(34-Unknown {'status': 61, 'data1': 247, 'data2': 0, 'data3': 0, 'timestamp': 4674, 'vice_id': 3})>]

Or, MIDI Chnl 3, Program Change 2

Code returns,

[[<Event(34-Unknown {'status': 240, 'data1': 65, 'data2': 0, 'data3': 0, 'timestamp': 2248, 'vice_id': 3})>, <Event(34-Unknown {'status': 0, 'data1': 0, 'data2': 20, 'data3': 18, 'timestamp': 2248, 'vice_id': 3})>, <Event(34-Unknown {'status': 127, 'data1': 1, 'data2': 0, 'data3': 68, 'timestamp': 2248, 'vice_id': 3})>, <Event(34-Unknown {'status': 60, 'data1': 247, 'data2': 0, 'data3': 0, 'timestamp': 2248, 'vice_id': 3})>]

Scratching my head on how to translate that to Channel 3, PC 1 and/or Channel 3, PC 2

Any guidance out there?

Entire test script,

import pygame, pygame.midi
pygame.midi.init()
pygame.init()
count = pygame.midi.get_count()
print ("There are ", count, "MIDI devices")

dev1 = pygame.midi.get_device_info(1)
print ("1= ",dev1)

dev2 = pygame.midi.get_device_info(2)
print ("2= ",dev2)

dev3 = pygame.midi.get_device_info(3)
print ("3= ",dev3)

inp = pygame.midi.Input(3,100)
print(inp)

while True:
     if inp.poll():
         # no way to find number of messages in queue
         # so we just specify a high max value
         #print (inp.read(1000))
         read = inp.read(100)
         #print(read)
         convert = pygame.midi.midis2events(read,3)
         print (convert)
         

 
     # wait 10ms - this is arbitrary, but wait(0) still resulted
     # in 100% cpu utilization
     pygame.time.wait(10)

Ok, so continuing to dig...

I found my pedalboard was spitting out a whole lot of MIDI messages that did not pertain to what I was doing. Now, I have the pedalboard settled down I'm getting messages that make sense.

When I send a Chnl 3 PC 1 program change message I get,

[<Event(34-Unknown {'status': 196, 'data1': 0, 'data2': 0, 'data3': 0, 'timestamp': 3942, 'vice_id': 3})>, <Event(34-Unknown {'status': 180, 'data1': 80, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>, <Event(34-Unknown {'status': 180, 'data1': 81, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>]

When I send a Chnl 3 PC 1 2 get,

[<Event(34-Unknown {'status': 196, 'data1': 1, 'data2': 0, 'data3': 0, 'timestamp': 3942, 'vice_id': 3})>, <Event(34-Unknown {'status': 180, 'data1': 80, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>, <Event(34-Unknown {'status': 180, 'data1': 81, 'data2': 0, 'data3': 0, 'timestamp': 3943, 'vice_id': 3})>]

So, three "Events" I'm only interested in events with staus: 196 as that is chnl 3.

I found this, which shed some light on these MIDI messages,

[url]https://www.midimountain.com/midi/midi_status.htm[/url]

So, now I need to scan this long string for "'status': 196" and if found grab the value of data1

If anyone has an elegant way of doing that, I would be much appreciative...

解决方案

The midis2events() function is pointless if you are not using the event loop.

Just extract the values from the data you have read:

...
read = inp.read(100)
for event in read:
    ((status,data1,data2,data3),timestamp) = event
    if status == 196:
        print(f"change to program {data1}")

这篇关于pygame midi 读取PC(程序更改)消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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