Community Forums Archive

Go Back

Subject:Scripting - paste mix
Posted by: gmartina
Date:8/15/2013 7:43:48 AM

I have a couple thousand short wav file's. I need a script "mix paste" one of the same wav (background noise) from clipboard. Length of original files should not be changed.

Subject:RE: Scripting - paste mix
Reply by: roblesinge
Date:8/15/2013 8:04:29 AM

So you want to paste background noise into several files, I would imagine at a low level, sort of behind the original file (thus the "Mix Paste")? You mention not changing the length of the original files, does the background noise need to just paste in once at a specific position in the original file?

If you provide more details, this may be possible.

Thanks,
Rob.

Subject:RE: Scripting - paste mix
Reply by: gmartina
Date:8/15/2013 8:20:33 AM

Thanks for the quick response!!!
I need to mix a lot of short wav (original files) files (each a different length, 0-2 sec) with one and the same sound (background noise), which is longer (approx. 10 sec).
Original short files must remain the same length, not changed...
So the final files should not be length "background noise". Must be the same as they were (0-2 sec...).
I hope I have explained well :)

Subject:RE: Scripting - paste mix
Reply by: roblesinge
Date:8/15/2013 9:44:04 AM

I think this is possible with a script utilizing the DoMixReplace method of ISfFileHost. I can try and script this up for you, but I probably won't have time to get to it until this evening.

Rob.

Subject:RE: Scripting - paste mix
Reply by: gmartina
Date:8/15/2013 9:51:51 AM

I do not understand how to do it and it would be veeery grateful if you would have made ​​a script when you have the time...

Subject:RE: Scripting - paste mix
Reply by: gmartina
Date:8/15/2013 9:52:46 AM

This evening was more than ok! :)

Subject:RE: Scripting - paste mix
Reply by: roblesinge
Date:8/15/2013 10:31:29 PM

Okay, the script below should do what you're asking. It asks you to choose the folder of audio files you want to paste INTO and then choose the audio file (background) you want to paste FROM. Then it asks for the percentage of foreground audio you want (give it a whole number without a percent sign) and then asks for the percentage of audio you want in the background.

NOTE: You'll want to use this on a COPY of the folder with the audio files as it destructively edits them. You might want to test it out on just a few files before you try it on thousands. Also note that Sound Forge can be tricky and will sometimes throw errors that I haven't accounted for. Ideally you would check the results as thoroughly as you have time for.

Thanks,
Rob.



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

/*Script will take in a folder of files and a "background file." It will then paste/mix the background file onto the
*input files.
*
* IMPORTANT NOTE: This script will destructively edit the files in the chosen folder. It is wise to only use this
* script on a copy of your original files.
*/

public class EntryPoint
{
public void Begin(IScriptableApp app)
{
//pops up a choose folder dialog that lets you select the folder you want to paste to. You can change the
//default start location by changing the path inside the quotes after the "@"
string strFolder = SfHelpers.ChooseDirectory("Choose the folder of files you want to paste into", @"C:\");

//create a string array to hold our file paths and pull them from the strFolder.
string[] pathHolder = Directory.GetFiles(strFolder);

//choose the file of background noise we want to paste INTO our other files.
OpenFileDialog openFile = new OpenFileDialog();

openFile.Title = "Open the background noise file.";

//string to hold the path of our background audio file.
String backgroundFilePath = String.Empty;

if (openFile.ShowDialog() == DialogResult.OK)
{
backgroundFilePath = openFile.FileName.ToString();
}

double gainForeground = 0;
double gainBackground = 0;

//Ask the user to set the level of the foreground audio.
String foreGround = SfHelpers.WaitForInputString("Enter the percentage of FOREGROUND audio you want (w/out a percent sign)");

//Ask the user to set the level of the background audio.
String backGround = SfHelpers.WaitForInputString("Enter the percentage of BACKGROUND audio you want (w/out a percent sign)");

double result;
if (Double.TryParse(foreGround, out result))
gainForeground = result;

if (Double.TryParse(backGround, out result))
gainBackground = result;

gainBackground = gainBackground / 100;
gainForeground = gainForeground / 100;

//open the background noise file and assign it to an ISfFileHost object for processing.
ISfFileHost backFile = app.OpenFile(backgroundFilePath, true, true);

//iterate through the audio files in the folder and paste the backFile into each of them
foreach (String s in pathHolder)
{
//open each file
ISfFileHost file = app.OpenFile(s, false, true);

long fileLen = file.Length;

//create an audio selection that is the same length as the file.
SfAudioSelection asel = new SfAudioSelection(file);

//do our paste mix
file.DoMixReplace(SfAudioSelection.All, gainForeground, gainBackground, backFile, new SfAudioSelection(0, file.Length),
null, null, EffectOptions.EffectOnly | EffectOptions.WaitForDoneOrCancel);

file.Save(SaveOptions.WaitForDoneOrCancel);

//close the file saving any changes
file.Close(CloseOptions.SaveChanges);

}

backFile.Close(CloseOptions.DiscardChanges);

}

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

Message last edited on8/15/2013 10:34:00 PM byroblesinge.
Subject:RE: Scripting - paste mix
Reply by: gmartina
Date:8/16/2013 1:47:05 AM

Rob you're a genius. Thank you veeery much. I do not know how to return the favor?!
Works perfectly!

Subject:RE: Scripting - paste mix
Reply by: roblesinge
Date:8/16/2013 7:57:26 AM

I'm glad it worked for you. No favor-returning necessary. I edit audio for a living, but I program for fun. One of the reasons I use Sound Forge is that occasionally it lets me combine the two.

Cheers,
Rob.

Go Back