Files
Vocaluxe/VocaluxeLib/Utils/Player/CSongPlayer.cs
T
Quentin Bray 6ffa63b923 Apply R# clean-up for reliability (#877)
* Apply R# clean-up for reliability

- Migrate to NUnit4 Assert.That
- Update dependencise versions
- Fix LangVersion to 9.0 (until net core migration)
- Reformat code
- Apply 'var' style (easier for refactoring)
- Remove redundant parentheses
- Add braces on every condition blocks (1 line)
- Uses new null checking patterns
- Uses AutoProperties when possible
- Uses string scalar type instead of class
- Clean usings
- Add .orig files in git ignore
- Rename some properties to respect defined conventions and US format definition
	- ID -> Id
	- BPM -> Bpm
	- Finish -> End

* Update R# settings according to the clean-up made
2026-06-22 19:52:06 +02:00

220 lines
5.4 KiB
C#

#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 <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.IO;
using VocaluxeLib.Draw;
using VocaluxeLib.Songs;
namespace VocaluxeLib.Utils.Player
{
public class CSongPlayer : CSoundPlayer
{
private CSong _Song;
private bool _VideoEnabled;
private CVideoStream _Video;
private CFading _VideoFading;
public bool VideoEnabled
{
get { return _VideoEnabled; }
set
{
if (_VideoEnabled == value)
{
return;
}
_VideoEnabled = value;
if (_VideoEnabled)
{
_LoadVideo();
}
else
{
_CloseVideo();
}
}
}
public int SongId
{
get { return _Song == null ? -1 : _Song.Id; }
}
public bool SongHasVideo
{
get
{
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 base.ArtistAndTitle;
}
}
public CTextureRef Cover
{
get { return _Song == null ? CBase.Cover.GetNoCover() : _Song.CoverTextureBig; }
}
public CSongPlayer(bool loop = false) : base(loop) { }
public CTextureRef GetVideoTexture()
{
if (_Video == null || _Song == null)
{
return null;
}
if (CBase.Video.GetFrame(_Video, CBase.Sound.GetPosition(_StreamId)))
{
if (_VideoFading != null)
{
bool finished;
_Video.Texture.Color.A = _VideoFading.GetValue(out finished);
if (finished)
{
_VideoFading = null;
}
}
return _Video.Texture;
}
return null;
}
public void Load(CSong song, float position = 0f, bool autoplay = false)
{
if (song == null)
{
throw new ArgumentNullException("song");
}
Load(song.GetMP3(), position, autoplay);
_Song = song;
_LoadVideo();
}
private void _LoadVideo()
{
if (_Song == null)
{
return;
}
if (_Video != null || !SongHasVideo)
{
return;
}
var videoFilePath = Path.Combine(_Song.Folder, _Song.VideoFileName);
_Video = CBase.Video.Load(videoFilePath);
if (_Video == null)
{
return;
}
_VideoFading = new CFading(0f, 1f, 3f);
if (IsPlaying)
{
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();
}
}
}