Vegas and soundforge script compatibility

Daemach wrote on 6/5/2016, 12:20 PM
We've been using soundforge 9 for several years to process audio for podcasts. Will the scripts I use for soundforge work in Vegas? I would like to start recording video as well, but still use the scripting to generate the audio-only podcasts. Here's the script:

using System;
using System.IO;
using System.Windows.Forms;
using SoundForge;
using System.Text;

public class EntryPoint {
public void Begin(IScriptableApp app) {

DPF("Trimming...");

app.DoMenuAndWait("Edit.TrimCrop", false);

string[] effects = new string[] {
"Normalize|Normalize RMS to -10 dB (speech)",
"Insert Silence|1 second at start of file",
// "Reverse",
// "Graphic Fade|-20 dB exponential fade out"
};


// ok, now do the deed.
//
ISfFileHost file = app.CurrentFile;
if (null == file)
return;

try
{
foreach (string effect in effects)
{
string[] str = effect.Split(new char[]{'|',';'},2);
string preset = null;
if (str.Length > 1)
preset = str[1];

DPF("DoEffect({0},{1})", str[0], preset);
file.DoEffect(str[0], preset,
new SfAudioSelection(0, file.Length),
EffectOptions.EffectOnly);

SfStatus result = file.WaitWithProgress(new SfProgressCallback(ProgressCallback));
}

} catch(Exception)
{
DPF("effect did not complete, quitting script");
return;
}

app.DoMenuAndWait("Edit.SelectAll", false);
DPF("Beginning rendering stage...");

// ".wav|44,100 Hz, 16 Bit, Stereo, PCM",
// ".mp3|128 Kbps, CD Quality Audio",

string szSermon = renderFile(app, ".wav", "44,100 Hz, 16 Bit, Stereo, PCM", "E:\\Podcast\\CD", getService());
string szPodcast = renderFile(app, ".mp3", "32 Kbps, Voice Audio", "E:\\Podcast\\Output", getService());

string szIMacros = String.Format("{0},{1}", getDateAsString(), szPodcast);

AppendToFile("E:\\podcast\\iMacros\\podcastData.csv", szIMacros);
}

public string renderFile(IScriptableApp app, string szType, string szPreset, string szPath, string szFileFragment)
{
ISfRenderer rend = null;
if (szType.StartsWith("."))
rend = app.FindRenderer(null, szType);
else
rend = app.FindRenderer(szType, null);
if (null == rend)
{
DPF("renderer for type {0} not found - quitting", szType);
return szType;
}

ISfFileHost file = app.CurrentFile;
if (null == file)
{
DPF("No files are open. Script canceled.");
return szType;
}

string szFile = String.Format("{0}_{1}.{2}", getDateAsString2(), szFileFragment, rend.Extension);
DPF("rendering {0}", szFile);
string szFileName = Path.Combine(szPath, szFile);
DPF(" ~ {0}", szFileName);
DPF(rend.Guid + " " + szPreset + " '" + szPath + "'");

file.RenderAs(szFileName, rend.Guid, szPreset, null, RenderOptions.RenderOnly);
SfStatus result2 = app.WaitWithProgress(new SfProgressCallback(ProgressCallback));

return szFile;
}

public string getService()
{
DateTime dn = DateTime.Now;
DateTime d2 = Convert.ToDateTime("12:00 PM");
DateTime d3 = Convert.ToDateTime("5:00 PM");
string service = "";

if (dn.TimeOfDay.Ticks > d3.TimeOfDay.Ticks)
service = "5pm_sermon_only";
else if (dn.TimeOfDay.Ticks > d2.TimeOfDay.Ticks)
service = "11am_sermon_only";
else
service = "9am_sermon_only";

return service;
}

public string getDateAsString()
{
DateTime dt = DateTime.Now;
//DateTime dt = Convert.ToDateTime("SOMESTRING");
string dateAsString = dt.Year.ToString() + "-";
if (dt.Month.ToString().Length == 1)
dateAsString += "0" + dt.Month.ToString() + "-";
else
dateAsString += dt.Month.ToString() + "-";
if (dt.Day.ToString().Length == 1)
dateAsString += "0" + dt.Day.ToString();
else
dateAsString += dt.Day.ToString();

return dateAsString;
}

public string getDateAsString2()
{
DateTime dt = DateTime.Now;
//DateTime dt = Convert.ToDateTime("SOMESTRING");
string dateAsString = "";

if (dt.Month.ToString().Length == 1)
dateAsString += "0" + dt.Month.ToString() + "-";
else
dateAsString += dt.Month.ToString() + "-";

if (dt.Day.ToString().Length == 1)
dateAsString += "0" + dt.Day.ToString() + "-";
else
dateAsString += dt.Day.ToString() + "-";

dateAsString += Right(dt.Year.ToString(),2);

return dateAsString;
}

public void AppendToFile(string filePath, string textToAppend)
{
checkFileExists(filePath);

/*
StreamWriter sw;
sw = File.AppendText(filePath);
sw.WriteLine(textToAppend);
sw.Close();
*/

using (StreamWriter sw = new StreamWriter(File.Open(filePath, FileMode.Create), Encoding.UTF8))
{
sw.WriteLine(textToAppend);
}

}

public void checkFileExists(string filePath)
{
FileInfo f = new FileInfo(filePath);
// if (!f.Exists)
// f.Create();
f.Delete();

}


public string CleanFilename(string szName)
{
return szName;
}

static string Right( string s, int count )
{
string newString = String.Empty;
if (s != null && count > 0)
{
int startIndex = s.Length - count;
if (startIndex > 0)
newString = s.Substring( startIndex, count );
else
newString = s;
}
return newString;
}

static string strTask = "";
static int ProgressCallback(double dPercentDone, string strTaskIn) {

if (strTaskIn.Length > 0)
{
if (strTaskIn == strTask)
strTaskIn = "";
else
strTask = strTaskIn;
}

if (strTaskIn.Length > 0)
{
DPF("");
DPF("# {0} - {1:##.#}%", strTask, dPercentDone);
}
else
DPF("~ {0} ", dPercentDone);
return 0;
}


public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(String.Format("Script '{0}' is done.", Script.Name));
}
public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, object o) { ForgeApp.OutputText(String.Format(fmt,o)); }
public static void DPF(string fmt, object o, object o2) { ForgeApp.OutputText(String.Format(fmt,o,o2)); }
public static void DPF(string fmt, object o, object o2, object o3) { ForgeApp.OutputText(String.Format(fmt,o,o2,o3)); }
public static string GETARG(string k, string d) { string val = Script.Args.ValueOf(k); if (val == null || val.Length == 0) val = d; return val; }
public static int GETARG(string k, int d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsInt(k); }
public static bool GETARG(string k, bool d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsBool(k); }
} //EntryPoint