如何更改音符时间输出的干湿Midi格式

问题描述 投票:0回答:1

这是我来自 Visual Studio Code 的代码 我想获取注释的时间格式 [00:00.000] 第一分钟。 00:第二秒。 00.第三毫秒。 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();
              
    }   
} 

终端中的输出如下所示

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 是非常短的 midi 文件,仅供测试用途

我尝试更改 Dry Wet Midi 中此位置的时间输出格式,但没有成功。

在此输入图片描述

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]
        }
}

c# time format midi
1个回答
0
投票

首先,正如您所说,您不必担心代码示例的格式。不仅如此,您的代码将无法编译(例如,

chord .Time As <Metric Time Span>
,您看到这些空格了吗?)。这意味着你不尊重免费帮助你的社区。

其次,库名称为DryWetMIDI,不带空格。如果人们在 Stack Overflow 中搜索库名称(正确名称),他们就会错过您的问题和答案。

好吧,至于你的问题。

MetricTimeSpan
可以铸造为标准
TimeSpan
类型。根据 Custom TimeSpan format strings 文章,您可以像这样解决您的任务:

    var timeSpan = (TimeSpan)chord.TimeAs<MetricTimeSpan>(tempoMap);
    Console.Write($@"       
Time = [{timeSpan:mm\:ss\:fff}] CHORD {chord}
Chan =  {chord.Channel} ");
© www.soinside.com 2019 - 2024. All rights reserved.