Community Forums Archive

Go Back

Subject:24 bit output format
Posted by: timaddy
Date:11/1/2006 8:37:26 AM

Hi All,

I am new to this forum so greetings to you all.

I have been writing a few scripts recently to automate the reading of an input file, processing the file (glitch removal, noise removal, etc) and finally output the processed file. I want the configuration of the output file format to be automated (ie if the input audio format is 24 bit I want the output format to be 24 bit but the problem I seem to have encountered is how to create a format for 24 bit by using a script)?

It seems possible by using //ISfWriteAudioStream was = fileOut.OpenWriteAudioStream(fileIn.SampleType, fileIn.Channels, 0); but the problem that I have the input file has been processed as an SfAudioSelection and I have not been able to work out how to convert this to an AudioStream for output? I did wonder about using the //fileOut.RenderAs(szWavOutFile, fileOut.SaveFormat.Guid, "Custom", aselAllOut, RenderOptions.RenderOnly); command but the main problem is that I don't want to have to maunally create a custom format for 24 bit as i'd like to do this all within the script.

I will post some scripts that i've created to help myself learn scripting that other people may find useful.

Below is my example script that i've tried. Any help would be appeciated.



//NOTE: This script requires Sound Forge 8.0a or higher to run.
// Requires IScriptableApp.Win32Window, which is not available in Sound Forge 8.0

//This script loads in all the wav files from a directory, processes it and writes them out in the same format (16 bit to 16 bit
//24 bit to 24 bit)


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

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

string szPath = PromptForPath(app.Win32Window, @"h:\");
string szFilter = "*.wav";

//Loop to the audio restoration plugin on each WAV file
foreach (string szWAVFile in Directory.GetFiles(szPath, szFilter))
{
string szWavFile = Path.GetFileNameWithoutExtension(szWAVFile);
szWavFile = Path.Combine(Path.GetDirectoryName(szWAVFile),szWavFile) + ".wav";//could remove this line if the extension is included in the szWavFile
DPF("{0} check the WAV File variable contents", szWavFile);

if ( ! File.Exists(szWavFile))
{
DPF("{0} does not exist, it will be skipped.", szWavFile);
continue;
}

//process the file using the audio restoration effect
SfStatus result = ProcessFile(app, szWavFile);//call the process file function

DPF("Completed the audio process");

DPF("~ - {0}", result);
if (result != SfStatus.Success)
break;
}
} // Begin

//Function used to process the WAV input file and call the audio de-clicker function

public SfStatus ProcessFile(IScriptableApp app, string szWavFile)
{
SfStatus result = SfStatus.Success;

try
{
ISfFileHost fileIn = app.OpenFile(szWavFile, false, false);//not read-only, allow soundforge window

int ii = 0;

DPF("file[" + ii + "]" + " '" + fileIn.Window.Title + "'");
DPF("~ length is " + fileIn.Length + " which is " + fileIn.PositionToSeconds(fileIn.Length) + " seconds");

SfWaveFormat wfx = fileIn.DataFormat;
DPF(" format(" + fileIn.SampleRate + "," + fileIn.SampleType + "," + fileIn.Channels + ")");
DPF("~ (" + wfx.SampleRate + "," + wfx.BitDepth + "," + wfx.Channels + ")");

//Process the file using an Effect
// **** Insert Effect Settings******//
//app.DoEffect("<Insert Effect Name>", <preset_name>, EffectOptions.EffectOnly);

//Create the output directory and write the output WAV file
string szDir = @"c:\\temp";
if ( ! Directory.Exists(szDir))
{
Directory.CreateDirectory(szDir); // make sure that the directory exists...
}

//Create the output file path and name to write the WAV output
string szWavOutFile = Path.Combine(szDir,szWavFile);
//Write out the new audio file
ISfFileHost fileOut = fileIn.NewFile(new SfAudioSelection(0,-1));//select all of the current AudioSelection.

//Select all of the audio from the present file, then overwrite the fileOut with this from fileIn
SfAudioSelection aselAllOut = new SfAudioSelection(0,-1);
fileOut.OverwriteAudio(0, 0, fileIn, aselAllOut);

ISfWriteAudioStream was = fileOut.OpenWriteAudioStream(fileIn.SampleType, fileIn.Channels, 0);
//fileOut.RenderAs(szWavOutFile, fileOut.SaveFormat.Guid, "Custom", aselAllOut, RenderOptions.RenderOnly);
result = fileOut.WaitForDoneOrCancel();
fileIn.Close(CloseOptions.DiscardChanges);//close the input file
fileOut.Close(CloseOptions.SaveChanges);//close the output file

if (fileOut.IsZombie)//checks to make sure that the file is dead and not yet released
{
DPF(" the file is closed");
}
else
{
DPF(" the file is NOT closed");
}

}
catch(FileNotFoundException ex)
{
result = SfStatus.Fail;
//write the error message to the console
Console.Write("Error: " + ex.Message);
Console.WriteLine(" May be the file doesn't exist or it was incorrectly typed in!");
}

finally
{
}

return result;
}

public string PromptForPath(IWin32Window hParent, string szDir)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();

dlg.Description = "Select folder for rendered files:";
if (null != szDir && "" != szDir)
dlg.SelectedPath = szDir;
else
dlg.SelectedPath = @"C:\";
dlg.ShowNewFolderButton = true;
DialogResult res = dlg.ShowDialog(hParent);
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));
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


Message last edited on11/2/2006 2:13:05 AM bytimaddy.
Subject:RE: 24 bit output format
Reply by: _TJ
Date:11/1/2006 8:21:34 PM

ISfWriteAudioStream is for use by scripts that want to generate audio using math in the script rather than by using Sound Forge's editing functions.

See the script Generate a constantly falling tone.cs from the scripting SDK for the proper way to use ISfWriteAudioStream.

But if I understand your problem correctly. All you really want to do is to process a file and then save it back out in it's original format.

To do that just use

fileOut.RenderAs(szWaveOutFile, fileOut.SaveFormat.Guid, 0, new SfAudioSelection(fileOut), RenderOptions.RenderOnly);


The key here is fileOut.SaveFormat.GUID tells us to use the renderer that matches the current file type, and 0 tells us to leave the format unchanged. So a file that was read in as 24 bit will be saved back out as 24.

tj





Subject:RE: 24 bit output format
Reply by: timaddy
Date:11/2/2006 1:09:50 AM

Hi TJ,

Thanks for the reply and explanantion. You are absolutely correct all I want to be able to do is output the same format as the input file. I will give it a go (from the explanantion in the API docs I thought that it mean't that using Guid would put the output to the original format which I thought mean't 16 bit, 44100khz).

BR

Tim

Subject:RE: 24 bit output format
Reply by: _TJ
Date:11/2/2006 12:10:39 PM

Actually the GUID is used to choose the file type (WAVE, AVI, MPEG etc).
and the template is used to choose the wave format (i.e. 44khz, 16 bit stereo is a wave format).

So the combination of the SaveFormat.GUID (which is the file type that Save... will use), and 0 for the template (i.e. use the current wave format). gives you the ability to Render or SaveAs to the same file type and format as the file was opened from.

tj

Subject:RE: 24 bit output format
Reply by: timaddy
Date:11/20/2006 3:02:40 AM

Tested and works fine.

Thanks

Tim

Go Back