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

问题描述 投票:1回答:1

我正在尝试使用接收器从正在读取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蜱结束。这应该是那么高吗?

java midi javasound
1个回答
0
投票

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

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

效果很好

© www.soinside.com 2019 - 2024. All rights reserved.