示例#1
0
    private void cutMusic(int from, int to, MidiMusic music, string outputFilename)
    {
        FileStream stream = File.Create(outputFilename);
        SmfWriter  writer = new SmfWriter(stream);

        writer.WriteHeader(music.Format, (short)music.Tracks.Count, music.DeltaTimeSpec);
        IList <MidiTrack> tracks = music.Tracks;

        for (var i = 0; i < tracks.Count; i++)
        {
            var  track          = tracks[i];
            int  passedTime     = 0;
            var  newTrack       = new MidiTrack();
            bool isFirstMessage = true;

            for (var j = 0; j < track.Messages.Count; j++)
            {
                var message = track.Messages[j];
                passedTime += message.DeltaTime;

                if (passedTime < from && message.Event.EventType != MidiEvent.NoteOn && message.Event.EventType != MidiEvent.NoteOff)
                {
                    var convertedMsg = MidiUtil.convertTimeToZero(message);
                    newTrack.AddMessage(convertedMsg);
                }
                else if (passedTime >= from && passedTime < to)
                {
                    if (isFirstMessage)
                    {
                        int newDeltaTime = passedTime - from;
                        newTrack.AddMessage(new MidiMessage(newDeltaTime, message.Event));
                        isFirstMessage = false;
                    }
                    else
                    {
                        newTrack.AddMessage(message);
                    }
                }
                else if (passedTime >= to && message.Event.EventType == MidiEvent.Meta && message.Event.MetaType == MidiMetaType.EndOfTrack)
                {
                    MidiMessage convertedMsg = MidiUtil.convertTimeToZero(message);
                    newTrack.AddMessage(convertedMsg);
                }
            }

            track = newTrack;
            // Debug.Log("Track " + (i + 1) + " Passed time:" + passedTime);
            writer.WriteTrack(track);
        }

        stream.Close();
    }
示例#2
0
        /// <summary>
        /// Writes tracks to specified file
        /// </summary>
        /// <param name="outputFileName">Name of the destination file</param>
        /// <param name="tracks">Tracks to write</param>
        public void writeTracksToFile(IList <MidiTrack> tracks, string outputFileName)
        {
            foreach (MidiTrack midiTrack in tracks)
            {
                FileStream stream = this.createFile(outputFileName);
                SmfWriter  writer = new SmfWriter(stream);

                writer.WriteHeader(music.Format, (short)tracks.Count(), music.DeltaTimeSpec);
                foreach (MidiTrack track in tracks)
                {
                    writer.WriteTrack(track);
                }

                stream.Close();
            }
        }