我正在尝试实现自己的单轨MIDI文件输出。它将存储在多个帧中的8x8网格颜色转换为MIDI文件,可以导入数字音频界面并通过Novation Launchpad播放。更多上下文here。
我设法输出一个程序识别为MIDI的文件,但是生成的MIDI不能播放,而且它不匹配通过相同帧数据生成的文件。我一直在通过专用的MIDI程序录制我的节目现场MIDI信息进行比较,然后通过该程序吐出一个MIDI文件。然后我通过十六进制编辑器将生成的文件与正确生成的文件进行比较。就标题而言,事情是正确的,但似乎就是这样。
我一直在为MIDI规范的多个演绎和现有的Stack Overflow问题而不是100%的解决方案。
这是我的代码,基于我研究过的内容。我情不自禁地觉得我错过了一些简单的事情。我正在避免使用现有的MIDI库,因为我只需要这个MIDI功能就可以工作(并希望从头开始学习这种经验)。任何指导都会非常有用。
/// <summary>
/// Outputs an MIDI file based on frames for the Novation Launchpad.
/// </summary>
/// <param name="filename"></param>
/// <param name="frameData"></param>
/// <param name="bpm"></param>
/// <param name="ppq"></param>
public static void WriteMidi(string filename, List<FrameData> frameData, int bpm, int ppq) {
decimal totalLength = 0;
using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) {
// Output midi file header
stream.WriteByte(77);
stream.WriteByte(84);
stream.WriteByte(104);
stream.WriteByte(100);
for (int i = 0; i < 3; i++) {
stream.WriteByte(0);
}
stream.WriteByte(6);
// Set the track mode
byte[] trackMode = BitConverter.GetBytes(Convert.ToInt16(0));
stream.Write(trackMode, 0, trackMode.Length);
// Set the track amount
byte[] trackAmount = BitConverter.GetBytes(Convert.ToInt16(1));
stream.Write(trackAmount, 0, trackAmount.Length);
// Set the delta time
byte[] deltaTime = BitConverter.GetBytes(Convert.ToInt16(60000 / (bpm * ppq)));
stream.Write(deltaTime, 0, deltaTime.Length);
// Output track header
stream.WriteByte(77);
stream.WriteByte(84);
stream.WriteByte(114);
stream.WriteByte(107);
for (int i = 0; i < 3; i++) {
stream.WriteByte(0);
}
stream.WriteByte(12);
// Get our total byte length for this track. All colour arrays are the same length in the FrameData class.
byte[] bytes = BitConverter.GetBytes(frameData.Count * frameData[0].Colours.Count * 6);
// Write our byte length to the midi file.
stream.Write(bytes, 0, bytes.Length);
// Cycle through frames and output the necessary MIDI.
foreach (FrameData frame in frameData) {
// Calculate our relative delta for this frame. Frames are originally stored in milliseconds.
byte[] delta = BitConverter.GetBytes((double) frame.TimeStamp / 60000 / (bpm * ppq));
for (int i = 0; i < frame.Colours.Count; i++) {
// Output the delta length to MIDI file.
stream.Write(delta, 0, delta.Length);
// Get the respective MIDI note based on the colours array index.
byte note = (byte) NoteIdentifier.GetIntFromNote(NoteIdentifier.GetNoteFromPosition(i));
// Check if the current color signals a MIDI off event.
if (!CheckEqualColor(frame.Colours[i], Color.Black) && !CheckEqualColor(frame.Colours[i], Color.Gray) && !CheckEqualColor(frame.Colours[i], Color.Purple)) {
// Signal a MIDI on event.
stream.WriteByte(144);
// Write the current note.
stream.WriteByte(note);
// Check colour and write the respective velocity.
if (CheckEqualColor(frame.Colours[i], Color.Red)) {
stream.WriteByte(7);
} else if (CheckEqualColor(frame.Colours[i], Color.Orange)) {
stream.WriteByte(83);
} else if (CheckEqualColor(frame.Colours[i], Color.Green) || CheckEqualColor(frame.Colours[i], Color.Aqua) || CheckEqualColor(frame.Colours[i], Color.Blue)) {
stream.WriteByte(124);
} else if (CheckEqualColor(frame.Colours[i], Color.Yellow)) {
stream.WriteByte(127);
}
} else {
// Calculate the delta that the frame had.
byte[] offDelta = BitConverter.GetBytes((double) (frameData[frame.Index - 1].TimeStamp / 60000 / (bpm * ppq)));
// Write the delta to MIDI.
stream.Write(offDelta, 0, offDelta.Length);
// Signal a MIDI off event.
stream.WriteByte(128);
// Write the current note.
stream.WriteByte(note);
// No need to set our velocity to anything.
stream.WriteByte(0);
}
}
}
}
}
答案 0 :(得分:2)
BitConverter.GetBytes
以本机字节顺序返回字节,但MIDI文件使用big-endian值。如果您在x86或ARM上运行,则必须反转字节。ppq
。12
;你必须写实际长度。
由于delta时间的可变长度编码(见下文),在收集轨道的所有字节之前通常不可能这样做。TimeStamp * bpm * ppq / 60000
。double
浮点数,而是存储为可变长度数;规范有编码的示例代码。答案 1 :(得分:0)
另一种方法是使用其中一个.NET MIDI库来编写MIDI文件。您只需将帧转换为Midi对象并将其传递到库中进行保存。该库将处理所有MIDI细节。
您可以尝试MIDI.NET和C# Midi Toolkit。不确定NAudio是否写入MIDI文件以及是什么抽象级别...
以下是有关MIDI文件格式规范的更多信息: http://www.blitter.com/~russtopia/MIDI/~jglatt/tech/midifile.htm
希望它有所帮助, 马克