Extracting tempo from MIDI file in java using MetaMessage getData() and/or MetaMessage value?

Using this thread I have figured out how to use getData(), but instead of getting anything with 0x51, I am getting random values such as [B@37d78d93, [B@29d74462 and [B@1c5ca652

Outputting the meta message itself also results in similar values such as javax.sound.midi.MetaMessage@364d4fca, javax.sound.midi.MetaMessage@5581f86d and javax.sound.midi.MetaMessage@3f011b2b

For example, using

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());

outputs

the meta message is javax.sound.midi.MetaMessage@3f011b2b, the type of meta message is 81 and the data is [B@1c5ca652

How can I use the value at the end of the outputted mm value or the values taken from mm.getData() to get the tempo of a MIDI file?

Upvotes: 0

Views: 370

Answers (1)

jjazzboss
jjazzboss

Reputation: 1422

Once you get the tempo MetaMessage, you can use this code to get the tempo in beats per minute.

   /**
     * 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;
    }

Upvotes: 1

Related Questions