final DecimatedStake das;
final Span extSpan;
final long fullrateStop, fullrateLen;
final int numFullBuf;
final Object enc_this = this;
final AudioFile cacheReadAF;
final AudioFile cacheWriteAF;
for( int i = 0; i < SUBNUM; i++ ) {
this.decimHelps[ i ] = new DecimationHelp( afd.rate, decimations[ i ]);
}
synchronized( fileSync ) {
das = allocAsync( viewSpan );
}
extSpan = das.getSpan();
// XXX SWINGOSC XXX
// fullrateStop = Math.min( extSpan.getStop(), fullScale.editGetSpan( ce ).stop );
fullrateStop = Math.min( extSpan.stop, viewSpan.stop );
fullrateLen = fullrateStop - extSpan.start;
// numFullBuf = (int) (fullrateLen >> MAXSHIFT);
cacheReadAF = openCacheForRead( model );
if( cacheReadAF == null ) {
// cacheWriteAS = fullScale.openCacheForWrite( model,
// decimHelps[ 0 ].fullrateToSubsample( union.getLength() ));
cacheWriteAF = openCacheForWrite( model, (fullrateLen + MAXCEILADD) & MAXMASK );
numFullBuf = (int) (fullrateLen >> MAXSHIFT);
} else {
numFullBuf = (int) ((fullrateLen + MAXCEILADD) >> MAXSHIFT); // cached files always have integer fullBufs!
cacheWriteAF = null;
}
synchronized( bufSync ) {
createBuffers();
}
// XXX SWINGOSC XXX
// editClear( source, das.getSpan(), ce );
// editAdd( source, das, ce );
decimatedStake = das;
launched = true;
threadAsync = new Thread( new Runnable() {
public void run() {
final int minCoarse;
boolean success = false;
long pos = extSpan.getStart();
// long framesWrittenCache = 0;
boolean cacheWriteComplete = false;
long framesWritten = 0;
Span tag2;
float f1;
int len, repaint = 0;
long time, nextTime = System.currentTimeMillis() + 100;
minCoarse = MAXCOARSE >> decimHelps[ 0 ].shift;
try {
for( int i = 0; (i < numFullBuf) && keepAsyncRunning; i++ ) {
synchronized( bufSync ) {
if( cacheReadAF != null ) {
tag2 = new Span( pos, pos + minCoarse );
cacheReadAF.readFrames( tmpBuf2, 0, minCoarse );
das.continueWrite( 0, tmpBuf2, 0, minCoarse );
subsampleWrite2( tmpBuf2, das, minCoarse );
pos += minCoarse;
} else {
tag2 = new Span( pos, pos + MAXCOARSE );
// fullScale.readFrames( tmpBuf, 0, tag2, ce );
fullScale.readFrames( tmpBuf, 0, MAXCOARSE );
// for( int k = 0; k < tmpBuf.length; k++ ) { for( int j = 0; j < MAXCOARSE; j++ ) { tmpBuf[ k ][ j ] = 0.125f; }}
subsampleWrite( tmpBuf, tmpBuf2, das, MAXCOARSE, cacheWriteAF );
pos += MAXCOARSE;
// framesWrittenCache += minCoarse;
}
framesWritten += MAXCOARSE;
}
time = System.currentTimeMillis();
if( time >= nextTime ) {
readProgress = (float) ((double) framesWritten / (double) fullrateLen);
nextTime = time + 100;
if( asyncManager != null ) asyncManager.dispatchEvent( new AsyncEvent( enc_this, AsyncEvent.UPDATE, time ));
// XXX SWINGOSC XXX
repaint = (repaint + 1) % 20;
if( repaint == 0 ) {
needsImageUpdate = true;
repaint();
}
}
}
if( (cacheReadAF == null) && keepAsyncRunning ) { // cached files always have integer fullBufs!
len = (int) (fullrateStop - pos);
if( len > 0 ) {
synchronized( bufSync ) {
tag2 = new Span( pos, pos + len );
// XXX SWINGOSC XXX
// fullScale.readFrames( tmpBuf, 0, tag2, null );
if( fullScale.getFramePosition() != tag2.start ) {
fullScale.seekFrame( tag2.start );
}
fullScale.readFrames( tmpBuf, 0, (int) tag2.getLength() );
for( int ch = 0; ch < fullChannels; ch++ ) {
f1 = tmpBuf[ ch ][ len - 1 ];
for( int i = len; i < MAXCOARSE; i++ ) {
tmpBuf[ ch ][ i ] = f1;
}
}
subsampleWrite( tmpBuf, tmpBuf2, das, MAXCOARSE, cacheWriteAF );
pos += MAXCOARSE;
framesWritten += MAXCOARSE;
// framesWrittenCache += minCoarse;
}
}
}
cacheWriteComplete = true;
if( cacheWriteAF != null ) cm.addFile( cacheWriteAF.getFile() );
success = true;
}
catch( IOException e1 ) {
// XXX SWINGOSC XXX
// System.err.println( e1 );
// System.out.println( e1 );
e1.printStackTrace( System.out );
}
finally {
if( cacheReadAF != null ) cacheReadAF.cleanUp();
if( cacheWriteAF != null ) {
cacheWriteAF.cleanUp();
if( !cacheWriteComplete ) { // indicates process was aborted ...
final File f = createCacheFileName();
if( (cm != null) && (f != null) ) { // ... therefore delete incomplete cache files!
cm.removeFile( f );
}