Community Forums Archive

Go Back

Subject:Importing temporary file
Posted by: sjm
Date:5/31/2007 2:49:37 PM

I'm writing a script to import data from a proprietary file format. The script runs a command-line tool to convert the file to a temporary .wav file, then opens a new file with the data:

ISfFileHost tempFile = app.OpenFile(tempWavFile, false, true);
ISfFileHost newFile = tempFile.NewFile(new SfAudioSelection(tempFile));
tempFile.Close(CloseOptions.DiscardChanges);

I had thought this would copy the audio data from the temporary wav file into a new window. If I then delete the temporary wav file, though, Sound Forge shows a dialog box telling me that I've deleted a file that it needs; it looks like newFile still has some dependency on the temporary file, even though I've closed tempFile.

This works OK:

ISfFileHost tempFile = app.OpenFile(tempWavFile, false, false);
tempFile.Window.Selection = new SfAudioSelection(tempFile);
app.DoMenuAndWait("Edit.Copy", false);
SfWaveFormat format = tempFile.DataFormat;
tempFile.Close(CloseOptions.DiscardChanges);
ISfFileHost newFile = app.NewFile(format, false);
app.DoMenuAndWait("Edit.Paste", false);

.. but using DoMenu() is not ideal because it changes the undo/redo stack.

Is there a better way to do this?

Subject:RE: Importing temporary file
Reply by: _TJ
Date:6/7/2007 12:17:38 PM

tempFile.NewFile(new SfAudioSelection(tempFile)); does indeed open the temp file into a new window, but it doesn't copy the data into a new .wav file until you save, or process, until then it still references the original .wav file.

So this will probably do what you want.

ISfFileHost tempFile = app.OpenFile(tempWavFile, false, true);
ISfFileHost newFile = tempFile.NewFile(new SfAudioSelection(tempFile));
tempFile.Close(CloseOptions.DiscardChanges);

// this could by any effect, so long as you process the whole file.
//
newFile.DoEffect("Normalize", null, SfAudioSelection(newFile), EffectOptions.EffectOnly);
newFile.WaitForDoneOrCancel();


The other way to do this would be create newFile with silence at the correct length, then Mix() from tempFile into newFile.

tj




Subject:RE: Importing temporary file
Reply by: sjm
Date:6/8/2007 9:19:23 AM

I found a different way (somewhat more involved) that seems to work:

ISfFileHost tempFile = app.OpenFile(filename, false, true);
ISfFileHost newFile = app.NewFile(tempFile.DataFormat, false);
ISfWriteAudioStream stream = newFile.OpenWriteAudioStream(tempFile.SampleType, 0, 0);
float[] buf = new float[10000];
int read = 0;
int pos = 0;
for (;;)
{
read = tempFile.ReadAudio(buf, pos);
if (read == 0)
break;
pos += read;
stream.Append(buf, 0, read, SfSampleType.WavFloat);
}
newFile.WriteAudio(new SfAudioSelection(0, stream.Length), stream);

// have to explicitly copy markers
foreach (SfAudioMarker srcMarker in tempFile.Markers)
{
if (srcMarker.IsRegion)
newFile.Markers.AddRegion(srcMarker.Start, srcMarker.Length, srcMarker.Name);
else
newFile.Markers.AddMarker(srcMarker.Start, srcMarker.Name);
}
tempFile.Close(CloseOptions.DiscardChanges);

Go Back