Community Forums Archive

Go Back

Subject: add new function to existing script
Posted by: pb21
Date: 9/12/2015 6:01:05 AM

I have amended this script:
http://www.sonycreativesoftware.com/forums/showmessage.asp?messageid=922516

I have created a new function so that I can change the region names to a name that uses a part of the actual filename.

I dont know c# so dont know how to add my function to the existing script.

I know where regionname is used I just pass parameters to my function ie getFileName(regionName, nameofcurrentopenfile)

I had some help on another question to get the current open file name but not sure how to integrate that either.

I do believe that most of the design is done now, just want help to insert this function.
static String getFileName(String n1, String n2)
{
try
{
String v = "";

switch (n1.ToLower())
{
case "region 1":
default:
v = Regex.Replace(n2, @"C\dC\d", "C1C2");
break;
case "region 2":
v = Regex.Replace(n2, @"C\dC\d", "C3C4");
break;
case "region 3":
v = Regex.Replace(n2, @"C\dC\d", "C5C6");
break;
}
return v;
}
catch (Exception e)
{

return "";
}
}

Message last edited on 9/12/2015 10:21:36 AM by pb21.
Subject: RE: add new function to existing script
Reply by: pb21
Date: 9/12/2015 12:58:38 PM

ive got it to compile but it returns an error message still when it runs, i wonder if anyone can assist, i must be close now to solving this.


using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using SoundForge;
using System.Text.RegularExpressions;

// Adds regions every 16 seconds to an open audio file.
// Exports every odd region to a user supplied output directory in the same format as the original open audio file.

public class EntryPoint
{
public void Begin(IScriptableApp app)
{
// select the output directory for the exported regions
string outDirectory = SfHelpers.ChooseDirectory("Choose the output directory.", @"C:\");

ISfFileHost work_file = app.CurrentFile;

// File extension for naming the ouput files
string outExt = work_file.SaveFormat.Extension;

// undo wrapper for this script.
int scriptUndo = work_file.BeginUndo("ScriptUndo");

SfAudioSelection asel = new SfAudioSelection(work_file);
SfAudioMarkerList mark_list = work_file.Markers;
// clear any existing regions
mark_list.Clear();

long file_length = asel.Length;
double lengthLeft = work_file.Formatter.PositionToTime(file_length);
long regionLength = work_file.Formatter.TimeToPosition(16);
long currentPos = work_file.Formatter.TimeToPosition(0);

bool keepGoing = true;
int regionCount = 1;
while(keepGoing){
// check to see if there is enough length left to add the next region
if (lengthLeft > 8.0)
{
string regionName = getFileName(String.Format("Region_{0}", regionCount),app.CurrentFile.Filename);

// add region
mark_list.AddRegion(currentPos, regionLength, regionName);

// export to file if this is an export region
if (regionCount % 2 != 0)
{
SfAudioSelection regionSel = new SfAudioSelection(currentPos, regionLength);
string saveName = Path.Combine(outDirectory, String.Format("{0}.{1}", regionName, outExt));
exportRegionToFile(saveName, work_file, regionSel, app);
}

// update our tracking vars.
currentPos += regionLength;
lengthLeft -= 8.0;
regionCount++;

}
// else make a region out of whatever time is leftover and set keepGoing to False
else{
string regionName = getFileName(String.Format("Region_{0}", regionCount),app.CurrentFile.Filename);
regionLength = work_file.Formatter.TimeToPosition(lengthLeft);
mark_list.AddRegion(currentPos, regionLength, regionName);
// export to file if this is an export region
if (regionCount % 2 != 0)
{
SfAudioSelection regionSel = new SfAudioSelection(currentPos, regionLength);
string saveName = Path.Combine(outDirectory, String.Format("{0}.{1}", regionName, outExt));
exportRegionToFile(saveName, work_file, regionSel, app);
}

// stop our loop
keepGoing = false;
}
}

// close the undo wrapper
work_file.EndUndo(scriptUndo, false);
}






public void exportRegionToFile(string regionName, ISfFileHost workFile, SfAudioSelection regionSelection, IScriptableApp app){
ISfFileHost newFile = app.NewFile(workFile.DataFormat, true);
newFile.OverwriteAudio(0, 0, workFile, regionSelection);
newFile.SaveAs(regionName, workFile.SaveFormat.Guid, "Default Template", RenderOptions.WaitForDoneOrCancel);
newFile.Close(CloseOptions.SaveChanges);
}

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, params object[] args) { ForgeApp.OutputText(String.Format(fmt, args)); }

public String getFileName(String n1, String n2)
{
// try
// {
String v = "";

switch (n1.ToLower())
{
case "region 1":
default:
v = Regex.Replace(n2, @"C\dC\d", "C1C2");
break;
case "region 2":
v = Regex.Replace(n2, @"C\dC\d", "C3C4");
break;
case "region 3":
v = Regex.Replace(n2, @"C\dC\d", "C5C6");
break;
}
return v;
}
// catch (Exception e)
// {

// return "";
//}
//}


} //EntryPoint

