Rework CBackgroundMusic to avoid crash on startup and assure correct behaviour on runtime

This commit is contained in:
Flamefire committed 2014-03-18 16:46:59 +01:00
1 parent f9d29b6141
commit 6053102b8f
11 files changed
+354 -461

No files matched your search

+217 -396
View File
@@ -17,8 +17,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using VocaluxeLib; using VocaluxeLib;
using VocaluxeLib.Songs; using VocaluxeLib.Songs;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
@@ -27,25 +27,28 @@ namespace Vocaluxe.Base
{ {
static class CBackgroundMusic static class CBackgroundMusic
{ {
private static int _CurrentMusicStream = -1; private static bool _Initialized;
private static int _PreviousMusicIndex;
private static CPlaylistElement _CurrentPlaylistElement = new CPlaylistElement();
private static readonly List<CPlaylistElement> _AllFileNames = new List<CPlaylistElement>(); private static bool _OwnSongsAvailable;
private static readonly List<CPlaylistElement> _NotPlayedFileNames = new List<CPlaylistElement>(); private static EBackgroundMusicSource _MusicSource;
private static readonly List<CPlaylistElement> _BGMusicFileNames = new List<CPlaylistElement>(); private static float _Volume;
private static readonly List<CPlaylistElement> _PreviousFileNames = new List<CPlaylistElement>(); 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<CPlaylistElement> _BGMusicFiles = new List<CPlaylistElement>(); //Background music files
private static readonly List<CPlaylistElement> _NotPlayedFiles = new List<CPlaylistElement>(); //Not played files
private static readonly List<CPlaylistElement> _PreviousFiles = new List<CPlaylistElement>(); //Played files
private static int _Video = -1; private static int _Video = -1;
private static CTexture _CurrentVideoTexture; private static CTexture _CurrentVideoTexture;
private static CFading _VideoFading; private static CFading _VideoFading;
private static bool _VideoEnabled;
private static bool _OwnMusicAdded; private static bool _OwnMusicAdded;
private static bool _BackgroundMusicAdded; private static bool _BackgroundMusicAdded;
private static bool _Disabled; private static bool _Disabled;
private static bool _CanSing;
private static bool _RepeatSong;
public static bool VideoEnabled public static bool VideoEnabled
{ {
@@ -54,16 +57,19 @@ namespace Vocaluxe.Base
{ {
if (_VideoEnabled == value) if (_VideoEnabled == value)
return; return;
ToggleVideo();
_VideoEnabled = value; _VideoEnabled = value;
if (_VideoEnabled)
_LoadVideo();
else if (_Video != -1)
{
CVideo.Close(_Video);
CDraw.RemoveTexture(ref _CurrentVideoTexture);
_Video = -1;
}
} }
} }
public static bool CanSing public static bool CanSing { get; private set; }
{
get { return _CanSing; }
set { _CanSing = value; }
}
public static bool Disabled public static bool Disabled
{ {
@@ -78,20 +84,16 @@ namespace Vocaluxe.Base
} }
} }
public static bool RepeatSong public static bool RepeatSong { get; set; }
{
get { return _RepeatSong; }
set { _RepeatSong = value; }
}
public static int SongID public static int SongID
{ {
get { return _CurrentPlaylistElement.SongID; } get { return _CurrentPlaylistElement == null ? -1 : _CurrentPlaylistElement.SongID; }
} }
public static bool SongHasVideo 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; } public static bool IsPlaying { get; private set; }
@@ -100,6 +102,8 @@ namespace Vocaluxe.Base
{ {
get get
{ {
if (_CurrentPlaylistElement == null)
return "";
if (_CurrentPlaylistElement.Artist != "" && _CurrentPlaylistElement.Title != "") if (_CurrentPlaylistElement.Artist != "" && _CurrentPlaylistElement.Title != "")
return _CurrentPlaylistElement.Artist + " - " + _CurrentPlaylistElement.Title; return _CurrentPlaylistElement.Artist + " - " + _CurrentPlaylistElement.Title;
return Path.GetFileNameWithoutExtension(_CurrentPlaylistElement.MusicFilePath); return Path.GetFileNameWithoutExtension(_CurrentPlaylistElement.MusicFilePath);
@@ -108,50 +112,63 @@ namespace Vocaluxe.Base
public static CTexture Cover 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();
}
}
/// <summary>
/// Initializes the background music with values from config
/// Has to be called before any other method is used
/// </summary>
public static void Init() public static void Init()
{ {
var templist = new List<string>(); if (_Initialized)
return;
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();
_Volume = CConfig.BackgroundMusicVolume;
_VideoEnabled = (CConfig.VideoBackgrounds == EOffOn.TR_CONFIG_ON && CConfig.VideosToBackground == EOffOn.TR_CONFIG_ON); _VideoEnabled = (CConfig.VideoBackgrounds == EOffOn.TR_CONFIG_ON && CConfig.VideosToBackground == EOffOn.TR_CONFIG_ON);
_MusicSource = CConfig.BackgroundMusicSource;
List<string> 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; IsPlaying = false;
_Initialized = true;
} }
public static void Play() public static void Play()
{ {
if (IsPlaying) if (IsPlaying || CConfig.BackgroundMusic == EOffOn.TR_CONFIG_OFF)
return; return;
if (CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON) if (_CurrentMusicStream != -1)
{ {
if (_AllFileNames.Count > 0) //Resume
{ CSound.SetStreamVolume(_CurrentMusicStream, 0f);
if (_CurrentMusicStream != -1) CSound.Fade(_CurrentMusicStream, _Volume, CSettings.BackgroundMusicFadeTime);
{ CSound.Play(_CurrentMusicStream);
CSound.Fade(_CurrentMusicStream, 100f, CSettings.BackgroundMusicFadeTime); if (_VideoEnabled && _Video != -1)
CSound.Play(_CurrentMusicStream); CVideo.Resume(_Video);
if (_VideoEnabled && _Video != -1) IsPlaying = true;
CVideo.Resume(_Video);
IsPlaying = true;
}
else
Next();
_CanSing = !_IsBackgroundFile(_CurrentPlaylistElement);
}
} }
else
Next();
} }
public static void Stop() public static void Stop()
@@ -159,7 +176,7 @@ namespace Vocaluxe.Base
if (!IsPlaying) if (!IsPlaying)
return; return;
if (_VideoEnabled && _Video != -1) if (_Video != -1)
{ {
CVideo.Close(_Video); CVideo.Close(_Video);
CDraw.RemoveTexture(ref _CurrentVideoTexture); CDraw.RemoveTexture(ref _CurrentVideoTexture);
@@ -168,7 +185,7 @@ namespace Vocaluxe.Base
CSound.FadeAndClose(_CurrentMusicStream, 0f, CSettings.BackgroundMusicFadeTime); CSound.FadeAndClose(_CurrentMusicStream, 0f, CSettings.BackgroundMusicFadeTime);
_CurrentMusicStream = -1; _CurrentMusicStream = -1;
_CurrentPlaylistElement = new CPlaylistElement(); _CurrentPlaylistElement = null;
IsPlaying = false; IsPlaying = false;
} }
@@ -177,7 +194,7 @@ namespace Vocaluxe.Base
if (!IsPlaying) if (!IsPlaying)
return; return;
if (_VideoEnabled && _Video != -1) if (_Video != -1)
{ {
CVideo.Pause(_Video); CVideo.Pause(_Video);
CVideo.Skip(_Video, CSound.GetPosition(_CurrentMusicStream) + CSettings.BackgroundMusicFadeTime, _CurrentPlaylistElement.VideoGap); CVideo.Skip(_Video, CSound.GetPosition(_CurrentMusicStream) + CSettings.BackgroundMusicFadeTime, _CurrentPlaylistElement.VideoGap);
@@ -188,275 +205,202 @@ namespace Vocaluxe.Base
public static void Update() public static void Update()
{ {
if (_AllFileNames.Count > 0 && _CurrentMusicStream != -1) if (!IsPlaying)
{ return;
float timeToPlay; Debug.Assert(_CurrentMusicStream != -1 && _CurrentPlaylistElement != null);
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); float timeToPlay;
if (IsPlaying && ((timeToPlay <= CSettings.BackgroundMusicFadeTime && timeToPlay > 0) || finished)) 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();
}
}
/// <summary>
/// (Re-)Starts the _CurrentPlaylistElement
/// </summary>
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) if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON)
{ CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap + _CurrentPlaylistElement.Start);
//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 else
Next(); CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap);
} }
else
_LoadVideo();
} }
} }
public static void Next() 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 in the previous list and next element exists
{ _PreviousMusicIndex++;
//We are not currently in the previous list _CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex];
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();
} }
else 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() 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) Stop(); //stop last song
{ _PreviousMusicIndex--;
//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
_CurrentPlaylistElement = _PreviousFileNames[_PreviousMusicIndex]; _CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex];
CanSing = !_IsBackgroundFile(_CurrentPlaylistElement);
_CurrentMusicStream = CSound.Load(_CurrentPlaylistElement.MusicFilePath);
CSound.SetStreamVolumeMax(_CurrentMusicStream, CConfig.BackgroundMusicVolume);
if (_VideoEnabled)
_LoadVideo();
CSound.SetStreamVolume(_CurrentMusicStream, 0f);
Play();
}
} }
else if (_CurrentMusicStream != -1) _StartSong();
{
//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);
} }
public static void AddOwnMusic() public static void AddOwnMusic()
{ {
if (!_OwnMusicAdded) if (_OwnMusicAdded)
{ return;
foreach (CSong song in CSongs.AllSongs) foreach (CSong song in CSongs.AllSongs)
{ _NotPlayedFiles.Add(new CPlaylistElement(song));
_AllFileNames.Add(new CPlaylistElement(song));
_NotPlayedFileNames.Add(new CPlaylistElement(song));
}
}
_OwnMusicAdded = true; _OwnMusicAdded = true;
} }
public static void RemoveOwnMusic() private static void _RemoveOwnMusic()
{ {
_AllFileNames.Clear(); if (!_OwnMusicAdded)
_NotPlayedFileNames.Clear(); return;
_NotPlayedFiles.RemoveAll(el => el.SongID >= 0);
_PreviousFiles.RemoveAll(el => el.SongID >= 0);
_PreviousMusicIndex = _PreviousFiles.IndexOf(_CurrentPlaylistElement);
if (_BackgroundMusicAdded) if (IsPlaying && !_IsBackgroundFile(_CurrentPlaylistElement))
{
_AllFileNames.AddRange(_BGMusicFileNames);
_NotPlayedFileNames.AddRange(_BGMusicFileNames);
}
if (IsPlaying && !_IsBackgroundFile(_CurrentPlaylistElement) || _AllFileNames.Count == 0)
Next(); Next();
_OwnMusicAdded = false; _OwnMusicAdded = false;
} }
public static void AddBackgroundMusic() private static void _AddBackgroundMusic()
{
if (!_BackgroundMusicAdded)
{
_AllFileNames.AddRange(_BGMusicFileNames);
_NotPlayedFileNames.AddRange(_BGMusicFileNames);
_BackgroundMusicAdded = true;
}
}
public static void RemoveBackgroundMusic()
{ {
if (_BackgroundMusicAdded) if (_BackgroundMusicAdded)
{ return;
_AllFileNames.Clear(); _NotPlayedFiles.AddRange(_BGMusicFiles);
_OwnMusicAdded = false; _BackgroundMusicAdded = true;
AddOwnMusic(); }
_BackgroundMusicAdded = false;
if (IsPlaying && _IsBackgroundFile(_CurrentPlaylistElement) || _AllFileNames.Count == 0) private static void _RemoveBackgroundMusic()
Next(); {
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)) _Volume = volume;
{ if (_CurrentMusicStream >= 0)
CConfig.BackgroundMusicSource = newSource; CSound.SetStreamVolume(_CurrentMusicStream, _Volume);
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();
} }
public static CTexture GetVideoTexture() 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 (_VideoFading != null)
if (CVideo.GetFrame(_Video, ref _CurrentVideoTexture, CSound.GetPosition(_CurrentMusicStream), out vtime))
{ {
if (_VideoFading != null) bool finished;
{ _CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished);
bool finished; if (finished)
_CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished); _VideoFading = null;
if (finished)
_VideoFading = null;
}
return _CurrentVideoTexture;
} }
return _CurrentVideoTexture;
} }
return null; return null;
} }
private static bool _IsBackgroundFile(CPlaylistElement element) private static bool _IsBackgroundFile(CPlaylistElement element)
{ {
return _BGMusicFileNames.Any(elements => elements.MusicFilePath == element.MusicFilePath); return _BGMusicFiles.Contains(element);
} }
private static void _LoadVideo() private static void _LoadVideo()
@@ -464,134 +408,11 @@ namespace Vocaluxe.Base
if (_Video != -1 || String.IsNullOrEmpty(_CurrentPlaylistElement.VideoFilePath)) if (_Video != -1 || String.IsNullOrEmpty(_CurrentPlaylistElement.VideoFilePath))
return; return;
_Video = CVideo.Load(_CurrentPlaylistElement.VideoFilePath); _Video = CVideo.Load(_CurrentPlaylistElement.VideoFilePath);
if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) if (CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON)
CVideo.Skip(_Video, _CurrentPlaylistElement.Start, _CurrentPlaylistElement.VideoGap); CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap + _CurrentPlaylistElement.Start); //Use gap, otherwhise we will overwrite position in GetVideoTexture
else else
CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap);
_VideoFading = new CFading(0f, 1f, 3f); _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;
}
}
} }
+2 -6
View File
@@ -236,8 +236,6 @@ namespace Vocaluxe.Base
_Screens[(int)_CurrentScreen].OnClose(); _Screens[(int)_CurrentScreen].OnClose();
_CurrentScreen = _NextScreen; _CurrentScreen = _NextScreen;
_NextScreen = EScreens.ScreenNull; _NextScreen = EScreens.ScreenNull;
if (CBackgroundMusic.IsPlaying)
CBackgroundMusic.Play();
_Screens[(int)_CurrentScreen].OnShowFinish(); _Screens[(int)_CurrentScreen].OnShowFinish();
_Screens[(int)_CurrentScreen].ProcessMouseMove(_Cursor.X, _Cursor.Y); _Screens[(int)_CurrentScreen].ProcessMouseMove(_Cursor.X, _Cursor.Y);
@@ -431,7 +429,7 @@ namespace Vocaluxe.Base
else else
{ {
CConfig.BackgroundMusicVolume += diff; CConfig.BackgroundMusicVolume += diff;
CBackgroundMusic.ApplyVolume(); CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume);
} }
break; break;
@@ -442,7 +440,7 @@ namespace Vocaluxe.Base
default: default:
CConfig.BackgroundMusicVolume += diff; CConfig.BackgroundMusicVolume += diff;
CBackgroundMusic.ApplyVolume(); CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume);
break; break;
} }
CConfig.SaveConfig(); CConfig.SaveConfig();
@@ -453,8 +451,6 @@ namespace Vocaluxe.Base
ShowPopup(EPopupScreens.PopupVolumeControl); ShowPopup(EPopupScreens.PopupVolumeControl);
_VolumePopupTimer.Restart(); _VolumePopupTimer.Restart();
} }
CBackgroundMusic.ApplyVolume();
} }
if (keyEvent.ModShift && (keyEvent.Key == Keys.F1)) if (keyEvent.ModShift && (keyEvent.Key == Keys.F1))
-5
View File
@@ -360,11 +360,6 @@ namespace Vocaluxe.Base
CBackgroundMusic.Play(); CBackgroundMusic.Play();
} }
public void ApplyVolume()
{
CBackgroundMusic.ApplyVolume();
}
public CTexture GetVideoTexture() public CTexture GetVideoTexture()
{ {
return CBackgroundMusic.GetVideoTexture(); return CBackgroundMusic.GetVideoTexture();
+67
View File
@@ -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;
}
}
}
-8
View File
@@ -153,14 +153,6 @@ namespace Vocaluxe.Base
public const float SlideShowImageTime = 3500f; public const float SlideShowImageTime = 3500f;
public const float SlideShowFadeTime = 500f; public const float SlideShowFadeTime = 500f;
public static readonly List<string> MusicFileTypes = new List<string>
{
"*.mp3",
"*.wma",
"*.ogg",
"*.wav"
};
private static AssemblyName _Assembly private static AssemblyName _Assembly
{ {
get { return Assembly.GetExecutingAssembly().GetName(); } get { return Assembly.GetExecutingAssembly().GetName(); }
@@ -115,7 +115,7 @@ namespace Vocaluxe.Screens
else else
{ {
CConfig.BackgroundMusicVolume = _SelectSlides[_SelectSlideVolume].Selection * 5; CConfig.BackgroundMusicVolume = _SelectSlides[_SelectSlideVolume].Selection * 5;
CBackgroundMusic.ApplyVolume(); CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume);
} }
break; break;
@@ -125,7 +125,7 @@ namespace Vocaluxe.Screens
default: default:
CConfig.BackgroundMusicVolume = _SelectSlides[_SelectSlideVolume].Selection * 5; CConfig.BackgroundMusicVolume = _SelectSlides[_SelectSlideVolume].Selection * 5;
CBackgroundMusic.ApplyVolume(); CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume);
break; break;
} }
CConfig.SaveConfig(); CConfig.SaveConfig();
+8 -28
View File
@@ -42,8 +42,6 @@ namespace Vocaluxe.Screens
private bool _IntroOutPlayed; private bool _IntroOutPlayed;
private CVideoPlayer[] _Intros; private CVideoPlayer[] _Intros;
private bool _BGMusicStarted;
public override void Init() public override void Init()
{ {
base.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 " + _Texts[_TextStatus].Text = CLanguage.Translate("TR_SCREENLOAD_TOTAL") + ": 0 " +
CLanguage.Translate("TR_SCREENLOAD_SONGS") + " (0 " + CLanguage.Translate("TR_SCREENLOAD_SONGS") + " (0 " +
CLanguage.Translate("TR_SCREENLOAD_LOADED") + ")"; CLanguage.Translate("TR_SCREENLOAD_LOADED") + ")";
@@ -115,8 +111,6 @@ namespace Vocaluxe.Screens
_Intros[i].Load(_IntroVideo[i]); _Intros[i].Load(_IntroVideo[i]);
_Texts[_TextProgramName].Visible = false; _Texts[_TextProgramName].Visible = false;
} }
_BGMusicStarted = false;
} }
public override void OnShowFinish() public override void OnShowFinish()
@@ -130,21 +124,11 @@ namespace Vocaluxe.Screens
} }
CLog.StartBenchmark("Load Songs Full"); CLog.StartBenchmark("Load Songs Full");
_SongLoaderThread.IsBackground = true; _SongLoaderThread = new Thread(CSongs.LoadSongs) {Name = "SongLoader", IsBackground = true};
_SongLoaderThread.Start(); _SongLoaderThread.Start();
CBackgroundMusic.OwnSongsAvailable = false;
if (CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON && CBackgroundMusic.Play();
CConfig.BackgroundMusicSource == EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC && !_BGMusicStarted)
{
CBackgroundMusic.AddOwnMusic();
if (!CBackgroundMusic.IsPlaying)
CBackgroundMusic.Next();
_BGMusicStarted = true;
}
CBackgroundMusic.CanSing = false;
} }
public override bool UpdateGame() public override bool UpdateGame()
@@ -164,15 +148,11 @@ namespace Vocaluxe.Screens
CLanguage.Translate("TR_SCREENLOAD_SONGS") + " (" + CSongs.NumSongsWithCoverLoaded + " " + CLanguage.Translate("TR_SCREENLOAD_SONGS") + " (" + CSongs.NumSongsWithCoverLoaded + " " +
CLanguage.Translate("TR_SCREENLOAD_LOADED") + ")"; CLanguage.Translate("TR_SCREENLOAD_LOADED") + ")";
if (CSongs.SongsLoaded && CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON && if (CSongs.SongsLoaded && !CBackgroundMusic.OwnSongsAvailable)
CConfig.BackgroundMusicSource != EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC && !_BGMusicStarted)
{ {
CBackgroundMusic.AddOwnMusic(); CBackgroundMusic.OwnSongsAvailable = true;
if (!CBackgroundMusic.IsPlaying) CBackgroundMusic.Play();
CBackgroundMusic.Next();
_BGMusicStarted = true;
} }
return true; return true;
@@ -196,8 +176,6 @@ namespace Vocaluxe.Screens
foreach (CVideoPlayer videoPlayer in _Intros) foreach (CVideoPlayer videoPlayer in _Intros)
videoPlayer.Close(); videoPlayer.Close();
CBackgroundMusic.CanSing = true;
CLog.StopBenchmark("Load Songs Full"); CLog.StopBenchmark("Load Songs Full");
//Init Playlists //Init Playlists
@@ -224,6 +202,7 @@ namespace Vocaluxe.Screens
} }
else if (_CurrentIntroVideo == 0 && _Intros[0].IsFinished && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART) else if (_CurrentIntroVideo == 0 && _Intros[0].IsFinished && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART)
{ {
_Intros[_CurrentIntroVideo].Close();
_CurrentIntroVideo = 1; _CurrentIntroVideo = 1;
_Intros[1].Loop = true; _Intros[1].Loop = true;
_Intros[1].Start(); _Intros[1].Start();
@@ -231,6 +210,7 @@ namespace Vocaluxe.Screens
else if ((_CurrentIntroVideo == 1 && CSongs.CoverLoaded) || else if ((_CurrentIntroVideo == 1 && CSongs.CoverLoaded) ||
(_CurrentIntroVideo == 0 && _Intros[0].IsFinished)) (_CurrentIntroVideo == 0 && _Intros[0].IsFinished))
{ {
_Intros[_CurrentIntroVideo].Close();
_CurrentIntroVideo = 2; _CurrentIntroVideo = 2;
_Intros[2].Start(); _Intros[2].Start();
} }
+8 -5
View File
@@ -150,13 +150,16 @@ namespace Vocaluxe.Screens
{ {
CConfig.GameMusicVolume = _SelectSlides[_SelectSlideGameMusicVolume].Selection * 5; CConfig.GameMusicVolume = _SelectSlides[_SelectSlideGameMusicVolume].Selection * 5;
CConfig.PreviewMusicVolume = _SelectSlides[_SelectSlidePreviewMusicVolume].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(); CConfig.SaveConfig();
var newOffOn = (EOffOn)_SelectSlides[_SelectSlideBackgroundMusic].Selection; CBackgroundMusic.SetMusicSource(CConfig.BackgroundMusicSource);
var newSource = (EBackgroundMusicSource)_SelectSlides[_SelectSlideBackgroundMusicSource].Selection; CBackgroundMusic.SetVolume(CConfig.BackgroundMusicVolume);
float newVolume = _SelectSlides[_SelectSlideBackgroundMusicVolume].Selection * 5; if (CConfig.BackgroundMusic == EOffOn.TR_CONFIG_ON)
CBackgroundMusic.Play();
CBackgroundMusic.CheckAndApplyConfig(newOffOn, newSource, newVolume); else CBackgroundMusic.Pause();
} }
} }
} }
+1
View File
@@ -216,6 +216,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Base\CBackgroundMusic.cs" /> <Compile Include="Base\CBackgroundMusic.cs" />
<Compile Include="Base\CPlaylistElement.cs" />
<Compile Include="Base\CRecord.cs" /> <Compile Include="Base\CRecord.cs" />
<Compile Include="COSFunctions.cs" /> <Compile Include="COSFunctions.cs" />
<Compile Include="Lib\Sound\Playback\GstreamerSharp\CGstreamerSharpAudioStream.cs" /> <Compile Include="Lib\Sound\Playback\GstreamerSharp\CGstreamerSharpAudioStream.cs" />
+49 -9
View File
@@ -26,6 +26,22 @@ namespace VocaluxeLib
{ {
public static class CHelper public static class CHelper
{ {
private static readonly List<string> _SoundFileTypes = new List<string>
{
"*.mp3",
"*.wma",
"*.ogg",
"*.wav"
};
private static readonly List<string> _ImageFileTypes = new List<string>
{
"*.jpg",
"*.jpeg",
"*.png",
"*.gif"
};
public static int CombinationCount(int n, int k) public static int CombinationCount(int n, int k)
{ {
if (k > n) if (k > n)
@@ -61,7 +77,7 @@ namespace VocaluxeLib
/// </summary> /// </summary>
public static string ListStrings(string[] str) public static string ListStrings(string[] str)
{ {
string result = string.Empty; string result = String.Empty;
for (int i = 0; i < str.Length; i++) for (int i = 0; i < str.Length; i++)
{ {
result += str[i]; result += str[i];
@@ -95,7 +111,7 @@ namespace VocaluxeLib
return 0; return 0;
} }
int result; 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) public static void SetRect(RectangleF bounds, out RectangleF rect, float rectAspect, EAspect aspect)
@@ -178,9 +194,25 @@ namespace VocaluxeLib
return files; return files;
} }
/// <summary>
/// Returns a list with all files in the given path that matches at least one of the given patterns
/// </summary>
/// <param name="path">Path to search for</param>
/// <param name="searchPatterns">List of patterns to match</param>
/// <param name="recursive">Search directories recursively</param>
/// <param name="fullpath">False for just file names, True for full path</param>
/// <returns>List of file names</returns>
public static List<string> ListFiles(string path, IEnumerable<string> searchPatterns, bool recursive = false, bool fullpath = false)
{
var files = new List<string>();
foreach (string pattern in searchPatterns)
files.AddRange(ListFiles(path, pattern, recursive, fullpath));
return files;
}
/// <summary> /// <summary>
/// Returns a list with all image files in the given path /// Returns a list with all image files in the given path
/// Searches for: jpg, jpeg, png, gif
/// </summary> /// </summary>
/// <param name="path">Path to search for</param> /// <param name="path">Path to search for</param>
/// <param name="recursive">Search directories recursively</param> /// <param name="recursive">Search directories recursively</param>
@@ -188,11 +220,19 @@ namespace VocaluxeLib
/// <returns>List of image file names</returns> /// <returns>List of image file names</returns>
public static List<string> ListImageFiles(string path, bool recursive = false, bool fullpath = false) public static List<string> ListImageFiles(string path, bool recursive = false, bool fullpath = false)
{ {
List<string> files = ListFiles(path, "*.jpg", recursive, fullpath); return ListFiles(path, _ImageFileTypes, recursive, fullpath);
files.AddRange(ListFiles(path, "*.jpeg", recursive, fullpath)); }
files.AddRange(ListFiles(path, "*.png", recursive, fullpath));
files.AddRange(ListFiles(path, "*.gif", recursive, fullpath)); /// <summary>
return files; /// Returns a list with all image files in the given path
/// </summary>
/// <param name="path">Path to search for</param>
/// <param name="recursive">Search directories recursively</param>
/// <param name="fullpath">False for just file names, True for full path</param>
/// <returns>List of image file names</returns>
public static List<string> ListSoundFiles(string path, bool recursive = false, bool fullpath = false)
{
return ListFiles(path, _SoundFileTypes, recursive, fullpath);
} }
public static bool TryParse<T>(string value, out T result, bool ignoreCase = false) public static bool TryParse<T>(string value, out T result, bool ignoreCase = false)
@@ -212,7 +252,7 @@ namespace VocaluxeLib
public static bool TryParse(string value, out float result) public static bool TryParse(string value, out float result)
{ {
value = value.Replace(',', '.'); 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) public static bool IsInBounds(SRectF bounds, SMouseEvent mouseEvent)
-2
View File
@@ -124,8 +124,6 @@ namespace VocaluxeLib
void Pause(); void Pause();
void Play(); void Play();
void ApplyVolume();
CTexture GetVideoTexture(); CTexture GetVideoTexture();
} }