Backend changes for new music player

This commit is contained in:
Paul Chote
2010-08-13 19:46:13 +12:00
parent de12233edc
commit 3f3ac377b2
6 changed files with 254 additions and 111 deletions

View File

@@ -13,38 +13,16 @@ using OpenRA.FileFormats;
namespace OpenRA.GameRules namespace OpenRA.GameRules
{ {
public class MusicInfo public class MusicInfo
{ {
public readonly MusicPool Pool; public readonly string Filename = null;
public readonly string[] Music = { }; public readonly string Title = null;
public readonly float Length = 0; // seconds
public MusicInfo( MiniYaml y ) public MusicInfo( string key, MiniYaml value )
{ {
FieldLoader.Load(this, y); FieldLoader.Load(this, value);
Pool = new MusicPool(Music); if (Filename == null)
Filename = key+".aud";
} }
} }
public class MusicPool
{
readonly string[] clips;
int playing = 0;
public MusicPool(params string[] clips)
{
this.clips = clips;
}
public string GetNext()
{
playing = (playing + 1) % clips.Length;
return clips[playing];
}
public string GetPrev()
{
playing = (playing + clips.Length - 1) % clips.Length;
return clips[playing];
}
public string GetCurrent(){ return clips[playing];}
}
} }

View File

@@ -32,7 +32,7 @@ namespace OpenRA
Info = LoadYamlRules(m.Rules, map.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y)); Info = LoadYamlRules(m.Rules, map.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y));
Weapons = LoadYamlRules(m.Weapons, map.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value)); Weapons = LoadYamlRules(m.Weapons, map.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value));
Voices = LoadYamlRules(m.Voices, map.Voices, (k, _) => new VoiceInfo(k.Value)); Voices = LoadYamlRules(m.Voices, map.Voices, (k, _) => new VoiceInfo(k.Value));
Music = LoadYamlRules(m.Music, map.Music, (k, _) => new MusicInfo(k.Value)); Music = LoadYamlRules(m.Music, map.Music, (k, _) => new MusicInfo(k.Key, k.Value));
Movies = LoadYamlRules(m.Movies, new Dictionary<string,MiniYaml>(), (k, v) => k.Value.Value); Movies = LoadYamlRules(m.Movies, new Dictionary<string,MiniYaml>(), (k, v) => k.Value.Value);
TileSets = new Dictionary<string, TileSet>(); TileSets = new Dictionary<string, TileSet>();

View File

@@ -21,11 +21,10 @@ namespace OpenRA
{ {
static ISoundEngine soundEngine; static ISoundEngine soundEngine;
static Cache<string, ISoundSource> sounds; static Cache<string, ISoundSource> sounds;
static ISoundSource rawSource;
static ISound music; static ISound music;
static ISound video; static ISound video;
static string currentMusic;
static bool paused;
static bool stopped;
static ISoundSource LoadSound(string filename) static ISoundSource LoadSound(string filename)
{ {
@@ -43,26 +42,12 @@ namespace OpenRA
soundEngine = new OpenAlSoundEngine(); soundEngine = new OpenAlSoundEngine();
sounds = new Cache<string, ISoundSource>(LoadSound); sounds = new Cache<string, ISoundSource>(LoadSound);
music = null; music = null;
currentMusic = null;
video = null; video = null;
paused = false;
stopped = false;
} }
public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); } public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); }
static ISoundSource rawSource;
public static void PlayVideoSoundtrack(byte[] raw)
{
rawSource = LoadSoundRaw(raw);
video = soundEngine.Play2D(rawSource, false, true, float2.Zero, SoundVolume);
}
public static void StopVideoSoundtrack()
{
if (video != null)
soundEngine.StopSound(video);
}
public static void Play(string name) public static void Play(string name)
{ {
if (name == "" || name == null) if (name == "" || name == null)
@@ -93,36 +78,47 @@ namespace OpenRA
Play(name, pos); Play(name, pos);
} }
public static void PlayVideoSoundtrack(byte[] raw)
{
rawSource = LoadSoundRaw(raw);
video = soundEngine.Play2D(rawSource, false, true, float2.Zero, SoundVolume);
}
public static void StopVideoSoundtrack()
{
if (video != null)
soundEngine.StopSound(video);
}
public static void PlayMusic(string name) public static void PlayMusic(string name)
{ {
if (name == "" || name == null) if (name == "" || name == null)
return; return;
if (music != null) if (name == currentMusic && music != null)
soundEngine.StopSound(music); {
soundEngine.PauseSound(music, false);
return;
}
StopMusic();
currentMusic = name;
var sound = sounds[name]; var sound = sounds[name];
music = soundEngine.Play2D(sound, true, true, float2.Zero, MusicVolume); music = soundEngine.Play2D(sound, true, true, float2.Zero, MusicVolume);
} }
public static bool MusicPaused public static void StopMusic()
{ {
get { return paused; } if (music != null)
set { soundEngine.StopSound(music);
paused = value;
if (music != null) currentMusic = null;
soundEngine.PauseSound(music, paused);
}
} }
public static bool MusicStopped public static void PauseMusic()
{ {
get { return stopped; } if (music != null)
set { soundEngine.PauseSound(music, true);
stopped = value;
if (music != null && stopped)
soundEngine.StopSound(music);
}
} }
public static float GlobalVolume public static float GlobalVolume

