skip to Main Content

This is my code from Visual Studio Code
I want to get time format for note On
[00:00.000] first minutes. 00: second seconds. 00. third miliseconds. 000



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 Melanchall.DryWetMidi.Common;
using System.Security.Cryptography.X509Certificates;
using Microsoft.VisualBasic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection.Metadata;



namespace Proba3
{
    class Program
   {
          static void Main(string[] a r g s)
       {
            var SONG = "3Ako.mid";
          Console.WriteLine("__________________________________________");
Console.WriteLine("");
Console.WriteLine($"            Name of mid file  =  {SONG} ");
          
          var midiFile = MidiFile.Read(SONG);
          var tempoMap = midiFile.GetTempoMap();
          var notes = midiFile.GetNotes();
          var fileHeaderToken = midiFile.GetTrackChunks();

Melanchall.DryWetMidi.Core.TrackChunkUtilities.GetChannels(fileHeaderToken);  
var timedEvents = midiFile.GetTimedEvents();
var a = new Melanchall.DryWetMidi.MusicTheory.ChordProgression(); 
          
//IEnumerable<Melanchall.DryWetMidi.Interaction.Chord> //chordsAt20seconds = midiFile
//          .GetChords()
//          .AtTime(
//            new MetricTimeSpan(0, 0, 20),
//            tempoMap,
//            LengthedObjectPart.Entire);
//            TimeSpan midiFileDuration = 
// midiFile.GetDuration<MetricTimeSpan>();

Console.WriteLine($"Midi File duration [{SONG}] {midiFileDuration}");
            
   foreach (var chord in midiFile.GetChords())
           {
   Console.Write($@"       
Time = [{chord .Time As <Metric Time Span>( tempo Map)}] CHORD {chord}
Chan =  {chord. Channel} "); 
           }
Console. WriteLine("__________________________________");
Console.WriteLine($"{timedEvents.Count} timed events found.");   
          
Console.WriteLine("     ---- MID END");
Console.ReadKey();
              
    }   
} 

Output in terminal looks like this

Name of mid file  =  3Ako.mid
Midi File duration [ 3Ako.mid ] = 00:00:04.9203250

                                     Time = [0:0:0:741] CHORD: D3
                                     Chan =  1

                                     Time = [0:0:2:182] CHORD: C3
                                     Chan =  1        

                                     Time = [0:0:3:542] CHORD: D3
                                     Chan =  1 __________________________________
10 timed events found.

     ---- MID END

3Ako.mid is very short midi file just for testing purpose please

I tried to change output format for time on this position in Dry Wet Midi but without success.

enter image description here

using Melanchall.DryWetMidi.Common;
using System;
using System.Component.Model; 


namespace Melanchall.DryWetMidi.Interaction 


public sealed class Metric TimeSpan : ITimeSpan,IComparable<MetricTime Span>,IEquatable<MetricTimeSpan>

{

public override string ToString()
        {
            return $"{Minutes}:{Seconds}:{Milliseconds}";
            // I change upper line in - return $"[{Minutes}:{Seconds}. 
            {Milliseconds}]";
            // to get format [00:00.000]  * first min. * second 
            // seconds * third milisec. 
            // I deleted code with hours definition 
            // I can not change format in
            // $"{Minutes}:{Seconds}.{Milliseconds}";
            // Terminal shows [0:0:3:542]
        }
}

2

Answers


  1. Chosen as BEST ANSWER

    I am really sorry for spaces but I have problem with red underline lines in that parts of code and I can not put code in and sent Question. Then I make spaces to avoid block of main and code windows in stackoverflow window for Question. Because of that I make spaces. Latter I delete it. I put lot of code for testing and because of that, code looks mess. So I apologize all community and you. And thanks lot for your answer.


  2. First of all, as you’ve been said, you don’t worry about formatting of your code samples. More than that, your code won’t compile (for example, chord .Time As <Metric Time Span>, do you see those spaces?). It means you don’t respect community which helps you for free.

    Secondly, the library name is DryWetMIDI, without spaces. If people will search Stack Overflow for the library name (correct name), they miss your question and answer on it.

    Well, as for your problem. MetricTimeSpan can be cast to standard TimeSpan type. According to the Custom TimeSpan format strings article you can solve your task like this:

        var timeSpan = (TimeSpan)chord.TimeAs<MetricTimeSpan>(tempoMap);
        Console.Write($@"       
    Time = [{timeSpan:mm:ss:fff}] CHORD {chord}
    Chan =  {chord.Channel} ");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search