public GATData[] LoadNext( BackgroundWorker worker, float[] deInterleaveBuffer )
{
GATData[] containers;
int i;
int channels;
if( _paths.Count == 0 )
{
Status = LoadOperationStatus.Done;
return null;
}
AGATAudioFile file;
try
{
file = AGATAudioFile.OpenAudioFileAtPath( _paths.Dequeue() );
}
catch( System.Exception e )
{
#if UNITY_EDITOR
Debug.LogException( e );
#endif
FailReason = LoadOperationFailReason.CannotOpenFile;
Status = LoadOperationStatus.Failed;
return null;
}
using( file )
{
channels = file.Channels;
CurrentFileName = file.FileName;
if( !_forceMono && channels > 1 )
{
containers = new GATData[ channels ];
}
else
{
containers = new GATData[ 1 ];
}
for( i = 0; i < containers.Length; i++ )
{
if( _allocationMode == GATDataAllocationMode.Fixed ) //GZComment: allocation fail leaks
{
try{ containers[ i ] = GATManager.GetFixedDataContainer( file.NumFrames, file.FileName ); }
catch( System.Exception ex )
{
ReleaseContainers( containers );
Status = LoadOperationStatus.Failed;
FailReason = LoadOperationFailReason.OutOfPreAllocatedMemory;
#if UNITY_EDITOR
Debug.LogException( ex );
#endif
return null;
}
}
else if( _allocationMode == GATDataAllocationMode.Managed ) //GZComment: allocation fail leaks
{
try
{
containers[ i ] = GATManager.GetDataContainer( file.NumFrames );
}
catch( System.Exception ex )
{
ReleaseContainers( containers );
Status = LoadOperationStatus.Failed;
FailReason = LoadOperationFailReason.NoLargeEnoughChunkInAllocator;
#if UNITY_EDITOR
Debug.LogException( ex );
#endif
return null;
}
}
else
{
containers[ i ] = new GATData( new float[ file.NumFrames ] );
}
}
int framesPerRead;
int framesRead;
int totalFramesRead = 0;
if( channels > 1 )
{
framesPerRead = deInterleaveBuffer.Length / channels;
while( true )
{
if( worker.CancellationPending )
{
ReleaseContainers( containers );
return null;
}
framesRead = file.ReadNextChunk( deInterleaveBuffer, 0, framesPerRead );
if( _forceMono ) // Only supported for stereo files
{
int numSamples = framesRead * channels;
for( i = 0; i < numSamples; i+= channels )
{
deInterleaveBuffer[ i ] += deInterleaveBuffer[ i + 1 ];
}
containers[ 0 ].CopyFromInterlaced( deInterleaveBuffer, framesRead, totalFramesRead, 0, channels );
}
else
{
for( i = 0; i < channels; i++ )
{
containers[ i ].CopyFromInterlaced( deInterleaveBuffer, framesRead, totalFramesRead, i, channels );
}
}
totalFramesRead += framesRead;
if( _reportsProgress )
worker.ReportProgress( 0, new ProgressInfo( ( float )totalFramesRead / file.NumFrames, file.FileName ) );
if( framesRead < framesPerRead )
break;
}
}
else
{
int framesLeft;
while( totalFramesRead < file.NumFrames )
{
if( worker.CancellationPending )
{
ReleaseContainers( containers );
return null;
}
framesLeft = file.NumFrames - totalFramesRead;
framesPerRead = ( framesLeft < 16384 ? framesLeft : 16384 );
framesRead = file.ReadNextChunk( containers[ 0 ].ParentArray, containers[ 0 ].MemOffset + totalFramesRead, framesPerRead );
totalFramesRead += framesRead;
if( _reportsProgress )
worker.ReportProgress( 0, new ProgressInfo( ( float )totalFramesRead / file.NumFrames, file.FileName ) );
}
}
}
return containers;
}