diff --git a/OpenRA.FileFormats/FileFormats/AudLoader.cs b/OpenRA.FileFormats/FileFormats/AudLoader.cs index fe3abca8fe..2717945aa4 100644 --- a/OpenRA.FileFormats/FileFormats/AudLoader.cs +++ b/OpenRA.FileFormats/FileFormats/AudLoader.cs @@ -102,6 +102,20 @@ namespace OpenRA.FileFormats return output; } + public static float SoundLength(Stream s) + { + var br = new BinaryReader(s); + var sampleRate = br.ReadUInt16(); + /*var dataSize = */ br.ReadInt32(); + var outputSize = br.ReadInt32(); + var flags = (SoundFlags) br.ReadByte(); + + var samples = outputSize; + if (0 != (flags & SoundFlags.Stereo)) samples /= 2; + if (0 != (flags & SoundFlags._16Bit)) samples /= 2; + return samples / sampleRate; + } + public static byte[] LoadSound(Stream s) { var br = new BinaryReader(s); diff --git a/OpenRA.Game/GameRules/MusicInfo.cs b/OpenRA.Game/GameRules/MusicInfo.cs index 049bab7239..ca3a31d4ea 100644 --- a/OpenRA.Game/GameRules/MusicInfo.cs +++ b/OpenRA.Game/GameRules/MusicInfo.cs @@ -14,15 +14,21 @@ namespace OpenRA.GameRules { public class MusicInfo { - [FieldLoader.Load] public readonly string Filename = null; - [FieldLoader.Load] public readonly string Title = null; - [FieldLoader.Load] public readonly int Length = 0; // seconds + public readonly string Filename = null; + public readonly string Title = null; + public readonly int Length = 0; // seconds + public readonly bool Exists = false; public MusicInfo( string key, MiniYaml value ) { - FieldLoader.Load(this, value); - if (Filename == null) - Filename = key+".aud"; + Filename = key+".aud"; + Title = value.Value; + + if (!FileSystem.Exists(Filename)) + return; + + Exists = true; + Length = (int)AudLoader.SoundLength(FileSystem.Open(Filename)); } } } diff --git a/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs b/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs index 723133ae6c..1e616bb203 100644 --- a/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs @@ -86,12 +86,9 @@ namespace OpenRA.Widgets.Delegates var itemTemplate = ml.GetWidget("MUSIC_TEMPLATE"); int offset = itemTemplate.Bounds.Y; - foreach (var kv in Rules.Music) + foreach (var kv in Rules.Music.Where(m => m.Value.Exists)) { var song = kv.Key; - if (!FileSystem.Exists(Rules.Music[song].Filename)) - continue; - if (CurrentSong == null) CurrentSong = song; diff --git a/mods/cnc/music.yaml b/mods/cnc/music.yaml index 6fdcf19653..e0453e8a27 100644 --- a/mods/cnc/music.yaml +++ b/mods/cnc/music.yaml @@ -1,3 +1 @@ -aoi: - Title: Act on Instinct - Length: 0 \ No newline at end of file +aoi: Act on Instinct \ No newline at end of file diff --git a/mods/ra/music.yaml b/mods/ra/music.yaml index 4a75a60c40..c95ce2b197 100644 --- a/mods/ra/music.yaml +++ b/mods/ra/music.yaml @@ -1,83 +1,21 @@ -await_r: - Title: Afterlife - Length: 0 - -bigf225m: - Title: Bigfoot - Length: 0 - -credits: - Title: End Credits - Length: 0 - -crus226m: - Title: Crush - Length: 0 - -dense_r: - Title: Dense - Length: 0 - -fac1225m: - Title: Face to the Enemy 1 - Length: 0 - -fac2226m: - Title: Face to the Enemy 2 - Length: 0 - -fogger1a: - Title: Fogger - Length: 0 - -hell225m: - Title: Hell March - Length: 0 - -mud1a: - Title: Mud - Length: 0 - -radio2: - Title: Radio 2 - Length: 0 - -rollout: - Title: Roll Out - Length: 0 - -run1225m: - Title: Run for your Life - Length: 0 - -score: - Title: Mission Accomplished - Length: 0 - -smsh225m: - Title: Smash - Length: 0 - -snake: - Title: Snake - Length: 0 - -terminat: - Title: Terminate - Length: 0 - -tren226m: - Title: Trenches - Length: 0 - -twin: - Title: Twin - Length: 0 - -vector1a: - Title: Vector - Length: 0 - -work226m: - Title: Workmen - Length: 0 \ No newline at end of file +await_r: Afterlife +bigf226m: Bigfoot +credits: End Credits +crus226m: Crush +dense_r: Dense +fac1226m: Face to the Enemy 1 +fac2226m: Face to the Enemy 2 +fogger1a: Fogger +hell226m: Hell March +mud1a: Mud +radio2: Radio 2 +rollout: Roll Out +run1226m: Run for your Life +score: Mission Accomplished +smsh226m: Smash +snake: Snake +terminat: Terminate +tren226m: Trenches +twin: Twin +vector1a: Vector +work226m: Workmen \ No newline at end of file