定序器接收器时间戳始终输出 -1
Sequencer receiver timestamp always outputs -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() {
}
}
发送方法应该输出时间戳,但它没有。它只输出-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());
效果不错
我正在尝试使用接收器从正在读取 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() {
}
}
发送方法应该输出时间戳,但它没有。它只输出-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());
效果不错