Implemented MP3 detection.

This commit is contained in:
IceReaper
2023-03-31 08:50:21 +02:00
committed by Matthias Mailänder
parent 138715b509
commit 4dd532f60e

View File

@@ -18,12 +18,37 @@ namespace OpenRA.Mods.Common.AudioLoaders
{ {
public class Mp3Loader : ISoundLoader public class Mp3Loader : ISoundLoader
{ {
bool IsMp3(Stream s)
{
var start = s.Position;
// First try: MP3 may have ID3 meta data in front.
var idTag = s.ReadASCII(3);
s.Position = start;
if (idTag == "ID3")
return true;
// Second try: MP3 without metadata, starts with MPEG chunk.
var frameSync = s.ReadUInt16();
s.Position = start;
if (frameSync == 0xfbff)
return true;
// Neither found, not an MP3!
return false;
}
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound) bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
{ {
try try
{ {
sound = new Mp3Format(stream); if (IsMp3(stream))
return true; {
sound = new Mp3Format(stream);
return true;
}
} }
catch catch
{ {