midi java,Java将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 transmitters = device.getTransmitters();

for(int j = 0; 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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值