Java将Midi消息发送到设备 [英] Java send midi message to device
本文介绍了Java将Midi消息发送到设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个连接到树莓派的APC40 MkII.在pi上,我正在运行Java.使用java.sound.midi包,我可以毫无问题地建立与APC的连接并接收其Midi消息.但是经过大量研究,我仍然不知如何向APC发送Midi消息.
I have an APC40 MkII connected to a raspberry pi. On the pi, I have java running. With the java.sound.midi package, I can establish a connection to the APC and receive its midi messages without a problem. But after a lot of research I somehow still don't get how I can send midi messages to the APC.
这是我到目前为止的代码:
Here is the code I have so far:
package lightorgansandbox;
import javax.sound.midi.*;
import java.util.List;
public class MidiHandler {
public MidiHandler() {
MidiDevice device;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (MidiDevice.Info info : infos) {
System.out.println("Info: '" + info.toString() + "'");
try {
device = MidiSystem.getMidiDevice(info);
List<Transmitter> transmitters = device.getTransmitters();
for(int j = 0; j<transmitters.size();j++) {
transmitters.get(j).setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));
}
Transmitter trans = device.getTransmitter();
trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));
device.open();
}catch (MidiUnavailableException e) {}
}
}
public class MidiInputReceiver implements Receiver {
public String name;
public MidiInputReceiver(String name) {
this.name = name;
}
public void send(MidiMessage msg, long timeStamp) {
if (msg instanceof ShortMessage) {
ShortMessage shortMessage = (ShortMessage) msg;
int channel = shortMessage.getChannel();
int pitch = shortMessage.getData1();
int vel = shortMessage.getData2();
System.out.println("Channel: " + channel);
System.out.println("Pitch: " + pitch);
System.out.println("vel: " + vel);
}
}
public void close() {}
}
}
这一切都按照我想要的方式工作.但是我可以在哪里发送消息回设备?
This all works just the way I want. But where in this can I send a message back to the device?
推荐答案
只需将消息发送到设备的接收者. 从文档:
Just send the message to the device's receiver. From the documentation:
Receiver rcvr = MidiSystem.getReceiver(); /* or device.getReceiver() */
ShortMessage myMsg = new ShortMessage();
myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
long timeStamp = -1;
rcvr.send(myMsg, timeStamp);
这篇关于Java将Midi消息发送到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文