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

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

No files matched your search

+74 -73
View File
@@ -39,28 +39,32 @@ namespace VocaluxeLib.Utils.Player
_VideoEnabled = value;
if (_VideoEnabled)
_LoadVideo();
else if (_Video != null)
CBase.Video.Close(ref _Video);
else
_CloseVideo();
}
}
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
public bool SongHasVideo
{
get
{
if (_Song == null)
return "";
if (_Song.Artist != "" && _Song.Title != "")
return _Song != null && !String.IsNullOrEmpty(_Song.Folder) && !String.IsNullOrEmpty(_Song.VideoFileName) &&
File.Exists(Path.Combine(_Song.Folder, _Song.VideoFileName));
}
}
public override string ArtistAndTitle
{
get
{
if (_Song != null && !String.IsNullOrEmpty(_Song.Artist) && !String.IsNullOrEmpty(_Song.Title))
return _Song.Artist + " - " + _Song.Title;
return Path.GetFileNameWithoutExtension(_Song.MP3FileName);
return base.ArtistAndTitle;
}
}
@@ -69,26 +73,8 @@ namespace VocaluxeLib.Utils.Player
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 CSongPlayer(string file, bool loop = false, float position = 0f, bool autoplay = false)
: base(file, loop, position, autoplay)
{
_Song = null;
}
public CTextureRef GetVideoTexture()
{
if (_Video == null || _Song == null)
@@ -110,49 +96,11 @@ namespace VocaluxeLib.Utils.Player
public void Load(CSong song, float position = 0f, bool autoplay = false)
{
if (song == null)
return;
throw new ArgumentNullException("song");
Stop();
_Song = song;
Load(Path.Combine(song.Folder, song.MP3FileName), position, autoplay);
Load(song.GetMP3(), position, autoplay);
_LoadVideo();
if (autoplay)
Play();
}
public void LoadFile(string file, float position = 0f, bool autoplay = false)
{
if (String.IsNullOrEmpty(file))
return;
Stop();
Load(file, position, autoplay);
}
public override void Stop()
{
base.Stop();
_Song = null;
if (_Video != null)
CBase.Video.Close(ref _Video);
}
public override void TogglePause()
{
if (IsPlaying && _Video != null)
{
CBase.Video.Pause(_Video);
CBase.Video.Skip(_Video, CBase.Sound.GetPosition(_StreamID) + _FadeTime, _Song.VideoGap);
}
else if (_Video != null)
CBase.Video.Resume(_Video);
base.TogglePause();
_Song = song;
}
private void _LoadVideo()
@@ -160,11 +108,13 @@ namespace VocaluxeLib.Utils.Player
if (_Song == null)
return;
string videoFilePath = Path.Combine(_Song.Folder, _Song.VideoFileName);
if (_Video != null || String.IsNullOrEmpty(videoFilePath))
if (_Video != null || !SongHasVideo)
return;
string videoFilePath = Path.Combine(_Song.Folder, _Song.VideoFileName);
_Video = CBase.Video.Load(videoFilePath);
CBase.Video.Skip(_Video, 0f, _Song.VideoGap); //Use gap, otherwhise we will overwrite position in GetVideoTexture
if (_Video == null)
return;
_VideoFading = new CFading(0f, 1f, 3f);
if (IsPlaying)
@@ -172,6 +122,57 @@ namespace VocaluxeLib.Utils.Player
CBase.Video.Skip(_Video, Position, _Song.VideoGap);
CBase.Video.Resume(_Video);
}
else
CBase.Video.Skip(_Video, 0f, _Song.VideoGap);
}
public override bool Play()
{
if (!base.Play())
return false;
if (_Video != null)
{
CBase.Video.Skip(_Video, Position, _Song.VideoGap);
CBase.Video.Resume(_Video);
}
return true;
}
public override bool Pause()
{
if (!base.Pause())
return false;
if (_Video != null)
CBase.Video.Pause(_Video);
return true;
}
public override bool Stop()
{
if (!base.Stop())
return false;
if (_Video != null)
{
CBase.Video.Pause(_Video);
CBase.Video.Skip(_Video, 0f, _Song.VideoGap);
}
return true;
}
private void _CloseVideo()
{
if (_Video != null)
CBase.Video.Close(ref _Video);
}
public override void Close()
{
base.Close();
_Song = null;
_CloseVideo();
}
}
}
+83 -87
View File
@@ -15,58 +15,52 @@
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System.IO;
namespace VocaluxeLib.Utils.Player
{
public class CSoundPlayer
{
protected int _StreamID = -1;
protected int _Volume = 100;
protected float _StartPosition;
protected readonly float _FadeTime = CBase.Settings.GetSoundPlayerFadeTime();
public string FilePath { get; private set; }
public int Volume
{
set
{
_Volume = value;
_ApplyVolume();
}
get { return _Volume; }
}
public bool Loop;
/// <summary>
/// Gets the current stream position or sets it
/// </summary>
public float Position
{
set
{
if (_StreamID == -1)
if (!SoundLoaded)
return;
_StartPosition = value;
}
get
{
if (_StreamID == -1)
return -1;
return CBase.Sound.GetPosition(_StreamID);
CBase.Sound.SetPosition(_StreamID, value);
}
get { return !SoundLoaded ? -1 : CBase.Sound.GetPosition(_StreamID); }
}
public float Length
{
get
{
if (_StreamID == -1)
return 0;
return CBase.Sound.GetLength(_StreamID);
}
get { return !SoundLoaded ? -1 : CBase.Sound.GetLength(_StreamID); }
}
public bool RepeatSong;
public bool IsPlaying { get; protected set; }
public bool IsPlaying { get; private set; }
public bool IsFinished
{
get { return !RepeatSong && CBase.Sound.IsFinished(_StreamID); }
get { return !Loop && CBase.Sound.IsFinished(_StreamID); }
}
public bool SoundLoaded
{
get { return _StreamID != -1; }
}
public virtual string ArtistAndTitle
{
get { return string.IsNullOrEmpty(FilePath) ? "" : Path.GetFileNameWithoutExtension(FilePath); }
}
public CSoundPlayer(bool loop = false)
@@ -74,95 +68,97 @@ namespace VocaluxeLib.Utils.Player
Loop = loop;
}
public CSoundPlayer(string file, bool loop = false, float position = 0f, bool autoplay = false)
public void Load(string file, float position = -1f, 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();
Close();
_StreamID = CBase.Sound.Load(file, false, true);
if (position > 0f)
Position = position;
if (autoplay)
Play();
}
public void Play()
{
if (_StreamID == -1 || IsPlaying)
if (_StreamID < 0)
return;
FilePath = file;
if (position > 0f)
Position = position;
if (autoplay)
Play();
}
/// <summary>
/// Starts or resumes the player
/// </summary>
/// <returns>True if state changed, false if nothing loaded or already playing</returns>
public virtual bool Play()
{
if (!SoundLoaded || IsPlaying)
return false;
CBase.Sound.SetPosition(_StreamID, _StartPosition);
CBase.Sound.SetStreamVolume(_StreamID, 0);
CBase.Sound.Fade(_StreamID, Volume, _FadeTime);
CBase.Sound.Fade(_StreamID, 100, _FadeTime);
CBase.Sound.Play(_StreamID);
IsPlaying = true;
return true;
}
public virtual void TogglePause()
/// <summary>
/// Pauses the player
/// </summary>
/// <returns>True if state changed, false if nothing loaded or already paused</returns>
public virtual bool Pause()
{
if (_StreamID == -1)
return;
if (!SoundLoaded || CBase.Sound.IsPaused(_StreamID))
return false;
if (IsPlaying)
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Pause);
else
{
CBase.Sound.Fade(_StreamID, Volume, _FadeTime);
CBase.Sound.Play(_StreamID);
}
IsPlaying = !IsPlaying;
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Pause);
IsPlaying = false;
return true;
}
public virtual void Stop()
/// <summary>
/// Stops the player (no playback and position is set to start)
/// </summary>
/// <returns>True if playback was stopped</returns>
public virtual bool Stop()
{
if (_StreamID == -1 || !IsPlaying)
if (!SoundLoaded)
return false;
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Stop);
IsPlaying = false;
return true;
}
public virtual void Close()
{
if (!SoundLoaded)
return;
CBase.Sound.Fade(_StreamID, 0, _FadeTime, EStreamAction.Close);
_StreamID = -1;
FilePath = "";
IsPlaying = false;
}
public void Update()
{
if (_StreamID == -1 || !IsPlaying)
if (!IsPlaying)
return;
bool finished = CBase.Sound.IsFinished(_StreamID);
if (Loop)
{
if (finished)
{
// Restart
Stop();
Play();
}
return;
}
float len = CBase.Sound.GetLength(_StreamID);
float timeToPlay = (len > 0f) ? len - CBase.Sound.GetPosition(_StreamID) : _FadeTime + 1f;
bool finished = CBase.Sound.IsFinished(_StreamID);
if (timeToPlay <= _FadeTime || finished)
{
if (RepeatSong)
{
//Set to false for restarting
IsPlaying = false;
Play();
}
else
Stop();
}
}
private void _ApplyVolume()
{
if (_StreamID == -1)
return;
CBase.Sound.SetStreamVolume(_StreamID, Volume);
Stop();
}
}
}