簡體   English   中英

使用 MetaMessage getData() 和/或 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM