Make the sound engine less dumb about music. Fix the music player not knowing about already playing tracks.

This commit is contained in:
Paul Chote
2011-05-18 18:58:33 +12:00
parent 42d8722cbc
commit c76d2e37dc
5 changed files with 32 additions and 30 deletions

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Traits; using OpenRA.Traits;
using Tao.OpenAl; using Tao.OpenAl;
@@ -24,7 +25,7 @@ namespace OpenRA
static ISoundSource rawSource; static ISoundSource rawSource;
static ISound music; static ISound music;
static ISound video; static ISound video;
static string currentMusic; static MusicInfo currentMusic;
static ISoundSource LoadSound(string filename) static ISoundSource LoadSound(string filename)
{ {
@@ -106,28 +107,30 @@ namespace OpenRA
static Action OnMusicComplete; static Action OnMusicComplete;
public static bool MusicPlaying { get; private set; } public static bool MusicPlaying { get; private set; }
public static MusicInfo CurrentMusic { get { return currentMusic; } }
public static void PlayMusic(string name)
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; OnMusicComplete = then;
if (name == "" || name == null) if (m == currentMusic && music != null)
return;
if (name == currentMusic && music != null)
{ {
soundEngine.PauseSound(music, false); soundEngine.PauseSound(music, false);
return; return;
} }
StopMusic(); StopMusic();
currentMusic = name; currentMusic = m;
MusicPlaying = true; MusicPlaying = true;
var sound = sounds[name]; var sound = sounds[m.Filename];
music = soundEngine.Play2D(sound, false, true, float2.Zero, MusicVolume); music = soundEngine.Play2D(sound, false, true, float2.Zero, MusicVolume);
} }

View File

@@ -51,10 +51,10 @@ namespace OpenRA.Mods.RA
{ {
if (!Game.Settings.Game.ShellmapMusic || if (!Game.Settings.Game.ShellmapMusic ||
Info.Music == null || Info.Music == null ||
!Rules.Music[Info.Music].Exists) !Rules.Music.ContainsKey(Info.Music))
return; return;
Sound.PlayMusicThen(Rules.Music[Info.Music].Filename, () => LoopMusic()); Sound.PlayMusicThen(Rules.Music[Info.Music], () => LoopMusic());
} }
int ticks = 0; int ticks = 0;

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Cnc
Scripting.Media.PlayFMVFullscreen(w, "gdi1.vqa", Scripting.Media.PlayFMVFullscreen(w, "gdi1.vqa",
() => Scripting.Media.PlayFMVFullscreen(w, "landing.vqa", () => () => Scripting.Media.PlayFMVFullscreen(w, "landing.vqa", () =>
{ {
Sound.PlayMusic(Rules.Music["aoi"].Filename); Sound.PlayMusic(Rules.Music["aoi"]);
started = true; started = true;
})); }));
} }

View File

