Search code examples
c#timemidi

MIDI file NoteOn time in ms using DryWetMidi


using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.MusicTheory;
using InteractionNote = Melanchall.DryWetMidi.Interaction.Note;
using System.Security.Cryptography.X509Certificates;
using Microsoft.VisualBasic;

namespace Proba3
{
    class Program
    {
  
        static void Main(string[] args)
       {
          string SONG = "3Ako.mid";
          var midiFile = MidiFile.Read(SONG);
          var notes = midiFile.GetNotes();

         // I tried to use this - var TempoMap = midiFile.GetTempoMap();
         // I tried to use this - TempoMap tempoMap = midiFile.GetTempoMap(); 

          Console.WriteLine(SONG);

          foreach (var note in midiFile.GetNotes())         
            {             
             Console.Write($@"
             {note.Time} {note} {notes.Count}");
            }

            Console.WriteLine("        ");
            Console.WriteLine("     ---- MID END");
            
            Console.ReadKey();   
        }
    }
}   

Ouput in terminal look like this

3Ako.mid

         540 D3 3
         1589 C3 3
         2579 D3 3
 ---- MID END

I use DryWetMidi for list NoteOn time in midi file but i have no idea how recalculate delta time but veriable tempo (live playing tempo of midi file not sequenced one with exact tempo). This is code in Visual Studio Code

The 540 and 1589 and 2579 are delta time ? I need to recalculated this time to next format [minutes:seconds:miliseconds][name of note] File 3Ako.mid are cut to be short for easier manipulation this midi file are recorded in live music session and has no exact tempo like sequenced standard midi files so divide with tempo of song will not give exact time. Thank lot in advance for any kind of help.I know is lot of questions in one question.

Primary I am musician and new in C# environment.


Solution

  • var tempoMap = midiFile.GetTempoMap();
    
    Console.WriteLine(SONG);
    
    foreach (var note in midiFile.GetNotes())
    {
        Console.Write($@"{note.TimeAs<MetricTimeSpan>(tempoMap)} {note} {notes.Count}");
    }