Ejemplo n.º 1
0
        /// <summary>Converts a MIDI sequence from its current format to the specified format.</summary>
        /// <param name="sequence">The sequence to be converted.</param>
        /// <param name="format">The format to which we want to convert the sequence.</param>
        /// <param name="options">Options used when doing the conversion.</param>
        /// <returns>The converted sequence.</returns>
        /// <remarks>
        /// This may or may not return the same sequence as passed in.
        /// Regardless, the reference passed in should not be used after this call as the old
        /// sequence could be unusable if a different reference was returned.
        /// </remarks>
        public static MidiSequence Convert(MidiSequence sequence, int format, FormatConversionOptions options)
        {
            // Validate the parameters
            if (sequence == null)
            {
                throw new ArgumentNullException("sequence");
            }
            if (format < 0 || format > 2)
            {
                throw new ArgumentOutOfRangeException("format", format, "The format must be 0, 1, or 2.");
            }

            // Handle the simple cases
            if (sequence.Format == format)
            {
                return(sequence);                                       // already in requested format
            }
            if (format != 0 || sequence.NumberOfTracks == 1)            // only requires change in format #
            {
                // Change the format and return the same sequence
                sequence.SetFormat(format);
                return(sequence);
            }

            // Now the hard one, converting to format 0.
            // We need to combine all tracks into 1.
            MidiSequence newSequence = new MidiSequence(format, sequence.Division);
            MidiTrack    newTrack    = newSequence.AddTrack();

            // Iterate through all events in all tracks and change deltaTimes to actual times.
            // We'll then be able to sort based on time and change them back to deltas later
            foreach (MidiTrack track in sequence)
            {
                track.Events.ConvertDeltasToTotals();
            }

            // Add all events to new track (except for end of track markers!)
            int trackNumber = 0;

            foreach (MidiTrack track in sequence)
            {
                foreach (MidiEvent midiEvent in track.Events)
                {
                    // If this event has a channel, and if we're storing tracks as channels, copy to it
                    if ((options & FormatConversionOptions.CopyTrackToChannel) > 0 &&
                        (midiEvent is VoiceMidiEvent) &&
                        trackNumber >= 0 && trackNumber <= 0xF)
                    {
                        ((VoiceMidiEvent)midiEvent).Channel = (byte)trackNumber;
                    }

                    // Add all events, except for end of track markers (we'll add our own)
                    if (!(midiEvent is EndOfTrack))
                    {
                        newTrack.Events.Add(midiEvent);
                    }
                }
                trackNumber++;
            }

            // Sort the events
            newTrack.Events.SortByTime();

            // Now go back through all of the events and update the times to be deltas
            newTrack.Events.ConvertTotalsToDeltas();

            // Put an end of track on for good measure as we've already taken out
            // all of the ones that were in the original tracks.
            newTrack.Events.Add(new EndOfTrack(0));

            // Return the new sequence
            return(newSequence);
        }
 public static MidiSequence Convert(MidiSequence sequence, int format, FormatConversionOptions options)
 {
     if (sequence == null)
     {
         throw new ArgumentNullException("sequence");
     }
     if ((format < 0) || (format > 2))
     {
         throw new ArgumentOutOfRangeException("format", format, "The format must be 0, 1, or 2.");
     }
     if (sequence.Format == format)
     {
         return sequence;
     }
     if ((format != 0) || (sequence.NumberOfTracks == 1))
     {
         sequence.SetFormat(format);
         return sequence;
     }
     MidiSequence sequence2 = new MidiSequence(format, sequence.Division);
     MidiTrack track = sequence2.AddTrack();
     foreach (MidiTrack track2 in sequence)
     {
         track2.Events.ConvertDeltasToTotals();
     }
     int num = 0;
     foreach (MidiTrack track3 in sequence)
     {
         foreach (MidiEvent event2 in track3.Events)
         {
             if ((((options & FormatConversionOptions.CopyTrackToChannel) > FormatConversionOptions.None) && (event2 is VoiceMidiEvent)) && ((num >= 0) && (num <= 15)))
             {
                 ((VoiceMidiEvent) event2).Channel = (byte) num;
             }
             if (!(event2 is EndOfTrack))
             {
                 track.Events.Add(event2);
             }
         }
         num++;
     }
     track.Events.SortByTime();
     track.Events.ConvertTotalsToDeltas();
     track.Events.Add(new EndOfTrack(0L));
     return sequence2;
 }
Ejemplo n.º 3
0
        /// <summary>Converts a MIDI sequence from its current format to the specified format.</summary>
        /// <param name="sequence">The sequence to be converted.</param>
        /// <param name="format">The format to which we want to convert the sequence.</param>
        /// <param name="options">Options used when doing the conversion.</param>
        /// <returns>The converted sequence.</returns>
        /// <remarks>
        /// This may or may not return the same sequence as passed in.
        /// Regardless, the reference passed in should not be used after this call as the old
        /// sequence could be unusable if a different reference was returned.
        /// </remarks>
        public static MidiSequence Convert(MidiSequence sequence, int format, FormatConversionOptions options)
        {
            // Validate the parameters
            if (sequence == null) throw new ArgumentNullException("sequence");
            if (format < 0 || format > 2) throw new ArgumentOutOfRangeException("format", format, "The format must be 0, 1, or 2.");

            // Handle the simple cases
            if (sequence.Format == format) return sequence; // already in requested format
            if (format != 0 || sequence.NumberOfTracks == 1) // only requires change in format #
            {
                // Change the format and return the same sequence
                sequence.SetFormat(format);
                return sequence;
            }

            // Now the hard one, converting to format 0.
            // We need to combine all tracks into 1.
            MidiSequence newSequence = new MidiSequence(format, sequence.Division);
            MidiTrack newTrack = newSequence.AddTrack();

            // Iterate through all events in all tracks and change deltaTimes to actual times.
            // We'll then be able to sort based on time and change them back to deltas later
            foreach(MidiTrack track in sequence) track.Events.ConvertDeltasToTotals();

            // Add all events to new track (except for end of track markers!)
            int trackNumber = 0;
            foreach(MidiTrack track in sequence)
            {
                foreach(MidiEvent midiEvent in track.Events)
                {
                    // If this event has a channel, and if we're storing tracks as channels, copy to it
                    if ((options & FormatConversionOptions.CopyTrackToChannel) > 0 &&
                        (midiEvent is VoiceMidiEvent) &&
                        trackNumber >= 0 && trackNumber <= 0xF)
                    {
                        ((VoiceMidiEvent)midiEvent).Channel = (byte)trackNumber;
                    }

                    // Add all events, except for end of track markers (we'll add our own)
                    if (!(midiEvent is EndOfTrack)) newTrack.Events.Add(midiEvent);
                }
                trackNumber++;
            }

            // Sort the events
            newTrack.Events.SortByTime();

            // Now go back through all of the events and update the times to be deltas
            newTrack.Events.ConvertTotalsToDeltas();

            // Put an end of track on for good measure as we've already taken out
            // all of the ones that were in the original tracks.
            newTrack.Events.Add(new EndOfTrack(0));

            // Return the new sequence
            return newSequence;
        }