使用 MetaMessage getData() and/or MetaMessage 值从 java 中的 MIDI 文件中提取速度?
Extracting tempo from MIDI file in java using MetaMessage getData() and/or MetaMessage value?
使用 我已经弄清楚如何使用 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;
}
使用
输出元消息本身也会产生类似的值,例如 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;
}