从Java获取来自Receiver的midi消息

时间:2017-08-14 08:27:46

标签: java audio midi receiver

我正在尝试使用接收器,这样我就可以在播放时提取midi事件信息。我想听听播放的音符并同步它们是一些事件。我已经阅读了文档,但我不确定如何实际使用发送器/接收器。如果有人可以帮助我指出如何从接收器中获取midi事件,我将非常感激。

编辑:下面提供的可能副本并不能解释接收器的实际工作方式。它提供了关于实现的源代码,但作为一个初学者,源代码太高级了,我无法理解。

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Main {

    public static Sequencer sequencer;
    public static Sequence sequence;
    public static Receiver synthRcvr;
    public static Transmitter seqTrans;

    public static void main(String[] args) {

        try {
            sequencer = MidiSystem.getSequencer();
            sequence = MidiSystem.getSequence(new File("test.midi"));

            seqTrans = sequencer.getTransmitter();
            synthRcvr = sequencer.getReceiver();
            seqTrans.setReceiver(synthRcvr);

            sequencer.open(); 
            sequencer.setSequence(sequence);

            sequencer.start();
        } catch (IOException | MidiUnavailableException | InvalidMidiDataException e) {
            System.out.println(e);
        }
    }

1 个答案:

答案 0 :(得分:2)

这是收听所有midi消息的解决方案。

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Main {

public static Receiver synthRcvr = new CustomReceiver();
public static Transmitter seqTrans;
public static Synthesizer synth;

public static void main(String[] args) {

    try {
        sequencer = MidiSystem.getSequencer();
        sequence = MidiSystem.getSequence(new File("test.midi"));

        Sequencer sequencer = MidiSystem.getSequencer();
        seqTrans = sequencer.getTransmitter();
        seqTrans.setReceiver(synthRcvr);

        sequencer.open(); 
        sequencer.setSequence(sequence);

        sequencer.start();
    } catch (IOException | MidiUnavailableException | InvalidMidiDataException e) {
        System.out.println(e);
    }
}

这是实现接收器的类:

import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;

public class CustomReceiver implements Receiver {

    public CustomReceiver() {

    }

    @Override
    public void send(MidiMessage message, long timeStamp) {
        // Process midi messages here
    }

    @Override
    public void close() {

    }
}