如何连接midi接收器输出其数据?

时间:2017-02-10 00:24:08

标签: java midi

我正在尝试编写一个小代码,它将从钢琴中输入midi输入,并在每次按下控制台键时输出音符信息。到目前为止,我已经获得了列出可用MIDI设备的代码,并连接到设备的发送器,然后发送器将信息发送到接收器。

如何将(MIDI音符的)数据显示到控制台并不断刷新?我在考虑使用while循环。

import java.util.Scanner;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Track;
import javax.sound.midi.Transmitter;
import javax.sound.sampled.DataLine.Info;

public class Get {

    public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, InterruptedException{
        javax.sound.midi.MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
        System.out.println("Select Midi Device Number:");
        for(int i=0;i<infos.length;i++){
            System.out.println("Device Number " + i + ": " + infos[i].getName() + " - " + infos[i].getDescription());
        }
        System.out.println("Enter device number then press enter...");
        Scanner in = new Scanner(System.in);
        int deviceNumber = in.nextInt();
        MidiDevice input1Device = MidiSystem.getMidiDevice(infos[deviceNumber]);
        MidiDevice inputDevice = MidiSystem.getMidiDevice(infos[deviceNumber]);
        Sequencer sequencer = MidiSystem.getSequencer();
        Transmitter trans = inputDevice.getTransmitter();
        trans.setReceiver(MidiSystem.getReceiver());
        Receiver rec = trans.getReceiver();

    }

}

1 个答案:

答案 0 :(得分:0)

当发射器有消息时,它会调用所有已连接接收器的send()方法。因此,要获取消息,您必须实现自己的类,以实现Receiver接口。

请参阅示例程序Listens to a MIDI port and dump the received event to the console