From 6053102b8f8d6aabacf42898fe6f1decbd4160af Mon Sep 17 00:00:00 2001 From: Flamefire Date: Tue, 18 Mar 2014 16:41:42 +0100 Subject: [PATCH] Rework CBackgroundMusic to avoid crash on startup and assure correct behaviour on runtime --- Vocaluxe/Base/CBackgroundMusic.cs | 613 +++++++----------- Vocaluxe/Base/CGraphics.cs | 8 +- Vocaluxe/Base/CMain.cs | 5 - Vocaluxe/Base/CPlaylistElement.cs | 67 ++ Vocaluxe/Base/CSettings.cs | 8 - Vocaluxe/Screens/CPopupScreenVolumeControl.cs | 4 +- Vocaluxe/Screens/CScreenLoad.cs | 36 +- Vocaluxe/Screens/CScreenOptionsSound.cs | 13 +- Vocaluxe/Vocaluxe.csproj | 1 + VocaluxeLib/CHelper.cs | 58 +- VocaluxeLib/IBasic.cs | 2 - 11 files changed, 354 insertions(+), 461 deletions(-) create mode 100644 Vocaluxe/Base/CPlaylistElement.cs diff --git a/Vocaluxe/Base/CBackgroundMusic.cs b/Vocaluxe/Base/CBackgroundMusic.cs index 9afc32dd..f23943f8 100644 --- a/Vocaluxe/Base/CBackgroundMusic.cs +++ b/Vocaluxe/Base/CBackgroundMusic.cs @@ -17,8 +17,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; -using System.Linq; using VocaluxeLib; using VocaluxeLib.Songs; using VocaluxeLib.Draw; @@ -27,25 +27,28 @@ namespace Vocaluxe.Base { static class CBackgroundMusic { - private static int _CurrentMusicStream = -1; - private static int _PreviousMusicIndex; - private static CPlaylistElement _CurrentPlaylistElement = new CPlaylistElement(); + private static bool _Initialized; - private static readonly List _AllFileNames = new List(); - private static readonly List _NotPlayedFileNames = new List(); - private static readonly List _BGMusicFileNames = new List(); - private static readonly List _PreviousFileNames = new List(); + private static bool _OwnSongsAvailable; + private static EBackgroundMusicSource _MusicSource; + private static float _Volume; + private static bool _VideoEnabled; + + private static CPlaylistElement _CurrentPlaylistElement; //Currently played music + private static int _CurrentMusicStream = -1; //Current active music stream + private static int _PreviousMusicIndex; //Index of _CurrentPlaylistElement in _PreviousFiles (kept for performance) + + private static readonly List _BGMusicFiles = new List(); //Background music files + private static readonly List _NotPlayedFiles = new List(); //Not played files + private static readonly List _PreviousFiles = new List(); //Played files private static int _Video = -1; private static CTexture _CurrentVideoTexture; private static CFading _VideoFading; - private static bool _VideoEnabled; private static bool _OwnMusicAdded; private static bool _BackgroundMusicAdded; private static bool _Disabled; - private static bool _CanSing; - private static bool _RepeatSong; public static bool VideoEnabled { @@ -54,16 +57,19 @@ namespace Vocaluxe.Base { if (_VideoEnabled == value) return; - ToggleVideo(); _VideoEnabled = value; + if (_VideoEnabled) + _LoadVideo(); + else if (_Video != -1) + { + CVideo.Close(_Video); + CDraw.RemoveTexture(ref _CurrentVideoTexture); + _Video = -1; + } } } - public static bool CanSing - { - get { return _CanSing; } - set { _CanSing = value; } - } + public static bool CanSing { get; private set; } public static bool Disabled { @@ -78,20 +84,16 @@ namespace Vocaluxe.Base } } - public static bool RepeatSong - { - get { return _RepeatSong; } - set { _RepeatSong = value; } - } + public static bool RepeatSong { get; set; } public static int SongID { - get { return _CurrentPlaylistElement.SongID; } + get { return _CurrentPlaylistElement == null ? -1 : _CurrentPlaylistElement.SongID; } } public static bool SongHasVideo { - get { return File.Exists(_CurrentPlaylistElement.VideoFilePath); } + get { return _CurrentPlaylistElement != null && File.Exists(_CurrentPlaylistElement.VideoFilePath); } } public static bool IsPlaying { get; private set; } @@ -100,6 +102,8 @@ namespace Vocaluxe.Base { get { + if (_CurrentPlaylistElement == null) + return ""; if (_CurrentPlaylistElement.Artist != "" && _CurrentPlaylistElement.Title != "") return _CurrentPlaylistElement.Artist + " - " + _CurrentPlaylistElement.Title; return Path.GetFileNameWithoutExtension(_CurrentPlaylistElement.MusicFilePath); @@ -108,50 +112,63 @@ namespace Vocaluxe.Base public static CTexture Cover { - get { return _CurrentPlaylistElement.Cover; } + get { return _CurrentPlaylistElement == null ? CCover.NoCover : _CurrentPlaylistElement.Cover; } } + //Use this to set whether own songs are available for access + public static bool OwnSongsAvailable + { + get { return _OwnSongsAvailable; } + set + { + _OwnSongsAvailable = value; + if (_OwnSongsAvailable && _MusicSource != EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC) + AddOwnMusic(); + } + } + + /// + /// Initializes the background music with values from config + /// Has to be called before any other method is used + /// public static void Init() { - var templist = new List(); - - foreach (string ending in CSettings.MusicFileTypes) - templist.AddRange(CHelper.ListFiles(CSettings.FolderBackgroundMusic, ending, true, true)); - - foreach (string path in templist) - _BGMusicFileNames.Add(new CPlaylistElement(path)); - - if (CConfig.BackgroundMusicSource != EBackgroundMusicSource.TR_CONFIG_ONLY_OWN_MUSIC) - AddBackgroundMusic(); + if (_Initialized) + return; + _Volume = CConfig.BackgroundMusicVolume; _VideoEnabled = (CConfig.VideoBackgrounds == EOffOn.TR_CONFIG_ON && CConfig.VideosToBackground == EOffOn.TR_CONFIG_ON); + _MusicSource = CConfig.BackgroundMusicSource; + + List soundFiles = CHelper.ListSoundFiles(CSettings.FolderBackgroundMusic, true, true); + + foreach (string path in soundFiles) + _BGMusicFiles.Add(new CPlaylistElement(path)); + + if (_MusicSource != EBackgroundMusicSource.TR_CONFIG_ONLY_OWN_MUSIC) + _AddBackgroundMusic(); IsPlaying = false; + _Initialized = true; } public static void Play() { - if (IsPlaying) + if (IsPlaying || CConfig.BackgroundMusic == EOffOn.TR_CONFIG_OFF) return; - if (CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON) + if (_CurrentMusicStream != -1) { - if (_AllFileNames.Count > 0) - { - if (_CurrentMusicStream != -1) - { - CSound.Fade(_CurrentMusicStream, 100f, CSettings.BackgroundMusicFadeTime); - CSound.Play(_CurrentMusicStream); - if (_VideoEnabled && _Video != -1) - CVideo.Resume(_Video); - IsPlaying = true; - } - else - Next(); - - _CanSing = !_IsBackgroundFile(_CurrentPlaylistElement); - } + //Resume + CSound.SetStreamVolume(_CurrentMusicStream, 0f); + CSound.Fade(_CurrentMusicStream, _Volume, CSettings.BackgroundMusicFadeTime); + CSound.Play(_CurrentMusicStream); + if (_VideoEnabled && _Video != -1) + CVideo.Resume(_Video); + IsPlaying = true; } + else + Next(); } public static void Stop() @@ -159,7 +176,7 @@ namespace Vocaluxe.Base if (!IsPlaying) return; - if (_VideoEnabled && _Video != -1) + if (_Video != -1) { CVideo.Close(_Video); CDraw.RemoveTexture(ref _CurrentVideoTexture); @@ -168,7 +185,7 @@ namespace Vocaluxe.Base CSound.FadeAndClose(_CurrentMusicStream, 0f, CSettings.BackgroundMusicFadeTime); _CurrentMusicStream = -1; - _CurrentPlaylistElement = new CPlaylistElement(); + _CurrentPlaylistElement = null; IsPlaying = false; } @@ -177,7 +194,7 @@ namespace Vocaluxe.Base if (!IsPlaying) return; - if (_VideoEnabled && _Video != -1) + if (_Video != -1) { CVideo.Pause(_Video); CVideo.Skip(_Video, CSound.GetPosition(_CurrentMusicStream) + CSettings.BackgroundMusicFadeTime, _CurrentPlaylistElement.VideoGap); @@ -188,275 +205,202 @@ namespace Vocaluxe.Base public static void Update() { - if (_AllFileNames.Count > 0 && _CurrentMusicStream != -1) - { - float timeToPlay; - if (Math.Abs(_CurrentPlaylistElement.Finish) < 0.001) //No End-Tag defined - timeToPlay = CSound.GetLength(_CurrentMusicStream) - CSound.GetPosition(_CurrentMusicStream); - else //End-Tag found - timeToPlay = _CurrentPlaylistElement.Finish - CSound.GetPosition(_CurrentMusicStream); + if (!IsPlaying) + return; + Debug.Assert(_CurrentMusicStream != -1 && _CurrentPlaylistElement != null); - bool finished = CSound.IsFinished(_CurrentMusicStream); - if (IsPlaying && ((timeToPlay <= CSettings.BackgroundMusicFadeTime && timeToPlay > 0) || finished)) + float timeToPlay; + if (Math.Abs(_CurrentPlaylistElement.Finish) < 0.001) //No End-Tag defined + timeToPlay = CSound.GetLength(_CurrentMusicStream) - CSound.GetPosition(_CurrentMusicStream); + else //End-Tag found + timeToPlay = _CurrentPlaylistElement.Finish - CSound.GetPosition(_CurrentMusicStream); + + bool finished = CSound.IsFinished(_CurrentMusicStream); + if (timeToPlay <= CSettings.BackgroundMusicFadeTime || finished) + { + if (RepeatSong) + _StartSong(); + else + Next(); + } + } + + /// + /// (Re-)Starts the _CurrentPlaylistElement + /// + private static void _StartSong() + { + Debug.Assert(_CurrentPlaylistElement != null); + //If we have an active stream, reuse it + if (_CurrentMusicStream < 0) + { + //otherwhise load + _CurrentMusicStream = CSound.Load(_CurrentPlaylistElement.MusicFilePath); + if (_CurrentMusicStream < 0) + return; + CSound.SetStreamVolume(_CurrentMusicStream, 0f); + CSound.Fade(_CurrentMusicStream, _Volume, CSettings.BackgroundMusicFadeTime); + } + //Seek to #Start-Tag, if found + if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) + CSound.SetPosition(_CurrentMusicStream, _CurrentPlaylistElement.Start); + + if (_VideoEnabled) + { + if (_Video != -1) { - if (_RepeatSong) - { - //Seek to #Start-Tag, if found - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CSound.SetPosition(_CurrentMusicStream, _CurrentPlaylistElement.Start); - else - CSound.SetPosition(_CurrentMusicStream, 0); - if (_VideoEnabled && _Video != -1) - { - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CVideo.Skip(_Video, _CurrentPlaylistElement.Start, _CurrentPlaylistElement.VideoGap); - else - CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); - } - } + if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) + CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap + _CurrentPlaylistElement.Start); else - Next(); + CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); } + else + _LoadVideo(); } } public static void Next() { - if (_AllFileNames.Count > 0) + Stop(); //stop last song if any + if (_PreviousMusicIndex < _PreviousFiles.Count - 2) { - if (_PreviousMusicIndex == _PreviousFileNames.Count - 1 || _PreviousFileNames.Count == 0) - { - //We are not currently in the previous list - Stop(); - if (_NotPlayedFileNames.Count == 0) - _NotPlayedFileNames.AddRange(_AllFileNames); - - _CurrentPlaylistElement = _NotPlayedFileNames[CGame.Rand.Next(_NotPlayedFileNames.Count)]; - _NotPlayedFileNames.Remove(_CurrentPlaylistElement); - - _PreviousFileNames.Add(_CurrentPlaylistElement); - _PreviousMusicIndex = _PreviousFileNames.Count - 1; - } - else if (_PreviousFileNames.Count > 0) - { - //We are in the previous list - Stop(); - _PreviousMusicIndex++; - - _CurrentPlaylistElement = _PreviousFileNames[_PreviousMusicIndex]; - } - _CurrentMusicStream = CSound.Load(_CurrentPlaylistElement.MusicFilePath); - CSound.SetStreamVolumeMax(_CurrentMusicStream, CConfig.BackgroundMusicVolume); - - //Seek to #Start-Tag, if found - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CSound.SetPosition(_CurrentMusicStream, _CurrentPlaylistElement.Start); - - if (_VideoEnabled) - _LoadVideo(); - CSound.SetStreamVolume(_CurrentMusicStream, 0f); - Play(); + //We are in the previous list and next element exists + _PreviousMusicIndex++; + _CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex]; } else - Stop(); + { + //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(); + CanSing = !_IsBackgroundFile(_CurrentPlaylistElement); } public static void Previous() { - if (_PreviousFileNames.Count > 0 && _PreviousMusicIndex >= 0) + if (_PreviousMusicIndex < 0) + return; + Debug.Assert(_CurrentMusicStream != -1 && _CurrentPlaylistElement != null); + if (CSound.GetPosition(_CurrentMusicStream) <= 1.5f && _PreviousMusicIndex > 0) { - if (CSound.GetPosition(_CurrentMusicStream) >= 1.5f) - { - //Seek to #Start-Tag, if found - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CSound.SetPosition(_CurrentMusicStream, _CurrentPlaylistElement.Start); - else - CSound.SetPosition(_CurrentMusicStream, 0); - if (_VideoEnabled && _Video != -1) - { - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CVideo.Skip(_Video, _CurrentPlaylistElement.Start, _CurrentPlaylistElement.VideoGap); - else - CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); - } - } - else - { - Stop(); - _PreviousMusicIndex--; - if (_PreviousMusicIndex < 0) - _PreviousMusicIndex = 0; //No previous songs left, so play the first + Stop(); //stop last song + _PreviousMusicIndex--; - _CurrentPlaylistElement = _PreviousFileNames[_PreviousMusicIndex]; - - _CurrentMusicStream = CSound.Load(_CurrentPlaylistElement.MusicFilePath); - CSound.SetStreamVolumeMax(_CurrentMusicStream, CConfig.BackgroundMusicVolume); - if (_VideoEnabled) - _LoadVideo(); - CSound.SetStreamVolume(_CurrentMusicStream, 0f); - Play(); - } + _CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex]; + CanSing = !_IsBackgroundFile(_CurrentPlaylistElement); } - else if (_CurrentMusicStream != -1) - { - //Seek to #Start-Tag, if found - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CSound.SetPosition(_CurrentMusicStream, _CurrentPlaylistElement.Start); - else - CSound.SetPosition(_CurrentMusicStream, 0); - if (_VideoEnabled && _Video != -1) - { - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CVideo.Skip(_Video, _CurrentPlaylistElement.Start, _CurrentPlaylistElement.VideoGap); - else - CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); - } - } - } - - public static void ApplyVolume() - { - CSound.SetStreamVolumeMax(_CurrentMusicStream, CConfig.BackgroundMusicVolume); + _StartSong(); } public static void AddOwnMusic() { - if (!_OwnMusicAdded) - { - foreach (CSong song in CSongs.AllSongs) - { - _AllFileNames.Add(new CPlaylistElement(song)); - _NotPlayedFileNames.Add(new CPlaylistElement(song)); - } - } + if (_OwnMusicAdded) + return; + foreach (CSong song in CSongs.AllSongs) + _NotPlayedFiles.Add(new CPlaylistElement(song)); _OwnMusicAdded = true; } - public static void RemoveOwnMusic() + private static void _RemoveOwnMusic() { - _AllFileNames.Clear(); - _NotPlayedFileNames.Clear(); + if (!_OwnMusicAdded) + return; + _NotPlayedFiles.RemoveAll(el => el.SongID >= 0); + _PreviousFiles.RemoveAll(el => el.SongID >= 0); + _PreviousMusicIndex = _PreviousFiles.IndexOf(_CurrentPlaylistElement); - if (_BackgroundMusicAdded) - { - _AllFileNames.AddRange(_BGMusicFileNames); - _NotPlayedFileNames.AddRange(_BGMusicFileNames); - } - - if (IsPlaying && !_IsBackgroundFile(_CurrentPlaylistElement) || _AllFileNames.Count == 0) + if (IsPlaying && !_IsBackgroundFile(_CurrentPlaylistElement)) Next(); _OwnMusicAdded = false; } - public static void AddBackgroundMusic() - { - if (!_BackgroundMusicAdded) - { - _AllFileNames.AddRange(_BGMusicFileNames); - _NotPlayedFileNames.AddRange(_BGMusicFileNames); - _BackgroundMusicAdded = true; - } - } - - public static void RemoveBackgroundMusic() + private static void _AddBackgroundMusic() { if (_BackgroundMusicAdded) - { - _AllFileNames.Clear(); - _OwnMusicAdded = false; - AddOwnMusic(); - _BackgroundMusicAdded = false; + return; + _NotPlayedFiles.AddRange(_BGMusicFiles); + _BackgroundMusicAdded = true; + } - if (IsPlaying && _IsBackgroundFile(_CurrentPlaylistElement) || _AllFileNames.Count == 0) - Next(); + private static void _RemoveBackgroundMusic() + { + if (!_BackgroundMusicAdded) + return; + _NotPlayedFiles.RemoveAll(el => el.SongID < 0); + _PreviousFiles.RemoveAll(el => el.SongID < 0); + _PreviousMusicIndex = _PreviousFiles.IndexOf(_CurrentPlaylistElement); + + if (IsPlaying && _IsBackgroundFile(_CurrentPlaylistElement)) + Next(); + + _BackgroundMusicAdded = false; + } + + public static void SetMusicSource(EBackgroundMusicSource source) + { + if (_MusicSource == source) + return; + _MusicSource = source; + switch (_MusicSource) + { + case EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC: + _AddBackgroundMusic(); + _RemoveOwnMusic(); + break; + + case EBackgroundMusicSource.TR_CONFIG_ONLY_OWN_MUSIC: + AddOwnMusic(); + _RemoveBackgroundMusic(); + break; + case EBackgroundMusicSource.TR_CONFIG_OWN_MUSIC: + _AddBackgroundMusic(); + AddOwnMusic(); + break; } } - public static void CheckAndApplyConfig(EOffOn newOnOff, EBackgroundMusicSource newSource, float newVolume) + public static void SetVolume(float volume) { - if (CConfig.BackgroundMusicSource != newSource || (newOnOff == EOffOn.TR_CONFIG_ON && !_OwnMusicAdded && !_BackgroundMusicAdded)) - { - CConfig.BackgroundMusicSource = newSource; - - switch (newSource) - { - case EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC: - if (!_BackgroundMusicAdded) - AddBackgroundMusic(); - - RemoveOwnMusic(); - break; - - case EBackgroundMusicSource.TR_CONFIG_ONLY_OWN_MUSIC: - if (!_OwnMusicAdded) - AddOwnMusic(); - - RemoveBackgroundMusic(); - break; - case EBackgroundMusicSource.TR_CONFIG_OWN_MUSIC: - if (!_BackgroundMusicAdded) - AddBackgroundMusic(); - - if (!_OwnMusicAdded) - AddOwnMusic(); - break; - } - } - - if (CConfig.BackgroundMusic != newOnOff) - { - CConfig.BackgroundMusic = newOnOff; - if (newOnOff == EOffOn.TR_CONFIG_ON) - Play(); - else - Pause(); - } - - if (Math.Abs(CConfig.BackgroundMusicVolume - newVolume) > 0.01) - { - CConfig.BackgroundMusicVolume = (int)newVolume; - ApplyVolume(); - } - - CConfig.SaveConfig(); - } - - public static void ToggleVideo() - { - if (_Video != -1) - { - CVideo.Close(_Video); - CDraw.RemoveTexture(ref _CurrentVideoTexture); - _Video = -1; - } - else - _LoadVideo(); + _Volume = volume; + if (_CurrentMusicStream >= 0) + CSound.SetStreamVolume(_CurrentMusicStream, _Volume); } public static CTexture GetVideoTexture() { - if (_Video != -1) + if (_Video == -1) + return null; + float vtime; + if (CVideo.GetFrame(_Video, ref _CurrentVideoTexture, CSound.GetPosition(_CurrentMusicStream), out vtime)) { - float vtime; - if (CVideo.GetFrame(_Video, ref _CurrentVideoTexture, CSound.GetPosition(_CurrentMusicStream), out vtime)) + if (_VideoFading != null) { - if (_VideoFading != null) - { - bool finished; - _CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished); - if (finished) - _VideoFading = null; - } - return _CurrentVideoTexture; + bool finished; + _CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished); + if (finished) + _VideoFading = null; } + return _CurrentVideoTexture; } return null; } private static bool _IsBackgroundFile(CPlaylistElement element) { - return _BGMusicFileNames.Any(elements => elements.MusicFilePath == element.MusicFilePath); + return _BGMusicFiles.Contains(element); } private static void _LoadVideo() @@ -464,134 +408,11 @@ namespace Vocaluxe.Base if (_Video != -1 || String.IsNullOrEmpty(_CurrentPlaylistElement.VideoFilePath)) return; _Video = CVideo.Load(_CurrentPlaylistElement.VideoFilePath); - if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CVideo.Skip(_Video, _CurrentPlaylistElement.Start, _CurrentPlaylistElement.VideoGap); + if (CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) + CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap + _CurrentPlaylistElement.Start); //Use gap, otherwhise we will overwrite position in GetVideoTexture else CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); _VideoFading = new CFading(0f, 1f, 3f); } } - - class CPlaylistElement - { - private readonly CSong _Song; - - private readonly int _SongID; - public int SongID - { - get { return _SongID; } - } - - private string _MusicFilePath = String.Empty; - - public string MusicFilePath - { - get - { - if (_Song != null) - return _Song.GetMP3(); - - return _MusicFilePath; - } - - set { _MusicFilePath = value; } - } - - public string VideoFilePath - { - get - { - if (_Song != null) - return _Song.GetVideo(); - - return string.Empty; - } - } - - public string Title - { - get - { - if (_Song != null) - return _Song.Title; - - return ""; - } - } - - public string Artist - { - get - { - if (_Song != null) - return _Song.Artist; - - return ""; - } - } - - public float Start - { - get - { - if (_Song != null) - return _Song.Start; - - return 0f; - } - } - - public float Finish - { - get - { - if (_Song != null) - return _Song.Finish; - - return 0f; - } - } - - public CTexture Cover - { - get - { - if (_Song != null) - return _Song.CoverTextureSmall; - - return CCover.NoCover; - } - } - - public float VideoGap - { - get - { - if (_Song != null) - return _Song.VideoGap; - - return 0; - } - } - - public CPlaylistElement(CSong song) - { - MusicFilePath = string.Empty; - _SongID = song.ID; - - _Song = song; - } - - public CPlaylistElement(string filePath) - { - MusicFilePath = filePath; - _SongID = -1; - } - - public CPlaylistElement() - { - MusicFilePath = string.Empty; - _SongID = -1; - } - } } \ No newline at end of file diff --git a/Vocaluxe/Base/CGraphics.cs b/Vocaluxe/Base/CGraphics.cs index bd6f7e39..50314cb2 100644 --- a/Vocaluxe/Base/CGraphics.cs +++ b/Vocaluxe/Base/CGraphics.cs @@ -236,8 +236,6 @@ namespace Vocaluxe.Base _Screens[(int)_CurrentScreen].OnClose(); _CurrentScreen = _NextScreen; _NextScreen = EScreens.ScreenNull; - if (CBackgroundMusic.IsPlaying) - CBackgroundMusic.Play(); _Screens[(int)_CurrentScreen].OnShowFinish(); _Screens[(int)_CurrentScreen].ProcessMouseMove(_Cursor.X, _Cursor.Y); @@ -431,7 +429,7 @@ namespace Vocaluxe.Base else { CConfig.BackgroundMusicVolume += diff; - CBackgroundMusic.ApplyVolume(); + CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume); } break; @@ -442,7 +440,7 @@ namespace Vocaluxe.Base default: CConfig.BackgroundMusicVolume += diff; - CBackgroundMusic.ApplyVolume(); + CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume); break; } CConfig.SaveConfig(); @@ -453,8 +451,6 @@ namespace Vocaluxe.Base ShowPopup(EPopupScreens.PopupVolumeControl); _VolumePopupTimer.Restart(); } - - CBackgroundMusic.ApplyVolume(); } if (keyEvent.ModShift && (keyEvent.Key == Keys.F1)) diff --git a/Vocaluxe/Base/CMain.cs b/Vocaluxe/Base/CMain.cs index 36e686ba..31a66c59 100644 --- a/Vocaluxe/Base/CMain.cs +++ b/Vocaluxe/Base/CMain.cs @@ -360,11 +360,6 @@ namespace Vocaluxe.Base CBackgroundMusic.Play(); } - public void ApplyVolume() - { - CBackgroundMusic.ApplyVolume(); - } - public CTexture GetVideoTexture() { return CBackgroundMusic.GetVideoTexture(); diff --git a/Vocaluxe/Base/CPlaylistElement.cs b/Vocaluxe/Base/CPlaylistElement.cs new file mode 100644 index 00000000..195be8e7 --- /dev/null +++ b/Vocaluxe/Base/CPlaylistElement.cs @@ -0,0 +1,67 @@ +using System; +using VocaluxeLib.Draw; +using VocaluxeLib.Songs; + +namespace Vocaluxe.Base +{ + class CPlaylistElement + { + private readonly CSong _Song; + private readonly string _MusicFilePath = String.Empty; + + public int SongID + { + get { return (_Song == null) ? -1 : _Song.ID; } + } + + public string MusicFilePath + { + get { return _Song != null ? _Song.GetMP3() : _MusicFilePath; } + } + + public string VideoFilePath + { + get { return _Song != null ? _Song.GetVideo() : string.Empty; } + } + + public string Title + { + get { return _Song != null ? _Song.Title : ""; } + } + + public string Artist + { + get { return _Song != null ? _Song.Artist : ""; } + } + + public float Start + { + get { return _Song != null ? _Song.Start : 0f; } + } + + public float Finish + { + get { return _Song != null ? _Song.Finish : 0f; } + } + + public CTexture Cover + { + get { return _Song != null ? _Song.CoverTextureSmall : CCover.NoCover; } + } + + public float VideoGap + { + get { return _Song != null ? _Song.VideoGap : 0; } + } + + public CPlaylistElement(CSong song) + { + _Song = song; + } + + public CPlaylistElement(string filePath) + { + _MusicFilePath = filePath; + } + } +} \ No newline at end of file diff --git a/Vocaluxe/Base/CSettings.cs b/Vocaluxe/Base/CSettings.cs index e978691d..a4ccca09 100644 --- a/Vocaluxe/Base/CSettings.cs +++ b/Vocaluxe/Base/CSettings.cs @@ -153,14 +153,6 @@ namespace Vocaluxe.Base public const float SlideShowImageTime = 3500f; public const float SlideShowFadeTime = 500f; - public static readonly List MusicFileTypes = new List - { - "*.mp3", - "*.wma", - "*.ogg", - "*.wav" - }; - private static AssemblyName _Assembly { get { return Assembly.GetExecutingAssembly().GetName(); } diff --git a/Vocaluxe/Screens/CPopupScreenVolumeControl.cs b/Vocaluxe/Screens/CPopupScreenVolumeControl.cs index f95f651a..42b132bb 100644 --- a/Vocaluxe/Screens/CPopupScreenVolumeControl.cs +++ b/Vocaluxe/Screens/CPopupScreenVolumeControl.cs @@ -115,7 +115,7 @@ namespace Vocaluxe.Screens else { CConfig.BackgroundMusicVolume = _SelectSlides[_SelectSlideVolume].Selection * 5; - CBackgroundMusic.ApplyVolume(); + CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume); } break; @@ -125,7 +125,7 @@ namespace Vocaluxe.Screens default: CConfig.BackgroundMusicVolume = _SelectSlides[_SelectSlideVolume].Selection * 5; - CBackgroundMusic.ApplyVolume(); + CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume); break; } CConfig.SaveConfig(); diff --git a/Vocaluxe/Screens/CScreenLoad.cs b/Vocaluxe/Screens/CScreenLoad.cs index 0132a3af..03e29b55 100644 --- a/Vocaluxe/Screens/CScreenLoad.cs +++ b/Vocaluxe/Screens/CScreenLoad.cs @@ -42,8 +42,6 @@ namespace Vocaluxe.Screens private bool _IntroOutPlayed; private CVideoPlayer[] _Intros; - private bool _BGMusicStarted; - public override void Init() { base.Init(); @@ -99,8 +97,6 @@ namespace Vocaluxe.Screens } } - _SongLoaderThread = new Thread(CSongs.LoadSongs) {Name = "SongLoader"}; - _Texts[_TextStatus].Text = CLanguage.Translate("TR_SCREENLOAD_TOTAL") + ": 0 " + CLanguage.Translate("TR_SCREENLOAD_SONGS") + " (0 " + CLanguage.Translate("TR_SCREENLOAD_LOADED") + ")"; @@ -115,8 +111,6 @@ namespace Vocaluxe.Screens _Intros[i].Load(_IntroVideo[i]); _Texts[_TextProgramName].Visible = false; } - - _BGMusicStarted = false; } public override void OnShowFinish() @@ -130,21 +124,11 @@ namespace Vocaluxe.Screens } CLog.StartBenchmark("Load Songs Full"); - _SongLoaderThread.IsBackground = true; + _SongLoaderThread = new Thread(CSongs.LoadSongs) {Name = "SongLoader", IsBackground = true}; _SongLoaderThread.Start(); + CBackgroundMusic.OwnSongsAvailable = false; - if (CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON && - CConfig.BackgroundMusicSource == EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC && !_BGMusicStarted) - { - CBackgroundMusic.AddOwnMusic(); - - if (!CBackgroundMusic.IsPlaying) - CBackgroundMusic.Next(); - - _BGMusicStarted = true; - } - - CBackgroundMusic.CanSing = false; + CBackgroundMusic.Play(); } public override bool UpdateGame() @@ -164,15 +148,11 @@ namespace Vocaluxe.Screens CLanguage.Translate("TR_SCREENLOAD_SONGS") + " (" + CSongs.NumSongsWithCoverLoaded + " " + CLanguage.Translate("TR_SCREENLOAD_LOADED") + ")"; - if (CSongs.SongsLoaded && CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON && - CConfig.BackgroundMusicSource != EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC && !_BGMusicStarted) + if (CSongs.SongsLoaded && !CBackgroundMusic.OwnSongsAvailable) { - CBackgroundMusic.AddOwnMusic(); + CBackgroundMusic.OwnSongsAvailable = true; - if (!CBackgroundMusic.IsPlaying) - CBackgroundMusic.Next(); - - _BGMusicStarted = true; + CBackgroundMusic.Play(); } return true; @@ -196,8 +176,6 @@ namespace Vocaluxe.Screens foreach (CVideoPlayer videoPlayer in _Intros) videoPlayer.Close(); - CBackgroundMusic.CanSing = true; - CLog.StopBenchmark("Load Songs Full"); //Init Playlists @@ -224,6 +202,7 @@ namespace Vocaluxe.Screens } else if (_CurrentIntroVideo == 0 && _Intros[0].IsFinished && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART) { + _Intros[_CurrentIntroVideo].Close(); _CurrentIntroVideo = 1; _Intros[1].Loop = true; _Intros[1].Start(); @@ -231,6 +210,7 @@ namespace Vocaluxe.Screens else if ((_CurrentIntroVideo == 1 && CSongs.CoverLoaded) || (_CurrentIntroVideo == 0 && _Intros[0].IsFinished)) { + _Intros[_CurrentIntroVideo].Close(); _CurrentIntroVideo = 2; _Intros[2].Start(); } diff --git a/Vocaluxe/Screens/CScreenOptionsSound.cs b/Vocaluxe/Screens/CScreenOptionsSound.cs index 847586f8..8e417a2c 100644 --- a/Vocaluxe/Screens/CScreenOptionsSound.cs +++ b/Vocaluxe/Screens/CScreenOptionsSound.cs @@ -150,13 +150,16 @@ namespace Vocaluxe.Screens { CConfig.GameMusicVolume = _SelectSlides[_SelectSlideGameMusicVolume].Selection * 5; CConfig.PreviewMusicVolume = _SelectSlides[_SelectSlidePreviewMusicVolume].Selection * 5; + CConfig.BackgroundMusic=(EOffOn)_SelectSlides[_SelectSlideBackgroundMusic].Selection; + CConfig.BackgroundMusicSource = (EBackgroundMusicSource)_SelectSlides[_SelectSlideBackgroundMusicSource].Selection; + CConfig.BackgroundMusicVolume= _SelectSlides[_SelectSlideBackgroundMusicVolume].Selection * 5; CConfig.SaveConfig(); - var newOffOn = (EOffOn)_SelectSlides[_SelectSlideBackgroundMusic].Selection; - var newSource = (EBackgroundMusicSource)_SelectSlides[_SelectSlideBackgroundMusicSource].Selection; - float newVolume = _SelectSlides[_SelectSlideBackgroundMusicVolume].Selection * 5; - - CBackgroundMusic.CheckAndApplyConfig(newOffOn, newSource, newVolume); + CBackgroundMusic.SetMusicSource(CConfig.BackgroundMusicSource); + CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume); + if (CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON) + CBackgroundMusic.Play(); + else CBackgroundMusic.Pause(); } } } \ No newline at end of file diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index 047fbfd9..0dc33afe 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -216,6 +216,7 @@ + diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index b10cdb74..77c8f729 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -26,6 +26,22 @@ namespace VocaluxeLib { public static class CHelper { + private static readonly List _SoundFileTypes = new List + { + "*.mp3", + "*.wma", + "*.ogg", + "*.wav" + }; + + private static readonly List _ImageFileTypes = new List + { + "*.jpg", + "*.jpeg", + "*.png", + "*.gif" + }; + public static int CombinationCount(int n, int k) { if (k > n) @@ -61,7 +77,7 @@ namespace VocaluxeLib /// public static string ListStrings(string[] str) { - string result = string.Empty; + string result = String.Empty; for (int i = 0; i < str.Length; i++) { result += str[i]; @@ -95,7 +111,7 @@ namespace VocaluxeLib return 0; } int result; - return int.TryParse(value, out result) ? result : 0; + return Int32.TryParse(value, out result) ? result : 0; } public static void SetRect(RectangleF bounds, out RectangleF rect, float rectAspect, EAspect aspect) @@ -178,9 +194,25 @@ namespace VocaluxeLib return files; } + /// + /// Returns a list with all files in the given path that matches at least one of the given patterns + /// + /// Path to search for + /// List of patterns to match + /// Search directories recursively + /// False for just file names, True for full path + /// List of file names + public static List ListFiles(string path, IEnumerable searchPatterns, bool recursive = false, bool fullpath = false) + { + var files = new List(); + foreach (string pattern in searchPatterns) + files.AddRange(ListFiles(path, pattern, recursive, fullpath)); + + return files; + } + /// /// Returns a list with all image files in the given path - /// Searches for: jpg, jpeg, png, gif /// /// Path to search for /// Search directories recursively @@ -188,11 +220,19 @@ namespace VocaluxeLib /// List of image file names public static List ListImageFiles(string path, bool recursive = false, bool fullpath = false) { - List files = ListFiles(path, "*.jpg", recursive, fullpath); - files.AddRange(ListFiles(path, "*.jpeg", recursive, fullpath)); - files.AddRange(ListFiles(path, "*.png", recursive, fullpath)); - files.AddRange(ListFiles(path, "*.gif", recursive, fullpath)); - return files; + return ListFiles(path, _ImageFileTypes, recursive, fullpath); + } + + /// + /// Returns a list with all image files in the given path + /// + /// Path to search for + /// Search directories recursively + /// False for just file names, True for full path + /// List of image file names + public static List ListSoundFiles(string path, bool recursive = false, bool fullpath = false) + { + return ListFiles(path, _SoundFileTypes, recursive, fullpath); } public static bool TryParse(string value, out T result, bool ignoreCase = false) @@ -212,7 +252,7 @@ namespace VocaluxeLib public static bool TryParse(string value, out float result) { value = value.Replace(',', '.'); - return float.TryParse(value, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo, out result); + return Single.TryParse(value, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo, out result); } public static bool IsInBounds(SRectF bounds, SMouseEvent mouseEvent) diff --git a/VocaluxeLib/IBasic.cs b/VocaluxeLib/IBasic.cs index 616a8ebc..7fcf74ca 100644 --- a/VocaluxeLib/IBasic.cs +++ b/VocaluxeLib/IBasic.cs @@ -124,8 +124,6 @@ namespace VocaluxeLib void Pause(); void Play(); - void ApplyVolume(); - CTexture GetVideoTexture(); }