本文介绍了如何从MetaMessage.getData()返回的字节数组中获取整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从midi文件中获取速度值。我发现,set_tempo命令的值为0x51,所以我有这段代码:

I need to get the tempo value from midi file. I found out, that the set_tempo command has value 0x51, so i have this piece of code:

    for (int i = 0; i < tracks[0].size(); i++) {
        MidiEvent event = tracks[0].get(i);
        MidiMessage message = event.getMessage();
        if (message instanceof MetaMessage) {
            MetaMessage mm = (MetaMessage) message;
            if(mm.getType()==SET_TEMPO){
                // now what?
                mm.getData();
            }
        }
    }

但方法getData()返回一个字节数组!如何将其转换为正常的人形,a.k.a。整数?
我读过它以这样的格式存储:tt tt tt,但整个大/小端,签名/无符号和可变长度的东西使它太混乱了。

But the method getData() returns an array of bytes! How can I convert it to normal human form, a.k.a. integer?I have read it is stored in format like this: "tt tt tt", but the whole big/little endian, signed/unsigned, and variable length things make it too confusing.

推荐答案

Tempo 是一个3字节的大端整数, Bits Per Minute 计算为
BPM = 60,000,000 /(tt tt tt)

Tempo is a 3-byte big-endian integer and Bits Per Minute is calculated as
BPM = 60,000,000 / (tt tt tt)

byte[] data = mm.getData();
int tempo = (data[0] & 0xff) << 16 | (data[1] & 0xff) << 8 | (data[2] & 0xff);
int bpm = 60000000 / tempo;

这篇关于如何从MetaMessage.getData()返回的字节数组中获取整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:34