View File

@@ -1,3 +1,5 @@
using System.Linq;
using OpenRA.FileFormats;
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS) * Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
@@ -12,85 +14,86 @@ namespace OpenRA.Widgets.Delegates
{ {
public class MusicPlayerDelegate : IWidgetDelegate public class MusicPlayerDelegate : IWidgetDelegate
{ {
string CurrentSong = null;
public MusicPlayerDelegate() public MusicPlayerDelegate()
{ {
var bg = Widget.RootWidget.GetWidget("MUSIC_BG"); var bg = Widget.RootWidget.GetWidget("MUSIC_BG");
bg.Visible = Game.Settings.MusicPlayer; bg.Visible = Game.Settings.MusicPlayer;
CurrentSong = GetNextSong();
bg.GetWidget("BUTTON_PLAY").OnMouseUp = mi => bg.GetWidget("BUTTON_PLAY").OnMouseUp = mi =>
{ {
var foo = Widget.RootWidget.GetWidget<VqaPlayerWidget>("VIDEOPLAYER"); if (CurrentSong == null)
foo.Load("intro2.vqa"); return true;
foo.Play();
/* Sound.PlayMusic(Rules.Music[CurrentSong].Filename);
if (Sound.MusicStopped)
Sound.PlayMusic(GetSong());
Sound.MusicStopped = false;
Sound.MusicPaused = false;
bg.GetWidget("BUTTON_PLAY").Visible = false; bg.GetWidget("BUTTON_PLAY").Visible = false;
bg.GetWidget("BUTTON_PAUSE").Visible = true; bg.GetWidget("BUTTON_PAUSE").Visible = true;
*/
return true; return true;
}; };
/*
bg.GetWidget("BUTTON_PAUSE").OnMouseUp = mi => bg.GetWidget("BUTTON_PAUSE").OnMouseUp = mi =>
{ {
Sound.MusicPaused = true; Sound.PauseMusic();
bg.GetWidget("BUTTON_PAUSE").Visible = false; bg.GetWidget("BUTTON_PAUSE").Visible = false;
bg.GetWidget("BUTTON_PLAY").Visible = true; bg.GetWidget("BUTTON_PLAY").Visible = true;
return true; return true;
}; };
*/
bg.GetWidget("BUTTON_STOP").OnMouseUp = mi => bg.GetWidget("BUTTON_STOP").OnMouseUp = mi =>
{ {
var foo = Widget.RootWidget.GetWidget<VqaPlayerWidget>("VIDEOPLAYER"); Sound.StopMusic();
foo.Stop();
/*
Sound.MusicStopped = true;
bg.GetWidget("BUTTON_PAUSE").Visible = false; bg.GetWidget("BUTTON_PAUSE").Visible = false;
bg.GetWidget("BUTTON_PLAY").Visible = true; bg.GetWidget("BUTTON_PLAY").Visible = true;
*/
return true; return true;
}; };
/*
bg.GetWidget("BUTTON_NEXT").OnMouseUp = mi => bg.GetWidget("BUTTON_NEXT").OnMouseUp = mi =>
{ {
Sound.PlayMusic(GetNextSong()); CurrentSong = GetNextSong();
Sound.MusicStopped = false; return bg.GetWidget("BUTTON_PLAY").OnMouseUp(mi);
Sound.MusicPaused = false;
bg.GetWidget("BUTTON_PLAY").Visible = false;
bg.GetWidget("BUTTON_PAUSE").Visible = true;
return true;
}; };
bg.GetWidget("BUTTON_PREV").OnMouseUp = mi => bg.GetWidget("BUTTON_PREV").OnMouseUp = mi =>
{ {
Sound.PlayMusic(GetPrevSong()); CurrentSong = GetPrevSong();
Sound.MusicStopped = false; return bg.GetWidget("BUTTON_PLAY").OnMouseUp(mi);
Sound.MusicPaused = false;
bg.GetWidget("BUTTON_PLAY").Visible = false;
bg.GetWidget("BUTTON_PAUSE").Visible = true;
return true;
}; };
*/
} }
string GetNextSong() string GetNextSong()
{ {
if (!Rules.Music.ContainsKey("allmusic")) return null; var songs = Rules.Music.Select(a => a.Key)
return Rules.Music["allmusic"].Pool.GetNext(); .Where(a => FileSystem.Exists(Rules.Music[a].Filename));
var nextSong = songs
.SkipWhile(m => m != CurrentSong)
.Skip(1)
.FirstOrDefault();
if (nextSong == null)
nextSong = songs.FirstOrDefault();
return nextSong;
} }
string GetPrevSong() string GetPrevSong()
{ {
if (!Rules.Music.ContainsKey("allmusic")) return null; var songs = Rules.Music.Select(a => a.Key)
return Rules.Music["allmusic"].Pool.GetPrev(); .Where(a => FileSystem.Exists(Rules.Music[a].Filename))
} .Reverse();
string GetSong() var nextSong = songs
{ .SkipWhile(m => m != CurrentSong)
if (!Rules.Music.ContainsKey("allmusic")) return null; .Skip(1)
return Rules.Music["allmusic"].Pool.GetCurrent(); .FirstOrDefault();
if (nextSong == null)
nextSong = songs.FirstOrDefault();
return nextSong;
} }
} }
} }

