첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Program.cs

using System;
using System.Linq;

using NAudio.Midi;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region MBT 문자열 구하기 - GetMBTString(eventTime, tickCountPerQuarterNote, timeSignatureEvent)

        /// <summary>
        /// MBT 문자열 구하기
        /// </summary>
        /// <param name="eventTime">이벤트 시간</param>
        /// <param name="tickCountPerQuarterNote">분기별 눈금 수</param>
        /// <param name="timeSignatureEvent">시간 서명 이벤트</param>
        /// <returns>MBT 문자열</returns>
        private static string GetMBTString(long eventTime, int tickCountPerQuarterNote, TimeSignatureEvent timeSignatureEvent)
        {
            int beatCountPerBar  = timeSignatureEvent == null ? 4 : timeSignatureEvent.Numerator;
            int tickCountPerBar  = timeSignatureEvent == null ? tickCountPerQuarterNote * 4 :
                                       (timeSignatureEvent.Numerator * tickCountPerQuarterNote * 4) / (1 << timeSignatureEvent.Denominator);
            int tickCountPerBeat = tickCountPerBar / beatCountPerBar;

            long bar  = 1 + (eventTime / tickCountPerBar);
            long beat = 1 + ((eventTime % tickCountPerBar) / tickCountPerBeat);
            long tick = eventTime % tickCountPerBeat;

            return $"{bar}:{beat}:{tick}";
        }

        #endregion
        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            string filePath = @"MIDI\sample.mid";

            MidiFile midiFile = new MidiFile(filePath, false);

            Console.WriteLine($"포맷 : {midiFile.FileFormat}, 트랙 수 : {midiFile.Tracks}, 분기별 델타 눈금 수 : {midiFile.DeltaTicksPerQuarterNote}");

            Console.WriteLine();

            for(int i = 0; i < midiFile.Tracks; i++)
            {
                TimeSignatureEvent timeSignatureEvent = midiFile.Events[i].OfType<TimeSignatureEvent>().FirstOrDefault();

                foreach(MidiEvent midiEvent in midiFile.Events[i])
                {
                    if(!MidiEvent.IsNoteOff(midiEvent))
                    {
                        string mbt = GetMBTString
                        (
                            midiEvent.AbsoluteTime,
                            midiFile.DeltaTicksPerQuarterNote,
                            timeSignatureEvent
                        );

                        Console.WriteLine("{0} {1}", mbt, midiEvent);
                    }
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요