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;
}
