From c76d2e37dc0e63104dbaa13e4b68f6f5ecfebdd0 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 18 May 2011 18:58:33 +1200 Subject: [PATCH] Make the sound engine less dumb about music. Fix the music player not knowing about already playing tracks. --- OpenRA.Game/Sound.cs | 25 +++++++++------- OpenRA.Mods.Cnc/Missions/CncShellmapScript.cs | 4 +-- OpenRA.Mods.Cnc/Missions/Gdi01Script.cs | 2 +- .../Widgets/CncMusicPlayerLogic.cs | 29 +++++++++---------- .../Widgets/Delegates/MusicPlayerDelegate.cs | 2 +- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/OpenRA.Game/Sound.cs b/OpenRA.Game/Sound.cs index 59ef4ee745..fc552aaba1 100644 --- a/OpenRA.Game/Sound.cs +++ b/OpenRA.Game/Sound.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; using System.Linq; using OpenRA.FileFormats; +using OpenRA.GameRules; using OpenRA.Traits; using Tao.OpenAl; @@ -24,7 +25,7 @@ namespace OpenRA static ISoundSource rawSource; static ISound music; static ISound video; - static string currentMusic; + static MusicInfo currentMusic; static ISoundSource LoadSound(string filename) { @@ -106,28 +107,30 @@ namespace OpenRA static Action OnMusicComplete; public static bool MusicPlaying { get; private set; } - - public static void PlayMusic(string name) + public static MusicInfo CurrentMusic { get { return currentMusic; } } + + public static void PlayMusic(MusicInfo m) { - PlayMusicThen(name, () => { }); + PlayMusicThen(m, () => { }); } - public static void PlayMusicThen(string name, Action then) + + public static void PlayMusicThen(MusicInfo m, Action then) { + if (m == null || !m.Exists) + return; + OnMusicComplete = then; - if (name == "" || name == null) - return; - - if (name == currentMusic && music != null) + if (m == currentMusic && music != null) { soundEngine.PauseSound(music, false); return; } StopMusic(); - currentMusic = name; + currentMusic = m; MusicPlaying = true; - var sound = sounds[name]; + var sound = sounds[m.Filename]; music = soundEngine.Play2D(sound, false, true, float2.Zero, MusicVolume); } diff --git a/OpenRA.Mods.Cnc/Missions/CncShellmapScript.cs b/OpenRA.Mods.Cnc/Missions/CncShellmapScript.cs index aa161972d3..8ac1f78d30 100755 --- a/OpenRA.Mods.Cnc/Missions/CncShellmapScript.cs +++ b/OpenRA.Mods.Cnc/Missions/CncShellmapScript.cs @@ -51,10 +51,10 @@ namespace OpenRA.Mods.RA { if (!Game.Settings.Game.ShellmapMusic || Info.Music == null || - !Rules.Music[Info.Music].Exists) + !Rules.Music.ContainsKey(Info.Music)) return; - Sound.PlayMusicThen(Rules.Music[Info.Music].Filename, () => LoopMusic()); + Sound.PlayMusicThen(Rules.Music[Info.Music], () => LoopMusic()); } int ticks = 0; diff --git a/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs b/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs index 2d957dba7b..a6de2970f5 100644 --- a/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs +++ b/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs @@ -35,7 +35,7 @@ namespace OpenRA.Mods.Cnc Scripting.Media.PlayFMVFullscreen(w, "gdi1.vqa", () => Scripting.Media.PlayFMVFullscreen(w, "landing.vqa", () => { - Sound.PlayMusic(Rules.Music["aoi"].Filename); + Sound.PlayMusic(Rules.Music["aoi"]); started = true; })); } diff --git a/OpenRA.Mods.Cnc/Widgets/CncMusicPlayerLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncMusicPlayerLogic.cs index 8874a06c04..bd38944cf2 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncMusicPlayerLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncMusicPlayerLogic.cs @@ -13,6 +13,7 @@ using System.IO; using System.Linq; using System.Threading; using OpenRA.FileFormats; +using OpenRA.GameRules; using OpenRA.Graphics; using OpenRA.Traits; using OpenRA.Widgets; @@ -22,10 +23,10 @@ namespace OpenRA.Mods.Cnc.Widgets public class CncMusicPlayerLogic : IWidgetDelegate { bool installed; - string currentSong = null; + MusicInfo currentSong = null; Widget panel; - string[] music; - string[] random; + MusicInfo[] music; + MusicInfo[] random; [ObjectCreator.UseCtor] public CncMusicPlayerLogic([ObjectCreator.Param] Widget widget, @@ -34,7 +35,7 @@ namespace OpenRA.Mods.Cnc.Widgets panel = widget.GetWidget("MUSIC_PANEL"); BuildMusicTable(panel); - currentSong = GetNextSong(); + currentSong = Sound.CurrentMusic ?? GetNextSong(); installed = Rules.Music.Where(m => m.Value.Exists).Any(); Func noMusic = () => !installed; @@ -91,13 +92,12 @@ namespace OpenRA.Mods.Cnc.Widgets panel.GetWidget("TIME_LABEL").GetText = () => (currentSong == null) ? "" : "{0:D2}:{1:D2} / {2:D2}:{3:D2}".F((int)Sound.MusicSeekPosition / 60, (int)Sound.MusicSeekPosition % 60, - Rules.Music[currentSong].Length / 60, Rules.Music[currentSong].Length % 60); + currentSong.Length / 60, currentSong.Length % 60); } void BuildMusicTable(Widget panel) { - music = Rules.Music.Where(a => a.Value.Exists) - .Select(a => a.Key).ToArray(); + music = Rules.Music.Where(a => a.Value.Exists).Select(a => a.Value).ToArray(); random = music.Shuffle(Game.CosmeticRandom).ToArray(); var ml = panel.GetWidget("MUSIC_LIST"); @@ -110,7 +110,7 @@ namespace OpenRA.Mods.Cnc.Widgets currentSong = song; var item = ScrollItemWidget.Setup(itemTemplate, () => currentSong == song, () => { currentSong = song; Play(); }); - item.GetWidget("TITLE").GetText = () => Rules.Music[song].Title; + item.GetWidget("TITLE").GetText = () => song.Title; item.GetWidget("LENGTH").GetText = () => SongLengthLabel(song); ml.AddChild(item); } @@ -122,7 +122,7 @@ namespace OpenRA.Mods.Cnc.Widgets if (currentSong == null) return; - Sound.PlayMusicThen(Rules.Music[currentSong].Filename, () => + Sound.PlayMusicThen(currentSong, () => { if (!Game.Settings.Sound.Repeat) currentSong = GetNextSong(); @@ -147,13 +147,12 @@ namespace OpenRA.Mods.Cnc.Widgets panel.GetWidget("BUTTON_PLAY").Visible = true; } - string SongLengthLabel(string song) + string SongLengthLabel(MusicInfo song) { - return "{0:D1}:{1:D2}".F(Rules.Music[song].Length / 60, - Rules.Music[song].Length % 60); + return "{0:D1}:{1:D2}".F(song.Length / 60, song.Length % 60); } - string GetNextSong() + MusicInfo GetNextSong() { if (!music.Any()) return null; @@ -163,14 +162,14 @@ namespace OpenRA.Mods.Cnc.Widgets .Skip(1).FirstOrDefault() ?? songs.FirstOrDefault(); } - string GetPrevSong() + MusicInfo GetPrevSong() { if (!music.Any()) return null; var songs = Game.Settings.Sound.Shuffle ? random : music; return songs.Reverse().SkipWhile(m => m != currentSong) - .Skip(1).FirstOrDefault() ?? songs.FirstOrDefault(); + .Skip(1).FirstOrDefault() ?? songs.Reverse().FirstOrDefault(); } } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/MusicPlayerDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/MusicPlayerDelegate.cs index e6ac51bf3f..0283b07dee 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/MusicPlayerDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/MusicPlayerDelegate.cs @@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates if (CurrentSong == null) return true; - Sound.PlayMusicThen(Rules.Music[CurrentSong].Filename, + Sound.PlayMusicThen(Rules.Music[CurrentSong], () => bg.GetWidget(Game.Settings.Sound.Repeat ? "BUTTON_PLAY" : "BUTTON_NEXT").OnMouseUp(new MouseInput())); bg.GetWidget("BUTTON_PLAY").Visible = false; bg.GetWidget("BUTTON_PAUSE").Visible = true;