在 iOS 中解析 Midi 数据包
我很难解析 Midi 数据包。有时一个流上有 3 个字节,也可能有 155 个字节。 如何遍历庞大的数据包并获得我需要的内容? 假设“b0”只有 3 个字节的 12 个字节数据包,我只想拆分“b0”及其以下字节: [b02c2c] 然后是同一个数据包中的其他 [b02c2d] 或 [f0....]...
这是我一直在做的事情,这让我很头疼..
NSString *StringFromPacket(const MIDIPacket *packet,id self)
{
NSMutableString * result = [[NSMutableString alloc] init];
for (int i = 0; i < packet->length; i++)
{
NSString *s = [NSString stringWithFormat:@"%02x",packet->data[i]];
for (NSString *line in [s componentsSeparatedByString:@"b0"])
{
// This appends to string but b0 disappears and only get the following 2 bytes
// Along with the others like f0,a0,90. I would like to filter without losing b0
[result appendFormat:line];
}
}
[self controlEvent:result];
}
-(void)controlEvent:(NSString *)line
{
if (line == @"b02c2c")
{
//Do Something
}
}
I'm having a hard time parsing Midi Packets. At times its 3 bytes then it can be 155 bytes on one stream.
How can I iterate through the massive packet and just get what I need?
Say for "b0" its only 3 bytes of 12 a byte packet, I just want to split "b0" and its following bytes:
[b02c2c] then the others [b02c2d] or [f0....] in the same packet...
Heres what I've been working on and is giving me a headache..
NSString *StringFromPacket(const MIDIPacket *packet,id self)
{
NSMutableString * result = [[NSMutableString alloc] init];
for (int i = 0; i < packet->length; i++)
{
NSString *s = [NSString stringWithFormat:@"%02x",packet->data[i]];
for (NSString *line in [s componentsSeparatedByString:@"b0"])
{
// This appends to string but b0 disappears and only get the following 2 bytes
// Along with the others like f0,a0,90. I would like to filter without losing b0
[result appendFormat:line];
}
}
[self controlEvent:result];
}
-(void)controlEvent:(NSString *)line
{
if (line == @"b02c2c")
{
//Do Something
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有 iPhone 编程经验,但我快速了解了 iOS MIDI API。
在 MIDI 中,有各种 MIDI 事件(音符打开、音符关闭、控制器更改等)。每个事件可以有不同数量的字节。从第一个字节,您可以确定事件类型,并从类型中了解消息长度(请参阅 http:// /www.midi.org/techspecs/midimessages.php )唯一的例外是系统独占 MIDI 消息(有关更多详细信息,请参阅 MIDI 参考和可以发送和接收系统独占消息的特定设备的参考)
看来 MIDIPacket可以包含可变数量的 MIDI 消息。所以你可以看看第一个字节。假设您在 MIDI 通道 1 上收到一个 Note On midi 事件。在第一个字节中,您将获得值 80(十六进制)。从文档中您可以看到事件注释有两个数据字节。下一个字节是 MIDI 音符编号(您可以在 http://midikits 上查看 MIDI 音符编号列表。 net23.net/midi_analysisr/midi_note_numbers_for_octaves.htm)。下一个音符编号是速度(按键的速度(或难度))。然后您可以重复此过程。
请注意,对于某些消息(例如音调轮更改),两个数据字节携带一个值(MSB 和 LSB)。
了解当字节以 0 开头时,它是 MIDI 数据字节可能对您有用。 MIDI 事件的第一个字节为 1。
I have no experience with IPhone programming, but I had quick look at iOS MIDI API.
In MIDI there are various MIDI events (note on, note off, controller change, etc.) Each event can have different number of bytes. From first byte you can event type and from the type you know message length (see MIDI reference at http://www.midi.org/techspecs/midimessages.php ) The only exception is System Exclusive MIDI message (for more details see MIDi reference and reference for specific device that can send and receive System Exclusive messages)
It seems that MIDIPacket can contain variable number of MIDI messages. So you can have look at first byte. Let's suppose you received a Note On midi event on MIDI channel 1. In first byte you will have value 80 (hex). From documentation you can see that Note On Event has two data bytes. Next byte is MIDI note number (you can see list of MIDI note numbers e.g. on http://midikits.net23.net/midi_analyser/midi_note_numbers_for_octaves.htm ). And the next note number is velocity (how fast (or hardly) was key pressed). And then you can repeat this procedure.
Note that for some messages, like Pitch Wheel Change, two data bytes carry one value (MSB and LSB).
It might be useful to you to know that when the byte starts with 0, it is MIDI data byte. MIDI events have 1 in their first byte.
另一个有用的资源: http://home.roadrunner.com/~jgglatt
具体来说,您可能想要查看有关 MIDI 协议消息的部分:http://home.roadrunner.com/~jgglatt/tech/midispec.htm
除此之外,解析 MIDI 只需遍历字节并相应地处理消息即可。
Another useful resource: http://home.roadrunner.com/~jgglatt
Specifically, you might want to check out the section on the MIDI protocol messages: http://home.roadrunner.com/~jgglatt/tech/midispec.htm
Beyond that, parsing MIDI is a matter of just walking through the bytes and handling the messages accordingly.