■ MidiFile 클래스를 사용해 MIDI 파일을 탐색하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
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 } } |