使用this thread我已经弄清楚了如何使用getData(),但是我没有获得0x51的任何东西,而是获得了诸如[B @ 37d78d93,[B @ 29d74462和[B @ 1c5ca652]
输出元消息本身也会产生类似的值,例如javax.sound.midi.MetaMessage @ 364d4fca,javax.sound.midi.MetaMessage @ 5581f86d和javax.sound.midi.MetaMessage@3f011b2b
例如,使用System.out.print ("the meta message is " + mm + ", ");
System.out.print ("the type of meta message is " + mm.getType());
System.out.println(" and the data is " + mm.getData());
输出the meta message is javax.sound.midi.MetaMessage@3f011b2b, the type of meta message is 81 and the data is [B@1c5ca652
如何使用输出的mm
值末尾的值或从mm.getData()
获取的值来获取MIDI文件的速度?
最佳答案
一旦获得速度MetaMessage,就可以使用此代码以每分钟节拍的速度获得速度。
/**
* Get the tempo in BPM coded in a Tempo Midi message.
*
* @param mm Must be a tempo MetaMessage (type=81)
* @return
*/
static public int getTempoInBPM(MetaMessage mm)
{
byte[] data = mm.getData();
if (mm.getType() != 81 || data.length != 3)
{
throw new IllegalArgumentException("mm=" + mm);
}
int mspq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
int tempo = Math.round(60000001f / mspq);
return tempo;
}