Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Naudio获得midi事件的实时性_C#_Unity3d_Midi_Naudio - Fatal编程技术网

C# 如何使用Naudio获得midi事件的实时性

C# 如何使用Naudio获得midi事件的实时性,c#,unity3d,midi,naudio,C#,Unity3d,Midi,Naudio,我试图阅读midi音符,并使用NAudio库提取每一个音符的实时性 我写了这段代码,但它没有正确计算时间,我使用了我发现的公式 ((note.AbsTime-lastTempoEvent.AbsTime)/midi.ticksPerQuarterNote)*tempo+lastTempoEvent.RealTime 守则: var strictMode = false; var mf = new MidiFile("Assets/Audios/Evangelion Midi.mi

我试图阅读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];


    }
var stricmode=false;
var mf=新的Midi文件(“Assets/Audios/Evangelion Midi.mid”,strictMode);
mf.Events.MidiFileType=0;
List midiNotes=新列表();
List tempoEvents=new List();
对于(int n=0;n
我得到的这些显然是不正确的


有人知道我错了吗?

在这里看到有人使用MIDI真是太好了

参考代码中的
note.AbsTime-lastTempoEvent.AbsTime
部分在您这方面的实现不正确

此代码中的
lastTempoEvent
变量不能表示midi文件中的最后一次节奏更改(正如您使用
notesArray[i].AbsoluteTime-tempoEventsArr[tempoEventsArr.Length-1].AbsoluteTime实现的那样)

参考代码试图做的是获取当前
注释
时的节奏(可能通过将上次出现的节奏更改事件存储在此变量中),同时代码减去整个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);
    }

}
static void CalculateMidiRealTimes()
{
var-strictMode=false;
var mf=新的MIDI文件(“C:\\Windows\\Media\\onestop.mid”,strictMode);
mf.Events.MidiFileType=0;
//只有一个集合用于非备忘和节奏变化事件
列表事件=新列表();
对于(int n=0;n
编辑:

计算实际时间时的除法是int/float,当事件之间的滴答声小于每季度音符的增量滴答声时,结果为零

以下是使用精度最高的数字类型decimal计算值的正确方法

midi歌曲onestop.midİ的长度为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);
    }
} 
static void CalculateMidiRealTimes()
{
var-strictMode=false;
var mf=新的MIDI文件(“C:\\Windows\\Media\\onestop.mid”,strictMode);
mf.Events.MidiFileType=0;
//只有一个集合用于非备忘和节奏变化事件
列表事件=新列表();
对于(int n=0;n