예제 #1
0
    public void FromVegas(Vegas vegas)
    {
        // select a midi file
        MessageBox.Show("请选择一个MIDI文件。");
        OpenFileDialog openFileDialog = new OpenFileDialog();

        openFileDialog.Filter           = "*.mid|*.mid|所有文件|*.*";
        openFileDialog.RestoreDirectory = true;
        openFileDialog.FilterIndex      = 1;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            midiName = openFileDialog.FileName;
        }
        else
        {
            return;
        }

        MidiFile midi = new MidiFile(midiName);

        // generate statistics of each midi track
        String[] trackInfo       = new String[midi.Events.Tracks];
        int      ticksPerQuarter = midi.DeltaTicksPerQuarterNote;
        double   msPerQuarter    = 0;

        for (int i = 0; i < midi.Events.Tracks; i++)
        {
            String info1      = "轨道 " + i.ToString() + ": ";
            String info2      = "";
            int    notesCount = 0;
            String info3      = "起音 ";

            foreach (MidiEvent midiEvent in midi.Events[i])
            {
                if ((midiEvent is NoteEvent) && !(midiEvent is NoteOnEvent))
                {
                    NoteEvent noteEvent = midiEvent as NoteEvent;
                    if (notesCount == 0)
                    {
                        info3 = info3 + noteEvent.NoteName;
                    }
                    notesCount++;
                }
                if ((midiEvent is PatchChangeEvent) && info2.Length == 0)
                {
                    PatchChangeEvent patchEvent = midiEvent as PatchChangeEvent;
                    for (int j = 4; j < patchEvent.ToString().Split(' ').Length; j++)
                    {
                        info2 += patchEvent.ToString().Split(' ')[j];
                    }
                }
                if ((midiEvent is TempoEvent) && msPerQuarter == 0)
                {
                    TempoEvent tempoEvent = midiEvent as TempoEvent;
                    msPerQuarter = Convert.ToDouble(tempoEvent.MicrosecondsPerQuarterNote) / 1000;
                }
            }

            trackInfo[i] = info1 + info2 + "; 音符数: " + notesCount.ToString() + "; " + info3;
        }

        // select a video clip
        MessageBox.Show("请选择一个视频或图片素材片段。");
        openFileDialog.Filter           = "所有文件|*.*";
        openFileDialog.RestoreDirectory = true;
        openFileDialog.FilterIndex      = 1;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            clipName = openFileDialog.FileName;
        }
        else
        {
            return;
        }
        Media  media       = new Media(clipName);
        double mediaLength = media.Length.ToMilliseconds();

        // start configuration
        Form2 configForm = new Form2();

        for (int i = 1; i < midi.Events.Tracks; i++)
        {
            configForm.comboBox1.Items.Add(trackInfo[i]);
        }
        configForm.comboBox1.SelectedIndex = 0;
        Application.Run(configForm);

        // apply condiguration
        for (int i = 1; i < midi.Events.Tracks; i++)
        {
            if (trackInfo[i] == configForm.comboBox1.SelectedItem.ToString())
            {
                midiTrack = i;
            }
        }
        sheetWidth    = int.Parse(configForm.width);
        sheetPosition = int.Parse(configForm.position);
        sheetGap      = int.Parse(configForm.gap);
        if (configForm.comboBox2.Text == "2/4")
        {
            sheetTempo = 2;
        }
        if (configForm.comboBox2.Text == "3/4")
        {
            sheetTempo = 3;
        }
        if (configForm.comboBox3.Text == "低音")
        {
            sheetCelf = 1;
        }

        // start processing MIDI
        VideoTrack[] noteTracks   = new VideoTrack[100];
        int          trackCount   = -1;
        int          trackPointer = 0;
        double       barStartTime = 0;
        double       barLength    = msPerQuarter * sheetTempo;

        foreach (MidiEvent midiEvent in midi.Events[midiTrack])
        {
            if (midiEvent is NoteOnEvent)
            {
                NoteEvent   noteEvent   = midiEvent as NoteEvent;
                NoteOnEvent noteOnEvent = midiEvent as NoteOnEvent;
                double      startTime   = midiEvent.AbsoluteTime * msPerQuarter / ticksPerQuarter;
                double      duration    = noteOnEvent.NoteLength * msPerQuarter / ticksPerQuarter;
                int         pitch       = noteEvent.NoteNumber;


                // next page
                while (startTime >= barStartTime + barLength)
                {
                    barStartTime = barStartTime + barLength;
                    trackPointer = 0;
                }

                // generate video events
                if (trackPointer > trackCount)
                {
                    trackCount             = trackCount + 1;
                    noteTracks[trackCount] = vegas.Project.AddVideoTrack();
                }

                VideoEvent videoEvent = noteTracks[trackPointer].AddVideoEvent(Timecode.FromMilliseconds(startTime), Timecode.FromMilliseconds(barStartTime + barLength - startTime));
                Take       take       = videoEvent.AddTake(media.GetVideoStreamByIndex(0));
                TrackEvent trackEvent = videoEvent as TrackEvent;
                trackEvent.Loop = true;

                TrackMotionKeyframe keyFrame = noteTracks[trackPointer].TrackMotion.InsertMotionKeyframe(Timecode.FromMilliseconds(startTime));
                keyFrame.Type      = VideoKeyframeType.Hold;
                keyFrame.Width     = sheetGap * 2 * vegas.Project.Video.Width / vegas.Project.Video.Height;
                keyFrame.Height    = sheetGap * 2;
                keyFrame.PositionX = -sheetWidth / 2 + sheetWidth / barLength * (startTime - barStartTime);
                int octave = pitch / 12;
                int line   = pitchMap[pitch % 12];
                keyFrame.PositionY = sheetPosition - sheetGap * 3 + (octave - 5) * sheetGap * 3.5 + line * sheetGap * 0.5 + sheetCelf * 12;

                trackPointer = trackPointer + 1;
            }
        }
    }
