Files
RedBookPlayer/RedBookPlayer/GUI/PlayerViewModel.cs

333 lines
9.8 KiB
C#
Raw Normal View History

2021-07-04 23:17:30 -07:00
using System.ComponentModel;
using System.Linq;
2021-06-06 21:42:14 -07:00
using ReactiveUI;
using RedBookPlayer.Hardware;
2021-06-06 21:42:14 -07:00
2021-06-29 14:23:33 -07:00
namespace RedBookPlayer.GUI
2021-06-06 21:42:14 -07:00
{
public class PlayerViewModel : ReactiveObject
{
/// <summary>
/// Player representing the internal state
/// </summary>
private Player _player;
2021-07-03 16:21:14 -07:00
/// <summary>
/// Last volume for mute toggling
/// </summary>
private int? _lastVolume = null;
2021-07-04 23:17:30 -07:00
#region Player Passthrough
2021-07-04 23:36:09 -07:00
#region OpticalDisc Passthrough
2021-07-03 21:15:23 -07:00
/// <summary>
2021-07-04 23:36:09 -07:00
/// Current track number
2021-07-03 21:15:23 -07:00
/// </summary>
2021-07-04 23:36:09 -07:00
public int CurrentTrackNumber
{
2021-07-04 23:36:09 -07:00
get => _currentTrackNumber;
private set => this.RaiseAndSetIfChanged(ref _currentTrackNumber, value);
}
2021-07-03 21:15:23 -07:00
/// <summary>
2021-07-04 23:36:09 -07:00
/// Current track index
2021-07-03 21:15:23 -07:00
/// </summary>
2021-07-04 23:36:09 -07:00
public ushort CurrentTrackIndex
2021-07-03 15:00:51 -07:00
{
2021-07-04 23:36:09 -07:00
get => _currentTrackIndex;
private set => this.RaiseAndSetIfChanged(ref _currentTrackIndex, value);
2021-07-03 15:00:51 -07:00
}
2021-07-03 21:15:23 -07:00
/// <summary>
2021-07-04 23:36:09 -07:00
/// Current sector number
2021-07-03 21:15:23 -07:00
/// </summary>
public ulong CurrentSector
{
get => _currentSector;
2021-07-04 23:36:09 -07:00
private set => this.RaiseAndSetIfChanged(ref _currentSector, value);
}
2021-07-04 23:36:09 -07:00
/// <summary>
/// Represents if the disc has a hidden track
/// </summary>
public bool HiddenTrack
{
2021-07-04 23:36:09 -07:00
get => _hasHiddenTrack;
private set => this.RaiseAndSetIfChanged(ref _hasHiddenTrack, value);
}
2021-07-04 23:36:09 -07:00
/// <summary>
/// Represents the 4CH flag [CompactDisc only]
/// </summary>
public bool QuadChannel
2021-06-06 21:42:14 -07:00
{
get => _quadChannel;
2021-07-04 23:36:09 -07:00
private set => this.RaiseAndSetIfChanged(ref _quadChannel, value);
2021-06-06 21:42:14 -07:00
}
2021-07-04 23:36:09 -07:00
/// <summary>
/// Represents the DATA flag [CompactDisc only]
/// </summary>
public bool IsDataTrack
2021-06-06 21:42:14 -07:00
{
get => _isDataTrack;
2021-07-04 23:36:09 -07:00
private set => this.RaiseAndSetIfChanged(ref _isDataTrack, value);
2021-06-06 21:42:14 -07:00
}
2021-07-04 23:36:09 -07:00
/// <summary>
/// Represents the DCP flag [CompactDisc only]
/// </summary>
2021-06-06 21:42:14 -07:00
public bool CopyAllowed
{
get => _copyAllowed;
2021-07-04 23:36:09 -07:00
private set => this.RaiseAndSetIfChanged(ref _copyAllowed, value);
2021-06-06 21:42:14 -07:00
}
2021-07-04 23:36:09 -07:00
/// <summary>
/// Represents the PRE flag [CompactDisc only]
/// </summary>
public bool TrackHasEmphasis
2021-06-06 21:42:14 -07:00
{
get => _trackHasEmphasis;
2021-07-04 23:36:09 -07:00
private set => this.RaiseAndSetIfChanged(ref _trackHasEmphasis, value);
2021-06-06 21:42:14 -07:00
}
2021-07-04 23:36:09 -07:00
/// <summary>
/// Represents the total tracks on the disc
/// </summary>
public int TotalTracks => _player.TotalTracks;
/// <summary>
/// Represents the total indices on the disc
/// </summary>
public int TotalIndexes => _player.TotalIndexes;
/// <summary>
/// Total sectors in the image
/// </summary>
public ulong TotalSectors => _player.TotalSectors;
/// <summary>
/// Represents the time adjustment offset for the disc
/// </summary>
public ulong TimeOffset => _player.TimeOffset;
/// <summary>
/// Represents the total playing time for the disc
/// </summary>
public ulong TotalTime => _player.TotalTime;
private int _currentTrackNumber;
private ushort _currentTrackIndex;
private ulong _currentSector;
private bool _hasHiddenTrack;
private bool _quadChannel;
private bool _isDataTrack;
private bool _copyAllowed;
private bool _trackHasEmphasis;
#endregion
#region SoundOutput Passthrough
/// <summary>
/// Indicate if the model is ready to be used
/// </summary>
public bool Initialized => _player?.Initialized ?? false;
/// <summary>
/// Indicate if the output is playing
/// </summary>
public bool? Playing
{
get => _playing;
private set => this.RaiseAndSetIfChanged(ref _playing, value);
}
/// <summary>
/// Indicates if de-emphasis should be applied
/// </summary>
public bool ApplyDeEmphasis
2021-06-06 21:42:14 -07:00
{
2021-07-04 23:36:09 -07:00
get => _applyDeEmphasis;
private set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
/// <summary>
/// Current playback volume
/// </summary>
public int Volume
{
get => _volume;
set => this.RaiseAndSetIfChanged(ref _volume, value);
2021-06-06 21:42:14 -07:00
}
2021-07-04 23:36:09 -07:00
private bool? _playing;
private bool _applyDeEmphasis;
private int _volume;
#endregion
#endregion
/// <summary>
/// Initialize the view model with a given image path
/// </summary>
/// <param name="path">Path to the disc image</param>
/// <param name="autoPlay">True if playback should begin immediately, false otherwise</param>
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
public void Init(string path, bool autoPlay, int defaultVolume)
{
2021-07-03 16:25:56 -07:00
// Stop current playback, if necessary
if(Playing != null) Playing = null;
// Create and attempt to initialize new Player
_player = new Player(path, autoPlay, defaultVolume);
if(Initialized)
2021-07-04 23:17:30 -07:00
{
_player.PropertyChanged += PlayerStateChanged;
PlayerStateChanged(this, null);
}
}
#region Playback
2021-07-04 23:17:30 -07:00
/// <summary>
/// Begin playback
/// </summary>
public void Play() => _player.Play();
/// <summary>
/// Pause current playback
/// </summary>
public void Pause() => _player.Pause();
/// <summary>
/// Stop current playback
/// </summary>
public void Stop() => _player.Stop();
/// <summary>
/// Move to the next playable track
/// </summary>
public void NextTrack() => _player?.NextTrack();
/// <summary>
/// Move to the previous playable track
/// </summary>
public void PreviousTrack() => _player?.PreviousTrack();
/// <summary>
/// Move to the next index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
public void NextIndex(bool changeTrack) => _player?.NextIndex(changeTrack);
/// <summary>
/// Move to the previous index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
public void PreviousIndex(bool changeTrack) => _player?.PreviousIndex(changeTrack);
/// <summary>
/// Fast-forward playback by 75 sectors, if possible
/// </summary>
public void FastForward() => _player?.FastForward();
/// <summary>
/// Rewind playback by 75 sectors, if possible
/// </summary>
public void Rewind() => _player?.Rewind();
#endregion
#region Helpers
/// <summary>
/// Generate the digit string to be interpreted by the frontend
/// </summary>
/// <returns>String representing the digits for the frontend</returns>
2021-07-04 23:36:09 -07:00
/// <remarks>
/// TODO: The model shouldn't care about this
/// </remarks>
public string GenerateDigitString()
{
// If the disc isn't initialized, return all '-' characters
2021-07-04 23:17:30 -07:00
if(_player?.Initialized != true)
return string.Empty.PadLeft(20, '-');
// Otherwise, take the current time into account
ulong sectorTime = _player.GetCurrentSectorTime();
int[] numbers = new int[]
{
2021-07-04 23:17:30 -07:00
_player.CurrentTrackNumber + 1,
_player.CurrentTrackIndex,
(int)(sectorTime / (75 * 60)),
(int)(sectorTime / 75 % 60),
(int)(sectorTime % 75),
2021-07-04 23:17:30 -07:00
_player.TotalTracks,
_player.TotalIndexes,
2021-07-04 23:17:30 -07:00
(int)(_player.TotalTime / (75 * 60)),
(int)(_player.TotalTime / 75 % 60),
(int)(_player.TotalTime % 75),
};
return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
}
2021-07-04 23:17:30 -07:00
/// <summary>
/// Set de-emphasis status
/// </summary>
/// <param name="apply"></param>
public void SetDeEmphasis(bool apply) => _player?.SetDeEmphasis(apply);
2021-07-03 16:21:14 -07:00
/// <summary>
/// Temporarily mute playback
/// </summary>
public void ToggleMute()
{
if(_lastVolume == null)
{
_lastVolume = Volume;
Volume = 0;
}
else
{
Volume = _lastVolume.Value;
_lastVolume = null;
}
}
/// <summary>
/// Update the UI from the internal player
/// </summary>
2021-07-04 23:17:30 -07:00
private void PlayerStateChanged(object sender, PropertyChangedEventArgs e)
{
if(_player?.Initialized != true)
return;
2021-07-04 23:36:09 -07:00
CurrentTrackNumber = _player.CurrentTrackNumber;
CurrentTrackIndex = _player.CurrentTrackIndex;
CurrentSector = _player.CurrentSector;
2021-07-04 23:17:30 -07:00
HiddenTrack = _player.HiddenTrack;
2021-07-04 23:17:30 -07:00
QuadChannel = _player.QuadChannel;
IsDataTrack = _player.IsDataTrack;
CopyAllowed = _player.CopyAllowed;
TrackHasEmphasis = _player.TrackHasEmphasis;
2021-07-04 23:36:09 -07:00
Playing = _player.Playing;
ApplyDeEmphasis = _player.ApplyDeEmphasis;
Volume = _player.Volume;
}
#endregion
2021-06-06 21:42:14 -07:00
}
}