从MIDI活动到INT的十六进制字节阵列

发布于 2025-01-25 06:54:43 字数 562 浏览 2 评论 0原文

我正在尝试通过javax.midi库来检索MIDI文件的节奏。

MidiMessage message = event.getMessage();
if(message instanceof MetaMessage)
 MetaMessage mm = (MetaMessage) message;
 System.out.println(Arrays.toString(mm.getData()));
}

我期望收到的是三个ex的数组,因为设置的节奏元消息(这种情况,只有3个字节指定了一个miliseconds数量。 这就是MIDI事件返回的方式

0x07 0xA1 0x20

在打印中返回,

[7, -95, 32]

,因此,如果您加入它们,则有500,000毫秒的0x07A120,但是Java首先 它将其解析为十进制,然后我需要获得500,000个数字。我不知道该怎么做,因为首先我需要加入所有十六进制,然后我将拥有这个数字,但我不知道该怎么做。

有人可以帮我吗?

I'm trying to retrieve the tempo of a midi file through the javax.midi library.

MidiMessage message = event.getMessage();
if(message instanceof MetaMessage)
 MetaMessage mm = (MetaMessage) message;
 System.out.println(Arrays.toString(mm.getData()));
}

What I am expected to receive is an array of three ex, because the Set Tempo meta message (which is this case, has just 3 bytes specifying a miliseconds amount.
This is how the midi event returns

0x07 0xA1 0x20

So if you join them you have 0x07A120 which is 500,000 ms, but Java returns in the print

[7, -95, 32]

First of all it is parsing it to decimal, and then I need to get this 500,000 number. I don't know how to do it because first I need to join all the hex, and then I will have the number, but I don't know how to do it.

Anyone can help me, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2025-02-01 06:54:44

metAmessage.getData()返回一个字节数组,您可以使用arrays.aslist()将其变成字节列表,这就是为什么打印字节列表的原因。

以下是一种从节奏元素中计算出每分钟节奏的节奏的方法。 MSPQ变量(每季度毫秒)是您寻求的价值。

/**
 * Get the tempo in BPM coded in a Tempo Midi message.
 *
 * @param tempoMsg Must be a tempo MetaMessage (type=81)
 * @return
 */
static public int getTempoInBPM(MetaMessage tempoMsg)
{
    byte[] data = tempoMsg.getData();
    if (tempoMsg.getType() != 81 || data.length != 3)
    {
        throw new IllegalArgumentException("tempoMsg=" + tempoMsg);   
    }
    int mspq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
    int tempo = Math.round(60000001f / mspq);
    return tempo;
}

您会在Java找到更多MIDI实用程序。

MetaMessage.getData() returns a byte array, which you turn into a list of bytes using Arrays.asList(), that's why you print a list of bytes.

Below is a method that compute the tempo in beats per minute from a TEMPO MetaMessage. The mspq variable (milliseconds per quarter) is the value you seek.

/**
 * Get the tempo in BPM coded in a Tempo Midi message.
 *
 * @param tempoMsg Must be a tempo MetaMessage (type=81)
 * @return
 */
static public int getTempoInBPM(MetaMessage tempoMsg)
{
    byte[] data = tempoMsg.getData();
    if (tempoMsg.getType() != 81 || data.length != 3)
    {
        throw new IllegalArgumentException("tempoMsg=" + tempoMsg);   
    }
    int mspq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
    int tempo = Math.round(60000001f / mspq);
    return tempo;
}

Here you'll find some more Midi utilities in java.

太阳男子 2025-02-01 06:54:44
int tempo = new BigInteger(mm.getData()).intValue();

会更简单

int tempo = new BigInteger(mm.getData()).intValue();

would be simpler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文