@@ -13,6 +13,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -22,10 +23,10 @@ namespace OpenRA.Mods.Cnc.Widgets
public class CncMusicPlayerLogic : IWidgetDelegate public class CncMusicPlayerLogic : IWidgetDelegate
{ {
bool installed; bool installed;
string currentSong = null; MusicInfo currentSong = null;
Widget panel; Widget panel;
string[] music; MusicInfo[] music;
string[] random; MusicInfo[] random;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CncMusicPlayerLogic([ObjectCreator.Param] Widget widget, public CncMusicPlayerLogic([ObjectCreator.Param] Widget widget,
@@ -34,7 +35,7 @@ namespace OpenRA.Mods.Cnc.Widgets
panel = widget.GetWidget("MUSIC_PANEL"); panel = widget.GetWidget("MUSIC_PANEL");
BuildMusicTable(panel); BuildMusicTable(panel);
currentSong = GetNextSong(); currentSong = Sound.CurrentMusic ?? GetNextSong();
installed = Rules.Music.Where(m => m.Value.Exists).Any(); installed = Rules.Music.Where(m => m.Value.Exists).Any();
Func<bool> noMusic = () => !installed; Func<bool> noMusic = () => !installed;
@@ -91,13 +92,12 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget<LabelWidget>("TIME_LABEL").GetText = () => (currentSong == null) ? "" : panel.GetWidget<LabelWidget>("TIME_LABEL").GetText = () => (currentSong == null) ? "" :
"{0:D2}:{1:D2} / {2:D2}:{3:D2}".F((int)Sound.MusicSeekPosition / 60, (int)Sound.MusicSeekPosition % 60, "{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) void BuildMusicTable(Widget panel)
{ {
music = Rules.Music.Where(a => a.Value.Exists) music = Rules.Music.Where(a => a.Value.Exists).Select(a => a.Value).ToArray();
.Select(a => a.Key).ToArray();
random = music.Shuffle(Game.CosmeticRandom).ToArray(); random = music.Shuffle(Game.CosmeticRandom).ToArray();
var ml = panel.GetWidget<ScrollPanelWidget>("MUSIC_LIST"); var ml = panel.GetWidget<ScrollPanelWidget>("MUSIC_LIST");
@@ -110,7 +110,7 @@ namespace OpenRA.Mods.Cnc.Widgets
currentSong = song; currentSong = song;
var item = ScrollItemWidget.Setup(itemTemplate, () => currentSong == song, () => { currentSong = song; Play(); }); var item = ScrollItemWidget.Setup(itemTemplate, () => currentSong == song, () => { currentSong = song; Play(); });
item.GetWidget<LabelWidget>("TITLE").GetText = () => Rules.Music[song].Title; item.GetWidget<LabelWidget>("TITLE").GetText = () => song.Title;
item.GetWidget<LabelWidget>("LENGTH").GetText = () => SongLengthLabel(song); item.GetWidget<LabelWidget>("LENGTH").GetText = () => SongLengthLabel(song);
ml.AddChild(item); ml.AddChild(item);
} }
@@ -122,7 +122,7 @@ namespace OpenRA.Mods.Cnc.Widgets
if (currentSong == null) if (currentSong == null)
return; return;
Sound.PlayMusicThen(Rules.Music[currentSong].Filename, () => Sound.PlayMusicThen(currentSong, () =>
{ {
if (!Game.Settings.Sound.Repeat) if (!Game.Settings.Sound.Repeat)
currentSong = GetNextSong(); currentSong = GetNextSong();
@@ -147,13 +147,12 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget("BUTTON_PLAY").Visible = true; 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, return "{0:D1}:{1:D2}".F(song.Length / 60, song.Length % 60);
Rules.Music[song].Length % 60);
} }
string GetNextSong() MusicInfo GetNextSong()
{ {
if (!music.Any()) if (!music.Any())
return null; return null;
@@ -163,14 +162,14 @@ namespace OpenRA.Mods.Cnc.Widgets
.Skip(1).FirstOrDefault() ?? songs.FirstOrDefault(); .Skip(1).FirstOrDefault() ?? songs.FirstOrDefault();
} }
string GetPrevSong() MusicInfo GetPrevSong()
{ {
if (!music.Any()) if (!music.Any())
return null; return null;
var songs = Game.Settings.Sound.Shuffle ? random : music; var songs = Game.Settings.Sound.Shuffle ? random : music;
return songs.Reverse().SkipWhile(m => m != currentSong) return songs.Reverse().SkipWhile(m => m != currentSong)
.Skip(1).FirstOrDefault() ?? songs.FirstOrDefault(); .Skip(1).FirstOrDefault() ?? songs.Reverse().FirstOrDefault();
} }
} }

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
if (CurrentSong == null) if (CurrentSong == null)
return true; 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(Game.Settings.Sound.Repeat ? "BUTTON_PLAY" : "BUTTON_NEXT").OnMouseUp(new MouseInput()));
bg.GetWidget("BUTTON_PLAY").Visible = false; bg.GetWidget("BUTTON_PLAY").Visible = false;
bg.GetWidget("BUTTON_PAUSE").Visible = true; bg.GetWidget("BUTTON_PAUSE").Visible = true;