Skip to content

Commit 7ff64fe

Browse files
committed
Fix not being able to read empty WAV files
1 parent 9f9c97b commit 7ff64fe

6 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/libs/Detach/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
This library uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## 0.30.2
6+
7+
### Fixed
8+
9+
- Fixed not being able to read WAV files with empty `data` chunk.
10+
- `RandomExtensions.Choose` now throws an exception when the `options` argument is empty.
11+
512
## 0.30.1
613

714
### Added

src/libs/Detach/Detach.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup Label="Package configuration">
44
<PackageId>NoahStolk.Detach</PackageId>
5-
<Version>0.30.1</Version>
5+
<Version>0.30.2</Version>
66
</PropertyGroup>
77

88
</Project>

src/libs/Detach/Extensions/RandomExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public static double RandomDouble(this Random random, double minValue, double ma
106106
/// <returns>The randomly chosen item.</returns>
107107
public static T Choose<T>(this Random random, params Span<T> options)
108108
{
109+
if (options.Length == 0)
110+
throw new ArgumentException("options cannot be empty", nameof(options));
111+
109112
return options[random.Next(options.Length)];
110113
}
111114

src/libs/Detach/Parsers/Sound/WavFormat/WaveParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static SoundData Parse(byte[] fileContents)
6262
if (blockAlign != expectedBlockAlign)
6363
throw new WaveParseException($"Expected block align to be {expectedBlockAlign} (got {blockAlign}).");
6464

65-
while (br.BaseStream.Position < br.BaseStream.Length - (DataHeader.Length + sizeof(int)))
65+
while (br.BaseStream.Position <= br.BaseStream.Length - (DataHeader.Length + sizeof(int)))
6666
{
6767
ReadOnlySpan<byte> dataHeader = br.ReadBytes(4);
6868
if (!dataHeader.SequenceEqual(DataHeader))
44 Bytes
Binary file not shown.

src/tests/Detach.Tests.Unit/Tests/Parsers/Sound/WavFormat/WaveParserTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public sealed class WaveParserTests
1212
[DataRow("Sample1.wav", (short)1, 11000, 11000, (short)1, (short)8, 2208, 2208, 0.2)]
1313
[DataRow("Sample2.wav", (short)1, 11025, 22050, (short)2, (short)16, 61544, 30772, 2.791)]
1414
[DataRow("Sample3.wav", (short)1, 44100, 88200, (short)2, (short)16, 18760, 9380, 0.221)]
15+
[DataRow("Empty.wav", (short)2, 44100, 176400, (short)4, (short)16, 0, 0, 0.000)]
1516
public void TestWaveParse(string fileName, short expectedChannels, int expectedSampleRate, int expectedByteRate, short expectedBlockAlign, short expectedBitsPerSample, int expectedDataSize, int expectedSampleCount, double expectedLengthInSeconds)
1617
{
1718
byte[] bytes = File.ReadAllBytes(ResourceUtils.GetResourcePath(fileName));

0 commit comments

Comments
 (0)