View File

@@ -92,4 +92,89 @@ Background@PERF_BG:
X:20 X:20
Y:205 Y:205
Width:170 Width:170
Height:40 Height:40
Background@MUSIC_BG:
Id:MUSIC_BG
Delegate:MusicPlayerDelegate
X:WINDOW_RIGHT - 175
Y:WINDOW_BOTTOM - 95
Width: 160
Height: 55
Visible: true
Children:
Button@BUTTON_PLAY:
Id:BUTTON_PLAY
Visible:false
X:50
Y:15
Width:25
Height:25
Children:
Image@IMAGE_PLAY:
Id:IMAGE_PLAY
X:0
Y:0
Width:25
Height:25
ImageCollection:music
ImageName:play
Button@BUTTON_PAUSE:
Id:BUTTON_PAUSE
X:50
Y:15
Width:25
Height:25
Children:
Image@IMAGE_PAUSE:
Id:IMAGE_PAUSE
X:0
Y:0
Width:25
Height:25
ImageCollection:music
ImageName:pause
Button@BUTTON_STOP:
Id:BUTTON_STOP
X:85
Y:15
Width:25
Height:25
Children:
Image@IMAGE_STOP:
Id:IMAGE_STOP
X:0
Y:0
Width:25
Height:25
ImageCollection:music
ImageName:stop
Button@BUTTON_NEXT:
Id:BUTTON_NEXT
X:120
Y:15
Width:25
Height:25
Children:
Image@IMAGE_NEXT:
Id:IMAGE_NEXT
X:0
Y:0
Width:25
Height:25
ImageCollection:music
ImageName:next
Button@BUTTON_PREV:
Id:BUTTON_PREV
X:15
Y:15
Width:25
Height:25
Children:
Image@IMAGE_PREV:
Id:IMAGE_PREV
X:0
Y:0
Width:25
Height:25
ImageCollection:music
ImageName:prev

View File

@@ -1,2 +1,83 @@
AllMusic: await_r:
Music: hell226m.aud 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