public void AddNote(Note newNote) { this.ListNote.Add(newNote); //if (newNote.NoteNumber < 60) //{ // LocationKey = 3; // BaseKey = 43; // NameKey = "F"; //} int min = int.MaxValue; int max = int.MinValue; foreach (Note note in ListNote) { if (note.NoteNumber < min) min = note.NoteNumber; if (note.NoteNumber > max) max = note.NoteNumber; } int[,] tabKey = new int[2, 2]; tabKey[0, 0] = 43; tabKey[0, 1] = 54; tabKey[1, 0] = 64; tabKey[1, 1] = 75; int[] tabResult = new int[2]; for (int j = min; j <= max; j++) { for (int i = 0; i < 2; i++) { if (j >= tabKey[i, 0] && j <= tabKey[i, 1]) tabResult[i]++; } } if (tabResult[0] > tabResult[1]) { LocationKey = 3; BaseKey = 43; NameKey = "F"; } else { LocationKey = 1; BaseKey = 64; NameKey = "G"; } }
public void OpenMidiFile(string fileName) { if (!File.Exists(fileName)) { Form1_Resize(null, null); return; } //NAudio.Midi.MidiFile midiFile = new MidiFile(@"D:\Libraries\Musics\Midi\beethoven-pour-elise.mid"); //flourish //town MidiFile midiFile = new MidiFile(fileName); string part = String.Empty; TimeSignatureEvent lastTimeSignature = null;// new TimeSignatureEvent(4,4,24,32, List<NoteEvent> listNote = new List<NoteEvent>(); Dictionary<float, string> dicNoteType = new Dictionary<float, string>(); //dicNoteType.Add(1.75f * 4f, "Ronde double pointée"); //dicNoteType.Add(1.5f * 4f, "Ronde pointée"); dicNoteType.Add(4f, "Ronde"); dicNoteType.Add(1.75f * 2f, "Blanche double pointée"); dicNoteType.Add(1.5f * 2f, "Blanche pointée"); dicNoteType.Add(2f, "Blanche"); dicNoteType.Add(1.75f * 1f, "Noire double pointée"); dicNoteType.Add(1.5f * 1f, "Noire pointée"); dicNoteType.Add(1f, "Noire"); dicNoteType.Add(1.75f * 0.5f, "Croche double pointée"); dicNoteType.Add(1.5f * 0.5f, "Croche pointée"); dicNoteType.Add(0.5f, "Croche"); dicNoteType.Add(1.75f * 0.25f, "Double croche double pointée"); dicNoteType.Add(1.5f * 0.25f, "Double croche pointée"); dicNoteType.Add(0.25f, "Double croche"); dicNoteType.Add(1.75f * 0.125f, "Triple croche double pointée"); dicNoteType.Add(1.5f * 0.125f, "Triple croche pointée"); dicNoteType.Add(0.125f, "Triple croche"); dicNoteType.Add(1.75f * 0.0625f, "Quadruple croche double pointée"); dicNoteType.Add(1.5f * 0.0625f, "Quadruple croche pointée"); dicNoteType.Add(0.0625f, "Quadruple croche"); music = new Music(); music.ListChanel = new List<Channel>(); Measure curMeasure = null; bool hasNote = false; StringBuilder partition = new StringBuilder(); if (midiFile != null) { part += String.Format(" [ DeltaTicksPerQuarterNote : {0} ] ", midiFile.DeltaTicksPerQuarterNote); for (int i = 0; i < midiFile.Tracks; i++) { Channel currentChannel = null; currentChannel = new Channel(); hasNote = false; partition.AppendLine(); foreach (MidiEvent midiEvent in midiFile.Events[i]) { NoteOnEvent noteEvent = midiEvent as NoteOnEvent; partition.AppendLine(); if (midiEvent is TimeSignatureEvent) { lastTimeSignature = midiEvent as TimeSignatureEvent; //part += String.Format(" [ {0} ] ", midiEvent.ToString()); partition.AppendLine(String.Format(" [ {0} ] ", midiEvent.ToString())); } else if (noteEvent != null && noteEvent.CommandCode == MidiCommandCode.NoteOn && noteEvent.Velocity > 0) { int noteLength = 1; noteLength = noteEvent.NoteLength; partition.AppendLine(String.Format(" [ AbsoluteTime {0} # DeltaTime {1} # NoteName {2} # NoteNumber {3} # NoteLength {4} # Channel {5}] ", noteEvent.AbsoluteTime, noteEvent.DeltaTime, noteEvent.NoteName, noteEvent.NoteNumber, noteLength, noteEvent.Channel)); float typeNote = (float)noteLength / (float)midiFile.DeltaTicksPerQuarterNote; string typeNoteString = String.Empty; if (dicNoteType.ContainsKey(typeNote)) typeNoteString = dicNoteType[typeNote]; partition.AppendLine(String.Format(" OFF :: {0} :: {1}", typeNote, typeNoteString)); listNote.Add(noteEvent); float measureLenght = (float)lastTimeSignature.Numerator * 4f / (float)lastTimeSignature.No32ndNotesInQuarterNote * (float)midiFile.DeltaTicksPerQuarterNote; Note newNote = new Note(); newNote.NoteLength = noteEvent.NoteLength; newNote.NoteName = noteEvent.NoteName; newNote.NoteNumber = noteEvent.NoteNumber; newNote.AbsoluteTime = noteEvent.AbsoluteTime; curMeasure = currentChannel.FindMeasure(noteEvent.AbsoluteTime); if (curMeasure == null) { int nbPrevMeasure = (int)(noteEvent.AbsoluteTime / measureLenght); curMeasure = new Measure(nbPrevMeasure * measureLenght, measureLenght); currentChannel.AddMeasure(curMeasure); } curMeasure.AddNote(newNote); hasNote = true; } else { partition.AppendLine(String.Format(" [ {0} ] ", midiEvent.ToString())); } } if (hasNote) { music.AddChannel(currentChannel); } } } Form1_Resize(null, null); }