Render Selected Audio as New Take

joelsonforte.br wrote on 6/14/2024, 10:37 AM

I'm trying to create a script that renders the audio from the selected event on the timeline in .wav format and imports it back into the timeline with a take along with the original audio from the selected event, but I don't know the logic for rendering the audio or how to import the audio back with a take. If anyone can help I would be grateful.

using System;
using System.IO;
using ScriptPortal.Vegas;
using System.Windows.Forms;

namespace RenderSelectedAudioToNewTake
{
    public class EntryPoint
    {
        public void FromVegas(Vegas vegas)
        {
            try
            {
                // Check if there are selected audio events on the timeline
                if (!HasSelectedAudioEvents(vegas.Project))
                {
                    System.Windows.Forms.MessageBox.Show("Please, Select the Audio Event in Timeline.", "Message");
                    return;
                }

                // Get the selected audio event on the timeline
                AudioEvent selectedEvent = FindFirstSelectedAudioEvent(vegas.Project);
                if (selectedEvent == null)
                {
                    System.Windows.Forms.MessageBox.Show("No Audio Events Selected in the Timeline.", "Message");
                    return;
                }

                // Get the file path of the audio file associated with the selected event
                string audioPath = selectedEvent.ActiveTake.MediaPath;

                // Render a copy of the audio from the selected event in .wav format in the same directory as the original file
                "Inserte here the logic to render a copy of the selected event in .wav format using native Vegas resources."
                
                //Importa a cópia do áudio renderizado de volta para a timeline como um Take
                "Insert here the logic to import back as a Take the rendered copy of the selected event in .wav format using native Vegas resources."
                
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("An Error Has Occurred: " + ex.Message, "Error");
            }
        }

        // Method to check if there are selected audio events on the timeline
        private bool HasSelectedAudioEvents(Project project)
        {
            foreach (Track track in project.Tracks)
            {
                if (track.IsAudio())
                {
                    foreach (TrackEvent trackEvent in track.Events)
                    {
                        if (trackEvent.Selected && trackEvent.IsAudio())
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        // Method to find the first selected audio event on the timeline
        private AudioEvent FindFirstSelectedAudioEvent(Project project)
        {
            foreach (Track track in project.Tracks)
            {
                if (track.IsAudio())
                {
                    foreach (TrackEvent trackEvent in track.Events)
                    {
                        if (trackEvent.Selected && trackEvent.IsAudio())
                        {
                            return trackEvent as AudioEvent;
                        }
                    }
                }
            }
            return null;
        }
    }
}

 

Comments

jetdv wrote on 6/14/2024, 11:42 AM

First of all, I'm not sure why you're doing this:

                 // Check if there are selected audio events on the timeline
                 if (!HasSelectedAudioEvents(vegas.Project))
                 {
                     System.Windows.Forms.MessageBox.Show("Please, Select the Audio Event in Timeline.", "Message");
                     return;
                 }

Isn't the next section doing the same thing?:

                // Get the selected audio event on the timeline
                 AudioEvent selectedEvent = FindFirstSelectedAudioEvent(vegas.Project);
                 if (selectedEvent == null)

If you have more audio tracks, you will first need to solo that one track (and make sure none of the others are) and then render just the section of the timeline containing that audio to WAV format. See the full rendering series that begins here:

Then simply open that media as a new media, create a new "Take", and then add that new take back to the original event. Then it will have two audio takes, the original plus the new WAV.

Don't forget to "unsolo" that track.

 

joelsonforte.br wrote on 6/14/2024, 12:12 PM

Thanks for your help. I managed to solve it by watching your tutorial.