예제 #2
0
    public void FromVegas(Vegas vegas)
    {
        // select a midi file
        MessageBox.Show("请选择一个MIDI文件。");
        OpenFileDialog openFileDialog = new OpenFileDialog();

        openFileDialog.Filter           = "*.mid|*.mid|所有文件|*.*";
        openFileDialog.RestoreDirectory = true;
        openFileDialog.FilterIndex      = 1;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            midiName = openFileDialog.FileName;
        }
        else
        {
            return;
        }

        MidiFile midi = new MidiFile(midiName);

        // generate statistics of each midi track
        String[] trackInfo       = new String[midi.Events.Tracks];
        int      ticksPerQuarter = midi.DeltaTicksPerQuarterNote;
        double   msPerQuarter    = 0;

        for (int i = 0; i < midi.Events.Tracks; i++)
        {
            String info1      = "轨道 " + i.ToString() + ": ";
            String info2      = "";
            int    notesCount = 0;
            String info3      = "起音 ";

            foreach (MidiEvent midiEvent in midi.Events[i])
            {
                if ((midiEvent is NoteEvent) && !(midiEvent is NoteOnEvent))
                {
                    NoteEvent noteEvent = midiEvent as NoteEvent;
                    if (notesCount == 0)
                    {
                        info3 = info3 + noteEvent.NoteName;
                    }
                    notesCount++;
                }
                if ((midiEvent is PatchChangeEvent) && info2.Length == 0)
                {
                    PatchChangeEvent patchEvent = midiEvent as PatchChangeEvent;
                    for (int j = 4; j < patchEvent.ToString().Split(' ').Length; j++)
                    {
                        info2 += patchEvent.ToString().Split(' ')[j];
                    }
                }
                if ((midiEvent is TempoEvent) && msPerQuarter == 0)
                {
                    TempoEvent tempoEvent = midiEvent as TempoEvent;
                    msPerQuarter = Convert.ToDouble(tempoEvent.MicrosecondsPerQuarterNote) / 1000;
                }
            }

            trackInfo[i] = info1 + info2 + "; 音符数: " + notesCount.ToString() + "; " + info3;
        }

        // select a video clip
        MessageBox.Show("请选择一个视频或音频素材片段。");
        openFileDialog.Filter           = "所有文件|*.*";
        openFileDialog.RestoreDirectory = true;
        openFileDialog.FilterIndex      = 1;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            clipName = openFileDialog.FileName;
        }
        else
        {
            return;
        }
        Media  media       = new Media(clipName);
        double mediaLength = media.Length.ToMilliseconds();

        // start configuration
        ConfigForm configForm = new ConfigForm();

        for (int i = 0; i < midi.Events.Tracks; i++)
        {
            configForm.comboBoxTrack.Items.Add(trackInfo[i]);
        }
        configForm.comboBoxTrack.SelectedIndex = 0;
        Application.Run(configForm);

        // apply configuration
        aconfig          = configForm.checkBoxA.Checked;
        aconfigNoTune    = configForm.checkBoxNoTune.Checked;
        vconfig          = configForm.checkBoxV.Checked;
        vconfigAutoFlip  = configForm.checkBoxFlip.Checked;
        vconfigStartSize = configForm.hScrollBar1.Value;
        vconfigEndSize   = configForm.hScrollBar2.Value;
        vconfigFadein    = configForm.hScrollBar4.Value;
        vconfigFadeout   = configForm.hScrollBar3.Value;
        aconfigBasePitch = pitchMap[configForm.comboBoxA1.SelectedItem.ToString() + configForm.comboBoxA2.SelectedItem.ToString()];
        for (int i = 0; i < midi.Events.Tracks; i++)
        {
            if (trackInfo[i] == configForm.comboBoxTrack.SelectedItem.ToString())
            {
                aconfigTrack = i;
            }
        }
        configStartTime = Convert.ToDouble(configForm.startT) * 1000;
        configEndTime   = Convert.ToDouble(configForm.endT) * 1000;

        // start processing MIDI
        VideoTrack vTrack = vegas.Project.AddVideoTrack();

        AudioTrack[] aTracks         = new AudioTrack[20];
        double       vTrackPosition  = 0;
        int          vTrackDirection = 1;

        double[] aTrackPositions = new double[20];
        aTracks[0]         = vegas.Project.AddAudioTrack();
        aTrackPositions[0] = 0;
        int aTrackCount = 1;

        foreach (MidiEvent midiEvent in midi.Events[aconfigTrack])
        {
            if (midiEvent is NoteOnEvent)
            {
                NoteEvent   noteEvent   = midiEvent as NoteEvent;
                NoteOnEvent noteOnEvent = midiEvent as NoteOnEvent;
                double      startTime   = midiEvent.AbsoluteTime * msPerQuarter / ticksPerQuarter;
                double      duration    = noteOnEvent.NoteLength * msPerQuarter / ticksPerQuarter;
                int         pitch       = noteEvent.NoteNumber;
                int         trackIndex  = 0;

                if (startTime < configStartTime)
                {
                    continue;
                }
                if (startTime > configEndTime)
                {
                    break;
                }

                // generate audio events
                if (aconfig == true)
                {
                    while (startTime < aTrackPositions[trackIndex])
                    {
                        trackIndex++;
                        if (trackIndex == aTrackCount)
                        {
                            aTrackCount++;
                            aTracks[trackIndex] = vegas.Project.AddAudioTrack();
                        }
                    }
                    AudioEvent audioEvent = aTracks[trackIndex].AddAudioEvent(Timecode.FromMilliseconds(startTime), Timecode.FromMilliseconds(duration));
                    Take       take       = audioEvent.AddTake(media.GetAudioStreamByIndex(0));
                    aTrackPositions[trackIndex] = startTime + duration;
                    TrackEvent trackEvent = audioEvent as TrackEvent;
                    trackEvent.PlaybackRate = mediaLength / duration;
                    trackEvent.Loop         = false;

                    // apply pitch shifting

                    if (aconfigNoTune == false)
                    {
                        int pitchDelta = pitch - aconfigBasePitch;
                        if (pitchDelta > 0)
                        {
                            while (pitchDelta > 12)
                            {
                                PlugInNode plugIn0 = vegas.AudioFX.FindChildByName("Pitch Shift");
                                Effect     effect0 = new Effect(plugIn0);
                                audioEvent.Effects.Add(effect0);
                                effect0.Preset = "12";
                                pitchDelta    -= 12;
                            }
                            PlugInNode plugIn = vegas.AudioFX.FindChildByName("Pitch Shift");
                            Effect     effect = new Effect(plugIn);
                            audioEvent.Effects.Add(effect);
                            effect.Preset = pitchDelta.ToString();
                        }
                        else
                        {
                            while (pitchDelta < -12)
                            {
                                PlugInNode plugIn0 = vegas.AudioFX.FindChildByName("Pitch Shift");
                                Effect     effect0 = new Effect(plugIn0);
                                audioEvent.Effects.Add(effect0);
                                effect0.Preset = "-12";
                                pitchDelta    += 12;
                            }
                            PlugInNode plugIn = vegas.AudioFX.FindChildByName("Pitch Shift");
                            Effect     effect = new Effect(plugIn);
                            audioEvent.Effects.Add(effect);
                            effect.Preset = pitchDelta.ToString();
                        }
                    }
                }

                // generate video events
                if (vconfig == true)
                {
                    vTrackPosition = startTime + duration;
                    VideoEvent videoEvent = vTrack.AddVideoEvent(Timecode.FromMilliseconds(startTime), Timecode.FromMilliseconds(duration));
                    Take       take       = videoEvent.AddTake(media.GetVideoStreamByIndex(0));
                    TrackEvent trackEvent = videoEvent as TrackEvent;
                    trackEvent.PlaybackRate = mediaLength / duration;
                    trackEvent.Loop         = false;

                    videoEvent.FadeIn.Length  = Timecode.FromMilliseconds(duration * vconfigFadein / 100);
                    videoEvent.FadeOut.Length = Timecode.FromMilliseconds(duration * vconfigFadeout / 100);

                    VideoMotionKeyframe key0 = videoEvent.VideoMotion.Keyframes[0];
                    VideoMotionKeyframe key1 = new VideoMotionKeyframe(Timecode.FromMilliseconds(duration));
                    videoEvent.VideoMotion.Keyframes.Add(key1);
                    key0.ScaleBy(new VideoMotionVertex((vconfigStartSize / 100) * vTrackDirection, (vconfigStartSize / 100)));
                    key1.ScaleBy(new VideoMotionVertex((vconfigEndSize / 100) * vTrackDirection, (vconfigEndSize / 100)));

                    if (vconfigAutoFlip == true)
                    {
                        vTrackDirection *= -1;
                    }
                }
            }
        }
    }