在 Actionscript 3 中使用 bytearray 附加 MIDI 文件
我需要附加 MIDI 文件:保留标头(所有文件都相同)和其他元信息,只需复制音乐/乐谱部分。 我已经在适当的字节数组中有 MIDI 文件,因为我猜我需要使用 writeBytes,但不幸的是找不到我需要获取和复制的字节。 像这样:
var newFileBytes:ByteArray=new ByteArray();
newFileBytes.writeBytes(firstMIDIBytes);
newFileBytes.writeBytes(secondMIDIBytes,8);
仅部分工作,文件可播放;第一部分完整,第二部分 - 只有一些音符(然后播放器挂起)
说实话,字节数组不是我的强项,因为 MIDI 文件结构。 你能建议如何解决这个问题吗? 提前致谢。
I need to append MIDI files: leave header (same for all files) and other meta information, just copy music/score part.
I already have MIDI files in appropriate bytearrays, as I guessed I need to use writeBytes, but unfortunately couldn't find which bytes I need to take and copy.
Something like this:
var newFileBytes:ByteArray=new ByteArray();
newFileBytes.writeBytes(firstMIDIBytes);
newFileBytes.writeBytes(secondMIDIBytes,8);
Works only partially, file is playable; first part fully and second - only some notes (then player hangs out)
To say truth byteArrays aren't my strong side, as the MIDI file structure.
Can you suggest how to solve this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的评论,您可能想附加这些文件,而不是合并它们。假设是这种情况,您不能简单地将第二个文件中的数据放到第一个文件的末尾。由于 MIDI 协议针对带宽进行了优化,因此它对事件流做出了许多假设。这些行为意味着您在附加 MIDI 数据时必须特别小心。
MIDI 文件可以(通常)使用运行状态,这意味着事件可能会省略状态字节,在这种情况下,事件应使用前一个事件的状态字节。这可能不是问题的原因,但您绝对确定您只解析原始 MIDI 数据,而不是文件头等?如果是这种情况,各种奇怪的数据都会被错误地解释为有效的 MIDI 事件。
MIDI 文件中的事件使用序列中前一个事件的相对偏移量。计算方法有点复杂,但它涉及 MIDI 文件头中定义的一些属性(例如速度、脉冲数/秒等)。如果您剥离了这些事件,并且第二个文件的属性不同,那么这些事件的时间将是错误的。
基本上,附加两个 MIDI 文件的唯一安全方法是通过音序器播放它们并将它们重新写入新的流。附加字节数组可能会导致许多神秘的错误。
As per my comment, you probably mean to append these files, not merge them. Assuming that to be the case, you can't simply slap the data from the second file to the end of the first. As the MIDI protocol is bandwidth-optimized, it makes a number of assumptions regarding the streaming of events. These behaviors mean that you must take special care when appending MIDI data.
MIDI files can (and usually) use running status, which means that an even may omit the status byte, in which case the event should use the status byte of the previous event. This may not be the cause of your problems, but are you absolutely sure that you are only parsing raw MIDI data, and not the file headers and such? If this were the case, all sorts of weird data would be erroneously interpreted as valid MIDI events.
Events in MIDI files use relative offsets to the previous event in the sequence. The way that this is calculated is a bit complicated, but it involves a few properties (such as tempo, number of pulses/sec, etc) which are defined in the MIDI file header. If you stripped these events, and the properties are different for the second file, then the timing of these events will be wrong.
Basically, the only safe way to append the two MIDI files is to play them through a sequencer and re-write them to a new stream. Appending the byte arrays will probably be the cause of many mysterious bugs.
MIDI 文件的结构不允许您向其中“追加”更多数据,原因如下:
如果将数据添加到 MIDI 文件,则需要确保保持文件格式的结构完整性。简单地附加数据并不能实现这一点。
The structure of a MIDI file doesn't allow you to just "append" more data to it, for the following reasons:
If you add data to a MIDI file, you need to make sure the structural integrity of the file format is maintained. Simply appending data does not accomplish this.