Community Forums Archive

Go Back

Subject:Regions to single file
Posted by: Phil-H
Date:10/11/2007 2:35:32 PM

I’m trying to write a script which will speed up what is at the moment a manual process. At present this involves marking out sections of relevant speech within a file as regions (which will not be scripted) and then copying all of the regions in to a single new file which is then saved (this is the part to be scripted). The process could also be considered as removing the material in between the regions.

I’ve been looking at various other postings and scripts that are part of the SDK and I’ve managed to put together a kind of Frankenstein script that sort of does the trick. My code is below. However, I’ve no idea if I’m going about this in the best (most efficient/elegant) way and there are a few other things which I’d like the script to do.

1) I’d like the new destination file to be of the same type (channels, sample rate etc) as the source file. I’d rather not have to select the correct option in the ‘New File’ window. I’m sure there must be some way to get the file information from the source file and use this to create the new destination file. This isn’t a problem in the manual process as I use Ctrl+E to paste the first region into a new file and then paste the other regions in to this same file.

2) I’d like the region markers from the source file to be retained in the destination file. I’ve got no idea how to do this in the script. In the manual process the region markers are retained during the copying and pasting process.

3) I’d like the regions in the destination file to appear in the same order as they appear in the source file. I’ve read in another post that in scripts Sound Forge processes regions in the order in which they were created rather than the order in which they appear in a file. I seem to have got round this by including ‘app.DoMenu("Transport.GoToEnd", false);’ although I’m not entirely sure why this works!

4) One final thing I’d like is for the destination file to be automatically saved in the same location as the source file but with the word ‘edit’ appended to the file name. From looking at other posts and scripts this seems to be reasonably straight forward but I haven’t implemented it yet since the whole structure of the script could change depending on any feedback I get from here.

Thanks in advance. A lot of the other posts on here have proved very helpful in getting my script to where it is now.

Phil



using System;
using System.Windows.Forms;
using SoundForge;

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

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

ISfFileHost newfile = app.NewFile(null,false);

ISfFileHostList files = app.Files;
ISfFileHost src = files[0];
ISfFileHost dest = files[1];

foreach (SfAudioMarker mk in file.Markers)
{

if (mk.IsRegion)

{

SfAudioSelection asel = new SfAudioSelection(mk.Start, mk.Length);

dest.ReplaceAudio(dest.Window.Selection, src, asel);

app.DoMenu("Transport.GoToEnd", false);

}

}

}

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: Regions to single file
Reply by: ForumAdmin
Date:10/12/2007 11:59:02 AM

All possible, just takes some looking:


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

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

ISfFileHost src = app.CurrentFile;
if (null == src)
return;

ISfFileHost dst = app.NewFile(src.DataFormat, false);

foreach (SfAudioMarker mk in src.Markers)
{
if (mk.IsRegion)
{
SfAudioSelection asel = new SfAudioSelection(mk.Start, mk.Length);
SfAudioMarker mk_dst = mk.Copy();
mk_dst.Start = dst.Length;

dst.OverwriteAudio(dst.Length, 0, src, asel);
dst.NewMarker(mk_dst);
}
}

ISfRenderer rend = src.SaveFormat;
string name = Path.GetFileNameWithoutExtension(src.Filename) + " (edit).";
string path = Path.GetDirectoryName(src.Filename);
string full = Path.Combine(path, name + rend.Extension);

dst.SaveAs(full, rend.Guid, "Default Template", RenderOptions.RenderOnly);
SfStatus status = dst.WaitForDoneOrCancel();
}

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



J.

Message last edited on10/12/2007 12:02:26 PM byForumAdmin.
Subject:RE: Regions to single file
Reply by: Phil-H
Date:10/12/2007 3:03:34 PM

Thanks a lot J. That is great. The script now does everything that I wanted.

Cheers

Phil

Subject:RE: Regions to single file
Reply by: _TJ
Date:10/16/2007 12:07:49 PM

Just a note on this point


3) I’d like the regions in the destination file to appear in the same order as they appear in the source file. I’ve read in another post that in scripts Sound Forge processes regions in the order in which they were created rather than the order in which they appear in a file. I seem to have got round this by including ‘app.DoMenu("Transport.GoToEnd", false);’ although I’m not entirely sure why this works!


In Sound Forge 8, this was true. Scripts see the marker list in creation order because that's how it was stored internally. But in Sound Forge 9, the marker list is always sorted by start time, so scripts see the marker list in that order as well.

Your "Transport.GoToEnd" doesn't solve this problem, but it is necessary anyway.

So Your script and the one from J. will both work in 9, and they will work in 8 as long as the regions were created in order from left to right in the file.

tj





Go Back