Parse Mp3 length from metadata tags.

This commit is contained in:
Paul Chote
2021-07-05 23:27:53 +01:00
committed by abcdefg30
parent 407372268d
commit 25c095619a
3 changed files with 33 additions and 1 deletions

View File

@@ -179,6 +179,9 @@ Nicholas Terry distributed under the MIT license.
Using MP3Sharp by Robert Bruke and Zane Wagner Using MP3Sharp by Robert Bruke and Zane Wagner
licensed under the GNU LGPL Version 3. licensed under the GNU LGPL Version 3.
Using TagLib# by Stephen Shaw licensed under the
GNU LGPL Version 2.1.
Using ICSharpCode.SharpZipLib initially by Mike Using ICSharpCode.SharpZipLib initially by Mike
Krueger and distributed under the GNU GPL terms. Krueger and distributed under the GNU GPL terms.

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.IO; using System.IO;
using MP3Sharp; using MP3Sharp;
using OpenRA.Primitives; using OpenRA.Primitives;
@@ -39,7 +40,7 @@ namespace OpenRA.Mods.Common.AudioLoaders
public int Channels { get { return mp3.ChannelCount; } } public int Channels { get { return mp3.ChannelCount; } }
public int SampleBits { get { return 16; } } public int SampleBits { get { return 16; } }
public int SampleRate { get { return mp3.Frequency; } } public int SampleRate { get { return mp3.Frequency; } }
public float LengthInSeconds { get { return mp3.Length * 8f / (2f * Channels * SampleRate); } } public float LengthInSeconds { get; private set; }
public Stream GetPCMInputStream() { return new MP3Stream(Clone(this)); } public Stream GetPCMInputStream() { return new MP3Stream(Clone(this)); }
public void Dispose() { mp3.Dispose(); } public void Dispose() { mp3.Dispose(); }
@@ -50,11 +51,38 @@ namespace OpenRA.Mods.Common.AudioLoaders
{ {
mp3 = new MP3Stream(stream); mp3 = new MP3Stream(stream);
this.stream = 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 { }
} }
Stream Clone(Mp3Format cloneFrom) Stream Clone(Mp3Format cloneFrom)
{ {
return SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length); return SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length);
} }
public class StreamAbstraction : TagLib.File.IFileAbstraction
{
readonly Stream s;
public StreamAbstraction(Stream s)
{
this.s = s;
}
public Stream ReadStream => s;
public Stream WriteStream => throw new NotImplementedException();
public void CloseStream(Stream stream) { }
public string Name => "";
}
} }
} }

View File

@@ -7,6 +7,7 @@
<PackageReference Include="OpenRA-FuzzyLogicLibrary" Version="1.0.1" /> <PackageReference Include="OpenRA-FuzzyLogicLibrary" Version="1.0.1" />
<PackageReference Include="DiscordRichPresence" Version="1.0.150" /> <PackageReference Include="DiscordRichPresence" Version="1.0.150" />
<PackageReference Include="MP3Sharp" Version="1.0.5" /> <PackageReference Include="MP3Sharp" Version="1.0.5" />
<PackageReference Include="TagLibSharp" Version="2.2.0" />
<PackageReference Include="rix0rrr.BeaconLib" Version="1.0.2" /> <PackageReference Include="rix0rrr.BeaconLib" Version="1.0.2" />
<PackageReference Include="Pfim" Version="0.9.1" /> <PackageReference Include="Pfim" Version="0.9.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="System.Net.Http" Version="4.3.4" />