Reworked ISoundFormat.LengthInSeconds implementations

This commit is contained in:
penev92
2022-01-04 23:13:56 +02:00
committed by Matthias Mailänder
parent 631297417c
commit 87b92b53a4
7 changed files with 21 additions and 59 deletions

View File

@@ -37,10 +37,10 @@ namespace OpenRA.Mods.Common.AudioLoaders
public sealed class Mp3Format : ISoundFormat
{
public int Channels { get { return mp3.ChannelCount; } }
public int SampleBits { get { return 16; } }
public int SampleRate { get { return mp3.Frequency; } }
public float LengthInSeconds { get; private set; }
public int Channels => mp3.ChannelCount;
public int SampleBits => 16;
public int SampleRate => mp3.Frequency;
public float LengthInSeconds { get; }
public Stream GetPCMInputStream() { return new MP3Stream(Clone(this)); }
public void Dispose() { mp3.Dispose(); }

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.AudioLoaders
public int SampleBits => 16;
public int Channels => reader.Channels;
public int SampleRate => reader.SampleRate;
public float LengthInSeconds => (float)reader.TotalTime.TotalSeconds;
public float LengthInSeconds { get; }
public Stream GetPCMInputStream() { return new OggStream(new OggFormat(this)); }
public void Dispose() { reader.Dispose(); }
@@ -51,6 +51,7 @@ namespace OpenRA.Mods.Common.AudioLoaders
{
this.stream = stream;
reader = new VorbisReader(stream);
LengthInSeconds = (float) reader.TotalTime.TotalSeconds;
}
OggFormat(OggFormat cloneFrom)

View File

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

View File

@@ -20,12 +20,13 @@ namespace OpenRA.Mods.Common.FileFormats
{
enum WaveType { Pcm = 0x1, MsAdpcm = 0x2, ImaAdpcm = 0x11 }
public static bool LoadSound(Stream s, out Func<Stream> result, out short channels, out int sampleBits, out int sampleRate)
public static bool LoadSound(Stream s, out Func<Stream> result, out short channels, out int sampleBits, out int sampleRate, out float lengthInSeconds)
{
result = null;
channels = -1;
sampleBits = -1;
sampleRate = -1;
lengthInSeconds = -1;
var type = s.ReadASCII(4);
if (type != "RIFF")
@@ -65,6 +66,7 @@ namespace OpenRA.Mods.Common.FileFormats
s.ReadInt32(); // Byte Rate
blockAlign = s.ReadInt16();
sampleBits = s.ReadInt16();
lengthInSeconds = (float)(s.Length * 8) / (channels * sampleRate * sampleBits);
s.ReadBytes(fmtChunkSize - 16);
break;
case "fact":
@@ -107,25 +109,6 @@ namespace OpenRA.Mods.Common.FileFormats
return true;
}
public static float WaveLength(Stream s)
{
s.Position = 12;
var fmt = s.ReadASCII(4);
if (fmt != "fmt ")
return 0;
s.Position = 22;
var channels = s.ReadInt16();
var sampleRate = s.ReadInt32();
s.Position = 34;
var bitsPerSample = s.ReadInt16();
var length = s.Length * 8;
return (float)length / (channels * sampleRate * bitsPerSample);
}
sealed class WavStreamImaAdpcm : ReadOnlyAdapterStream
{
readonly short channels;