From 4dd532f60e64d2dc4fb943cd0928da8af3fcf976 Mon Sep 17 00:00:00 2001 From: IceReaper Date: Fri, 31 Mar 2023 08:50:21 +0200 Subject: [PATCH] Implemented MP3 detection. --- OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs | 29 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs b/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs index 516fd2e80c..895dfb88fd 100644 --- a/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs @@ -18,12 +18,37 @@ namespace OpenRA.Mods.Common.AudioLoaders { 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) { try { - sound = new Mp3Format(stream); - return true; + if (IsMp3(stream)) + { + sound = new Mp3Format(stream); + return true; + } } catch {