diff --git a/PartyModes/PartyModeTicTacToe/CPartyScreenTicTacToeMain.cs b/PartyModes/PartyModeTicTacToe/CPartyScreenTicTacToeMain.cs index 2131cd14..bdfbf120 100644 --- a/PartyModes/PartyModeTicTacToe/CPartyScreenTicTacToeMain.cs +++ b/PartyModes/PartyModeTicTacToe/CPartyScreenTicTacToeMain.cs @@ -379,7 +379,7 @@ namespace VocaluxeLib.PartyModes.TicTacToe { base.OnClose(); CBase.BackgroundMusic.SetStatus(false); - CBase.Sound.FadeAndClose(_PreviewStream, 0f, 0.5f); + CBase.Sound.Fade(_PreviewStream, 0f, 0.5f, EStreamAction.Close); } private void _CreateFields() @@ -574,7 +574,7 @@ namespace VocaluxeLib.PartyModes.TicTacToe { _GameData.NumJokerRandom[teamNr]--; if (!CBase.Sound.IsFinished(_PreviewStream)) - CBase.Sound.FadeAndClose(_PreviewStream, 0, 1); + CBase.Sound.Fade(_PreviewStream, 0, 1, EStreamAction.Close); _FieldSelected(true); } break; @@ -586,7 +586,7 @@ namespace VocaluxeLib.PartyModes.TicTacToe _GameData.NumJokerRetry[teamNr]--; _GameData.Team = teamNr; if (!CBase.Sound.IsFinished(_PreviewStream)) - CBase.Sound.FadeAndClose(_PreviewStream, 0, 1); + CBase.Sound.Fade(_PreviewStream, 0, 1, EStreamAction.Close); _Status = EStatus.JokerRetry; _OldSelectedField = _SelectedField; _SelectedField = -1; @@ -627,7 +627,7 @@ namespace VocaluxeLib.PartyModes.TicTacToe private void _NextRound() { - CBase.Sound.FadeAndClose(_PreviewStream, 0f, 0.5f); + CBase.Sound.Fade(_PreviewStream, 0f, 0.5f, EStreamAction.Close); _Data.ScreenMain.Rounds = _GameData.Rounds; _Data.ScreenMain.FadeToNameSelection = false; _Data.ScreenMain.FadeToSinging = true; @@ -640,7 +640,7 @@ namespace VocaluxeLib.PartyModes.TicTacToe private void _EndParty() { - CBase.Sound.FadeAndClose(_PreviewStream, 0f, 0.5f); + CBase.Sound.Fade(_PreviewStream, 0f, 0.5f, EStreamAction.Close); _FadeTo(EScreens.ScreenParty); } @@ -659,7 +659,7 @@ namespace VocaluxeLib.PartyModes.TicTacToe private void _Back() { - CBase.Sound.FadeAndClose(_PreviewStream, 0f, 0.5f); + CBase.Sound.Fade(_PreviewStream, 0f, 0.5f, EStreamAction.Close); _Data.ScreenMain.FadeToNameSelection = true; _Data.ScreenMain.FadeToSinging = false; _Data.ScreenMain.SingRoundNr = _SelectedField; diff --git a/Vocaluxe/Base/CBackgroundMusic.cs b/Vocaluxe/Base/CBackgroundMusic.cs index b4be306f..0e4dbc44 100644 --- a/Vocaluxe/Base/CBackgroundMusic.cs +++ b/Vocaluxe/Base/CBackgroundMusic.cs @@ -22,6 +22,7 @@ using System.IO; using VocaluxeLib; using VocaluxeLib.Songs; using VocaluxeLib.Draw; +using VocaluxeLib.Utils.Player; namespace Vocaluxe.Base { @@ -29,46 +30,30 @@ namespace Vocaluxe.Base { private static bool _Initialized; + private static CSongPlayer _SongPlayer; + private static bool _OwnSongsAvailable; private static EBackgroundMusicSource _MusicSource; 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 int _PreviousMusicIndex = -1; 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 _OwnMusicAdded; private static bool _BackgroundMusicAdded; private static bool _Disabled; public static bool VideoEnabled { - get { return _VideoEnabled; } - set - { - if (_VideoEnabled == value) - return; - _VideoEnabled = value; - if (_VideoEnabled) - _LoadVideo(); - else if (_Video != -1) - { - CVideo.Close(_Video); - CDraw.RemoveTexture(ref _CurrentVideoTexture); - _Video = -1; - } - } + get { return _SongPlayer.VideoEnabled; } + set { _SongPlayer.VideoEnabled = value; } } - public static bool CanSing { get; private set; } + public static bool CanSing { get { return _BGMusicFiles.Contains(_CurrentPlaylistElement) ? false : _SongPlayer.CanSing; } } public static bool Disabled { @@ -83,35 +68,33 @@ namespace Vocaluxe.Base } } - public static bool RepeatSong { get; set; } + public static bool RepeatSong { + get { return _SongPlayer.RepeatSong; } + set { _SongPlayer.RepeatSong = value; } + } public static int SongID { - get { return _CurrentPlaylistElement == null ? -1 : _CurrentPlaylistElement.SongID; } + get { return _SongPlayer.SongID; } } public static bool SongHasVideo { - get { return _CurrentPlaylistElement != null && File.Exists(_CurrentPlaylistElement.VideoFilePath); } + get { return _SongPlayer.SongHasVideo; } } - public static bool IsPlaying { get; private set; } + public static bool IsPlaying { get { return _SongPlayer.IsPlaying; } } public static string ArtistAndTitle { - get - { - if (_CurrentPlaylistElement == null) - return ""; - if (_CurrentPlaylistElement.Artist != "" && _CurrentPlaylistElement.Title != "") - return _CurrentPlaylistElement.Artist + " - " + _CurrentPlaylistElement.Title; - return Path.GetFileNameWithoutExtension(_CurrentPlaylistElement.MusicFilePath); + get { + return _BGMusicFiles.Contains(_CurrentPlaylistElement) ? Path.GetFileNameWithoutExtension(_CurrentPlaylistElement.MusicFilePath) :_SongPlayer.ArtistAndTitle; } } public static CTexture Cover { - get { return _CurrentPlaylistElement == null ? CCover.NoCover : _CurrentPlaylistElement.Cover; } + get { return _SongPlayer.Cover; } } //Use this to set whether own songs are available for access @@ -143,8 +126,9 @@ namespace Vocaluxe.Base _MusicSource = EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC; _AddBackgroundMusic(); - _PreviousMusicIndex = -1; - IsPlaying = false; + _SongPlayer = new CSongPlayer(); + _SongPlayer.Volume = CConfig.BackgroundMusicVolume; + _VideoEnabled = (CConfig.VideoBackgrounds == EOffOn.TR_CONFIG_ON && CConfig.VideosToBackground == EOffOn.TR_CONFIG_ON); SetMusicSource(CConfig.BackgroundMusicSource); _Initialized = true; @@ -152,22 +136,12 @@ namespace Vocaluxe.Base public static void Close() { - if (_CurrentMusicStream >= 0) - { - CSound.Close(_CurrentMusicStream); - _CurrentMusicStream = -1; - } - if (_Video != -1) - { - CVideo.Close(_Video); - CDraw.RemoveTexture(ref _CurrentVideoTexture); - _Video = -1; - } + _SongPlayer.Close(); + _BGMusicFiles.Clear(); _NotPlayedFiles.Clear(); _PreviousFiles.Clear(); - IsPlaying = false; _Initialized = false; } @@ -176,15 +150,10 @@ namespace Vocaluxe.Base if (IsPlaying || CConfig.BackgroundMusic == EOffOn.TR_CONFIG_OFF) return; - if (_CurrentMusicStream != -1) + if (_SongPlayer.SongLoaded) { //Resume - CSound.SetStreamVolume(_CurrentMusicStream, 0f); - CSound.Fade(_CurrentMusicStream, 100f, CSettings.BackgroundMusicFadeTime); - CSound.Play(_CurrentMusicStream); - if (_VideoEnabled && _Video != -1) - CVideo.Resume(_Video); - IsPlaying = true; + _SongPlayer.TogglePause(); } else Next(); @@ -195,17 +164,9 @@ namespace Vocaluxe.Base if (!IsPlaying) return; - if (_Video != -1) - { - CVideo.Close(_Video); - CDraw.RemoveTexture(ref _CurrentVideoTexture); - _Video = -1; - } - CSound.FadeAndClose(_CurrentMusicStream, 0f, CSettings.BackgroundMusicFadeTime); - _CurrentMusicStream = -1; + _SongPlayer.Stop(); _CurrentPlaylistElement = null; - IsPlaying = false; } public static void Pause() @@ -213,13 +174,7 @@ namespace Vocaluxe.Base if (!IsPlaying) return; - if (_Video != -1) - { - CVideo.Pause(_Video); - CVideo.Skip(_Video, CSound.GetPosition(_CurrentMusicStream) + CSettings.BackgroundMusicFadeTime, _CurrentPlaylistElement.VideoGap); - } - CSound.FadeAndPause(_CurrentMusicStream, 0f, CSettings.BackgroundMusicFadeTime); - IsPlaying = false; + _SongPlayer.TogglePause(); } public static void Next() @@ -248,68 +203,36 @@ namespace Vocaluxe.Base _PreviousMusicIndex = _PreviousFiles.Count - 1; } _StartSong(); - CanSing = !_IsBackgroundFile(_CurrentPlaylistElement); } public static void Previous() { if (_PreviousMusicIndex < 0) return; - Debug.Assert(_CurrentMusicStream != -1 && _CurrentPlaylistElement != null); - if (CSound.GetPosition(_CurrentMusicStream) <= 1.5f && _PreviousMusicIndex > 0) + Debug.Assert(_CurrentPlaylistElement != null); + if (_SongPlayer.Position <= 1.5f && _PreviousMusicIndex > 0) { Stop(); //stop last song _PreviousMusicIndex--; _CurrentPlaylistElement = _PreviousFiles[_PreviousMusicIndex]; - CanSing = !_IsBackgroundFile(_CurrentPlaylistElement); } _StartSong(); } public static void Update() { - if (!IsPlaying) + if (!IsPlaying && !_SongPlayer.SongLoaded) return; - Debug.Assert(_CurrentMusicStream != -1 && _CurrentPlaylistElement != null); - float timeToPlay; - if (Math.Abs(_CurrentPlaylistElement.Finish) < 0.001) - { - //No End-Tag defined - float len = CSound.GetLength(_CurrentMusicStream); - timeToPlay = (len > 0f) ? len - CSound.GetPosition(_CurrentMusicStream) : CSettings.BackgroundMusicFadeTime + 1f; - } - 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(); - } + _SongPlayer.Update(); + if (_SongPlayer.IsFinished) + Next(); } public static CTexture GetVideoTexture() { - if (_Video == -1) - return null; - float vtime; - if (CVideo.GetFrame(_Video, ref _CurrentVideoTexture, CSound.GetPosition(_CurrentMusicStream), out vtime)) - { - if (_VideoFading != null) - { - bool finished; - _CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished); - if (finished) - _VideoFading = null; - } - return _CurrentVideoTexture; - } - return null; + return _SongPlayer.GetVideoTexture(); } public static void SetMusicSource(EBackgroundMusicSource source) @@ -341,32 +264,17 @@ namespace Vocaluxe.Base 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, 100f, 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 (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) - CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap + _CurrentPlaylistElement.Start); - else - CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap); - } - else - _LoadVideo(); - } + //If current song same as loaded restart only + if (_CurrentPlaylistElement.SongID == _SongPlayer.SongID) + _SongPlayer.Play(); + + //otherwhise load + //Seek to #Start-Tag, if found + float start = 0f; + if (_CurrentPlaylistElement.Start > 0.001 && CConfig.BackgroundMusicUseStart == EOffOn.TR_CONFIG_ON) + start = _CurrentPlaylistElement.Start; + _SongPlayer.Load(CBase.Songs.GetSongByID(_CurrentPlaylistElement.SongID), start, true); } private static void _AddOwnMusic() @@ -419,16 +327,5 @@ namespace Vocaluxe.Base return _BGMusicFiles.Contains(element); } - private static void _LoadVideo() - { - if (_Video != -1 || String.IsNullOrEmpty(_CurrentPlaylistElement.VideoFilePath)) - return; - _Video = CVideo.Load(_CurrentPlaylistElement.VideoFilePath); - 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); - } } } \ No newline at end of file diff --git a/Vocaluxe/Base/CMain.cs b/Vocaluxe/Base/CMain.cs index a47c74a7..3daec831 100644 --- a/Vocaluxe/Base/CMain.cs +++ b/Vocaluxe/Base/CMain.cs @@ -243,6 +243,11 @@ namespace Vocaluxe.Base { return CSettings.SlideShowFadeTime; } + + public float GetSoundPlayerFadeTime() + { + return CSettings.SoundPlayerFadeTime; + } } class CBtheme : ITheme @@ -774,6 +779,16 @@ namespace Vocaluxe.Base { CVideo.SetLoop(streamID, loop); } + + public void Pause(int streamID) + { + CVideo.Pause(streamID); + } + + public void Resume(int streamID) + { + CVideo.Resume(streamID); + } } class CBsound : ISound @@ -793,9 +808,14 @@ namespace Vocaluxe.Base CSound.Play(soundStream); } - public void Fade(int soundStream, float targetVolume, float duration) + public void Fade(int soundStream, float targetVolume, float duration, EStreamAction afterFadeAction = EStreamAction.Nothing) { - CSound.Fade(soundStream, targetVolume, duration); + CSound.Fade(soundStream, targetVolume, duration, afterFadeAction); + } + + public void Close(int soundStream) + { + CSound.Close(soundStream); } public bool IsFinished(int soundStream) @@ -813,11 +833,6 @@ namespace Vocaluxe.Base return CSound.GetLength(soundStream); } - public void FadeAndClose(int soundStream, float targetVolume, float duration) - { - CSound.FadeAndClose(soundStream, targetVolume, duration); - } - public void SetStreamVolume(int soundStream, float volume) { CSound.SetStreamVolume(soundStream, volume); diff --git a/Vocaluxe/Base/CSettings.cs b/Vocaluxe/Base/CSettings.cs index 3de72e98..a1326c55 100644 --- a/Vocaluxe/Base/CSettings.cs +++ b/Vocaluxe/Base/CSettings.cs @@ -145,6 +145,8 @@ namespace Vocaluxe.Base public const float BackgroundMusicFadeTime = 0.5f; public const float PauseResetTime = 1f; + public const float SoundPlayerFadeTime = 0.5f; + public const float SlideShowImageTime = 3500f; public const float SlideShowFadeTime = 500f; diff --git a/Vocaluxe/Lib/Sound/Playback/IAudioStream.cs b/Vocaluxe/Lib/Sound/Playback/IAudioStream.cs index 8ec763fe..15d88270 100644 --- a/Vocaluxe/Lib/Sound/Playback/IAudioStream.cs +++ b/Vocaluxe/Lib/Sound/Playback/IAudioStream.cs @@ -16,6 +16,7 @@ #endregion using System; +using VocaluxeLib; namespace Vocaluxe.Lib.Sound.Playback { diff --git a/Vocaluxe/Lib/Sound/Playback/IPlayback.cs b/Vocaluxe/Lib/Sound/Playback/IPlayback.cs index 04d9be75..a022d455 100644 --- a/Vocaluxe/Lib/Sound/Playback/IPlayback.cs +++ b/Vocaluxe/Lib/Sound/Playback/IPlayback.cs @@ -15,15 +15,10 @@ // along with Vocaluxe. If not, see . #endregion +using VocaluxeLib; + namespace Vocaluxe.Lib.Sound.Playback { - public enum EStreamAction - { - Nothing, - Pause, - Stop, - Close - } interface IPlayback { diff --git a/VocaluxeLib/Enums.cs b/VocaluxeLib/Enums.cs index 0df1d888..e7bf8270 100644 --- a/VocaluxeLib/Enums.cs +++ b/VocaluxeLib/Enums.cs @@ -288,6 +288,14 @@ namespace VocaluxeLib } #endregion Config + public enum EStreamAction + { + Nothing, + Pause, + Stop, + Close + } + public enum EProgramState { Start, diff --git a/VocaluxeLib/IBasic.cs b/VocaluxeLib/IBasic.cs index 9d84374d..4785d045 100644 --- a/VocaluxeLib/IBasic.cs +++ b/VocaluxeLib/IBasic.cs @@ -89,6 +89,8 @@ namespace VocaluxeLib float GetSlideShowImageTime(); float GetSlideShowFadeTime(); + + float GetSoundPlayerFadeTime(); } public interface ITheme @@ -253,6 +255,8 @@ namespace VocaluxeLib bool IsFinished(int streamID); bool Close(int streamID); void SetLoop(int streamID, bool loop = true); + void Resume(int streamID); + void Pause(int streamID); } public interface ISound @@ -260,12 +264,12 @@ namespace VocaluxeLib int Load(string soundFile, bool loop = false, bool prescan = false); void SetPosition(int soundStream, float newPosition); void Play(int soundStream); - void Fade(int soundStream, float targetVolume, float duration); + void Fade(int soundStream, float targetVolume, float duration, EStreamAction afterFadeAction = EStreamAction.Nothing); + void Close(int soundStream); bool IsFinished(int soundStream); float GetPosition(int soundStream); float GetLength(int soundStream); - void FadeAndClose(int soundStream, float targetVolume, float duration); void SetStreamVolume(int soundStream, float volume); } diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs index e7f260f9..70705f1c 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs @@ -561,7 +561,7 @@ namespace VocaluxeLib.Menu.SongMenu protected void _Reset() { foreach (int stream in _Streams) - CBase.Sound.FadeAndClose(stream, 0f, 0.75f); + CBase.Sound.Fade(stream, 0f, 0.75f, EStreamAction.Close); _Streams.Clear(); CBase.Video.Close(_PreviewVideoStream); diff --git a/VocaluxeLib/Utils/Player/CSongPlayer.cs b/VocaluxeLib/Utils/Player/CSongPlayer.cs new file mode 100644 index 00000000..55da4687 --- /dev/null +++ b/VocaluxeLib/Utils/Player/CSongPlayer.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +using VocaluxeLib.Draw; +using VocaluxeLib.Songs; + +namespace VocaluxeLib.Utils.Player +{ + public class CSongPlayer : CSoundPlayer + { + private CSong _Song; + private bool _VideoEnabled; + private int _Video = -1; + private CTexture _CurrentVideoTexture; + private CFading _VideoFading; + + public bool VideoEnabled + { + get { return _VideoEnabled; } + set + { + if (_VideoEnabled == value) + return; + _VideoEnabled = value; + if (_VideoEnabled) + _LoadVideo(); + else if (_Video != -1) + { + CBase.Video.Close(_Video); + CBase.Drawing.RemoveTexture(ref _CurrentVideoTexture); + _Video = -1; + } + } + } + public bool CanSing { get; private set; } + public int SongID + { + get { return _Song == null ? -1 : _Song.ID; } + } + public bool SongHasVideo + { + get { return _Song != null && File.Exists(Path.Combine(_Song.Folder,_Song.VideoFileName)); } + } + + public string ArtistAndTitle + { + get + { + if (_Song == null) + return ""; + if (_Song.Artist != "" && _Song.Title != "") + return _Song.Artist + " - " + _Song.Title; + return Path.GetFileNameWithoutExtension(_Song.MP3FileName); + } + } + + public CTexture Cover + { + get { return _Song == null ? CBase.Cover.GetNoCover() : _Song.CoverTextureBig; } + } + + public bool SongLoaded + { + get { return _StreamID != -1; } + } + + public CSongPlayer(bool loop = false) : base(loop) { } + + public CSongPlayer(CSong song, bool loop = false, float position = 0f, bool autoplay = false) + : base(Path.Combine(song.Folder,song.MP3FileName), loop, position, autoplay) + { + _Song = song; + _LoadVideo(); + } + + public CTexture GetVideoTexture() + { + if (_Video == -1 || _Song == null) + return null; + float vtime; + if (CBase.Video.GetFrame(_Video, ref _CurrentVideoTexture, CBase.Sound.GetPosition(_StreamID), out vtime)) + { + if (_VideoFading != null) + { + bool finished; + _CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished); + if (finished) + _VideoFading = null; + } + return _CurrentVideoTexture; + } + return null; + } + + public void Load(CSong song, float position = 0f, bool autoplay = false) + { + if (song == null) + return; + + _Song = song; + _LoadVideo(); + base.Load(Path.Combine(song.Folder, song.MP3FileName), position, autoplay); + } + + public new void Close() + { + base.Close(); + + if (_Video != -1) + { + CBase.Video.Close(_Video); + CBase.Drawing.RemoveTexture(ref _CurrentVideoTexture); + _Video = -1; + } + } + + public new void Play() + { + base.Play(); + + if (_VideoEnabled && _Video != -1) + CBase.Video.Resume(_Video); + } + + public new void Stop() + { + base.Stop(); + + if (_Video != -1) + { + CBase.Video.Close(_Video); + CBase.Drawing.RemoveTexture(ref _CurrentVideoTexture); + _Video = -1; + } + } + + public new void TogglePause() + { + base.TogglePause(); + if (_Video != -1) + { + CBase.Video.Pause(_Video); + CBase.Video.Skip(_Video, CBase.Sound.GetPosition(_StreamID) + _FadeTime, _Song.VideoGap); + } + } + + public new void Update() + { + if (_StreamID == -1 || !IsPlaying) + return; + + float timeToPlay; + float len = CBase.Sound.GetLength(_StreamID); + timeToPlay = (len > 0f) ? len - CBase.Sound.GetPosition(_StreamID) : _FadeTime + 1f; + + bool finished = CBase.Sound.IsFinished(_StreamID); + if (timeToPlay <= _FadeTime || finished) + { + if (RepeatSong) + Play(); + else + Stop(); + } + } + + private void _LoadVideo() + { + if (_Song == null) + return; + + string videoFilePath = Path.Combine(_Song.Folder, _Song.VideoFileName); + if (_Video != -1 || String.IsNullOrEmpty(videoFilePath)) + return; + _Video = CBase.Video.Load(videoFilePath); + CBase.Video.Skip(_Video, 0f, _Song.VideoGap + _StartPosition); //Use gap, otherwhise we will overwrite position in GetVideoTexture + _VideoFading = new CFading(0f, 1f, 3f); + } + } +} diff --git a/VocaluxeLib/Utils/Player/CSoundPlayer.cs b/VocaluxeLib/Utils/Player/CSoundPlayer.cs new file mode 100644 index 00000000..59e858de --- /dev/null +++ b/VocaluxeLib/Utils/Player/CSoundPlayer.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace VocaluxeLib.Utils.Player +{ + public class CSoundPlayer + { + protected int _StreamID = -1; + protected float _Volume; + protected float _StartPosition; + protected float _FadeTime = CBase.Settings.GetSoundPlayerFadeTime(); + + public float Volume + { + set + { + _Volume = value; + _ApplyVolume(); + } + get { return _Volume; } + } + public bool Loop; + + public float Position + { + set { + if(_StreamID == -1) + return; + CBase.Sound.SetPosition(_StreamID, value); + _StartPosition = value; + } + get { + if(_StreamID == -1) + return -1; + return CBase.Sound.GetPosition(_StreamID); + } + } + + public bool RepeatSong; + public bool IsPlaying { get; private set; } + + public bool IsFinished { get { return CBase.Sound.IsFinished(_StreamID); } } + + public CSoundPlayer(bool loop = false) + { + Loop = loop; + } + + public CSoundPlayer(string file, bool loop = false, float position = 0f, bool autoplay = false) + { + _StreamID = CBase.Sound.Load(file, false, true); + if(position > 0f) + Position = position; + Loop = loop; + if (autoplay) + Play(); + } + + public void Load(string file, float position = 0f, bool autoplay = false) + { + if (IsPlaying) + Stop(); + + _StreamID = CBase.Sound.Load(file, false, true); + if (position > 0f) + Position = position; + if (autoplay) + Play(); + } + + public void Play() + { + if (_StreamID == -1) + return; + + CBase.Sound.SetPosition(_StreamID, _StartPosition); + CBase.Sound.SetStreamVolume(_StreamID, 0f); + CBase.Sound.Fade(_StreamID, Volume, _FadeTime); + CBase.Sound.Play(_StreamID); + IsPlaying = true; + } + + public void TogglePause() + { + if (_StreamID == -1) + return; + + if(IsPlaying) + CBase.Sound.Fade(_StreamID, 0f, _FadeTime, EStreamAction.Pause); + else + { + CBase.Sound.Fade(_StreamID, Volume, _FadeTime); + CBase.Sound.Play(_StreamID); + } + + IsPlaying = !IsPlaying; + } + + public void Stop() + { + if (_StreamID == -1 || !IsPlaying) + return; + + CBase.Sound.Fade(_StreamID, 0f, _FadeTime, EStreamAction.Stop); + IsPlaying = false; + } + + public void Update() + { + if (_StreamID == -1 || !IsPlaying) + return; + + float timeToPlay; + float len = CBase.Sound.GetLength(_StreamID); + timeToPlay = (len > 0f) ? len - CBase.Sound.GetPosition(_StreamID) : _FadeTime + 1f; + + bool finished = CBase.Sound.IsFinished(_StreamID); + if (timeToPlay <= _FadeTime || finished) + { + if (RepeatSong) + Play(); + else + Stop(); + } + + } + + public void Close() + { + if (_StreamID == -1) + return; + + CBase.Sound.Close(_StreamID); + _StreamID = -1; + IsPlaying = false; + } + + private void _ApplyVolume() + { + if (_StreamID == -1) + return; + + CBase.Sound.SetStreamVolume(_StreamID, Volume); + } + } +} diff --git a/VocaluxeLib/VocaluxeLib.csproj b/VocaluxeLib/VocaluxeLib.csproj index 3e17ea26..a367e8e1 100644 --- a/VocaluxeLib/VocaluxeLib.csproj +++ b/VocaluxeLib/VocaluxeLib.csproj @@ -135,6 +135,8 @@ + +