From 4c01396e461e9b3a415643f5574ee1bc4aee49ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20K=C3=BCpper?= Date: Sat, 9 Aug 2014 17:12:27 +0200 Subject: [PATCH] Added preview-player which is used for playing preview in song-menu. --- Vocaluxe/Base/CConfig.cs | 2 + Vocaluxe/Base/CMain.cs | 46 ++++++++++++- Vocaluxe/Base/CPreviewPlayer.cs | 69 +++++++++++++++++++ Vocaluxe/Vocaluxe.csproj | 1 + VocaluxeLib/CBasic.cs | 5 +- VocaluxeLib/IBasic.cs | 13 ++++ .../Menu/SongMenu/CSongMenuFramework.cs | 47 +++---------- .../Menu/SongMenu/CSongMenuTileBoard.cs | 16 +++-- VocaluxeLib/Utils/Player/CSoundPlayer.cs | 10 +++ VocaluxeLib/VocaluxeLib.csproj | 1 + 10 files changed, 163 insertions(+), 47 deletions(-) create mode 100644 Vocaluxe/Base/CPreviewPlayer.cs diff --git a/Vocaluxe/Base/CConfig.cs b/Vocaluxe/Base/CConfig.cs index 2e73bc8e..fbf02ada 100644 --- a/Vocaluxe/Base/CConfig.cs +++ b/Vocaluxe/Base/CConfig.cs @@ -263,6 +263,8 @@ namespace Vocaluxe.Base xmlReader.TryGetEnumValue("//root/Sound/BackgroundMusicUseStart", ref BackgroundMusicUseStart); xmlReader.TryGetIntValueRange("//root/Sound/PreviewMusicVolume", ref _PreviewMusicVolume); xmlReader.TryGetIntValueRange("//root/Sound/GameMusicVolume", ref _GameMusicVolume); + + CPreviewPlayer.UpdateVolume(); #endregion Sound #region Game diff --git a/Vocaluxe/Base/CMain.cs b/Vocaluxe/Base/CMain.cs index bac2a91d..00650e9a 100644 --- a/Vocaluxe/Base/CMain.cs +++ b/Vocaluxe/Base/CMain.cs @@ -51,11 +51,12 @@ namespace Vocaluxe.Base private static readonly IDataBase _DataBase = new CBdataBase(); private static readonly IControllers _Controller = new CBcontrollers(); private static readonly IPlaylist _Playlist = new CBplaylist(); + private static readonly IPreviewPlayer _PreviewPlayer = new CBpreviewPlayer(); public static void Init() { CBase.Assign(_Config, _Settings, _Theme, _Log, _BackgroundMusic, _Draw, _Graphics, _Fonts, _Language, - _Game, _Profiles, _Record, _Songs, _Video, _Sound, _Cover, _DataBase, _Controller, _Playlist); + _Game, _Profiles, _Record, _Songs, _Video, _Sound, _Cover, _DataBase, _Controller, _Playlist, _PreviewPlayer); } } @@ -960,4 +961,47 @@ namespace Vocaluxe.Base return CPlaylists.GetSong(playlistID, songIndex); } } + + class CBpreviewPlayer : IPreviewPlayer + { + public void Play(float start = -1) + { + CPreviewPlayer.Play(); + } + + public void Load(CSong song, float start = 0f) + { + CPreviewPlayer.Load(song, start); + } + + public void Stop() + { + CPreviewPlayer.Stop(); + } + + public void Pause() + { + CPreviewPlayer.Pause(); + } + + public CTexture GetCover() + { + return CPreviewPlayer.Cover; + } + + public CTexture GetVideoTexture() + { + return CPreviewPlayer.VideoTexture; + } + + public bool IsPlaying() + { + return CPreviewPlayer.IsPlaying; + } + + public float GetLength() + { + return CPreviewPlayer.Length; + } + } } \ No newline at end of file diff --git a/Vocaluxe/Base/CPreviewPlayer.cs b/Vocaluxe/Base/CPreviewPlayer.cs new file mode 100644 index 00000000..3422b248 --- /dev/null +++ b/Vocaluxe/Base/CPreviewPlayer.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using VocaluxeLib.Utils.Player; +using VocaluxeLib.Draw; +using VocaluxeLib.Songs; + +namespace Vocaluxe.Base +{ + public static class CPreviewPlayer + { + private static CSongPlayer _Player = new CSongPlayer(true); + + public static CTexture VideoTexture + { + get { return _Player.GetVideoTexture(); } + } + + public static CTexture Cover + { + get { return _Player.Cover; } + } + + public static bool IsPlaying + { + get { return _Player.IsPlaying; } + } + + public static float Length + { + get { return _Player.Length; } + } + + public static void Load(CSong song, float start = 0f) + { + if (song == null) + return; + + if (start == 0f) + start = song.Preview.StartTime; + + _Player.Load(song, start); + } + + public static void Play(float start = -1) + { + if (start > -1) + _Player.Position = start; + _Player.Play(); + } + + public static void Pause() + { + _Player.TogglePause(); + } + + public static void Stop() + { + _Player.Stop(); + } + + public static void UpdateVolume() + { + _Player.Volume = CConfig.PreviewMusicVolume; + } + } +} diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index db1858e6..41753b7a 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -217,6 +217,7 @@ + diff --git a/VocaluxeLib/CBasic.cs b/VocaluxeLib/CBasic.cs index 7a588ee1..348f61de 100644 --- a/VocaluxeLib/CBasic.cs +++ b/VocaluxeLib/CBasic.cs @@ -38,10 +38,12 @@ namespace VocaluxeLib public static IDataBase DataBase; public static IControllers Controller; public static IPlaylist Playlist; + public static IPreviewPlayer PreviewPlayer; public static void Assign(IConfig config, ISettings settings, ITheme theme, ILog log, IBackgroundMusic backgroundMusic, IDrawing draw, IGraphics graphics, IFonts fonts, ILanguage language, IGame game, IProfiles profiles, IRecording record, - ISongs songs, IVideo video, ISound sound, ICover cover, IDataBase dataBase, IControllers controller, IPlaylist playlist) + ISongs songs, IVideo video, ISound sound, ICover cover, IDataBase dataBase, IControllers controller, IPlaylist playlist, + IPreviewPlayer previewPlayer) { Config = config; Settings = settings; @@ -62,6 +64,7 @@ namespace VocaluxeLib DataBase = dataBase; Controller = controller; Playlist = playlist; + PreviewPlayer = previewPlayer; } } } \ No newline at end of file diff --git a/VocaluxeLib/IBasic.cs b/VocaluxeLib/IBasic.cs index ab012974..a18b6811 100644 --- a/VocaluxeLib/IBasic.cs +++ b/VocaluxeLib/IBasic.cs @@ -315,4 +315,17 @@ namespace VocaluxeLib int GetSongCount(int playlistID); CPlaylistSong GetSong(int playlistID, int songIndex); } + + public interface IPreviewPlayer + { + void Play(float start = -1); + void Load(CSong song, float start = 0f); + void Stop(); + void Pause(); + CTexture GetCover(); + CTexture GetVideoTexture(); + bool IsPlaying(); + float GetLength(); + + } } \ No newline at end of file diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs index bcb6ceda..079c0cb6 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuFramework.cs @@ -121,12 +121,6 @@ namespace VocaluxeLib.Menu.SongMenu } } - protected int _PreviewSongStream { get; private set; } - - protected int _PreviewVideoStream { get; private set; } - private CFading _VideoFading; - protected CTexture _Vidtex; - private SColorF _ColorInternal; protected SColorF _Color { @@ -160,8 +154,6 @@ namespace VocaluxeLib.Menu.SongMenu { Visible = true; _PartyModeID = partyModeID; - _PreviewVideoStream = -1; - _PreviewSongStream = -1; _Theme = new SThemeSongMenu { SongMenuTileBoard = @@ -354,7 +346,7 @@ namespace VocaluxeLib.Menu.SongMenu { if (!_Initialized) return; - + /* if (_PreviewSongStream == -1 || _PreviewVideoStream == -1) return; @@ -380,7 +372,7 @@ namespace VocaluxeLib.Menu.SongMenu _VideoFading = null; } } - } + }*/ } public virtual void OnShow() {} @@ -399,8 +391,8 @@ namespace VocaluxeLib.Menu.SongMenu if (!_Initialized || !Visible) return; - if (_PreviewVideoStream != -1) - CBase.Drawing.DrawTexture(_Vidtex, new SRectF(0, 0, 1280, 720, 0)); + if (CBase.PreviewPlayer.IsPlaying()) + CBase.Drawing.DrawTexture(CBase.PreviewPlayer.GetVideoTexture(), new SRectF(0, 0, 1280, 720, 0)); } public bool IsMouseOverSelectedSong(SMouseEvent mEvent) @@ -509,44 +501,23 @@ namespace VocaluxeLib.Menu.SongMenu if (song == null) return; - //TODO: Using prescan here causes Vocaluxe to hang a bit on loading the song but we need the length below - _PreviewSongStream = CBase.Sound.Load(Path.Combine(song.Folder, song.MP3FileName), true, true); + CBase.PreviewPlayer.Load(song); float startposition = song.Preview.StartTime; - float length = CBase.Sound.GetLength(_PreviewSongStream); + + float length = CBase.PreviewPlayer.GetLength(); if (song.Preview.Source == EDataSource.None) startposition = length / 4f; else if (startposition > length - 5f) startposition = Math.Max(0f, Math.Min(length / 4f, length - 5f)); - CBase.Sound.SetPosition(_PreviewSongStream, startposition); - CBase.Sound.Play(_PreviewSongStream); - CBase.Sound.SetStreamVolume(_PreviewSongStream, 0f); - CBase.Sound.Fade(_PreviewSongStream, 100f, 3f); - - if (song.VideoFileName != "" && CBase.Config.GetVideoPreview() == EOffOn.TR_CONFIG_ON) - { - _PreviewVideoStream = CBase.Video.Load(Path.Combine(song.Folder, song.VideoFileName)); - if (_PreviewVideoStream == -1) - return; - CBase.Video.SetLoop(_PreviewVideoStream); - CBase.Video.Skip(_PreviewVideoStream, startposition, song.VideoGap); - _VideoFading = new CFading(0f, 1f, 3f); - } + CBase.PreviewPlayer.Play(startposition); } protected void _ResetPreview() { - if (_PreviewSongStream >= 0) - CBase.Sound.Fade(_PreviewSongStream, 0f, 0.75f, EStreamAction.Close); - - _PreviewSongStream = -1; - - CBase.Video.Close(_PreviewVideoStream); - _PreviewVideoStream = -1; - - CBase.Drawing.RemoveTexture(ref _Vidtex); + CBase.PreviewPlayer.Stop(); } #region ThemeEdit diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs index 00a31e7e..f1449689 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs @@ -179,7 +179,7 @@ namespace VocaluxeLib.Menu.SongMenu if (songOptions.Selection.RandomOnly) _PreviewNr = _SelectionNr; - if (_Length < 0 && CBase.Songs.IsInCategory() && CBase.Sound.GetLength(_PreviewSongStream) > 0) + if (_Length < 0 && CBase.Songs.IsInCategory() && CBase.PreviewPlayer.GetLength() > 0) _UpdateLength(CBase.Songs.GetVisibleSong(_PreviewNr)); } @@ -244,7 +244,7 @@ namespace VocaluxeLib.Menu.SongMenu { if (song == null) return; - float time = CBase.Sound.GetLength(_PreviewSongStream); + float time = CBase.PreviewPlayer.GetLength(); if (Math.Abs(song.Finish) > 0.001) time = song.Finish; @@ -462,18 +462,20 @@ namespace VocaluxeLib.Menu.SongMenu _TextBG.Draw(); - if (_Vidtex != null && _PreviewVideoStream != -1) + CTexture vidtex = CBase.PreviewPlayer.GetVideoTexture(); + + if (vidtex != null) { - if (_Vidtex.Color.A < 1) + if (vidtex.Color.A < 1) _CoverBig.Draw(EAspect.Crop); var bounds = new RectangleF(_CoverBig.Rect.X, _CoverBig.Rect.Y, _CoverBig.Rect.W, _CoverBig.Rect.H); RectangleF rect; - CHelper.SetRect(bounds, out rect, _Vidtex.OrigAspect, EAspect.Crop); + CHelper.SetRect(bounds, out rect, vidtex.OrigAspect, EAspect.Crop); var vidRect = new SRectF(rect.X, rect.Y, rect.Width, rect.Height, _CoverBig.Rect.Z); var vidRectBounds = new SRectF(bounds.X, bounds.Y, bounds.Width, bounds.Height, 0f); - CBase.Drawing.DrawTexture(_Vidtex, vidRect, _Vidtex.Color, vidRectBounds); - CBase.Drawing.DrawTextureReflection(_Vidtex, vidRect, _Vidtex.Color, vidRectBounds, _CoverBig.ReflectionSpace, _CoverBig.ReflectionHeight); + CBase.Drawing.DrawTexture(vidtex, vidRect, vidtex.Color, vidRectBounds); + CBase.Drawing.DrawTextureReflection(vidtex, vidRect, vidtex.Color, vidRectBounds, _CoverBig.ReflectionSpace, _CoverBig.ReflectionHeight); } else _CoverBig.Draw(EAspect.Crop); diff --git a/VocaluxeLib/Utils/Player/CSoundPlayer.cs b/VocaluxeLib/Utils/Player/CSoundPlayer.cs index 59e858de..533d4e21 100644 --- a/VocaluxeLib/Utils/Player/CSoundPlayer.cs +++ b/VocaluxeLib/Utils/Player/CSoundPlayer.cs @@ -38,6 +38,16 @@ namespace VocaluxeLib.Utils.Player } } + public float Length + { + get + { + if (_StreamID == -1) + return 0; + return CBase.Sound.GetLength(_StreamID); + } + } + public bool RepeatSong; public bool IsPlaying { get; private set; } diff --git a/VocaluxeLib/VocaluxeLib.csproj b/VocaluxeLib/VocaluxeLib.csproj index a367e8e1..04e04f51 100644 --- a/VocaluxeLib/VocaluxeLib.csproj +++ b/VocaluxeLib/VocaluxeLib.csproj @@ -135,6 +135,7 @@ +