Return a clone of a MIDI track. - Java javax.sound.midi

Java examples for javax.sound.midi:Track

Description

Return a clone of a MIDI track.

Demo Code


import javax.sound.midi.ShortMessage;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Track;
import javax.sound.midi.Sequence;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MetaMessage;
import java.util.Vector;

public class Main{
    /**/* www. j  a  v  a2s . c  om*/
     * Return a clone of a track.
     */
    public static Track cloneTrack(Track track) {
        Track newTrack = null;

        try {
            Sequence seq = new Sequence(IdealSequence.getDivisionType(),
                    IdealSequence.getResolution());
            newTrack = seq.createTrack();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        assert newTrack != null;

        for (int i = 0; i < track.size(); i++) {
            MidiEvent event = track.get(i);
            newTrack.add(event);
        }

        assert track.size() == newTrack.size();

        return newTrack;

    }
}

Related Tutorials