Skip to content

Commit

Permalink
Merge pull request #62 from segfault-bilibili/fix-loop-off-by-one
Browse files Browse the repository at this point in the history
Fix off-by-one error in loop support
  • Loading branch information
hozuki authored Feb 24, 2022
2 parents 31c316a + 9b8911f commit c364976
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Exchange/DereTore.Exchange.Audio.HCA/HcaAudioStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override long Length {
var waveDataBlockSize = _decoder.GetMinWaveDataBufferSize();
totalLength += waveDataBlockSize * hcaInfo.BlockCount;
if (HasLoop && audioParams.SimulatedLoopCount > 0) {
var loopedBlockCount = hcaInfo.LoopEnd - hcaInfo.LoopStart + 1;
var loopedBlockCount = hcaInfo.LoopEnd - hcaInfo.LoopStart;
totalLength += waveDataBlockSize * loopedBlockCount * audioParams.SimulatedLoopCount;
}
_length = totalLength;
Expand Down Expand Up @@ -201,7 +201,7 @@ private void EnsureSoundDataDecodedWithLoops(long waveDataOffset, int byteCount)
var audioParams = _audioParams;
var decodedSize = (waveDataOffset - headerSize) / waveBlockSize * waveBlockSize;
// For those before or in the loop range...
if (audioParams.InfiniteLoop || startBlockIndex <= hcaInfo.LoopEnd + (hcaInfo.LoopEnd - hcaInfo.LoopStart + 1) * audioParams.SimulatedLoopCount) {
if (audioParams.InfiniteLoop || startBlockIndex <= hcaInfo.LoopEnd - 1 + (hcaInfo.LoopEnd - hcaInfo.LoopStart) * audioParams.SimulatedLoopCount) {
var blockIndex = startBlockIndex;
var executed = false;
while (true) {
Expand All @@ -218,7 +218,7 @@ private void EnsureSoundDataDecodedWithLoops(long waveDataOffset, int byteCount)
EnsureDecodeOneBlock(blockIndex);
decodedSize += waveBlockSize;
++blockIndex;
if (blockIndex > hcaInfo.LoopEnd) {
if (blockIndex > hcaInfo.LoopEnd - 1) {
blockIndex = hcaInfo.LoopStart;
}
executed = true;
Expand All @@ -228,10 +228,10 @@ private void EnsureSoundDataDecodedWithLoops(long waveDataOffset, int byteCount)
if (audioParams.InfiniteLoop) {
return;
}
var endBlockIndex = hcaInfo.LoopEnd + (hcaInfo.LoopEnd - hcaInfo.LoopStart + 1) * audioParams.SimulatedLoopCount;
var endBlockIndex = hcaInfo.LoopEnd - 1 + (hcaInfo.LoopEnd - hcaInfo.LoopStart) * audioParams.SimulatedLoopCount;
var startOfAfterLoopRegion = endBlockIndex * waveBlockSize;
if (waveDataEnd >= startOfAfterLoopRegion) {
for (var blockIndex = hcaInfo.LoopEnd + 1; blockIndex < hcaInfo.BlockCount && decodedSize < waveDataEnd; ++blockIndex) {
for (var blockIndex = hcaInfo.LoopEnd; blockIndex < hcaInfo.BlockCount && decodedSize < waveDataEnd; ++blockIndex) {
EnsureDecodeOneBlock(blockIndex);
decodedSize += waveBlockSize;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ private long MapPosToNonLoopedWaveStreamPos(long requestedPosition) {
var waveBlockSize = _decoder.GetMinWaveDataBufferSize();
var headerSize = _headerSize;
var relativeDataPosition = requestedPosition - headerSize;
var endLoopDataPosition = (hcaInfo.LoopEnd + 1) * waveBlockSize;
var endLoopDataPosition = (hcaInfo.LoopEnd) * waveBlockSize;
if (relativeDataPosition < endLoopDataPosition) {
return requestedPosition;
}
Expand Down
2 changes: 1 addition & 1 deletion Exchange/DereTore.Exchange.Audio.HCA/HcaDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public int WriteWaveHeader(byte[] stream, AudioParams audioParams) {

var totalBlockCount = hcaInfo.BlockCount;
if (hcaInfo.LoopFlag) {
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart + 1) * audioParams.SimulatedLoopCount;
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart) * audioParams.SimulatedLoopCount;
}
wavData.DataSize = totalBlockCount * 0x80 * 8 * wavRiff.FmtSamplingSize;
wavRiff.RiffSize = (uint)(0x1c + (hcaInfo.Comment != null ? wavNote.NoteSize : 0) + Marshal.SizeOf(wavData) + wavData.DataSize);
Expand Down
4 changes: 2 additions & 2 deletions Exchange/DereTore.Exchange.Audio.HCA/HcaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static uint CalculateLengthInSamples(HcaInfo hcaInfo, uint loopCount, boo
return uint.MaxValue;
}
var totalBlockCount = hcaInfo.BlockCount;
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart + 1) * loopCount;
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart) * loopCount;
return totalBlockCount * hcaInfo.ChannelCount * 0x80 * 8;
}

Expand All @@ -104,7 +104,7 @@ public static float CalculateLengthInSeconds(HcaInfo hcaInfo, uint loopCount, bo
return float.MaxValue;
}
var totalBlockCount = hcaInfo.BlockCount;
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart + 1) * loopCount;
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart) * loopCount;
return totalBlockCount * 0x80 * 8 / (float)hcaInfo.SamplingRate;
}

Expand Down

0 comments on commit c364976

Please sign in to comment.