Community Forums Archive

Go Back

Subject: select 40 seconds and crop remaining end
Posted by: pb21
Date: 10/2/2015 4:45:25 AM

Hi

I wonder if you could let me know how to select from 0 to 40 seconds and crop 40+ to end in vb script.

I have this upto now this is a mixture of code from the site with my enhancements.

I would like to do the selection and crop just before it calls the save routine.


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 =@"C:\";

SfStatus result;



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)
{
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 -= 16.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;
}
}
result = app.DoMenuAndWait("Tools.ExtractRegions", 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("Jdxi");
int StrLen = n2.Length;





String strTemp= n2.Substring(Position);

String Newstr=Regex.Replace(strTemp, @".wav", "");



switch (n1.ToLower())
{
case "region_1":
default:

v = Regex.Replace(Newstr, @"C\dC\d", "C1C2");
//v = Regex.Replace(Newstr, @".wav", "");
break;
case "region_2":

v = Regex.Replace(Newstr, @"C\dC\d", "C3C4");
//v = Regex.Replace(Newstr, @".wav", "");
break;
case "region_3":

v = Regex.Replace(Newstr, @"C\dC\d", "C5C6");
//v = Regex.Replace(Newstr, @".wav", "");
break;
}
return v;
}
// catch (Exception e)
// {

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


} //EntryPoint


Message last edited on 10/2/2015 4:46:50 AM by pb21.

Go Back