c# - 如何使用 Nadio 获取 midi 事件的实时时间
问题描述
我正在尝试阅读 midi 笔记并使用 NAudio 库提取每个笔记的实时时间我编写了这段代码,但它没有正确计算时间,我使用了我在这里找到的公式
((note.AbsTime - lastTempoEvent.AbsTime) / midi.ticksPerQuarterNote) * tempo + lastTempoEvent.RealTime
编码:
var strictMode = false;
var mf = new MidiFile("Assets/Audios/Evangelion Midi.mid", strictMode);
mf.Events.MidiFileType = 0;
List<MidiEvent> midiNotes = new List<MidiEvent>();
List<TempoEvent> tempoEvents = new List<TempoEvent>();
for (int n = 0; n < mf.Tracks; n++)
{
foreach (var midiEvent in mf.Events[n])
{
if (!MidiEvent.IsNoteOff(midiEvent))
{
midiNotes.Add(midiEvent);
TempoEvent tempoE;
try { tempoE = (TempoEvent)midiEvent; tempoEvents.Add(tempoE);
Debug.Log("Absolute Time " + tempoE.AbsoluteTime);
}
catch { }
}
}
}
notesArray = midiNotes.ToArray();
tempoEventsArr = tempoEvents.ToArray();
eventsTimesArr = new float[notesArray.Length];
eventsTimesArr[0] = 0;
for (int i = 1; i < notesArray.Length; i++)
{
((notesArray[i].AbsoluteTime - tempoEventsArr[tempoEventsArr.Length - 1].AbsoluteTime) / mf.DeltaTicksPerQuarterNote)
* tempoEventsArr[tempoEventsArr.Length - 1].MicrosecondsPerQuarterNote + eventsTimesArr[i-1];
}
我得到了这些显然不正确的值
有人我错了吗?
解决方案
在这里看到有人进入 MIDI 真是太好了。
引用代码中的note.AbsTime - lastTempoEvent.AbsTime
部分在您这边实现不正确。
此代码中的lastTempoEvent
变量不能表示 midi 文件中的最后一个速度变化(因为您已经使用 实现了它notesArray[i].AbsoluteTime - tempoEventsArr[tempoEventsArr.Length - 1].AbsoluteTime
)。
引用的代码试图做的是获取当前时间的速度note
(可能通过将最后出现的速度变化事件存储在此变量中),而您的代码正在减去整体中最新速度变化的绝对时间.midi 文件。这是负数的根本原因(如果在当前音符之后有任何速度变化)。
旁注:我还建议保留注释事件的时间安排。如果您不知道何时发布便笺,您如何关闭它?尝试这个。我测试了它并且它有效。请仔细阅读内嵌评论。
注意安全。
static void CalculateMidiRealTimes()
{
var strictMode = false;
var mf = new MidiFile("C:\\Windows\\Media\\onestop.mid", strictMode);
mf.Events.MidiFileType = 0;
// Have just one collection for both non-note-off and tempo change events
List<MidiEvent> midiEvents = new List<MidiEvent>();
for (int n = 0; n < mf.Tracks; n++)
{
foreach (var midiEvent in mf.Events[n])
{
if (!MidiEvent.IsNoteOff(midiEvent))
{
midiEvents.Add(midiEvent);
// Instead of causing stack unwinding with try/catch,
// we just test if the event is of type TempoEvent
if (midiEvent is TempoEvent)
{
Debug.Write("Absolute Time " + (midiEvent as TempoEvent).AbsoluteTime);
}
}
}
}
// Now we have only one collection of both non-note-off and tempo events
// so we cannot be sure of the size of the time values array.
// Just employ a List<float>
List<float> eventsTimesArr = new List<float>();
// we introduce this variable to keep track of the tempo changes
// during play, which affects the timing of all the notes coming
// after it.
TempoEvent lastTempoChange = null;
for (int i = 0; i < midiEvents.Count; i++)
{
MidiEvent midiEvent = midiEvents[i];
TempoEvent tempoEvent = midiEvent as TempoEvent;
if (tempoEvent != null)
{
lastTempoChange = tempoEvent;
// Remove the tempo event to make events and timings match - index-wise
// Do not add to the eventTimes
midiEvents.RemoveAt(i);
i--;
continue;
}
if (lastTempoChange == null)
{
// If we haven't come accross a tempo change yet,
// set the time to zero.
eventsTimesArr.Add(0);
continue;
}
// This is the correct formula for calculating the real time of the event
// in microseconds:
var realTimeValue =
((midiEvent.AbsoluteTime - lastTempoChange.AbsoluteTime) / mf.DeltaTicksPerQuarterNote)
*
lastTempoChange.MicrosecondsPerQuarterNote + eventsTimesArr[eventsTimesArr.Count - 1];
// Add the time to the collection.
eventsTimesArr.Add(realTimeValue);
Debug.WriteLine("Time for {0} is: {1}", midiEvents.ToString(), realTimeValue);
}
}
编辑:
计算实时时间时的除法是 int/float,当事件之间的刻度小于每四分音符的增量刻度时,结果为零。
这是使用具有最佳精度的数字类型小数计算值的正确方法。
midi歌曲onestop.mid İS 4:08(248秒)长,我们的最终事件实时是247.3594906770833
static void CalculateMidiRealTimes()
{
var strictMode = false;
var mf = new MidiFile("C:\\Windows\\Media\\onestop.mid", strictMode);
mf.Events.MidiFileType = 0;
// Have just one collection for both non-note-off and tempo change events
List<MidiEvent> midiEvents = new List<MidiEvent>();
for (int n = 0; n < mf.Tracks; n++)
{
foreach (var midiEvent in mf.Events[n])
{
if (!MidiEvent.IsNoteOff(midiEvent))
{
midiEvents.Add(midiEvent);
// Instead of causing stack unwinding with try/catch,
// we just test if the event is of type TempoEvent
if (midiEvent is TempoEvent)
{
Debug.Write("Absolute Time " + (midiEvent as TempoEvent).AbsoluteTime);
}
}
}
}
// Switch to decimal from float.
// decimal has 28-29 digits percision
// while float has only 6-9
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types
// Now we have only one collection of both non-note-off and tempo events
// so we cannot be sure of the size of the time values array.
// Just employ a List<float>
List<decimal> eventsTimesArr = new List<decimal>();
// Keep track of the last absolute time and last real time because
// tempo events also can occur "between" events
// which can cause incorrect times when calculated using AbsoluteTime
decimal lastRealTime = 0m;
decimal lastAbsoluteTime = 0m;
// instead of keeping the tempo event itself, and
// instead of multiplying every time, just keep
// the current value for microseconds per tick
decimal currentMicroSecondsPerTick = 0m;
for (int i = 0; i < midiEvents.Count; i++)
{
MidiEvent midiEvent = midiEvents[i];
TempoEvent tempoEvent = midiEvent as TempoEvent;
// Just append to last real time the microseconds passed
// since the last event (DeltaTime * MicroSecondsPerTick
if (midiEvent.AbsoluteTime > lastAbsoluteTime)
{
lastRealTime += ((decimal)midiEvent.AbsoluteTime - lastAbsoluteTime) * currentMicroSecondsPerTick;
}
lastAbsoluteTime = midiEvent.AbsoluteTime;
if (tempoEvent != null)
{
// Recalculate microseconds per tick
currentMicroSecondsPerTick = (decimal)tempoEvent.MicrosecondsPerQuarterNote / (decimal)mf.DeltaTicksPerQuarterNote;
// Remove the tempo event to make events and timings match - index-wise
// Do not add to the eventTimes
midiEvents.RemoveAt(i);
i--;
continue;
}
// Add the time to the collection.
eventsTimesArr.Add(lastRealTime);
Debug.WriteLine("Time for {0} is: {1}", midiEvent, lastRealTime / 1000000m);
}
}
推荐阅读
- python - Matplotlib.pyplot 只显示最终图
- python - 用于在 python 中定义 index 和 len() 的 for 循环
- rdbms - RDBMS Ref. Integrity: A child with too many parents?
- lua - 循环崩溃时取消 on_input
- python - 如何在plotly python中参考日期设置色阶
- c++ - 接受 const 和非 const 对象的 C++ 模板
- c++ - RGBA buffer to JPEG libjpeg-turbo
- python - How to integrate python scripting in my python code
- r - r 从特定工作表批量读取 xls 文件
- sql - SQL 组详细信息