NEED HELP !! Add or Remove Crossfade On User Value...

iEmby wrote on 11/26/2024, 11:04 AM

@jetdv

hello sir..

i created this code for adding or removing crossfades on prompt value.

and i also add a short event logic.. which means if event's length is shorter then 4 x of ther prompt value then it will skip to overlap.

but it do very strange behaviour in some cases specily when i enter prompt value of 0 which is supposed to be remove overlaps.. even no fade in fade out remains there..

and also i want it to close gap all events if there are not overlaps on short events but it keep gaps...

please take a look on this code..

you can see how it is working in this video..

 private void AddCrossfade(Vegas vegas)
 {
     // Prompt for the overlap amount (in seconds)
     double overlapAmount = PromptForOverlapAmount();     if (overlapAmount >= 0)
     {
         // Close gaps first
         CloseGaps(vegas, overlapAmount);
         // Apply overlap or remove based on the input
         ApplyOverlap(vegas, overlapAmount);
     }
 } // Prompts the user for how much to overlap events (in seconds)
 private double PromptForOverlapAmount()
 {
     double amount = -1;     // Create form for user input
     Form prompt = new Form()
     {
         Width = 210,
         Height = 130,
         Left = 50,
         FormBorderStyle = FormBorderStyle.FixedDialog,
         StartPosition = FormStartPosition.CenterScreen,
         Text = "Input Overlap Duration"
     };     Label textLabel = new Label() { Left = 10, Top = 10, Width = 200, Text = "Overlap Duration (In Seconds):" };
     NumericUpDown numericUpDown = new NumericUpDown()
     {
         Left = 10,
         Top = 30,
         Width = 170,
         Minimum = 0.1m,   // Prevent 0 as a valid input
         DecimalPlaces = 1,
         Increment = 0.1m,
         Value = 1.0m
     };
     Button confirmation = new Button() { Text = "Ok", Left = 10, Width = 170, Top = 60, DialogResult = DialogResult.OK };     // Handle confirmation button click event
     confirmation.Click += (sender, e) =>
     {
         if (numericUpDown.Value <= 0)
         {
             MessageBox.Show("Please enter a positive overlap duration.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
         else
         {
             amount = (double)numericUpDown.Value;
             prompt.Close();
         }
     };     prompt.Controls.Add(confirmation);
     prompt.Controls.Add(textLabel);
     prompt.Controls.Add(numericUpDown);
     prompt.AcceptButton = confirmation;     // Show the prompt dialog and return the overlap amount
     prompt.ShowDialog();     return amount;
 }
 // Close gaps between selected events (move them to remove any gaps)
 private void CloseGaps(Vegas vegas, double overlapAmount)
 {
     foreach (Track track in vegas.Project.Tracks)
     {
         Timecode currentPosition = Timecode.FromMilliseconds(0);
         bool isFirstEvent = true;         foreach (TrackEvent trackEvent in track.Events)
         {
             if (trackEvent.Selected)
             {
                 // Skip events that are too short for overlap
                 if (trackEvent.Length < Timecode.FromMilliseconds(overlapAmount * 4))
                 {
                     // Move these events to the left to close the gap, but do not overlap
                     if (!isFirstEvent)
                     {
                         trackEvent.Start = currentPosition;
                     }                     // Update currentPosition after moving the event
                     currentPosition = trackEvent.Start + trackEvent.Length;
                     continue;  // Skip further processing for short events
                 }                 if (isFirstEvent)
                 {
                     currentPosition = trackEvent.Start + trackEvent.Length;
                     isFirstEvent = false;
                 }
                 else
                 {
                     // If there is a gap, move the current event to the left to close it
                     Timecode gapDuration = trackEvent.Start - currentPosition;
                     if (gapDuration > Timecode.FromMilliseconds(0))
                     {
                         trackEvent.Start = currentPosition;
                     }
                     currentPosition = trackEvent.Start + trackEvent.Length;
                 }
             }
         }
     }
 } // Apply overlap between selected events
 private void ApplyOverlap(Vegas vegas, double overlapAmount)
 {
     int overlapAmountMs = (int)(overlapAmount * 1000); // Convert overlap amount to milliseconds     // Loop through each track in the project
     foreach (Track track in vegas.Project.Tracks)
     {
         TrackEvent prevEvent = null;         // Start from the second event and process subsequent selected events
         for (int i = 0; i < track.Events.Count; i++)
         {
             TrackEvent currentEvent = track.Events[i];             if (currentEvent.Selected)
             {
                 // Skip events whose length is less than 4 times the overlap amount
                 if (currentEvent.Length < Timecode.FromMilliseconds(overlapAmountMs * 4))
                 {
                     prevEvent = currentEvent;
                     continue; // Skip the event if it's too short to overlap
                 }                 // If this is not the first event, check and apply overlap
                 if (prevEvent != null)
                 {
                     ApplyOverlapToEvent(prevEvent, currentEvent, overlapAmountMs);
                 }                 // Update the previous event to the current one for the next loop
                 prevEvent = currentEvent;
             }
         }
     }
 } // Helper method to apply overlap logic for each event
 private void ApplyOverlapToEvent(TrackEvent prevEvent, TrackEvent currentEvent, int overlapAmountMs)
 {
     Timecode existingOverlap = prevEvent.End - currentEvent.Start;     // Adjust the overlap based on the prompt value
     if (existingOverlap != Timecode.FromMilliseconds(overlapAmountMs))
     {
         // If overlap is too small or too large, adjust the start time of the current event
         currentEvent.Start = prevEvent.End - Timecode.FromMilliseconds(overlapAmountMs);         // Adjust the corresponding events (audio/video)
         List<TrackEvent> correspondingEvents = FindCorrespondingEvents(vegas, currentEvent);
         foreach (TrackEvent correspondingEvent in correspondingEvents)
         {
             correspondingEvent.Start = prevEvent.End - Timecode.FromMilliseconds(overlapAmountMs);
         }
     }
 }
 // Find corresponding events (audio/video matching takes)
 private List<TrackEvent> FindCorrespondingEvents(Vegas vegas, TrackEvent selectedEvent)
 {
     List<TrackEvent> correspondingEvents = new List<TrackEvent>();     // Look for corresponding events (audio/video matching takes)
     foreach (Track track in vegas.Project.Tracks)
     {
         foreach (TrackEvent trackEvent in track.Events)
         {
             if (trackEvent != selectedEvent && trackEvent.ActiveTake != null && selectedEvent.ActiveTake != null)
             {
                 // Match corresponding events based on the same media
                 if (trackEvent.ActiveTake.Media == selectedEvent.ActiveTake.Media)
                 {
                     correspondingEvents.Add(trackEvent);
                 }
             }
         }
     }
     return correspondingEvents;
 }

 

Last changed by iEmby

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

Comments

jetdv wrote on 11/26/2024, 1:57 PM

The problem is in how you are processing the events. Look at 1:53 in your video. The bright red one is last on the timeline. Now look at 2:02. It is no longer the "last" one. That's because when you are moving the events to the right, you've moved them far enough that you've "gone past" the last event before you've ever processed that last event. Notice the "most red" one did not move between the two timecodes I mentioned.

Try creating text events with "1", "2", "3", "4", "5", "6" and run the same process. See if they stay in the same order (they won't!)

Here's what I see happening:

  • event 2 is moved right of event 1 removing the overlap
  • event 3 is moved right of event 2 removing the overlap
  • event 4 is moved right of event 3 removing the overlap.
  • event 5 is moved right of event 4 removing the overlap - now HERE's where it gets funny. Event 5 was moved right far enough that it now begins RIGHT of event 6 so it now becomes event 6 and event 6 becomes event 5! They actually SWAP places
  • event 6 is moved right to the proper location - which WAS event 5 so the original event "6" is never touched which is why it remains with an overlap to event 4 and there's a gap between it and the old "event 5" which was moved to the proper location to where the 6th event should be located (i.e. after itself).

In reality, you need a double loop:

  1. for the main loop, go through the events and determine how far the events afterwards need to be moved to remove that specific overlap.
  2. for the second loop, go through the events BACKWARDS moving them all that set amount until you get to the event you're looking at and it gets moved.

There are other alternate methods that would work but this method seems logical. You just need to make sure that your events are not getting out of order.

iEmby wrote on 11/26/2024, 5:55 PM

Very nice observations sir ... I never noticed it..

Thanks..will check again.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

jetdv wrote on 11/27/2024, 7:59 AM

This tutorial from 3+ years ago might help:

iEmby wrote on 11/28/2024, 10:08 AM

@jetdv
sir i solved it but here some strange problem..
when i am applying it on different events of different media it is working perfectly
but when i use it on same media trimmed in different events..
it is messing up.

please help i am so close now..

please look what is happening



this is code now..

        private void AddCrossfade(Vegas vegas)
        {
            // Prompt for the overlap amount (in seconds)
            double overlapAmount = PromptForOverlapAmount();            if (overlapAmount >= 0)
            {
                // Close gaps first
                CloseGaps(vegas, overlapAmount);
                // Apply overlap or remove based on the input
                ApplyOverlap(vegas, overlapAmount);
            }
        }        private double PromptForOverlapAmount()
        {
            double amount = -1;            Form prompt = new Form()
            {
                Width = 210,
                Height = 130,
                Left = 50,
                FormBorderStyle = FormBorderStyle.FixedDialog,
                StartPosition = FormStartPosition.CenterScreen,
                Text = "Input Overlap Duration"
            };            Label textLabel = new Label() { Left = 10, Top = 10, Width = 200, Text = "Overlap Duration (In Seconds):" };
            NumericUpDown numericUpDown = new NumericUpDown()
            {
                Left = 10,
                Top = 30,
                Width = 170,
                Minimum = 0.0m,
                DecimalPlaces = 1,
                Increment = 0.1m,
                Value = 1.0m
            };
            Button confirmation = new Button() { Text = "Ok", Left = 10, Width = 170, Top = 60, DialogResult = DialogResult.OK };            confirmation.Click += (sender, e) =>
            {
                amount = (double)numericUpDown.Value;
                prompt.Close();
            };            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(numericUpDown);
            prompt.AcceptButton = confirmation;            prompt.ShowDialog();            return amount;
        }        private void CloseGaps(Vegas vegas, double overlapAmount)
        {
            foreach (Track track in vegas.Project.Tracks)
            {
                Timecode currentPosition = Timecode.FromMilliseconds(0);                foreach (TrackEvent trackEvent in track.Events)
                {
                    if (trackEvent.Selected)
                    {
                        // Check if the event is too short to be overlapped
                        if (trackEvent.Length < Timecode.FromMilliseconds(overlapAmount * 4 * 1000))
                        {
                            // Move only to close gaps, do not overlap
                            trackEvent.Start = currentPosition;
                            currentPosition = trackEvent.Start + trackEvent.Length;
                            continue;
                        }                        trackEvent.Start = currentPosition;                        currentPosition = trackEvent.Start + trackEvent.Length;                        if (overlapAmount > 0)
                        {
                            // Adjust the currentPosition for overlap
                            currentPosition -= Timecode.FromMilliseconds(overlapAmount * 1000);
                        }
                    }
                }
            }
        }        private void ApplyOverlap(Vegas vegas, double overlapAmount)
        {
            int overlapAmountMs = (int)(overlapAmount * 1000);            foreach (Track track in vegas.Project.Tracks)
            {
                List<TrackEvent> selectedEvents = new List<TrackEvent>();                foreach (TrackEvent trackEvent in track.Events)
                {
                    if (trackEvent.Selected)
                    {
                        selectedEvents.Add(trackEvent);
                    }
                }                for (int i = 1; i < selectedEvents.Count; i++)
                {
                    TrackEvent prevEvent = selectedEvents[i - 1];
                    TrackEvent currentEvent = selectedEvents[i];                    // Skip short events that are less than 4x overlapAmount
                    if (currentEvent.Length < Timecode.FromMilliseconds(overlapAmountMs * 4))
                    {
                        continue;
                    }                    // Calculate the new start time for overlap
                    Timecode targetStart = prevEvent.End - Timecode.FromMilliseconds(overlapAmountMs);                    // Apply the new start time
                    currentEvent.Start = targetStart;                    // Adjust corresponding events
                    List<TrackEvent> correspondingEvents = FindCorrespondingEvents(vegas, currentEvent);
                    foreach (TrackEvent correspondingEvent in correspondingEvents)
                    {
                        correspondingEvent.Start = targetStart;
                    }
                }
            }
        }
        private List<TrackEvent> FindCorrespondingEvents(Vegas vegas, TrackEvent selectedEvent)
        {
            List<TrackEvent> correspondingEvents = new List<TrackEvent>();            foreach (Track track in vegas.Project.Tracks)
            {
                foreach (TrackEvent trackEvent in track.Events)
                {
                    if (trackEvent != selectedEvent && trackEvent.ActiveTake != null && selectedEvent.ActiveTake != null)
                    {
                        if (trackEvent.ActiveTake.Media == selectedEvent.ActiveTake.Media)
                        {
                            correspondingEvents.Add(trackEvent);
                        }
                    }
                }
            }
            return correspondingEvents;
        }

 

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

jetdv wrote on 11/28/2024, 12:12 PM

I think your issue is probably here:

            foreach (Track track in vegas.Project.Tracks)
            {
                foreach (TrackEvent trackEvent in track.Events)
                {
                    if (trackEvent != selectedEvent && trackEvent.ActiveTake != null && selectedEvent.ActiveTake != null)
                    {
                        if (trackEvent.ActiveTake.Media == selectedEvent.ActiveTake.Media)
                        {
                            correspondingEvents.Add(trackEvent);
                        }
                    }
                }
            }

Specifically:

if (trackEvent.ActiveTake.Media == selectedEvent.ActiveTake.Media)

You need to be more specific in matching the media. For example:

track.Index != selectedEvent.Track.Index

and maybe even:

trackEvent.Start = selectedEvent.Start

Because they're all part of the same media, it should be selecting everything that isn't the original "selectedEvent".