Community Forums Archive

Go Back

Subject:Script: Create new files from each marker
Posted by: Sound Samurai
Date:6/9/2005 2:36:27 AM

Here is a script that create new files from each marker or region in a file, from left to right.
The new files are saved at the same location as the original file, with added 001, 002, 003 ... to the filename.

I wrote this to use with small files/loops, so i havent tried it with huge files or insane amounts of markers. Consider it as a manual "Recycle" process - you find the transients, the script extracts the slices for you.

The first time the script runs after opening soundforge it takes a second to run, but after that it is instant (depending on the file size ofcourse).



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

//
// A script that silently executes and does the following
//
// - for each marker or region in the current file, from left to right
// - create a new file from this section
// - name this file the same as the original file with a numerical extension
// - save this file at the same location as the original
//
// Typical usage is to put markers at transients where you want your sample/loop split,
// run script, and put the new cutups in your sampler
//
// Thanks to SonyTJ for super guru guidance
//

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

ISfFileHost file = app.CurrentFile;
if (null == file)
return "Open a file containing regions before running this script. Script stopped.";
if (null == file.Markers || file.Markers.Count <= 0)
return "The file does not have any markers.";

ISfRenderer render = file.SaveFormat;
string szDir = Path.GetDirectoryName(file.Filename);
string szOrigName = Path.GetFileNameWithoutExtension(file.Filename);
if (szOrigName.Length > 20)
szOrigName = szOrigName.Substring(0, 20);
SfAudioMarkerList markers = file.Markers;

Int64 ccTotalStart = 0; // file.Window.EditRegion.Start;
Int64 ccTotalEnd = file.Length; // file.Window.EditRegion.Start + file.Window.EditRegion.Length;

System.Collections.SortedList list = new SortedList(file.MarkerCount);

list.Add(ccTotalStart, "Total Start");
list.Add(ccTotalEnd, "Total End");

foreach (SfAudioMarker mk in file.Markers)
{
// check for duplicates
if (list.ContainsKey(mk.Start))
continue;
if (mk.IsRegion)
{
list.Add(mk.Start, mk.Name);
// if including region ends (regions can still have 0 length...)
if (mk.Length > 0)
list.Add(mk.Start + mk.Length, mk.Name + " (end)");
}
else
{
list.Add(mk.Start, mk.Name);
}
}

Int64 ccPos = ccTotalStart;
Int64 ccCount = 0;

foreach (Int64 ccNext in list.Keys)
{
Int64 ccLength = ccNext - ccPos;
if (ccLength > 2)
{
// DPF("ccPos = {0} ccLength = {1} name = '{2}'", ccPos, ccLength, list[ccPos]);

// do render ccPos, ccLength here

// fix the filename
string strMix = "";
string strNr = ccCount.ToString();
if (strNr.Length == 1)
strMix = String.Format("{0}{1}{2}", szOrigName, "00", strNr);
else if (strNr.Length == 2)
strMix = String.Format("{0}{1}{2}", szOrigName, "0", strNr);
else
strMix = String.Format("{0}{1}", szOrigName, strNr);

SfAudioSelection range = new SfAudioSelection(ccPos, ccLength);
string szName = String.Format("{0}.{1}", strMix, render.Extension);
szName = CleanFilename(szName);
// DPF("Queueing: '{0}'", szName);
string szFullName = Path.Combine(szDir, szName);
if (File.Exists(szFullName))
File.Delete(szFullName);
file.RenderAs(szFullName, render.Name, "Default Template", range, RenderOptions.RenderOnly);
// DPF("Path: '{0}'", szFullName);


ccPos = ccNext;
ccCount++;
}
if (ccNext > ccTotalEnd)
break;
}

DPF("Done.");
return null;
}

public string CleanFilename(string szName)
{
szName = szName.Replace(':', '_');
szName = szName.Replace('\\', '_');
szName = szName.Replace('/', '_');
foreach (char ch in Path.InvalidPathChars)
{
szName = szName.Replace(ch, '_');
}
return szName;
}

public string PromptForPath(string szDir) {

FolderBrowserDialog dlg = new FolderBrowserDialog();

dlg.Description = "Select the target folder for saved files:";
if (null != szDir && "" != szDir)
dlg.SelectedPath = szDir;
dlg.ShowNewFolderButton = true;
DialogResult res = dlg.ShowDialog();
if (res == DialogResult.OK)
return dlg.SelectedPath;
return null;
}


public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
string msg = Begin(app);
app.SetStatusText((msg != null) ? msg : 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

Go Back