/// <summary>
        /// Sorts, removes empty tracks and adds end track markers
        /// </summary>
        public void PrepareForExport()
        {
            var comparer = new MidiEventComparer();

            // 1. sort each track
            foreach (List <MidiEvent> list in trackEvents)
            {
                MergeSort.Sort(list, comparer);

                // 2. remove all End track events except one at the very end
                int index = 0;
                while (index < list.Count - 1)
                {
                    if (MidiEvent.IsEndTrack(list[index]))
                    {
                        list.RemoveAt(index);
                    }
                    else
                    {
                        index++;
                    }
                }
            }

            int track = 0;

            // 3. remove empty tracks and add missing
            while (track < trackEvents.Count)
            {
                IList <MidiEvent> list = trackEvents[track];
                if (list.Count == 0)
                {
                    RemoveTrack(track);
                }
                else
                {
                    if (list.Count == 1 && MidiEvent.IsEndTrack(list[0]))
                    {
                        RemoveTrack(track);
                    }
                    else
                    {
                        if (!MidiEvent.IsEndTrack(list[list.Count - 1]))
                        {
                            list.Add(new MetaEvent(MetaEventType.EndTrack, 0, list[list.Count - 1].AbsoluteTime));
                        }
                        track++;
                    }
                }
            }
        }
Example #2
0
        public void PrepareForExport()
        {
            MidiEventComparer comparer = new MidiEventComparer();

            foreach (IList <MidiEvent> list in this.trackEvents)
            {
                List <MidiEvent> list2 = (List <MidiEvent>)list;
                MergeSort.Sort <MidiEvent>(list2, comparer);
                int i = 0;
                while (i < list2.Count - 1)
                {
                    if (MidiEvent.IsEndTrack(list2[i]))
                    {
                        list2.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
            int j = 0;

            while (j < this.trackEvents.Count)
            {
                IList <MidiEvent> list3 = this.trackEvents[j];
                if (list3.Count == 0)
                {
                    this.RemoveTrack(j);
                }
                else if (list3.Count == 1 && MidiEvent.IsEndTrack(list3[0]))
                {
                    this.RemoveTrack(j);
                }
                else
                {
                    if (!MidiEvent.IsEndTrack(list3[list3.Count - 1]))
                    {
                        list3.Add(new MetaEvent(MetaEventType.EndTrack, 0, list3[list3.Count - 1].AbsoluteTime));
                    }
                    j++;
                }
            }
        }