Community Forums Archive

Go Back

Subject:Script to get stats on list of unopened wav files
Posted by: randym
Date:7/18/2007 9:01:31 AM

Is there a script that does this? I have in mind providing a list of wav files that the script processes one at a time, generating the same stats that the current "statistics" command generates and outputting them to a text file along with the name of the associated audio file.

A script like this would save a lot of time. For example, one application is to normalize LP recordings that are each split into two wav files--first find the peaks on both halves, figure the desired signal boost for each LP, and then do the normalization. It would also be useful for checking that the desired normalizations are achieved.

Subject:RE: Script to get stats on list of unopened w
Reply by: _TJ
Date:7/18/2007 3:54:22 PM

Statistics is scriptable, and theres a sample that gets and prints them for a single file. It's called statistics.cs


//requires Sound Forge 8.0a

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

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

ISfFileHost file = app.CurrentFile;

file.UpdateStatistics(null);
file.WaitForDoneOrCancel();

if (file.StatisticsAreUpToDate)
{
DPF("Statistics for file '{0}' {1}", file.Filename, file.DataFormat.ToString());
SfAudioStatistics[] stat = new SfAudioStatistics[file.Channels];
for (uint ii = 0; ii < file.Channels; ++ii)
{
stat[ii] = file.GetStatistics(ii);
}

ISfPositionFormatter pos = file.Formatter;

if (file.Channels == 1)
{
Int64 ccPeak = stat[0].MaxValueLocation;
double dPeak = stat[0].MaxValue;
if (Math.Abs(stat[0].MinValue) > dPeak)
{
dPeak = Math.Abs(stat[0].MinValue);
ccPeak = stat[0].MinValueLocation;
}
DPF("Start {0,20} ", pos.Format(stat[0].Start));
DPF("Length {0,20} ", pos.Format(stat[0].Length));
DPF("Average {0,20} ", stat[0].Average);
DPF("RMSLevel {0,20} dB", SfHelpers.RatioTodB(stat[0].RMSLevel));
DPF("PeakValue {0,20} dB", SfHelpers.RatioTodB(dPeak));
DPF("PeakValueLocation {0,20} ", pos.Format(ccPeak));
DPF("MinValue {0,20} % of full scale", stat[0].MinValue * 100);
DPF("MinValueLocation {0,20} ", pos.Format(stat[0].MinValueLocation));
DPF("MaxValue {0,20} % of full scale", stat[0].MaxValue * 100);
DPF("MaxValueLocation {0,20} ", pos.Format(stat[0].MaxValueLocation));
DPF("ZeroCrossingFrequency {0,20} Hz", SfHelpers.RatioTodB(stat[0].ZeroCrossingFrequency));
}
else if (file.Channels == 2)
{
Int64 ccPeak0 = stat[0].MaxValueLocation;
double dPeak0 = stat[0].MaxValue;
if (Math.Abs(stat[0].MinValue) > dPeak0)
{
dPeak0 = Math.Abs(stat[0].MinValue);
ccPeak0 = stat[0].MinValueLocation;
}

Int64 ccPeak1 = stat[1].MaxValueLocation;
double dPeak1 = stat[1].MaxValue;
if (Math.Abs(stat[1].MinValue) > dPeak1)
{
dPeak1 = Math.Abs(stat[1].MinValue);
ccPeak1 = stat[1].MinValueLocation;
}

DPF("-----Parameter------ {0,20} {1,20} ", "----Left----", "----Right----");
// DPF("Start {0,20} {1,20} ", pos.Format(stat[0].Start) , pos.Format(stat[1].Start) );
DPF("Length {0,20} {1,20} ", pos.Format(stat[0].Length) , pos.Format(stat[1].Length) );
DPF("Average {0,20} {1,20} ", stat[0].Average, stat[1].Average);
DPF("RMSLevel {0,20} dB {1,20} dB", SfHelpers.RatioTodB(stat[0].RMSLevel), SfHelpers.RatioTodB(stat[1].RMSLevel));
DPF("PeakValue {0,20} dB {1,20} dB", SfHelpers.RatioTodB(dPeak0), SfHelpers.RatioTodB(dPeak1));
DPF("PeakValueLocation {0,20} {1,20} ", pos.Format(ccPeak0), pos.Format(ccPeak1));
DPF("MinValue {0,20} % {1,20} %", stat[0].MinValue * 100, stat[1].MinValue * 100);
DPF("MinValueLocation {0,20} {1,20} ", pos.Format(stat[0].MinValueLocation), pos.Format(stat[1].MinValueLocation));
DPF("MaxValue {0,20} % {1,20} %", stat[0].MaxValue * 100, stat[1].MaxValue * 100);
DPF("MaxValueLocation {0,20} {1,20} ", pos.Format(stat[0].MaxValueLocation), pos.Format(stat[1].MaxValueLocation));
DPF("ZeroCrossingFrequency {0,20} Hz {1,20} Hz", stat[0].ZeroCrossingFrequency, stat[1].ZeroCrossingFrequency );
}
}
else
{
DPF("Statistics are NOT up to date");
}


}

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, 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

Subject:RE: Script to get stats on list of unopened wav files
Reply by: randym
Date:7/20/2007 6:43:43 AM

Thanks. I had tried that script last week when I had no open files and I got an error I didn’t understand, so gave up. Last night I tried it with an open file and the script ran but I have no clue what it did or where the output went to. As I was running a laptop with no printer connected, I guess it tried to print the output. (But there is nothing in the print que.)

I have no scripting experience but am interested to learn. What scripting language is this? Can you recommend a primer?

Thanks,
Randy

Subject:RE: Script to get stats on list of unopened w
Reply by: ForumAdmin
Date:7/23/2007 9:06:52 AM

It's not trying to print to a printer. The "DPF" output was printed into the output pane of the Script Editor window.

The posted script is written in C#. JScript and VBScript are also supported.

There are many resources for C# available online, as well as many samples available in the Sound Forge scripting SDK.

J.

Go Back