定序器接收器时间戳始终输出-1

时间:2019-02-17 06:30:42

标签: java midi javasound

我正在尝试使用接收器从正在读取Midi的音序器中输出时间戳。当我尝试将时间戳输出到控制台时,它输出-1。 运行midi的代码:

public class MidiReadAndPlay {

    public static Receiver synthRcvr = new CustomReceiver();
    public static Transmitter seqTrans;
    public static Sequencer sequencer;
    public static Sequence sequence;
    public static Synthesizer synth;

    public static void main(String[] args) throws Exception {


        sequencer = MidiSystem.getSequencer();
        sequence = MidiSystem.getSequence(new File("File.mid"));

        seqTrans = sequencer.getTransmitter();
        seqTrans.setReceiver(synthRcvr);
        sequencer.open(); 
        sequencer.setSequence(sequence);

        sequencer.start();



    }

}

接收方代码:

public class CustomReceiver implements Receiver {

    public CustomReceiver() {

    }
    public static final int NOTE_ON = 0x90;
    public static final int NOTE_OFF = 0x80;
    public static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

    @Override
    public void send(MidiMessage message, long timeStamp) {
        System.out.println(timeStamp);
        if (message instanceof ShortMessage) {
            ShortMessage sm = (ShortMessage) message;
            System.out.print("Channel: " + sm.getChannel() + " ");
            if (sm.getCommand() == NOTE_ON) {
                int key = sm.getData1();
                int octave = (key / 12)-1;
                int note = key % 12;
                String noteName = NOTE_NAMES[note];
                int velocity = sm.getData2();
                System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
            } else if (sm.getCommand() == NOTE_OFF) {
                int key = sm.getData1();
                int octave = (key / 12)-1;
                int note = key % 12;
                String noteName = NOTE_NAMES[note];
                int velocity = sm.getData2();
                System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
            } else {
                System.out.println("Command:" + sm.getCommand());
            }
        } else {
            System.out.println("Other message: " + message.getClass());
        }
    }

    @Override
    public void close() {

    }
}

send方法应该输出时间戳,但不会输出。它只输出-1。有办法解决这个问题还是我做错了什么?我正在尝试获取演奏音符的时间戳。 谢谢。

编辑

我提出了此解决方案,但它并不总是有效。

int milliseconds = Math.toIntExact(MidiReadAndPlay.sequencer.getMicrosecondPosition()) / 1000;
int ppq = MidiReadAndPlay.sequence.getResolution();
int bpm = Math.round(MidiReadAndPlay.sequencer.getTempoInBPM());
int msPerTick = 60000 / (bpm * ppq);
int tick = milliseconds/msPerTick;
System.out.println("milliseconds: " + milliseconds + ", ppq: " + ppq + ", bpm: " + bpm + ", msPerTick: " + msPerTick + ", tick: " + tick);

有时壁虱的确很大,通常成千上万。 对于一个Midi,ppq为480,bpm为120。我认为公式可能有误。

编辑2

我发现此函数可以找到刻度线,我觉得这比我上面写的解决方案要好。

int tick = Math.toIntExact(sequencer.getTickPosition());

我觉得它输出的跳动很高,因为3分钟的midi结束于176000跳动。应该这么高吗?

1 个答案:

答案 0 :(得分:0)

发送功能中的时间戳不起作用。所以我只是用:

int tick = Math.toIntExact(sequencer.getTickPosition());

效果很好