在 Actionscript 3 中使用 bytearray 附加 MIDI 文件

发布于 2025-01-05 07:45:01 字数 388 浏览 5 评论 0原文

我需要附加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2025-01-12 07:45:01

根据我的评论,您可能想附加这些文件,而不是合并它们。假设是这种情况,您不能简单地将第二个文件中的数据放到第一个文件的末尾。由于 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.

太阳公公是暖光 2025-01-12 07:45:01

MIDI 文件的结构不允许您向其中“追加”更多数据,原因如下:

  1. 每个轨道均以“轨道结束”事件结束,使该事件之后的所有音符都变得毫无意义。
  2. 每个轨道头块定义了后面的数据的大小。即使您追加新数据,任何读取器在开始寻找新轨道之前也只会读取 [size] 字节。
  3. MIDI 文件定义文件中存在多少个轨道,因此即使您附加了单个 MIDI 轨道的字节数组,除非您还更新了标头数据的轨道计数,否则任何读取器都会忽略您添加的轨道。

如果将数据添加到 MIDI 文件,则需要确保保持文件格式的结构完整性。简单地附加数据并不能实现这一点。

The structure of a MIDI file doesn't allow you to just "append" more data to it, for the following reasons:

  1. Each track ends with an End of Track event, rendering all notes after that event meaningless.
  2. Each track header chunk defines the size of the data that follows. Even if you append new data, any reader will only read [size] bytes before it starts looking for a new track.
  3. A MIDI file defines how many tracks are present in the file, so even if you appended the byte array of a single MIDI track, unless you also update the track count of the header data, any reader would simply ignore the track you added.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文