Message last edited on 9/12/2015 1:00:57 PM by pb21.
Subject: RE: add new function to existing script
Reply by: pb21
Date: 9/13/2015 11:07:45 AM

am closing off this thread now as have managed to get the behavior I was looking for.

using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using SoundForge;
using System.Text.RegularExpressions;


// Adds regions every 16 seconds to an open audio file.
// Exports every odd region to a user supplied output directory in the same format as the original open audio file.

public class EntryPoint
{
public void Begin(IScriptableApp app)
{
// select the output directory for the exported regions
string outDirectory = SfHelpers.ChooseDirectory("Choose the output directory.", @"C:\");

ISfFileHost work_file = app.CurrentFile;

// File extension for naming the ouput files
string outExt = work_file.SaveFormat.Extension;

// undo wrapper for this script.
int scriptUndo = work_file.BeginUndo("ScriptUndo");

SfAudioSelection asel = new SfAudioSelection(work_file);
SfAudioMarkerList mark_list = work_file.Markers;
// clear any existing regions
mark_list.Clear();

long file_length = asel.Length;
double lengthLeft = work_file.Formatter.PositionToTime(file_length);
long regionLength = work_file.Formatter.TimeToPosition(16);
long currentPos = work_file.Formatter.TimeToPosition(0);

bool keepGoing = true;
int regionCount = 1;
while(keepGoing){
// check to see if there is enough length left to add the next region
if (lengthLeft > 16.0)
{
string regionName = getFileName(String.Format("Region_{0}", regionCount),app.CurrentFile.Filename);

// add region
mark_list.AddRegion(currentPos, regionLength, regionName);

// export to file if this is an export region
if (regionCount % 2 != 0)
{
SfAudioSelection regionSel = new SfAudioSelection(currentPos, regionLength);
string saveName = Path.Combine(outDirectory, String.Format("{0}.{1}", regionName, outExt));
exportRegionToFile(saveName, work_file, regionSel, app);
}

// update our tracking vars.
currentPos += regionLength;
lengthLeft -= 8.0;
regionCount++;

}
// else make a region out of whatever time is leftover and set keepGoing to False
else{
string regionName = getFileName(String.Format("Region_{0}", regionCount),app.CurrentFile.Filename);
regionLength = work_file.Formatter.TimeToPosition(lengthLeft);
mark_list.AddRegion(currentPos, regionLength, regionName);
// export to file if this is an export region
if (regionCount % 2 != 0)
{
SfAudioSelection regionSel = new SfAudioSelection(currentPos, regionLength);
string saveName = Path.Combine(outDirectory, String.Format("{0}.{1}", regionName, outExt));
exportRegionToFile(saveName, work_file, regionSel, app);
}

// stop our loop
keepGoing = false;
}
}

// close the undo wrapper
work_file.EndUndo(scriptUndo, false);
}






public void exportRegionToFile(string regionName, ISfFileHost workFile, SfAudioSelection regionSelection, IScriptableApp app){
ISfFileHost newFile = app.NewFile(workFile.DataFormat, true);
newFile.OverwriteAudio(0, 0, workFile, regionSelection);
newFile.SaveAs(regionName, workFile.SaveFormat.Guid, "Default Template", RenderOptions.WaitForDoneOrCancel);
newFile.Close(CloseOptions.SaveChanges);
}

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, params object[] args) { ForgeApp.OutputText(String.Format(fmt, args)); }

public String getFileName(String n1, String n2)
{
// try
// {


String v = "";





int Position = n2.IndexOf("UN");
int StrLen = n2.Length;




String Newstr= n2.Substring(Position);




switch (n1.ToLower())
{
case "region_1":
default:
v = Regex.Replace(Newstr, @"C\dC\d", "C1C2");
break;
case "region_2":
v = Regex.Replace(Newstr, @"C\dC\d", "C3C4");
break;
case "region_3":
v = Regex.Replace(Newstr, @"C\dC\d", "C5C6");
break;
}
return v;
}
// catch (Exception e)
// {

// return "";
//}
//}


} //EntryPoint


Go Back