Refactor background music to fix some possible issues and smooth out transitions

This commit is contained in:
Flamefire committed 2014-12-26 22:39:44 +01:00
1 parent cd4c010f99
commit df82690cd0
16 files changed
+333 -389

No files matched your search

+114 -165
View File
@@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using VocaluxeLib;
using VocaluxeLib.Songs;
using VocaluxeLib.Draw;
@@ -30,12 +29,12 @@ namespace Vocaluxe.Base
{
private static bool _Initialized;
private static CSongPlayer _SongPlayer;
private static CSongPlayer _PreviewPlayer;
private static readonly CSongPlayer _BGPlayer = new CSongPlayer();
private static readonly CSongPlayer _PreviewPlayer = new CSongPlayer(true);
private static CSongPlayer _CurPlayer;
private static bool _OwnSongsAvailable;
private static EBackgroundMusicSource _MusicSource;
private static bool _VideoEnabled;
private static CPlaylistElement _CurrentPlaylistElement; //Currently played music
private static int _PreviousMusicIndex = -1;
@@ -47,29 +46,20 @@ namespace Vocaluxe.Base
private static bool _OwnMusicAdded;
private static bool _BackgroundMusicAdded;
private static bool _Disabled;
private static bool _PlaysPreview;
public static bool VideoEnabled
{
get { return _PlaysPreview ? _PreviewPlayer.VideoEnabled : _SongPlayer.VideoEnabled; }
set
{
if (_PlaysPreview)
_PreviewPlayer.VideoEnabled = value;
else
_SongPlayer.VideoEnabled = value;
}
get { return _CurPlayer.VideoEnabled; }
set { _CurPlayer.VideoEnabled = value; }
}
public static bool CanSing
{
get
{
if (_PlaysPreview)
if (IsPlayingPreview)
return false;
if (_BGMusicFiles.Contains(_CurrentPlaylistElement))
return false;
return true;
return !_BGMusicFiles.Contains(_CurrentPlaylistElement);
}
}
@@ -81,54 +71,63 @@ namespace Vocaluxe.Base
if (Disabled == value)
return;
_Disabled = value;
Pause();
if (_Disabled)
Pause();
else
Play();
}
}
/// <summary>
/// Repeat the background song (preview songs are always repeating!)
/// </summary>
public static bool RepeatSong
{
get { return _SongPlayer.RepeatSong; }
set { _SongPlayer.RepeatSong = value; }
get { return _BGPlayer.Loop; }
set { _BGPlayer.Loop = value; }
}
public static int SongID
{
get { return _SongPlayer.SongID; }
get { return _CurPlayer.SongID; }
}
public static bool SongHasVideo
{
get { return _SongPlayer.SongHasVideo; }
get { return _CurPlayer.SongHasVideo; }
}
public static bool IsPlaying
{
get { return _PlaysPreview ? _PreviewPlayer.IsPlaying : _SongPlayer.IsPlaying; }
get { return _CurPlayer.IsPlaying; }
}
public static bool IsPlayingPreview
{
get { return _PlaysPreview; }
get { return _CurPlayer == _PreviewPlayer; }
set
{
if (IsPlayingPreview == value)
return;
Pause();
_CurPlayer = value ? _PreviewPlayer : _BGPlayer;
Play();
}
}
public static string ArtistAndTitle
{
get
{
if (_PlaysPreview)
return _PreviewPlayer.ArtistAndTitle;
return _BGMusicFiles.Contains(_CurrentPlaylistElement) ? Path.GetFileNameWithoutExtension(_CurrentPlaylistElement.MusicFilePath) : _SongPlayer.ArtistAndTitle;
}
get { return _CurPlayer.ArtistAndTitle; }
}
public static float Length
{
get { return _PlaysPreview ? _PreviewPlayer.Length : _SongPlayer.Length; }
get { return _CurPlayer.Length; }
}
public static CTextureRef Cover
{
get { return _PlaysPreview ? _PreviewPlayer.Cover : _SongPlayer.Cover; }
get { return _CurPlayer.Cover; }
}
//Use this to set whether own songs are available for access
@@ -160,21 +159,15 @@ namespace Vocaluxe.Base
_MusicSource = EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC;
_AddBackgroundMusic();
_SongPlayer = new CSongPlayer();
_PreviewPlayer = new CSongPlayer {RepeatSong = true};
_VideoEnabled = (CConfig.Config.Video.VideoBackgrounds == EOffOn.TR_CONFIG_ON && CConfig.Config.Video.VideosToBackground == EOffOn.TR_CONFIG_ON);
SetMusicSource(CConfig.Config.Sound.BackgroundMusicSource);
_CurPlayer = _BGPlayer;
_Initialized = true;
}
public static void Close()
{
if (_SongPlayer != null)
_SongPlayer.Stop();
if (_PreviewPlayer != null)
_PreviewPlayer.Stop();
_BGPlayer.Close();
_PreviewPlayer.Close();
_BGMusicFiles.Clear();
_NotPlayedFiles.Clear();
@@ -185,124 +178,88 @@ namespace Vocaluxe.Base
public static void Play()
{
if (IsPlaying || (CConfig.Config.Sound.BackgroundMusic != EBackgroundMusicOffOn.TR_CONFIG_ON && !_PlaysPreview))
if (!IsPlayingPreview && CConfig.Config.Sound.BackgroundMusic == EBackgroundMusicOffOn.TR_CONFIG_OFF)
return;
if (_PlaysPreview)
_PreviewPlayer.Play();
else if (CConfig.Config.Sound.BackgroundMusic == EBackgroundMusicOffOn.TR_CONFIG_ON)
{
if (_SongPlayer.SongLoaded)
//Resume
_SongPlayer.TogglePause();
else
Next();
}
if (IsPlayingPreview || _BGPlayer.SoundLoaded)
_CurPlayer.Play();
else
Next();
}
public static void Stop()
{
if (!IsPlaying)
return;
if (_PlaysPreview)
{
_PreviewPlayer.Stop();
_PlaysPreview = false;
Pause();
CSound.SetGlobalVolume(CConfig.BackgroundMusicVolume);
}
else
{
_SongPlayer.Stop();
_CurPlayer.Stop();
if (!IsPlayingPreview)
_CurrentPlaylistElement = null;
}
}
public static void Pause()
{
if (_PlaysPreview)
{
if (!_PreviewPlayer.SongLoaded)
return;
_PreviewPlayer.TogglePause();
}
else if (CConfig.Config.Sound.BackgroundMusic == EBackgroundMusicOffOn.TR_CONFIG_ON)
{
if (!_SongPlayer.SongLoaded)
Next();
else
_SongPlayer.TogglePause();
}
_CurPlayer.Pause();
}
public static void Next()
{
if (!_PlaysPreview)
if (IsPlayingPreview)
return;
Stop(); //stop last song if any
if (_PreviousMusicIndex < _PreviousFiles.Count - 2)
{
Stop(); //stop last song if any
if (_PreviousMusicIndex < _PreviousFiles.Count - 2)
{
//We are in the previous list and next element exists
_PreviousMusicIndex++;
_CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex];
}
else
{
//We are not in the previous list (anymore)
if (_NotPlayedFiles.Count == 0)
{
if (_PreviousFiles.Count == 0)
return; //No songs to play
_NotPlayedFiles.AddRange(_PreviousFiles);
}
_CurrentPlaylistElement = _NotPlayedFiles[CGame.Rand.Next(_NotPlayedFiles.Count)];
_NotPlayedFiles.Remove(_CurrentPlaylistElement);
_PreviousFiles.Add(_CurrentPlaylistElement);
_PreviousMusicIndex = _PreviousFiles.Count - 1;
}
_StartSong();
//We are in the previous list and next element exists
_PreviousMusicIndex++;
_CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex];
}
else
{
//We are not in the previous list (anymore)
if (_NotPlayedFiles.Count == 0)
{
if (_PreviousFiles.Count == 0)
return; //No songs to play
_NotPlayedFiles.AddRange(_PreviousFiles);
}
_CurrentPlaylistElement = _NotPlayedFiles[CGame.Rand.Next(_NotPlayedFiles.Count)];
_NotPlayedFiles.Remove(_CurrentPlaylistElement);
_PreviousFiles.Add(_CurrentPlaylistElement);
_PreviousMusicIndex = _PreviousFiles.Count - 1;
}
_StartSong();
}
public static void Previous()
{
if (!_PlaysPreview)
{
if (_PreviousMusicIndex < 0)
return;
Debug.Assert(_CurrentPlaylistElement != null);
if (_SongPlayer.Position <= 1.5f && _PreviousMusicIndex > 0)
{
Stop(); //stop last song
_PreviousMusicIndex--;
if (IsPlayingPreview)
return;
if (_PreviousMusicIndex < 0)
return;
_CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex];
}
_StartSong();
if (_CurrentPlaylistElement == null || (_BGPlayer.Position <= 1.5f && _PreviousMusicIndex > 0))
{
Stop(); //stop last song
_PreviousMusicIndex--;
_CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex];
}
_StartSong();
}
public static void Update()
{
if (!IsPlaying && (!_SongPlayer.SongLoaded || !_PreviewPlayer.SongLoaded))
if (!IsPlaying)
return;
if (_PlaysPreview)
_PreviewPlayer.Update();
else
{
_SongPlayer.Update();
if (_SongPlayer.IsFinished)
Next();
}
_CurPlayer.Update();
if (!IsPlayingPreview && _BGPlayer.IsFinished)
Next();
}
public static CTextureRef GetVideoTexture()
{
return _PlaysPreview ? _PreviewPlayer.GetVideoTexture() : _SongPlayer.GetVideoTexture();
return _CurPlayer.GetVideoTexture();
}
public static void SetMusicSource(EBackgroundMusicSource source)
@@ -328,54 +285,42 @@ namespace Vocaluxe.Base
}
}
public static void LoadPreview(CSong song, float start = 0f)
public static void LoadPreview(CSong song, float start = -1f)
{
if (!_PlaysPreview)
Pause();
else
StopPreview();
if (song == null)
return;
throw new ArgumentNullException("song");
// ReSharper disable CompareOfFloatsByEqualityOperator
if (start == 0f)
start = song.Preview.StartTime;
// ReSharper restore CompareOfFloatsByEqualityOperator
if (!IsPlayingPreview)
{
Pause();
_CurPlayer = _PreviewPlayer;
CSound.SetGlobalVolume(CConfig.PreviewMusicVolume);
}
_PlaysPreview = true;
CSound.SetGlobalVolume(CConfig.PreviewMusicVolume);
_PreviewPlayer.Load(song, start);
float startposition = song.Preview.StartTime;
_PreviewPlayer.Load(song);
float length = _PreviewPlayer.Length;
if (length < 1)
length = 30; // If length is unknow or invalid assume a length of 30s
if (song.Preview.Source == EDataSource.None)
startposition = length / 4f;
else if (startposition > length - 5f)
startposition = Math.Max(0f, Math.Min(length / 4f, length - 5f));
if (start < 0)
start = (song.Preview.Source == EDataSource.None) ? length / 4f : song.Preview.StartTime;
if (start > length - 5f)
start = Math.Max(0f, Math.Min(length / 4f, length - 5f));
if (start >= 0.5f)
start -= 0.5f;
if (startposition >= 0.5f)
startposition -= 0.5f;
_PreviewPlayer.Position = startposition;
Play();
}
public static void PlayPreview(float start = -1f)
{
if (start > -1f)
_PreviewPlayer.Position = start;
_PreviewPlayer.Position = start;
Play();
}
public static void StopPreview()
{
_PreviewPlayer.Stop();
_PlaysPreview = false;
if (!IsPlayingPreview)
return;
Stop();
_CurPlayer = _BGPlayer;
CSound.SetGlobalVolume(CConfig.BackgroundMusicVolume);
}
@@ -387,19 +332,23 @@ namespace Vocaluxe.Base
Debug.Assert(_CurrentPlaylistElement != null);
//If current song same as loaded restart only
if (_CurrentPlaylistElement.SongID == _SongPlayer.SongID && _SongPlayer.SongLoaded)
_SongPlayer.Play();
if (_BGPlayer.SoundLoaded && _CurrentPlaylistElement.MusicFilePath == _BGPlayer.FilePath)
{
_BGPlayer.Stop();
_BGPlayer.Play();
return;
}
//otherwhise load
if (_BGMusicFiles.Contains(_CurrentPlaylistElement))
_SongPlayer.LoadFile(_CurrentPlaylistElement.MusicFilePath, 0f, true);
if (!_CurrentPlaylistElement.HasMetaData)
_BGPlayer.Load(_CurrentPlaylistElement.MusicFilePath, 0f, true);
else
{
//Seek to #Start-Tag, if found
float start = 0f;
if (_CurrentPlaylistElement.Start > 0.001 && CConfig.Config.Sound.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON)
start = _CurrentPlaylistElement.Start;
_SongPlayer.Load(CBase.Songs.GetSongByID(_CurrentPlaylistElement.SongID), start, true);
_BGPlayer.Load(_CurrentPlaylistElement.Song, start, true);
}
}
+8 -1
View File
@@ -224,6 +224,8 @@ namespace Vocaluxe.Base
NextScreen.ProcessMouseMove(_Cursor.X, _Cursor.Y);
HidePopup(EPopupScreens.PopupPlayerControl);
if (NextScreen.CurrentMusicType != EMusicType.Background && NextScreen.CurrentMusicType != EMusicType.Preview)
CBackgroundMusic.Disabled = true;
}
if (_Fading != null)
@@ -277,6 +279,11 @@ namespace Vocaluxe.Base
NextScreen = null;
CurrentScreen.OnShowFinish();
CSound.SetGlobalVolume(CConfig.GetVolumeByType(CurrentScreen.CurrentMusicType));
if (CurrentScreen.CurrentMusicType == EMusicType.Background || CurrentScreen.CurrentMusicType == EMusicType.Preview)
{
CBackgroundMusic.Disabled = false;
CBackgroundMusic.IsPlayingPreview = CurrentScreen.CurrentMusicType == EMusicType.Preview;
}
_Fading = null;
}
@@ -351,7 +358,7 @@ namespace Vocaluxe.Base
SKeyEvent inputKeyEvent = new SKeyEvent();
SMouseEvent inputMouseEvent = new SMouseEvent();
bool popupPlayerControlAllowed = CurrentScreen.CurrentMusicType == EMusicType.Background && !CBackgroundMusic.Disabled;
bool popupPlayerControlAllowed = CurrentScreen.CurrentMusicType == EMusicType.Background;
bool popupVolumeControlAllowed = CurrentScreen.CurrentMusicType != EMusicType.None;
//Hide volume control for bg-music if bg-music is disabled
if (popupVolumeControlAllowed && CurrentScreen.CurrentMusicType == EMusicType.Background && CConfig.Config.Sound.BackgroundMusic == EBackgroundMusicOffOn.TR_CONFIG_OFF)
+11 -6
View File
@@ -355,21 +355,21 @@ namespace Vocaluxe.Base
CBackgroundMusic.Stop();
}
public void LoadPreview(CSong song, float start = 0f)
public void LoadPreview(CSong song, float start = -1f)
{
CBackgroundMusic.LoadPreview(song, start);
}
public void PlayPreview(float start = -1f)
{
CBackgroundMusic.PlayPreview(start);
}
public void StopPreview()
{
CBackgroundMusic.StopPreview();
}
public void SetPlayingPreview(bool playPreview)
{
CBackgroundMusic.IsPlayingPreview = playPreview;
}
public CTextureRef GetVideoTexture()
{
return CBackgroundMusic.GetVideoTexture();
@@ -877,6 +877,11 @@ namespace Vocaluxe.Base
{
CSound.SetGlobalVolume(volume);
}
public bool IsPaused(int streamID)
{
return CSound.IsPaused(streamID);
}
}
class CBcover : ICover
+20 -11
View File
@@ -23,61 +23,70 @@ namespace Vocaluxe.Base
{
class CPlaylistElement
{
private readonly CSong _Song;
public readonly CSong Song;
private readonly string _MusicFilePath = String.Empty;
public bool HasMetaData
{
get { return Song != null; }
}
public int SongID
{
get { return (_Song == null) ? -1 : _Song.ID; }
get { return HasMetaData ? Song.ID : -1; }
}
public string MusicFilePath
{
get { return _Song != null ? _Song.GetMP3() : _MusicFilePath; }
get { return HasMetaData ? Song.GetMP3() : _MusicFilePath; }
}
public string VideoFilePath
{
get { return _Song != null ? _Song.GetVideo() : string.Empty; }
get { return HasMetaData ? Song.GetVideo() : string.Empty; }
}
public string Title
{
get { return _Song != null ? _Song.Title : ""; }
get { return HasMetaData ? Song.Title : ""; }
}
public string Artist
{
get { return _Song != null ? _Song.Artist : ""; }
get { return HasMetaData ? Song.Artist : ""; }
}
public float Start
{
get { return _Song != null ? _Song.Start : 0f; }
get { return HasMetaData ? Song.Start : 0f; }
}
public float Finish
{
get { return _Song != null ? _Song.Finish : 0f; }
get { return HasMetaData ? Song.Finish : 0f; }
}
public CTextureRef Cover
{
get { return _Song != null ? _Song.CoverTextureSmall : CCover.NoCover; }
get { return HasMetaData ? Song.CoverTextureSmall : CCover.NoCover; }
}
public float VideoGap
{
get { return _Song != null ? _Song.VideoGap : 0; }
get { return HasMetaData ? Song.VideoGap : 0; }
}
public CPlaylistElement(CSong song)
{
_Song = song;
if (song == null)
throw new ArgumentNullException("song");
Song = song;
}
public CPlaylistElement(string filePath)
{
if (String.IsNullOrEmpty(filePath))
throw new ArgumentNullException("filePath");
_MusicFilePath = filePath;
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ namespace Vocaluxe.Lib.Video
/// Loads the file with the given file path.
/// </summary>
/// <param name="videoFileName">Absolute file path of the video file</param>
/// <returns>stream &gt;=0 on success or -1 on failure</returns>
/// <returns>Loaded video stream or null on failure</returns>
CVideoStream Load(string videoFileName);
/// <summary>
@@ -112,7 +112,7 @@ namespace Vocaluxe.Screens
if (_Buttons[_ButtonPrevious].Selected)
CBackgroundMusic.Previous();
if (_Buttons[_ButtonPlay].Selected)
CBackgroundMusic.Pause();
CBackgroundMusic.Play();
if (_Buttons[_ButtonPause].Selected)
CBackgroundMusic.Pause();
if (_Buttons[_ButtonRepeat].Selected)
@@ -140,7 +140,7 @@ namespace Vocaluxe.Screens
if (_Buttons[_ButtonPrevious].Selected)
CBackgroundMusic.Previous();
if (_Buttons[_ButtonPlay].Selected)
CBackgroundMusic.Pause();
CBackgroundMusic.Play();
if (_Buttons[_ButtonPause].Selected)
CBackgroundMusic.Pause();
if (_Buttons[_ButtonRepeat].Selected)
+5
View File
@@ -65,6 +65,11 @@ namespace Vocaluxe.Screens
private bool _AvatarsChanged;
private bool _ProfilesChanged;
public override EMusicType CurrentMusicType
{
get { return EMusicType.Preview; }
}
#region public methods
public override void Init()
{
-2
View File
@@ -303,7 +303,6 @@ namespace Vocaluxe.Screens
public override void OnShowFinish()
{
base.OnShowFinish();
CBackgroundMusic.Disabled = true;
}
public override void Draw()
@@ -332,7 +331,6 @@ namespace Vocaluxe.Screens
CRecord.Stop();
_DelayTest.Reset();
CBackgroundMusic.Disabled = false;
}
private void _OnDeviceEvent()
-2
View File
@@ -124,8 +124,6 @@ namespace Vocaluxe.Screens
public override void OnShow()
{
CBackgroundMusic.Disabled = false;
base.OnShow();
//-1 --> Show average
_Round = CGame.NumRounds > 1 ? -1 : 0;
-1
View File
@@ -413,7 +413,6 @@ namespace Vocaluxe.Screens
public override void OnShowFinish()
{
base.OnShowFinish();
CBackgroundMusic.Disabled = true;
CGame.Start();
_LoadNextSong();