Exemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tracks"></param>
        /// <returns></returns>
        public static Track Merge(ArrayList tracks)
        {
            Track mergedTrack = new Track();
            Track currentTrack;
            ArrayList trackList = new ArrayList();
            ArrayList events = new ArrayList();
            ArrayList trackIndexes = new ArrayList();

            for(int i = 0; i < tracks.Count; i++)
            {
                currentTrack = (Track)tracks[i];

                if(currentTrack.Count > 1)
                {
                    trackList.Add(currentTrack);
                    trackIndexes.Add(0);
                    events.Add(currentTrack[0]);
                }
            }

            while(events.Count > 0)
            {
                int n = 0;
                MidiEvent e1 = (MidiEvent)events[0];
                MidiEvent e2;
                int ticks = e1.Ticks;

                for(int i = 1; i < events.Count; i++)
                {
                    e1 = (MidiEvent)events[i];

                    if(e1.Ticks < ticks)
                    {
                        ticks = e1.Ticks;
                        n = i;
                    }
                }

                e1 = (MidiEvent)events[n];
                mergedTrack.Add(e1);

                for(int i = 0; i < events.Count; i++)
                {
                    e2 = (MidiEvent)events[i];
                    e2.Ticks -= e1.Ticks;
                    events[i] = e2;
                }

                int counter = (int)trackIndexes[n] + 1;

                currentTrack = (Track)trackList[n];

                if(counter < currentTrack.Count - 1)
                {
                    events[n] = currentTrack[counter];
                    trackIndexes[n] = counter;
                }
                else
                {
                    trackList.RemoveAt(n);
                    trackIndexes.RemoveAt(n);
                    events.RemoveAt(n);
                }
            }

            return mergedTrack;
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tracks"></param>
        /// <returns></returns>
        public static Track Merge(ArrayList tracks)
        {
            Track     mergedTrack = new Track();
            Track     currentTrack;
            ArrayList trackList    = new ArrayList();
            ArrayList events       = new ArrayList();
            ArrayList trackIndexes = new ArrayList();

            for (int i = 0; i < tracks.Count; i++)
            {
                currentTrack = (Track)tracks[i];

                if (currentTrack.Count > 1)
                {
                    trackList.Add(currentTrack);
                    trackIndexes.Add(0);
                    events.Add(currentTrack[0]);
                }
            }

            while (events.Count > 0)
            {
                int       n  = 0;
                MidiEvent e1 = (MidiEvent)events[0];
                MidiEvent e2;
                int       ticks = e1.Ticks;

                for (int i = 1; i < events.Count; i++)
                {
                    e1 = (MidiEvent)events[i];

                    if (e1.Ticks < ticks)
                    {
                        ticks = e1.Ticks;
                        n     = i;
                    }
                }

                e1 = (MidiEvent)events[n];
                mergedTrack.Add(e1);

                for (int i = 0; i < events.Count; i++)
                {
                    e2        = (MidiEvent)events[i];
                    e2.Ticks -= e1.Ticks;
                    events[i] = e2;
                }

                int counter = (int)trackIndexes[n] + 1;

                currentTrack = (Track)trackList[n];

                if (counter < currentTrack.Count - 1)
                {
                    events[n]       = currentTrack[counter];
                    trackIndexes[n] = counter;
                }
                else
                {
                    trackList.RemoveAt(n);
                    trackIndexes.RemoveAt(n);
                    events.RemoveAt(n);
                }
            }

            return(mergedTrack);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Merges two tracks together.
        /// </summary>
        /// <param name="trackA">
        /// The first of two tracks to merge.
        /// </param>
        /// <param name="trackB">
        /// The second of two tracks to merge.
        /// </param>
        /// <returns>
        /// The merged track.
        /// </returns>
        public static Track Merge(Track trackA, Track trackB)
        {
            Track trkA = new Track(trackA);
            Track trkB = new Track(trackB);
            Track mergedTrack = new Track();
            int a = 0, b = 0;

            //
            // The following algorithm merges two Midi tracks together. It
            // assumes that both tracks are valid in that both end with a
            // end of track meta message.
            //

            // While neither the end of track A or track B has been reached.
            while(a < trkA.Count - 1 && b < trkB.Count - 1)
            {
                // While the end of track A has not been reached and the
                // current Midi event in track A comes before the current Midi
                // event in track B.
                while(a < trkA.Count - 1 && trkA[a].Ticks <= trkB[b].Ticks)
                {
                    // Slide the events in track B backwards by the amount of
                    // ticks in the current event in track A. This keeps both
                    // tracks in sync.
                    trkB.Slide(b, -trkA[a].Ticks);

                    // Add the current event in track A to the merged track.
                    mergedTrack.Add(trkA[a]);

                    // Move to the next Midi event in track A.
                    a++;
                }

                // If the end of track A has not yet been reached.
                if(a < trkA.Count - 1)
                {
                    // While the end of track B has not been reached and the
                    // current Midi event in track B comes before the current Midi
                    // event in track A.
                    while(b < trkB.Count - 1 && trkB[b].Ticks < trkA[a].Ticks)
                    {
                        // Slide the events in track A backwards by the amount of
                        // ticks in the current event in track B. This keeps both
                        // tracks in sync.
                        trkA.Slide(a, -trkB[b].Ticks);

                        // Add the current event in track B to the merged track.
                        mergedTrack.Add(trkB[b]);

                        // Move forward to the next Midi event in track B.
                        b++;
                    }
                }
            }

            // If the end of track A has not yet been reached.
            if(a < trkA.Count - 1)
            {
                // Add the rest of the events in track A to the merged track.
                while(a < trkA.Count - 1)
                {
                    mergedTrack.Add(trkA[a]);
                    a++;
                }
            }
                // Else if the end of track B has not yet been reached.
            else if(b < trkB.Count - 1)
            {
                // Add the rest of the events in track B to the merged track.
                while(b < trkB.Count - 1)
                {
                    mergedTrack.Add(trkB[b]);
                    b++;
                }
            }

            return mergedTrack;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Merges two tracks together.
        /// </summary>
        /// <param name="trackA">
        /// The first of two tracks to merge.
        /// </param>
        /// <param name="trackB">
        /// The second of two tracks to merge.
        /// </param>
        /// <returns>
        /// The merged track.
        /// </returns>
        public static Track Merge(Track trackA, Track trackB)
        {
            Track trkA = new Track(trackA);
            Track trkB = new Track(trackB);
            Track mergedTrack = new Track();
            int   a = 0, b = 0;

            //
            // The following algorithm merges two Midi tracks together. It
            // assumes that both tracks are valid in that both end with a
            // end of track meta message.
            //

            // While neither the end of track A or track B has been reached.
            while (a < trkA.Count - 1 && b < trkB.Count - 1)
            {
                // While the end of track A has not been reached and the
                // current Midi event in track A comes before the current Midi
                // event in track B.
                while (a < trkA.Count - 1 && trkA[a].Ticks <= trkB[b].Ticks)
                {
                    // Slide the events in track B backwards by the amount of
                    // ticks in the current event in track A. This keeps both
                    // tracks in sync.
                    trkB.Slide(b, -trkA[a].Ticks);

                    // Add the current event in track A to the merged track.
                    mergedTrack.Add(trkA[a]);

                    // Move to the next Midi event in track A.
                    a++;
                }

                // If the end of track A has not yet been reached.
                if (a < trkA.Count - 1)
                {
                    // While the end of track B has not been reached and the
                    // current Midi event in track B comes before the current Midi
                    // event in track A.
                    while (b < trkB.Count - 1 && trkB[b].Ticks < trkA[a].Ticks)
                    {
                        // Slide the events in track A backwards by the amount of
                        // ticks in the current event in track B. This keeps both
                        // tracks in sync.
                        trkA.Slide(a, -trkB[b].Ticks);

                        // Add the current event in track B to the merged track.
                        mergedTrack.Add(trkB[b]);

                        // Move forward to the next Midi event in track B.
                        b++;
                    }
                }
            }

            // If the end of track A has not yet been reached.
            if (a < trkA.Count - 1)
            {
                // Add the rest of the events in track A to the merged track.
                while (a < trkA.Count - 1)
                {
                    mergedTrack.Add(trkA[a]);
                    a++;
                }
            }
            // Else if the end of track B has not yet been reached.
            else if (b < trkB.Count - 1)
            {
                // Add the rest of the events in track B to the merged track.
                while (b < trkB.Count - 1)
                {
                    mergedTrack.Add(trkB[b]);
                    b++;
                }
            }

            return(mergedTrack);
        }