如何从音量级别创建 MIDI Sysex Master Volume 消息? [英] How to create MIDI Sysex Master Volume message from volume level?
问题描述
如果音量级别表示为 0 到 1 之间的浮点值,如何创建 Universal SysEx Master Volume 消息的数据字节?
If volume level is represented as a float value between 0 and 1, how to create data bytes of the Universal SysEx Master Volume message?
这是 Sysex 消息构造函数,具有给定的 Sysex 消息状态字节 (0xF0) 和数据字节:
This is Sysex message constructor, with given Sysex message status byte (0xF0) and data bytes:
new SysexMessage(0xF0, data, data.length)
根据 MIDI 规范,Master Volume 消息中有 2 个状态字节和 6 个数据字节(不含状态字节),最后两个数据字节指定音量级别:
According to MIDI specification, there are 2 status bytes and 6 data bytes in the Master Volume message (without status bytes), with the last two data bytes specifying volume level:
0xF0 SysEx(状态)
0x7F 通用实时
0x7F 忽略频道
0x04 子 ID -- 设备控制
0x01 Sub-ID2 -- 主音量
0xLL 14 位卷的第 0 位到第 6 位
0xMM 14 位卷的第 7 到 13 位
0xF7 SysEx 结束(状态)
0xF0 SysEx (Status)
0x7F Universal Realtime
0x7F Disregards channel
0x04 Sub-ID -- Device Control
0x01 Sub-ID2 -- Master Volume
0xLL Bits 0 to 6 of a 14-bit volume
0xMM Bits 7 to 13 of a 14-bit volume
0xF7 End of SysEx (Status)
所以,如果我没记错的话,数据字节应该是这样的:
So, if I'm not wrong, data bytes should look like this:
data = new byte[] { 0x7F, 0x7F, 0x04, 0x01, LL, MM }
我的问题是如何从 0 到 1 之间的 float 音量级别获取 LL 和 MM 字节?
My question is how to get LL and MM bytes from a float volume level between 0 and 1?
推荐答案
对于像这样的无符号类型,14 位的最小值为零,最大值为 214-1 =16383.所以要将 1.0 转换为 16383,只需乘以:
For an unsigned type like this, the smalles 14-bit value is zero, and the largest value is 214-1 = 16383. So to convert 1.0 to 16383, just multiply by that:
int value_14bits = (int)(float_value * 16383);
如果你是偏执狂,检查范围:
If you are paranoid, check the range:
value_14bits = Math.max(Math.min(value_14bits, 16383), 0);
然后提取上下七位字段:
Then extract the lower and upper seven-bit fields:
data = new byte[] {
0x7F, 0x7F, 0x04, 0x01,
value_14bits & 0x7f,
value_14bits >> 7 };
这篇关于如何从音量级别创建 MIDI Sysex Master Volume 消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!