Пример #1
0
    void ProcessMidi()
    {
        VideoTrack destVideoTrack = new VideoTrack(0, "destVideoTrack");
        AudioTrack destAudioTrack = new AudioTrack(0, "destAudioTrack");

        vegas.Project.Tracks.Add(destAudioTrack);
        vegas.Project.Tracks.Add(destVideoTrack);

        MidiFile midi = new MidiFile(midiFilePath);
        Dictionary <string, double> onNotes = new Dictionary <string, double>();

        TempoEvent lastTempoEvent     = new TempoEvent(0, 0);
        double     lastTempoEventTime = 0;

        for (int i = 0; i < midi.Events.Count(); i++)
        {
            foreach (MidiEvent midiEvent in midi.Events[i])
            {
                if (midiEvent is TempoEvent)
                {
                    lastTempoEvent     = (midiEvent as TempoEvent);
                    lastTempoEventTime = ((double)(midiEvent.AbsoluteTime - lastTempoEvent.AbsoluteTime) / midi.DeltaTicksPerQuarterNote) * lastTempoEvent.MicrosecondsPerQuarterNote + lastTempoEventTime;
                }
                else if (midiEvent is NoteEvent && midiEvent.Channel == midiReadLayer)
                {
                    NoteEvent noteEvent = midiEvent as NoteEvent;
                    double    usTime    = ((double)(noteEvent.AbsoluteTime - lastTempoEvent.AbsoluteTime) / midi.DeltaTicksPerQuarterNote) * lastTempoEvent.MicrosecondsPerQuarterNote + lastTempoEventTime;
                    double    msTime    = usTime / 1000;
                    int       velocity  = noteEvent.Velocity;

                    if (noteEvent.CommandCode == MidiCommandCode.NoteOn)
                    {
                        onNotes[noteEvent.NoteName] = msTime;
                    }
                    else if (noteEvent.CommandCode == MidiCommandCode.NoteOff)
                    {
                        if (onNotes.ContainsKey(noteEvent.NoteName))
                        {
                            double noteStartTime = onNotes[noteEvent.NoteName];
                            double noteEndTime   = msTime;
                            int    blipNum       = GetBlipFromPitch(noteEvent.NoteName);

                            if (blipNum == -1)
                            {
                                //note not found in pitchBlipMap
                                continue;
                            }

                            onNotes.Remove(noteEvent.NoteName);

                            //Map the MIDI note to a video/audio event
                            VideoEvent videoEvent = selectedVideoEvent.Copy(destVideoTrack, selectedVideoEvent.Start) as VideoEvent;
                            AudioEvent audioEvent = selectedAudioEvent.Copy(destAudioTrack, selectedAudioEvent.Start) as AudioEvent;

                            Timecode clipDurationMillis      = new Timecode(noteEndTime - noteStartTime + noteEndOffset);
                            Timecode globalClipStartLocation = new Timecode(selectedVideoEvent.Start.ToMilliseconds() + (blips[blipNum].locationInMicroseconds / 1000) + blipStartOffset);

                            TrackEvent tmpTrackEvent = videoEvent;
                            util.TrimEvent(ref tmpTrackEvent, globalClipStartLocation, clipDurationMillis);
                            videoEvent = tmpTrackEvent as VideoEvent;

                            tmpTrackEvent = audioEvent;
                            util.TrimEvent(ref tmpTrackEvent, globalClipStartLocation, clipDurationMillis);
                            audioEvent = tmpTrackEvent as AudioEvent;
                            audioEvent.FadeIn.Length  = new Timecode(fadeInLength);
                            audioEvent.FadeOut.Length = new Timecode(fadeOutLength);

                            videoEvent.Start = new Timecode(noteStartTime);
                            audioEvent.Start = new Timecode(noteStartTime);
                        }
                    }
                }
            }
        }
    }