public void WriteMIDI(string path) { HashSet<Note>.Enumerator enumerator = notes.GetEnumerator(); FastPriorityQueue<Note> startQueue = new FastPriorityQueue<Note>(numNotes); FastPriorityQueue<Note> endQueue = new FastPriorityQueue<Note>(numNotes); while (enumerator.MoveNext()) { startQueue.Enqueue(enumerator.Current, enumerator.Current.Priority); } using (BinaryWriter writer = new BinaryWriter(File.Open(path+trackName + ".mid", FileMode.Create))) { writer.Write(OneTrackHeader()); writer.Write(StandardTrackHeader()); long deltaStart = 0; do { if (endQueue.Peek() == null) { Note note = startQueue.Dequeue(); writer.Write(note.MidiStart(deltaStart)); deltaStart = (long) note.Priority; endQueue.Enqueue(note, note.Priority+note.GetDuration()); } else if (startQueue.Peek() == null) { Note note = endQueue.Dequeue(); writer.Write(note.MidiEnd(deltaStart)); deltaStart = (long) note.Priority; } else { if (endQueue.Peek().Priority > startQueue.Peek().Priority) { Note note = startQueue.Dequeue(); writer.Write(note.MidiStart(deltaStart)); deltaStart = (long)note.Priority; endQueue.Enqueue(note, note.Priority+note.GetDuration()); } else { Note note = endQueue.Dequeue(); writer.Write(note.MidiEnd(deltaStart)); deltaStart = (long)note.Priority; } } } while (startQueue.Count + endQueue.Count > 0); writer.Write(TrackEnd()); } }