Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off-by-one error in loop support #62

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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