Fix boundary issues on read

This commit is contained in:
Matt Nadareski
2021-06-29 13:49:12 -07:00
parent 6f7095a13f
commit 258b3e9fee

View File

@@ -148,7 +148,7 @@ namespace RedBookPlayer
do do
{ {
// Attempt to read 2 more sectors than requested // Attempt to read 2 more sectors than requested
sectorsToRead = ((ulong)(count / _opticalDisc.BytesPerSector)) + 2; sectorsToRead = ((ulong)count / (ulong)_opticalDisc.BytesPerSector) + 2;
zeroSectorsAmount = 0; zeroSectorsAmount = 0;
// Avoid overreads by padding with 0-byte data at the end // Avoid overreads by padding with 0-byte data at the end
@@ -156,7 +156,9 @@ namespace RedBookPlayer
{ {
ulong oldSectorsToRead = sectorsToRead; ulong oldSectorsToRead = sectorsToRead;
sectorsToRead = _opticalDisc.TotalSectors - _opticalDisc.CurrentSector; sectorsToRead = _opticalDisc.TotalSectors - _opticalDisc.CurrentSector;
zeroSectorsAmount = oldSectorsToRead - sectorsToRead;
int tempZeroSectorCount = (int)(oldSectorsToRead - sectorsToRead);
zeroSectorsAmount = (ulong)(tempZeroSectorCount < 0 ? 0 : tempZeroSectorCount);
} }
// TODO: Figure out when this value could be negative // TODO: Figure out when this value could be negative
@@ -175,6 +177,8 @@ namespace RedBookPlayer
var readSectorTask = Task.Run(() => var readSectorTask = Task.Run(() =>
{ {
lock(_readingImage) lock(_readingImage)
{
for (int i = 0; i < 4; i++)
{ {
try try
{ {
@@ -183,9 +187,11 @@ namespace RedBookPlayer
catch(ArgumentOutOfRangeException) catch(ArgumentOutOfRangeException)
{ {
_opticalDisc.LoadFirstTrack(); _opticalDisc.LoadFirstTrack();
return _opticalDisc.ReadSectors((uint)sectorsToRead).Concat(zeroSectors).ToArray();
} }
} }
return zeroSectors;
}
}); });
// Wait 100ms at longest for the read to occur // Wait 100ms at longest for the read to occur
@@ -201,7 +207,14 @@ namespace RedBookPlayer
// Load only the requested audio segment // Load only the requested audio segment
byte[] audioDataSegment = new byte[count]; byte[] audioDataSegment = new byte[count];
Array.Copy(audioData, _currentSectorReadPosition, audioDataSegment, 0, Math.Min(count, audioData.Length - _currentSectorReadPosition)); int copyAmount = Math.Min(count, audioData.Length - _currentSectorReadPosition);
if(Math.Max(0, copyAmount) == 0)
{
Array.Clear(buffer, offset, count);
return count;
}
Array.Copy(audioData, _currentSectorReadPosition, audioDataSegment, 0, copyAmount);
// Apply de-emphasis filtering, only if enabled // Apply de-emphasis filtering, only if enabled
if(ApplyDeEmphasis) if(ApplyDeEmphasis)