向 Java Sound 的 SysexMessage 发送大于 127 的字节时出现问题
我正在使用 JavaSound API 进行一些工作,以将 MIDI 系统专用 (Sysex) 消息发送到外部 MIDI 设备(电子键盘)。根据雅马哈手册,要发送点亮其中一个按键的数据是这一系列字节:F0 43 7F 00 00 03 00 41 F7。
根据 SysexMessage 上的 JavaDoc,发送消息数据的正确方法是使用 setMessage(int status, byte[] data, int length)
。在这种情况下,F0(或十进制 240)是状态,其他所有内容都是数据 - 包括末尾的 F7(十进制 247),它表示 Sysex 消息的结束。
问题是Java中的字节限制在-128..127范围内,所以我无法在字节数组中发送F7。但 SysexMessage 的 JavaDoc 似乎没有注意到这一事实,它说:“如果此消息包含该消息的所有系统独占数据,则它应该以状态字节 0xF7 结尾。”
关于如何正确发送最后一个字节有什么建议吗?我是否误解了 SysexMessage 的 JavaDoc?
I'm doing some work with the JavaSound API to send a MIDI System Exclusive (Sysex) message to an external MIDI device (an electronic keyboard). According to the Yamaha manual, the data to send to light up one of the keys is this series of bytes: F0 43 7F 00 00 03 00 41 F7.
According to the JavaDoc on SysexMessage, the correct way to send data for a message is with setMessage(int status, byte[] data, int length)
. In this case, F0 (or 240 decimal) is the status, and everything else is the data - including the F7 (247 decimal) at the end, which indicates the end of the Sysex message.
The problem is that bytes in Java are limited to the range -128..127, so I can't send F7 in a byte array. But the JavaDoc for SysexMessage seems oblivious to this fact, saying, "If this message contains all the system exclusive data for the message, it should end with the status byte 0xF7."
Any suggestions for how to correctly send that final byte? Am I misinterpreting the JavaDoc for SysexMessage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对 F7 这个数字的思考方式是错误的。虽然 F7 相当于 247,但它也是 -9。但是,无论您将 F7 解释为数字 247(作为无符号字节)还是数字 -9(作为有符号字节),它仍然是相同的位序列 11110111,并且当该位序列通过线路传输时对于您的键盘,键盘可以根据需要进行解释。
You are thinking about the number F7 the wrong way. While F7 is the equivalent to 247, it is also -9. But whether you interpret F7 to be the number 247 (as an unsigned byte) or to be the number -9 (as a signed byte) it is still the same sequence of bits 11110111, and when that sequence of bits is transmitted over the line to your keyboard, the keyboard may interpret however it likes.
对于此类问题,您可以安全地将任何小于或等于 255 (0xFF) 的整数值转换为字节。正如 Jessup 所说,原因是它们将由相同的位模式表示。
For this kind of problem, you can safely cast any integer value less than or equal to 255 (0xFF) to a byte. The reason being, as Jessup stated, they will be represented by the same bit pattern.