#region license
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see .
#endregion
using System.IO;
namespace VocaluxeLib.Utils.Player
{
public class CSoundPlayer
{
protected int _StreamId = -1;
protected readonly float _FadeTime = CBase.Settings.GetSoundPlayerFadeTime();
public string FilePath { get; private set; }
private bool _Loop;
public bool Loop
{
get { return _Loop; }
set
{
_Loop = value;
if (SoundLoaded)
{
CBase.Sound.SetLoop(_StreamId, value);
}
}
}
///
/// Gets the current stream position or sets it
///
public float Position
{
set
{
if (!SoundLoaded)
{
return;
}
CBase.Sound.SetPosition(_StreamId, value);
}
get { return !SoundLoaded ? -1 : CBase.Sound.GetPosition(_StreamId); }
}
public float Length
{
get { return !SoundLoaded ? -1 : CBase.Sound.GetLength(_StreamId); }
}
public bool IsPlaying { get; private set; }
public bool IsFinished
{
get { return !Loop && (CBase.Sound.IsFinished(_StreamId) || !IsPlaying); }
}
public bool SoundLoaded
{
get { return _StreamId != -1; }
}
public virtual string ArtistAndTitle
{
get { return string.IsNullOrEmpty(FilePath) ? "" : Path.GetFileNameWithoutExtension(FilePath); }
}
public CSoundPlayer(bool loop = false)
{
Loop = loop;
}
public void Load(string file, float position = -1f, bool autoplay = false)
{
Close();
_StreamId = CBase.Sound.Load(file, Loop, true);
if (_StreamId < 0)
{
return;
}
FilePath = file;
if (position > 0f)
{
Position = position;
}
if (autoplay)
{
Play();
}
}
///
/// Starts or resumes the player
///
/// True if state changed, false if nothing loaded or already playing
public virtual bool Play()
{
if (!SoundLoaded || IsPlaying)
{
return false;
}
CBase.Sound.SetStreamVolume(_StreamId, 0);
CBase.Sound.Fade(_StreamId, 100, _FadeTime);
CBase.Sound.Play(_StreamId);
IsPlaying = true;
return true;
}
///
/// Pauses the player
///
/// True if state changed, false if nothing loaded or already paused
public virtual bool Pause()
{
if (!SoundLoaded || CBase.Sound.IsPaused(_StreamId))
{
return false;
}
CBase.Sound.Fade(_StreamId, 0, _FadeTime, EStreamAction.Pause);
IsPlaying = false;
return true;
}
///
/// Stops the player (no playback and position is set to start)
///
/// True if playback was stopped
public virtual bool Stop()
{
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 (!IsPlaying)
{
return;
}
if (Loop)
{
return;
}
var finished = CBase.Sound.IsFinished(_StreamId);
var len = CBase.Sound.GetLength(_StreamId);
var timeToPlay = len > 0f ? len - CBase.Sound.GetPosition(_StreamId) : _FadeTime + 1f;
if (timeToPlay <= _FadeTime || finished)
{
Stop();
}
}
}
}