Fixed AudFormat and WavFormat implementations of ISoundFormat.LengthInSeconds

- Fixed a rounding issue in `WavReader.WaveLength()`.
- Fixed `AudReader.SoundLength()` not resetting the stream position.
- Fixed crashes caused by disposed streams because `LengthInSeconds` would try and calculate the length on the fly. It is now precalculated and cached (making it consistent across all 5 current `ISoundFormat` implementations).
- Fixed a crash in `AudReader.LoadSound()`'s `out Func<Stream> result` because that func would try and access the disposed stream's `Length` property. That works for `SegmentStream`, but not for `FileStream`.
- Fixed frameCount/soundLength label positioning in the AssetBrowser window to avoid text clipping .
This commit is contained in:
penev92
2021-02-21 15:10:59 +02:00
committed by Matthias Mailänder
parent 8b944e9c82
commit abea3a0f74
6 changed files with 16 additions and 6 deletions

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
public int Channels => channels; public int Channels => channels;
public int SampleBits => sampleBits; public int SampleBits => sampleBits;
public int SampleRate => sampleRate; public int SampleRate => sampleRate;
public float LengthInSeconds => AudReader.SoundLength(sourceStream); public float LengthInSeconds { get; }
public Stream GetPCMInputStream() { return audStreamFactory(); } public Stream GetPCMInputStream() { return audStreamFactory(); }
public void Dispose() { sourceStream.Dispose(); } public void Dispose() { sourceStream.Dispose(); }
@@ -68,6 +68,8 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
if (!AudReader.LoadSound(stream, out audStreamFactory, out sampleRate, out sampleBits, out channels)) if (!AudReader.LoadSound(stream, out audStreamFactory, out sampleRate, out sampleBits, out channels))
throw new InvalidDataException(); throw new InvalidDataException();
LengthInSeconds = AudReader.SoundLength(sourceStream);
} }
} }
} }

View File

@@ -34,6 +34,8 @@ namespace OpenRA.Mods.Cnc.FileFormats
{ {
public static float SoundLength(Stream s) public static float SoundLength(Stream s)
{ {
var originalPosition = s.Position;
var sampleRate = s.ReadUInt16(); var sampleRate = s.ReadUInt16();
/*var dataSize = */ s.ReadInt32(); /*var dataSize = */ s.ReadInt32();
var outputSize = s.ReadInt32(); var outputSize = s.ReadInt32();
@@ -46,6 +48,8 @@ namespace OpenRA.Mods.Cnc.FileFormats
if ((flags & SoundFlags._16Bit) != 0) if ((flags & SoundFlags._16Bit) != 0)
samples /= 2; samples /= 2;
s.Seek(originalPosition, SeekOrigin.Begin);
return (float)samples / sampleRate; return (float)samples / sampleRate;
} }
@@ -70,10 +74,12 @@ namespace OpenRA.Mods.Cnc.FileFormats
throw new NotImplementedException(); throw new NotImplementedException();
var offsetPosition = s.Position; var offsetPosition = s.Position;
var streamLength = s.Length;
var segmentLength = (int)(streamLength - offsetPosition);
result = () => result = () =>
{ {
var audioStream = SegmentStream.CreateWithoutOwningStream(s, offsetPosition, (int)(s.Length - offsetPosition)); var audioStream = SegmentStream.CreateWithoutOwningStream(s, offsetPosition, segmentLength);
return new AudStream(audioStream, outputSize, dataSize); return new AudStream(audioStream, outputSize, dataSize);
}; };
} }

View File

@@ -53,7 +53,7 @@ namespace OpenRA.Mods.Common.AudioLoaders
public int Channels => channels; public int Channels => channels;
public int SampleBits => sampleBits; public int SampleBits => sampleBits;
public int SampleRate => sampleRate; public int SampleRate => sampleRate;
public float LengthInSeconds => WavReader.WaveLength(sourceStream); public float LengthInSeconds { get; }
public Stream GetPCMInputStream() { return wavStreamFactory(); } public Stream GetPCMInputStream() { return wavStreamFactory(); }
public void Dispose() { sourceStream.Dispose(); } public void Dispose() { sourceStream.Dispose(); }
@@ -69,6 +69,8 @@ namespace OpenRA.Mods.Common.AudioLoaders
if (!WavReader.LoadSound(stream, out wavStreamFactory, out channels, out sampleBits, out sampleRate)) if (!WavReader.LoadSound(stream, out wavStreamFactory, out channels, out sampleBits, out sampleRate))
throw new InvalidDataException(); throw new InvalidDataException();
LengthInSeconds = WavReader.WaveLength(sourceStream);
} }
} }
} }

View File

@@ -123,7 +123,7 @@ namespace OpenRA.Mods.Common.FileFormats
var bitsPerSample = s.ReadInt16(); var bitsPerSample = s.ReadInt16();
var length = s.Length * 8; var length = s.Length * 8;
return length / (channels * sampleRate * bitsPerSample); return (float)length / (channels * sampleRate * bitsPerSample);
} }
sealed class WavStreamImaAdpcm : ReadOnlyAdapterStream sealed class WavStreamImaAdpcm : ReadOnlyAdapterStream

View File

@@ -190,7 +190,7 @@ Container@ASSETBROWSER_PANEL:
MinimumValue: 0 MinimumValue: 0
Label@FRAME_COUNT: Label@FRAME_COUNT:
X: PARENT_RIGHT - WIDTH + 5 X: PARENT_RIGHT - WIDTH + 5
Y: 1 Y: 0
Width: 80 Width: 80
Height: 25 Height: 25
Font: TinyBold Font: TinyBold

View File

@@ -180,7 +180,7 @@ Background@ASSETBROWSER_PANEL:
MinimumValue: 0 MinimumValue: 0
Label@FRAME_COUNT: Label@FRAME_COUNT:
X: PARENT_RIGHT - WIDTH + 5 X: PARENT_RIGHT - WIDTH + 5
Y: 1 Y: 0
Width: 85 Width: 85
Height: 25 Height: 25
Font: TinyBold Font: TinyBold