Revese TrackEvent

nonam3 wrote on 10/30/2021, 4:56 PM

Hello,

I want to reverse `TrackEvent`, in c# script, so it plays backwards, unfortunately I can't figure it out. Did seen API documentation and only method for reverse is Subclip but when doing same in Vegas I see no additional subclip so I suppose it's not the way it's done by reverse tool.

Currently I'm stuck on step when I have `TrackEvent` but can't figure out way to access media and reverse playback it.

Any advice more then welcome.
Best regards.

Comments

jetdv wrote on 10/30/2021, 8:05 PM
Did seen API documentation and only method for reverse is Subclip but when doing same in Vegas I see no additional subclip so I suppose it's not the way it's done by reverse tool.

If you right-click an event on the timeline and choose "Reverse", it DOES create a subclip! You can see the subclip in the Project Media tab.

jetdv wrote on 10/30/2021, 8:57 PM

Add a velocity envelope to the event and set it to -100%. That will play it from the beginning of the event backwards so make sure the "beginning" of the clip is set to the actual "end" position where you wish it to start once it goes backwards.

Set "PointLoc" to a timecode of 0 and "PointSpeed" to -1.

This will need to be done on a "VideoEvent" - not a "TrackEvent". But you can change a track event to a video event.

VideoEvent vevnt = (VideoEvent)TrackEvent;

 

VelEnv = new Envelope(EnvelopeType.Velocity);
vevnt.Envelopes.Add(VelEnv);

EnvelopePoint a = VelEnv.Points.GetPointAtX(PointLoc);  
if (a == null) {
    a = new EnvelopePoint(PointLoc, PointSpeed);
    VelEnv.Points.Add(a);
} else {
    a.Y = PointSpeed;
}

 

It appears you can also create a sub-clip and set it to be reversed (from the API file):

class Subclip : Media

Constructors:Summary:

Subclip

(

Project project,
String path,
Timecode start,
Timecode length,
Boolean reverse,
String displayName

)

Create a new media subclip.Parameters:

project: Project.ActiveProject or a specific project
path: full path of source media file
start: start offset in source media
length: length of subclip
reverse: whether the subclip is reversed
displayName: display name of subclip

 

nonam3 wrote on 10/31/2021, 4:32 PM

Thank You @jetdv for Your insights.

Indeed I overlooked sub clip creation because there was already such subclip in my test project.

In the spirit of sharing and for the posterity below is sample code (not tested much) with comments that does this, comments are more then welcome:

private TrackEvent duplicateEvent(TrackEvent tEvent, Timecode evStart, Timecode length, bool reverse, bool duplicateGroups = true)
{
    if (tEvent == null)
    {
        return null;
    }    

// tEvent - current event, first one
// duplicatedEvent - newly created event, second one    

TrackEvent duplicatedEvent = tEvent.Copy(tEvent.Track, tEvent.End);    
if (duplicatedEvent == null)
    {
        return null;
    }    

duplicatedEvent.Selected = true;    
// set position for start to end of original event
duplicatedEvent.Start = evStart;
duplicatedEvent.Length = length;    
tEvent.Selected = false;    

// reverse media
    if (reverse)
    {
// using SubClip to reverse source media in it
// subclips add suffix, last param, like: "- subclip 1(reversed)", below code doesn't        

// create reverse subclip
        Subclip subclip = new Subclip(tEvent.Project,
                                      tEvent.ActiveTake.Media.FilePath,
                                      new Timecode(),
                                      tEvent.ActiveTake.MediaStream.Length,
                                      true,
                                      tEvent.ActiveTake.Media.FilePath);        

// add new take with reverse subclip to list
        // TODO: proper stream detection, here just first stream is take
        Take revTake = new Take(subclip.Streams.First(), true);
        duplicatedEvent.Takes.Add(revTake);        
// make duplicated trackevent use that revese take
        duplicatedEvent.ActiveTake = revTake;        
// update offset, its reversed and substract event length
        duplicatedEvent.ActiveTake.Offset = (tEvent.ActiveTake.Media.Length - tEvent.ActiveTake.Offset) - tEvent.Length;
    }
    return duplicatedEvent;
}

and calling it like so, this was part of the loop over selected events in selected track:

TrackEvent tEvent = track.Events.ElementAt(j);
if (!tEvent.Selected) { continue; }
// duplicate first time, reverse
TrackEvent dupEvent = duplicateEvent(tEvent, tEvent.End, tEvent.Length, true);
// duplicate second time, no reverse
duplicateEvent(tEvent, dupEvent.End, tEvent.Length, false);

Yet again @jetdv thank You !

jetdv wrote on 11/1/2021, 9:34 AM

Glad I could help point you in the right direction.