Community Forums Archive

Go Back

Subject:Example Script: Save Channels as Files
Posted by: _TJ
Date:4/19/2007 8:34:46 PM

In the main Sound Forge 9.0 forum, it was suggested that it would be easy to write a script to write the channels of a file as separate files. Here is one possible way to do that.

Note that this example uses ISfFileHost.NewFile(), so that it can use Save() to prompt the user to choose a filename and format. Then it adds 2,3,4,5, etc to the end of the filename for subsequent channels.

One flaw in this script, is that it asks for the output template twice, since there doesn't seem to be any way to get the template back from the initial Save() call. (although you can get the file type).


using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

using SoundForge;
using SoundForge.BatchConverter;

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

ISfFileHost file = app.CurrentFile;
if (null == file)
return "no current file";

string strFilename = file.Filename;
string strDir = Path.GetDirectoryName(strFilename);
string strExt = Path.GetExtension(strFilename);
string strBase = Path.GetFileNameWithoutExtension(strFilename);

ISfRenderer rend = null;
ISfGenericPreset tpl = null;
SfStatus stat = SfStatus.Success;

for (uint ixChan = 0; ixChan < file.Channels; ++ixChan)
{
SfAudioSelection asel = new SfAudioSelection(file);
asel.ChanMask = (uint)(1 << (int)ixChan);

ISfFileHost fhChan = file.NewFile(asel);

string strOutBase = String.Format("{0}{1}", strBase, ixChan+1);
string strOutFilename = Path.Combine(strDir, strOutBase + strExt);

if (null == rend)
{
try { fhChan.Save(SaveOptions.PromptIfNoFilename); } catch { return "Cancelled"; }
stat = fhChan.WaitForDoneOrCancel();
rend = fhChan.SaveFormat;

DPF("Saving channel {0} as {1}", ixChan+1, strOutFilename);

strOutFilename = file.Filename;
strDir = Path.GetDirectoryName(strOutFilename);
strExt = Path.GetExtension(strOutFilename);
strBase = Path.GetFileNameWithoutExtension(strOutFilename);
if (strBase.EndsWith("1"))
strBase = strBase.Substring(0, strBase.Length-1);

fhChan.Close(CloseOptions.DiscardChanges);
continue;
}
else if (null == tpl)
{
tpl = rend.ChooseTemplate(app.Win32Window.Handle, null);
if (null == tpl)
return "no output format";
}
DPF("Saving channel {0} as {1}", ixChan+1, strOutFilename);

fhChan.SaveAs(strOutFilename, null, tpl, RenderOptions.AndClose | RenderOptions.OverwriteExisting);
stat = fhChan.WaitForDoneOrCancel();
if (stat != SfStatus.Success)
break;

if ( ! fhChan.IsZombie)
fhChan.Close(CloseOptions.DiscardChanges);
}


return null; // null for success
}

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, params object [] args) { ForgeApp.OutputText(String.Format(fmt,args)); }
public static string GETARG(string key, string str) { string val = Script.Args.ValueOf(key); if (val == null) val = str; return val; }
public static int GETARG(string key, int ii) { return Script.Args.AsInt(key,ii); }
public static Int64 GETARG(string key, Int64 cc) { return Script.Args.AsInt64(key,cc); }
public static bool GETARG(string key, bool ff) { return Script.Args.AsBool(key,ff); }
public static double GETARG(string key, double dd) { return Script.Args.AsDouble(key,dd); }
} //EntryPoint

Subject:RE: Example Script: Save Channels as Files
Reply by: MarkWWW
Date:4/20/2007 11:19:48 AM

Thanks very much for that.

Mark

Subject:RE: Example Script: Save Channels as Files
Reply by: noisyscott
Date:8/14/2012 12:14:39 PM

I have a good need for this script, but was hoping to modify it so that the resultant new files simply name and save themselves to the parent file's directory without bringing up the save dialog. Does anyone have any suggestions on how to morph the IsFileHost.NewFile() section to use RenderAs or some other method that will auto-name the new files?

Thanks,
Scott

Subject:RE: Example Script: Save Channels as Files
Reply by: roblesinge
Date:8/15/2012 9:39:46 AM

So, you want the save to overwrite the files or do you want them to have some kind of character appended to the filename so an overwrite isn't done?

Rob.

Subject:RE: Example Script: Save Channels as Files
Reply by: noisyscott
Date:8/15/2012 11:39:13 AM

Hello Rob,

I want to create new files with an appended character to the name and leave the original intact.

Scott

Go Back