Click here to Skip to main content
15,887,328 members
Home / Discussions / C#
   

C#

 
QuestionC# Object reference not set to an instance of an object Pin
LAPEC6-Feb-11 8:45
LAPEC6-Feb-11 8:45 
AnswerRe: C# Object reference not set to an instance of an object Pin
fjdiewornncalwe6-Feb-11 8:56
professionalfjdiewornncalwe6-Feb-11 8:56 
GeneralRe: C# Object reference not set to an instance of an object Pin
LAPEC6-Feb-11 10:31
LAPEC6-Feb-11 10:31 
GeneralRe: C# Object reference not set to an instance of an object Pin
RobCroll7-Feb-11 2:12
RobCroll7-Feb-11 2:12 
GeneralRe: C# Object reference not set to an instance of an object Pin
Rob Philpott7-Feb-11 3:51
Rob Philpott7-Feb-11 3:51 
AnswerRe: C# Object reference not set to an instance of an object Pin
Luc Pattyn6-Feb-11 9:15
sitebuilderLuc Pattyn6-Feb-11 9:15 
AnswerRe: C# Object reference not set to an instance of an object Pin
dan!sh 6-Feb-11 10:07
professional dan!sh 6-Feb-11 10:07 
QuestionObtain the path and save the file. Pin
sososm6-Feb-11 2:45
sososm6-Feb-11 2:45 
Hy everyone,

I am desperate, a newbie to programming and I really need your help guys. I work a lot with sound files in Sound Forge 10 and I started to use the included C# scripts, but I don't know how to personalize them for my personal use. If somebody can help me I'll appreciate it very much.

Here is what I want my script to do:
1. - with the file open, get the path and name of the file.
2. - render the file in the same format to a specified location with the name from an .xml file in the same location and
the same name as the sound file ex:
The name of the opened file is : oldies_20110206144516.wav there is a oldies_20110206144516.xml also,
and I want to replace "oldies" with <artist> from the xml file, insert a character between date and time like
20110206144516 to 2011-02-06_14.45.16 then add two more fields from xml; final name should be something like
<artist>_2011-02-06_14.45.16_<song>-<album>.wav
3. - Close the opened file without saving and without confirmation.

Here is a sample script from sound forge:
______________________________________________________________________________
/* =======================================================================================================
* Script Name: Normalize and Render to Multiple Formats
* Description: This script applies an effect the open file and renders it to each of the given formats.
*
* Initial State: Run with one file open and the Script Editor visible (Menu: View, Script Editor)
*
* Parameters (Args):
* Dir - the directory to render the file to - DEFAULT: prompts user
* fx - the effect to apply - DEFAULT: Normalize
* preset - the preset to use for the given effect - DEFAULT: Maximize peak values
*
* Output: The rendered file names and the format used
*
* ==================================================================================================== */

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

//Run with a file open and the Script Editor visible (Menu: View, Script Editor)
//BEHAVIOR:
//- Normalize to maximize peak value
//- If needed, prompts for a target folder
//- Saves the file to one or more formats
//NOTE: Look for MODIFY HERE to quickly update the Script Args with your own preferences

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

//start MODIFY HERE -------------------------
// the array called outputs stores the file formats used for rendering
//NOTE: some file formats share extensions (WAV) (MPG)
// For MainConcept MPEG-1 use this : "6529C198-A432-409f-8A91-3F62198B0E1A|Default Template"
// For Scott Studio WAV use this: "D284C499-FE95-11d3-BB48-0050DA1A5B06|Default Template"

string[] outputs = new string[] {
".pca|Default Template",
".wma|64 Kbps Stereo Music",
// ".mp3|192 Kbps, CD Transparent Audio", //Commented out because MP3 fails until it's registered.
};

string szDir = GETARG("Dir", @""); //type a default output folder between the quotes
string szPlug = GETARG("fx", "Normalize"); //change the effect here
string szPreset = GETARG("preset", "Maximize peak value"); //change the preset for the effect here

// GETARG is a function that defines the default script settings. You can use the Script Args field to over-ride
// the values within GETARG().
// Example: To over-ride GETARG(Key, valueA), type Key=valueB in the Script Args field.
// Use an ampersand (&) to separate different Script Args: KeyOne=valueB&KeyTwo=valueC

