Fixed Mp3Loader and OggLoader not resetting stream position

This commit is contained in:
penev92
2022-01-04 23:58:55 +02:00
committed by Matthias Mailänder
parent 87b92b53a4
commit 6fb228ddd1
2 changed files with 30 additions and 14 deletions

View File

@@ -49,19 +49,27 @@ namespace OpenRA.Mods.Common.AudioLoaders
public Mp3Format(Stream stream)
{
mp3 = new MP3Stream(stream);
this.stream = stream;
// Make a first guess based on the file size and bitrate
// This should be fine for constant bitrate files
LengthInSeconds = mp3.Length * 8f / (2f * Channels * SampleRate);
var startPosition = stream.Position;
try
{
// Attempt to parse a more accurate length from the file metadata;
LengthInSeconds = (float)new TagLib.Mpeg.AudioFile(new StreamAbstraction(stream)).Properties.Duration.TotalSeconds;
mp3 = new MP3Stream(stream);
this.stream = stream;
// Make a first guess based on the file size and bitrate
// This should be fine for constant bitrate files
LengthInSeconds = mp3.Length * 8f / (2f * Channels * SampleRate);
try
{
// Attempt to parse a more accurate length from the file metadata;
LengthInSeconds = (float)new TagLib.Mpeg.AudioFile(new StreamAbstraction(stream)).Properties.Duration.TotalSeconds;
}
catch { }
}
finally
{
stream.Position = startPosition;
}
catch { }
}
Stream Clone(Mp3Format cloneFrom)

View File

@@ -49,15 +49,23 @@ namespace OpenRA.Mods.Common.AudioLoaders
public OggFormat(Stream stream)
{
this.stream = stream;
reader = new VorbisReader(stream);
LengthInSeconds = (float) reader.TotalTime.TotalSeconds;
var startPosition = stream.Position;
try
{
this.stream = stream;
reader = new VorbisReader(stream, false);
LengthInSeconds = (float)reader.TotalTime.TotalSeconds;
}
finally
{
stream.Position = startPosition;
}
}
OggFormat(OggFormat cloneFrom)
{
stream = SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length);
reader = new VorbisReader(stream)
reader = new VorbisReader(stream, false)
{
// Tell NVorbis to clip samples so we don't have to range-check during reading.
ClipSamples = true