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

标签 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 刻度结束。应该这么高吗?

最佳答案

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

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

效果很好

关于java - 定序器接收器时间戳始终输出-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730759/

相关文章:

java - 尽管可用,但元素对于复选框不可见

java - 使用 selenium 驱动程序在鼠标悬停时测试工具提示

c++ - 类型名称不允许输出MIDI文件值

java - 在 Java 中访问 MidiDevice

audio - 在 Java 中交替播放 2 个不同的频率

java - 使用各种键作为值反序列化 json

Java 从文件中读取对象 vector 仅读取 vector 中的第一个对象

java - 使用 javax.sound.midi.Sequencer 的 Clojure 问题

java - 在没有外部库的情况下在java中将mp3转换为WAV

java - 我怎么知道我是否在事件派发线程上?