//Example Script Args: Dir=f:\Media&fx=Insert Silence&preset=[Sys] 1 second at end of file

//end MODIFY HERE -------------------------

ISfFileHost file = app.CurrentFile;
if (null == file)
return "No files are open. Script canceled.";


// make sure that the directory exists...
if (szDir == null || szDir.Length <= 0)
szDir = PromptForPath("C:\\");
if (szDir == null || szDir.Length <= 0)
return "No output directory. Script canceled.";

ISfRendererList rends = app.Renderers; // this can take a while, so do it before we show the dialog.

Directory.CreateDirectory(szDir);

// ok, now do the deed.

try
{
file.DoEffect(szPlug, szPreset,
new SfAudioSelection(0, file.Length),
EffectOptions.EffectOnly | EffectOptions.WaitForDoneOrCancel);
} catch(Exception)
{
return "Normalize did not complete, script canceled.";
}

string szBase = file.Window.Title;

DPF("Start Rendering... ");
foreach (string output in outputs)
{

string[] fmt = output.Split(new char[]{'|',';'},2);
if (fmt.Length != 2)
{
DPF("'{0}' is not a valid render format specifier - use \"<renderer name>|<preset name>\" ", output);
continue;
}

// find the renderer
//
ISfRenderer rend = null;
if (fmt[0].StartsWith("."))
rend = app.FindRenderer(null, fmt[0]);
else
rend = app.FindRenderer(fmt[0], null);
if (null == rend)
{
DPF("renderer for type {0} not found - skipping to next", fmt[0]);
continue;
}

// determine if the preset is a string or an integer
//
object vPreset = fmt[1];
try {
int iPreset = int.Parse(fmt[1]);
vPreset = iPreset;
} catch (FormatException) {}

string szName = String.Format("{0}-({1}).{2}", szBase, fmt[1], rend.Extension);
DPF("rendering {0}", szName);
string szFilename = Path.Combine(szDir, szName);
DPF(" ~ {0}", szFilename);

DPF(rend.Guid + " " + vPreset + " '" + szDir + "'");

// now, render the file
//
file.RenderAs(szFilename, rend.Guid, vPreset, null, RenderOptions.RenderOnly);
SfStatus result = file.WaitForDoneOrCancel();
if (result != SfStatus.Success)
continue;

} // foreach

return null;
}

public string PromptForPath(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();
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

_______________________________________________________________________________

Thank you in advance for your time.
1000000xxxxxxx
AnswerRe: Obtain the path and save the file. Pin
OriginalGriff6-Feb-11 2:52
mveOriginalGriff6-Feb-11 2:52 
GeneralRe: Obtain the path and save the file. Pin
sososm6-Feb-11 5:39
sososm6-Feb-11 5:39 
AnswerRe: Obtain the path and save the file. Pin
Abhinav S6-Feb-11 6:10
Abhinav S6-Feb-11 6:10 
QuestionError: "Could not use"file name"; file already in use." [SOLVED] Pin
Marat Beiner5-Feb-11 22:26
Marat Beiner5-Feb-11 22:26 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Elham M5-Feb-11 23:06
Elham M5-Feb-11 23:06 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff5-Feb-11 23:10
mveOriginalGriff5-Feb-11 23:10 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:18
Marat Beiner5-Feb-11 23:18 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff5-Feb-11 23:24
mveOriginalGriff5-Feb-11 23:24 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:30
Marat Beiner5-Feb-11 23:30 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff5-Feb-11 23:32
mveOriginalGriff5-Feb-11 23:32 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:37
Marat Beiner5-Feb-11 23:37 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff5-Feb-11 23:42
mveOriginalGriff5-Feb-11 23:42 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:57
Marat Beiner5-Feb-11 23:57 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff6-Feb-11 0:24
mveOriginalGriff6-Feb-11 0:24 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Eddy Vluggen6-Feb-11 1:09
professionalEddy Vluggen6-Feb-11 1:09 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Henry Minute6-Feb-11 7:09
Henry Minute6-Feb-11 7:09 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner6-Feb-11 7:22
Marat Beiner6-Feb-11 7:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.