Community Forums Archive

Go Back

Subject:Sound Forge script - exporting regions to file
Posted by: alejandro
Date:4/9/2015 4:21:23 AM

Hi everyone,

I am finding it difficult to write a script that I need - I thought this would be the best place to ask.

I have a load of vocal snaps to export for work, and require a script that does the following..

- Marks regions every 4 seconds
- Exports every 2nd region to a wav file (eg. export to file: region 1, region 3, region 5, region 7, region 9, region 11, etc)
- name of wave file can be anything but would be good to end with what the region is (eg. track 01, etc)

If someone can help me that would be so great. If it requires a small $$ thats fine too. I've tried to muck around with the code from various sources but I have no experience in scripting!

Thanks :)



Subject:RE: Sound Forge script - exporting regions to file
Reply by: roblesinge
Date:4/9/2015 9:04:23 AM

I think this is relatively easy to do. Let me look at it and I'll let you know.

-Rob.

Subject:RE: Sound Forge script - exporting regions to file
Reply by: alejandro
Date:4/9/2015 8:47:21 PM

Awesome. Thanks for looking into this for me Rob.

Subject:RE: Sound Forge script - exporting regions to file
Reply by: roblesinge
Date:4/26/2015 1:03:05 PM

Okay, my C# is getting rusty, but the following seems to do what you are asking. It works on an open audio file, adds a region ever 4 seconds until there is less than 4 seconds left (it makes a smaller region out of this last section). Every odd region is exported to a separate file using the format of the larger audio file. Right now the files/regions are named "Region_<number>". You can change this to whatever you want.


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

// Adds regions every 4 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(4);
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 > regionLength)
{
string regionName = String.Format("Region_{0}", regionCount);

// 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 -= regionLength;
regionCount++;

}
// else make a region out of whatever time is leftover and set keepGoing to False
else{
string regionName = String.Format("Region_{0}", regionCount);
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)); }
} //EntryPoint



-Rob.

Message last edited on4/26/2015 1:46:18 PM byroblesinge.
Subject:RE: Sound Forge script - exporting regions to file
Reply by: roblesinge
Date:4/28/2015 8:25:14 PM

Here is the unedited version of the script. Try this.

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

// Adds regions every 4 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(4);
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 > 4.0)
{
string regionName = String.Format("Region_{0}", regionCount);

// 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 -= 4.0;
regionCount++;

}
// else make a region out of whatever time is leftover and set keepGoing to False
else{
string regionName = String.Format("Region_{0}", regionCount);
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)); }
} //EntryPoint

Subject:RE: Sound Forge script - exporting regions to file
Reply by: alejandro
Date:4/28/2015 8:38:01 PM

Works like a charm. You are an absolute legend rob. Thank you for halving my workload by days.

Subject:RE: Sound Forge script - exporting regions to file
Reply by: pb21
Date:9/13/2015 3:18:25 AM

Hi roblesinge

I have amended this routine to split out at 16 second intervals and added a new function, which compiles but gets a message error.

I wonder if you can see what I did wrong, Ive had it posted for a whole day now but havnt had any reply yet number 5 in the list.

my function renames the regions based on the file name thats open, but i just dont know enough about this c# and soundforge integration.



wonder if you could help

regards in advance

Subject:RE: Sound Forge script - exporting regions to file
Reply by: pb21
Date:9/13/2015 11:06:05 AM

After much brain ache, I finally got it to get the naming convention and behaviour I wanted.

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