Community Forums Archive

Go Back

Subject:Supress Illegal Marker or Region error
Posted by: roblesinge
Date:3/26/2013 9:41:50 AM

I'm not sure this will get any answers, since I sometimes feel like I'm the only one reading/responding to this part of the forum anymore, but here goes.

Sometimes I get a "File may be corrupt" error when running a script on a batch of files. I think it's happening after a app.OpenFile() call, but I can't be sure, because SF has a window open and the file seems to be open. The error details read: This file contained an illegal marker or region. The marker or region has been corrected. So, SF has already fixed the problem and opened the file. How do I suppress the error message box so I don't have to click OK every stinking time this error occurs. For some reason SF doesn't like some files that have markers or regions in them (even though the files were all created in SF to begin with). This is annoying as heck and really sucks time when I have to babysit a script working on hundreds of files.

Anybody got any ideas? I'll post my code below JIC it is needed. Note it is a bit sloppy as I'm trying to fix this problem and this is a sub-script I made out of another, larger script so some functionality has been commented out.


using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using SoundForge;
using System.Collections;
using System.Collections.Generic;

public class EntryPoint
{
int silenceCount = 0;

int numFilesOrig = 0;
int numConverted44k, numConvertedMP3 = 0;
string errors;



public ISfGenericPreset GetNormPreset(IScriptableApp appl, int level, bool rms)
{
//takes in a integer level and a bool for rms and creates a norm preset
//----Example call: ISfGenericPreset normNeg4 = GetNormPreset(app, -4, false);-----

ISfGenericEffect fx = appl.FindEffect("Normalize");
ISfGenericPreset preset0 = fx.GetPreset(0);
byte[] abData = preset0.Bytes;
string levelString = level.ToString();

ISfGenericPreset normPreset = new SoundForge.SfGenericPreset(levelString, fx, abData);
Fields_Normalize field1 = Fields_Normalize.FromPreset(normPreset);

//If 0dB or a positive dB was requested, set the NormTo field as 0.
if (level >= 0)
{
DPF("Level is over 0");
field1.NormalizeTo = 0;
}
else
field1.NormalizeTo = SfHelpers.dBToRatio(level);

field1.RMS = rms;
if (rms)
{
field1.IfClip = Fields_Normalize.ClipAction.Compress;
}

field1.ToPreset(normPreset);

return normPreset;
}

public bool Silence(ISfFileHost file)
{
/* ---Takes a ISfFileHost object and returns a bool value based on whether or not the file has an RMS volume of 0dB
* -----Call = Silence(ISfFileHost fileName);*/

file.UpdateStatistics(new SfAudioSelection(0, file.Length));
SfAudioStatistics stats = file.GetStatistics(0);
if (stats.RMSLevel == 0)
return true;
else
return false;
}

public void SilenceChecker(ISfFileHost chkFile, string inputFileName, string outPutPath)
{
/*---Takes in a file, the complete path to that file and the preferred output path and determines if the file is silent or not. If it is, it creates an output folder and copies the file into it.
-----Call = SilenceChecker(ISfFileHost file, string inputFilePath, string outPutFilePath);*/

string outFolder = Path.Combine(outPutPath, "Silence Files");

if (Silence(chkFile))
{
if (!Directory.Exists(outFolder))
{
Directory.CreateDirectory(outFolder);
}

string temp = Path.GetFileName(inputFileName);
File.Copy(inputFileName, Path.Combine(outFolder, temp));

silenceCount++;
}

}

public void DoTheWavs(IScriptableApp appl, ISfFileHost file, uint rate, int level, bool peakOrRMS, string fileName, string outName, bool makeMP3)
{
//Check if the input file is silence. If so, skip the SilenceChecker step after the RenderAs.
bool isSilence = false;
if (Silence(file))
isSilence = true;

SfWaveFormat format = new SfWaveFormat(rate, 16, 1, false);

//create a new ISfFileHost object and overwrite it with the audio file we passed in.
ISfFileHost tempFile = appl.NewFile(file.DataFormat, true);
tempFile.OverwriteAudio(tempFile.Length, 0, file, new SfAudioSelection(file));

//Checks to make sure the file is at 16-bit.
if (tempFile.DataFormat.BitDepth != 16)
{
tempFile.DoConvertSampleType(format.SampleType, 0, 0, EffectOptions.WaitForDoneOrCancel);
}

//Checks the sample rate and resamples to match the desired rate if necessary.
if (tempFile.DataFormat.SampleRate != rate)
{
DPF("Resampling to {0}", rate.ToString());
tempFile.DoResample(rate, 4, EffectOptions.EffectOnly | EffectOptions.WaitForDoneOrCancel);
DPF(tempFile.DataFormat.SampleRate.ToString());
}

//Normalizes to the desired level.
if (level > 0)
{
appl.DoEffect("Normalize", GetNormPreset(appl, level, peakOrRMS), EffectOptions.EffectOnly | EffectOptions.WaitForDoneOrCancel);
appl.DoEffect("Volume", "+4dB boost", EffectOptions.EffectOnly | EffectOptions.WaitForDoneOrCancel);
}
else
appl.DoEffect("Normalize", GetNormPreset(appl, level, peakOrRMS), EffectOptions.EffectOnly | EffectOptions.WaitForDoneOrCancel);

//Gets the output filename and then renders the file.
string outPath = Path.Combine(SetOutputDir(fileName, outName), Path.GetFileName(fileName));
DPF(outPath);
tempFile.SaveAs(outPath, ".wav", "Default Template", RenderOptions.WaitForDoneOrCancel);

//If the file wasn't flagged as silence, we now check to see if it was rendered as silence accidentally.
if (!isSilence)
SilenceChecker(tempFile, fileName, Directory.GetParent(outPath).ToString());

//This will make an MP3 file if it was requested in the Method call.
if (makeMP3)
{
string outtie = Path.Combine(SetOutputDir(fileName, "4_final_MP3_files"), (Path.GetFileNameWithoutExtension(fileName) + ".mp3"));
DPF(outtie);
tempFile.SaveAs(outtie, ".mp3", "Default Template", RenderOptions.WaitForDoneOrCancel);
}

tempFile.Close(CloseOptions.DiscardChanges);

}

public ISfFileHost GoodFile(IScriptableApp apple, string inPath)
{
ISfFileHost goodFile;
try
{
goodFile = apple.OpenFile(inPath, true, true);
return goodFile;
}
catch (Exception e)
{
goodFile = apple.OpenFile(inPath, true, true);
errors = e.ToString();
return goodFile;
}
}

public string SetOutputDir(string filePath, string outFolder)
{
//Set up output directory
DirectoryInfo parentDir = Directory.GetParent(Directory.GetParent(filePath).ToString());
string outDir = Path.Combine(parentDir.ToString(), outFolder);

if (!Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}

return outDir;
}

public int fileCounter(string inputDir)
{
string[] holder = Directory.GetFiles(inputDir);
return holder.Length;
}

public void Begin(IScriptableApp app)
{
string strFolder = SfHelpers.ChooseDirectory("Choose the folder to convert from", @"N:\Populate_SAID\Julia_to_Upload\Renamed");
if (strFolder != null)
{
//Creates an array to hold filenames
string[] fileHolder;
fileHolder = Directory.GetFiles(strFolder, "*.wav");

numFilesOrig = fileHolder.Length;


string outFolder44k = "3_final3_44k_16b_neg4_wav", outFolderMP3 = "4_final_MP3_files";

foreach (string s in fileHolder)
{
ISfFileHost file = GoodFile(app, s);
file.Markers.Clear();
//if (false)
// DoTheWavs(app, file, 48000, -4, false, s, "3_final1_48k_16b_neg4_wav", false);

//if (false)
// DoTheWavs(app, file, 48000, -12, false, s, "3_final2_48k_16b_neg12_wav", false);

if (true)
{
DoTheWavs(app, file, 44100, -4, false, s, outFolder44k, true);
}

//DoTheWavs(app, file, file.DataFormat.SampleRate, 4, false, s, "2_norm1_pos4dB", false);

file.Close(CloseOptions.DiscardChanges);
}

numConverted44k = fileCounter(Path.Combine(Directory.GetParent(strFolder).ToString(), outFolder44k));
numConvertedMP3 = fileCounter(Path.Combine(Directory.GetParent(strFolder).ToString(), outFolderMP3));

if (silenceCount > 0)
MessageBox.Show(String.Format("{0} accidental silence files were created. Check your folders", silenceCount.ToString()), "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

else
return;

}

public class Form1 : Form
{
Form form1;

FolderBrowserDialog getFolder = new FolderBrowserDialog();
public string chosenFolder = null;
public bool box1 = false, box2 = false, box3 = false, box4 = false;
public bool doConversion, folderLoaded = false;

CheckBox cBox1, cBox2, cBox3;
Button browseFolder, okay, cancel;
StatusBar status;
Label folderChosen;

public void MyForm()
{
this.form1 = new Form1();

this.browseFolder = new Button();
browseFolder.Text = "Choose Folder";
browseFolder.Width = 100;
browseFolder.Location = new Point(10, 10);
browseFolder.Click += new EventHandler(browseFolder_Click);

this.folderChosen = new Label();
folderChosen.Text = "Choose Folder";
folderChosen.Location = new Point(browseFolder.Right + 10, 15);

this.cBox1 = new CheckBox();
this.cBox2 = new CheckBox();
this.cBox3 = new CheckBox();

cBox1.Text = "48k, 16bit, -4dB wav";
cBox2.Text = "48k, 16bit, -12dB wav";
cBox3.Text = "44.1k, 16bit, -4dB wav (MP3 will also be created)";

cBox1.Location = new Point(10, browseFolder.Bottom + 10);
cBox1.Width = 300;
cBox1.CheckedChanged += new EventHandler(cBox1_Checked);

cBox2.Location = new Point(10, cBox1.Bottom + 5);
cBox2.Width = 300;
cBox2.CheckedChanged += new EventHandler(cBox2_Checked);

cBox3.Location = new Point(10, cBox2.Bottom + 5);
cBox3.Width = 300;
cBox3.CheckedChanged += new EventHandler(cBox3_Checked);

this.okay = new Button();
okay.Text = "OK";
okay.Location = new Point(10, cBox3.Bottom + 15);
okay.Click += new EventHandler(okay_Clicked);


this.cancel = new Button();
cancel.Text = "Cancel";
cancel.Location = new Point(10, okay.Bottom + 10);
cancel.Click += new EventHandler(cancel_Clicked);

this.status = new StatusBar();
status.Text = "Ready";

form1.FormBorderStyle = FormBorderStyle.FixedDialog;
form1.Text = "Select the renamed folder and conversions";
form1.MaximizeBox = false;
form1.MinimizeBox = false;
//form1.AcceptButton = okay;
//form1.CancelButton = cancel;
form1.StartPosition = FormStartPosition.CenterScreen;

form1.Controls.Add(browseFolder);
form1.Controls.Add(cBox1);
form1.Controls.Add(cBox2);
form1.Controls.Add(cBox3);
form1.Controls.Add(okay);
form1.Controls.Add(cancel);
form1.Controls.Add(status);
form1.Controls.Add(folderChosen);

form1.ShowDialog();
}

public void browseFolder_Click(object sender, EventArgs e)
{
DialogResult result = getFolder.ShowDialog();
if (result == DialogResult.OK)
{
this.folderLoaded = true;
this.chosenFolder = getFolder.SelectedPath;
DirectoryInfo dInfo = new DirectoryInfo(this.chosenFolder);
this.status.Text = String.Format("Folder \"{0}\" is loaded", dInfo.Name);
this.folderChosen.ForeColor = Color.Red;
this.folderChosen.Text = "Folder Loaded";
}
}

public void cBox1_Checked(object sender, EventArgs e)
{
if (cBox1.Checked)
{
this.box1 = true;
}
else
{
this.box1 = false;
}
}

public void cBox2_Checked(object sender, EventArgs e)
{
if (cBox2.Checked)
{
this.box2 = true;
}
else
{
this.box2 = false;
}
}

public void cBox3_Checked(object sender, EventArgs e)
{
if (cBox3.Checked)
{
this.box3 = true;
}
else
{
this.box3 = false;
}
}

public void okay_Clicked(object sender, EventArgs e)
{
if (folderLoaded)
{
if (this.box1 || this.box2 || this.box3)
{
this.doConversion = true;
form1.Close();
}
else
{
status.Text = "Cannot Process, No conversions selected";
}
}
else
{
status.Text = "Cannot Process, No folder selected";
}
}

public void cancel_Clicked(object sender, EventArgs e)
{
this.doConversion = false;
form1.Close();
}
}

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. {1} silences found. orig - {2}; 44k - {3}; MP3 - {4}", Script.Name, silenceCount.ToString(), numFilesOrig, numConverted44k, numConvertedMP3));
}
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

Go Back