}
public static void CompressFile(String pInputFilename, String pOutputFilename, int nCompressionLevel, ProgressCallback progressor) throws IOException, JMACStoppedByUserException {
// declare the variables
IAPECompress spAPECompress = null;
InputSource spInputSource = null;
try {
byte[] spBuffer = null;
WaveFormat WaveFormatEx = new WaveFormat();
// create the input source
IntegerPointer nAudioBlocks = new IntegerPointer();
nAudioBlocks.value = 0;
IntegerPointer nHeaderBytes = new IntegerPointer();
nHeaderBytes.value = 0;
IntegerPointer nTerminatingBytes = new IntegerPointer();
nTerminatingBytes.value = 0;
spInputSource = InputSource.CreateInputSource(pInputFilename, WaveFormatEx, nAudioBlocks,
nHeaderBytes, nTerminatingBytes);
// create the compressor
spAPECompress = IAPECompress.CreateIAPECompress();
// figure the audio bytes
int nAudioBytes = nAudioBlocks.value * WaveFormatEx.nBlockAlign;
// start the encoder
if (nHeaderBytes.value > 0) spBuffer = new byte[nHeaderBytes.value];
spInputSource.GetHeaderData(spBuffer);
spAPECompress.Start(pOutputFilename, WaveFormatEx, nAudioBytes,
nCompressionLevel, spBuffer, nHeaderBytes.value);
// set-up the progress
ProgressHelper spMACProgressHelper = new ProgressHelper(nAudioBytes, progressor);
// master loop
int nBytesLeft = nAudioBytes;
spMACProgressHelper.UpdateStatus("Process data by compressor");
while (nBytesLeft > 0) {
int nBytesAdded = spAPECompress.AddDataFromInputSource(spInputSource, nBytesLeft);
nBytesLeft -= nBytesAdded;
// update the progress
spMACProgressHelper.UpdateProgress(nAudioBytes - nBytesLeft);
// process the kill flag
if (spMACProgressHelper.isKillFlag())
throw new JMACStoppedByUserException();
}
spMACProgressHelper.UpdateStatus("Finishing compression");
// finalize the file
if (nTerminatingBytes.value > 0) spBuffer = new byte[nTerminatingBytes.value];
spInputSource.GetTerminatingData(spBuffer);
spAPECompress.Finish(spBuffer, nTerminatingBytes.value, nTerminatingBytes.value);
// update the progress to 100%
spMACProgressHelper.UpdateStatus("Compression finished");
} finally {
// kill the compressor if we failed
if (spAPECompress != null)
spAPECompress.Kill();
if (spInputSource != null)
spInputSource.Close();
}
}