有效地解释串行数据
我正在开发一个从外部 USB 设备读取 MIDI 数据的 C++ 项目。该程序应该根据 USB 设备上哪个推子/旋钮/按钮被移动/旋转/按下(例如音量 +- 或静音/取消静音通道)来调用某些功能。
我想出找出哪个推子/旋钮/按钮被更改的唯一方法是使用一个相当大的 switch 语句,该语句基本上检查每个传入的 midi 事件。
看起来有点像这样:
switch(MidiMessage.get2ndByte()){
case 1 : cout << "Fader 1 Value : " << MidiMessage.get3rdByte() << endl;
case 2 : cout << "Fader 2 Value : " << MidiMessage.get3rdByte() << endl;
case 10 : cout << "Button 1 Value : << "MidiMessage.get3rdByte() << endl;
...
...
...
}
没有更有效/更智能的方法来做到这一点吗?
I'm working on a c++ project that reads MIDI-data from an external USB-device. The program is supposed to call certain functions depending on which fader/knob/button on the USB-device is shiftet/rotated/pressed (such as vol +- or mute/unmute channel).
The only way I came up with finding out which fader/knob/button was changed was using a pretty big switch statement that basically checks every incoming midi event.
looks sort of like this :
switch(MidiMessage.get2ndByte()){
case 1 : cout << "Fader 1 Value : " << MidiMessage.get3rdByte() << endl;
case 2 : cout << "Fader 2 Value : " << MidiMessage.get3rdByte() << endl;
case 10 : cout << "Button 1 Value : << "MidiMessage.get3rdByte() << endl;
...
...
...
}
Isn't there a more efficient/smart way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的切换是在一个字节上完成的(因此只有 256 个不同的值;我很确定 MIDI 文件基于 8 位字节),所以最好的选择可能是使用一个简单的函数指针数组:
这个数组只是使用 1KB(32 位平台)或 2KB(64 位平台),提供有保证的恒定时间查找,没有隐藏开销,而且编译器可能会在内部将大 switch 语句实现为查找表(因此,您获得的唯一开销是额外的开销)功能 称呼)。
请注意,如果存在无效字节值,则数组条目应指向显式错误函数(而不是简单的 0),以便您的程序可以优雅地处理错误。
Since your switching is done on a byte (and thus just has 256 different values; I'm pretty sure MIDI files are based on 8-bit bytes), the best option is probably to use a simple array of function pointers:
This array just uses 1KB (32 bit platforms) or 2KB (64 bit platforms), gives guaranteed constant time lookup, has no hidden overhead, and possibly your compiler implements your big switch statement internally as lookup table anyway (so the only overhead you get is an extra function call).
Note that if there are invalid byte values, the array entry should point to an explicit error function (instead of a simple 0), so your program can handle the error gracefully.
大多数编译器会将这样的大型开关编译成跳转表(或简单值的表查找),因此我建议您保留该开关。
如果情况之间的唯一区别是前缀字符串,我建议改为执行以下操作:
Most compilers will compile a large switch like that into a jump table (or a table lookup for simple values), so I would suggest you keep the switch.
If the only difference between the cases is the prefix string, I would suggest doing something like this instead: