Files
RedBookPlayer/RedBookPlayer.Models/Hardware/Player.cs

644 lines
20 KiB
C#
Raw Normal View History

2021-07-04 23:17:30 -07:00
using System.ComponentModel;
using Aaru.CommonTypes.Enums;
using ReactiveUI;
2021-07-12 15:40:56 -07:00
using RedBookPlayer.Models.Discs;
using RedBookPlayer.Models.Factories;
2021-03-19 17:07:27 -03:00
2021-07-12 15:40:56 -07:00
namespace RedBookPlayer.Models.Hardware
2021-03-19 17:07:27 -03:00
{
2021-07-04 23:17:30 -07:00
public class Player : ReactiveObject
2021-03-19 17:07:27 -03:00
{
/// <summary>
/// Indicate if the player is ready to be used
/// </summary>
2021-08-05 21:05:20 -07:00
public bool Initialized
{
get => _initialized;
private set => this.RaiseAndSetIfChanged(ref _initialized, value);
}
/// <summary>
/// Currently selected disc
/// </summary>
public int CurrentDisc
{
get => _currentDisc;
private set
{
int temp = value;
if (temp < 4)
temp = 0;
else if (temp >= 5)
temp = 0;
this.RaiseAndSetIfChanged(ref _currentDisc, temp);
}
}
2021-08-05 21:05:20 -07:00
private bool _initialized;
private int _currentDisc;
2021-06-06 20:28:36 +01:00
2021-07-04 23:17:30 -07:00
#region OpticalDisc Passthrough
/// <summary>
2021-07-04 23:17:30 -07:00
/// Current track number
/// </summary>
2021-07-04 23:17:30 -07:00
public int CurrentTrackNumber
{
get => _currentTrackNumber;
private set => this.RaiseAndSetIfChanged(ref _currentTrackNumber, value);
}
/// <summary>
2021-07-04 23:17:30 -07:00
/// Current track index
/// </summary>
2021-07-04 23:17:30 -07:00
public ushort CurrentTrackIndex
{
2021-07-04 23:17:30 -07:00
get => _currentTrackIndex;
private set => this.RaiseAndSetIfChanged(ref _currentTrackIndex, value);
}
2021-08-24 22:11:25 -07:00
/// <summary>
/// Current track session
/// </summary>
public ushort CurrentTrackSession
{
get => _currentTrackSession;
private set => this.RaiseAndSetIfChanged(ref _currentTrackSession, value);
}
2021-07-04 23:17:30 -07:00
/// <summary>
/// Current sector number
/// </summary>
public ulong CurrentSector
{
get => _currentSector;
private set => this.RaiseAndSetIfChanged(ref _currentSector, value);
}
2021-07-05 00:48:14 -07:00
/// <summary>
/// Represents the sector starting the section
/// </summary>
public ulong SectionStartSector
{
get => _sectionStartSector;
protected set => this.RaiseAndSetIfChanged(ref _sectionStartSector, value);
}
2021-07-04 23:17:30 -07:00
/// <summary>
/// Represents if the disc has a hidden track
/// </summary>
public bool HiddenTrack
{
get => _hasHiddenTrack;
private set => this.RaiseAndSetIfChanged(ref _hasHiddenTrack, value);
}
/// <summary>
2021-07-04 23:17:30 -07:00
/// Represents the 4CH flag [CompactDisc only]
/// </summary>
2021-07-04 23:17:30 -07:00
public bool QuadChannel
{
2021-07-04 23:17:30 -07:00
get => _quadChannel;
private set => this.RaiseAndSetIfChanged(ref _quadChannel, value);
}
/// <summary>
/// Represents the DATA flag [CompactDisc only]
/// </summary>
public bool IsDataTrack
{
get => _isDataTrack;
private set => this.RaiseAndSetIfChanged(ref _isDataTrack, value);
}
/// <summary>
/// Represents the DCP flag [CompactDisc only]
/// </summary>
public bool CopyAllowed
{
get => _copyAllowed;
private set => this.RaiseAndSetIfChanged(ref _copyAllowed, value);
}
/// <summary>
/// Represents the PRE flag [CompactDisc only]
/// </summary>
public bool TrackHasEmphasis
{
get => _trackHasEmphasis;
private set => this.RaiseAndSetIfChanged(ref _trackHasEmphasis, value);
}
/// <summary>
/// Represents the total tracks on the disc
/// </summary>
public int TotalTracks => _opticalDiscs[CurrentDisc]?.TotalTracks ?? 0;
2021-07-04 23:17:30 -07:00
/// <summary>
/// Represents the total indices on the disc
/// </summary>
public int TotalIndexes => _opticalDiscs[CurrentDisc]?.TotalIndexes ?? 0;
2021-07-04 23:17:30 -07:00
/// <summary>
/// Total sectors in the image
/// </summary>
public ulong TotalSectors => _opticalDiscs[CurrentDisc]?.TotalSectors ?? 0;
2021-07-04 23:17:30 -07:00
/// <summary>
/// Represents the time adjustment offset for the disc
/// </summary>
public ulong TimeOffset => _opticalDiscs[CurrentDisc]?.TimeOffset ?? 0;
2021-07-04 23:17:30 -07:00
/// <summary>
/// Represents the total playing time for the disc
/// </summary>
public ulong TotalTime => _opticalDiscs[CurrentDisc]?.TotalTime ?? 0;
2021-07-04 23:17:30 -07:00
private int _currentTrackNumber;
private ushort _currentTrackIndex;
2021-08-24 22:11:25 -07:00
private ushort _currentTrackSession;
2021-07-04 23:17:30 -07:00
private ulong _currentSector;
2021-07-05 00:48:14 -07:00
private ulong _sectionStartSector;
2021-07-04 23:17:30 -07:00
private bool _hasHiddenTrack;
private bool _quadChannel;
private bool _isDataTrack;
private bool _copyAllowed;
private bool _trackHasEmphasis;
#endregion
#region SoundOutput Passthrough
/// <summary>
2021-08-05 21:05:20 -07:00
/// Indicates the current player state
/// </summary>
public PlayerState PlayerState
{
get => _playerState;
private set => this.RaiseAndSetIfChanged(ref _playerState, value);
}
/// <summary>
/// Indicates how to handle playback of data tracks
2021-07-04 23:17:30 -07:00
/// </summary>
2021-08-05 21:05:20 -07:00
public DataPlayback DataPlayback
2021-07-04 23:17:30 -07:00
{
2021-08-05 21:05:20 -07:00
get => _dataPlayback;
private set => this.RaiseAndSetIfChanged(ref _dataPlayback, value);
}
2021-08-24 22:11:25 -07:00
/// <summary>
/// Indicates the repeat mode
/// </summary>
public RepeatMode RepeatMode
{
get => _repeatMode;
private set => this.RaiseAndSetIfChanged(ref _repeatMode, value);
}
2021-06-30 13:56:42 -07:00
/// <summary>
/// Indicates if de-emphasis should be applied
/// </summary>
public bool ApplyDeEmphasis
{
2021-07-04 23:17:30 -07:00
get => _applyDeEmphasis;
private set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
2021-06-30 13:56:42 -07:00
2021-07-04 23:17:30 -07:00
/// <summary>
/// Current playback volume
/// </summary>
public int Volume
{
get => _volume;
2021-07-05 16:23:50 -07:00
private set => this.RaiseAndSetIfChanged(ref _volume, value);
2021-07-04 23:17:30 -07:00
}
2021-08-05 21:05:20 -07:00
private PlayerState _playerState;
private DataPlayback _dataPlayback;
2021-08-24 22:11:25 -07:00
private RepeatMode _repeatMode;
2021-07-04 23:17:30 -07:00
private bool _applyDeEmphasis;
private int _volume;
#endregion
#region Private State Variables
/// <summary>
/// Sound output handling class
/// </summary>
2021-07-04 23:17:30 -07:00
private readonly SoundOutput _soundOutput;
/// <summary>
/// OpticalDisc object
/// </summary>
/// <remarks>TODO: Make the number of discs in the changer configurable</remarks>
private OpticalDiscBase[] _opticalDiscs = new OpticalDiscBase[5];
2021-07-12 10:41:11 -07:00
/// <summary>
/// Last volume for mute toggling
/// </summary>
private int? _lastVolume = null;
#endregion
/// <summary>
2021-08-05 21:05:20 -07:00
/// Constructor
/// </summary>
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
2021-08-05 21:05:20 -07:00
public Player(int defaultVolume)
2021-03-19 17:07:27 -03:00
{
Initialized = false;
_currentDisc = 0;
2021-08-05 21:05:20 -07:00
_soundOutput = new SoundOutput(defaultVolume);
2021-07-04 23:17:30 -07:00
_soundOutput.SetDeEmphasis(false);
2021-08-05 21:05:20 -07:00
}
/// <summary>
/// Initializes player from a given image path
/// </summary>
/// <param name="path">Path to the disc image</param>
/// <param name="options">Options to pass to the optical disc factory</param>
2021-08-24 22:11:25 -07:00
/// <param name="repeatMode">RepeatMode for sound output</param>
2021-08-05 21:05:20 -07:00
/// <param name="autoPlay">True if playback should begin immediately, false otherwise</param>
2021-08-24 22:11:25 -07:00
public void Init(string path, OpticalDiscOptions options, RepeatMode repeatMode, bool autoPlay)
2021-08-05 21:05:20 -07:00
{
// Reset initialization
Initialized = false;
2021-07-05 00:31:19 -07:00
// Initalize the disc
_opticalDiscs[CurrentDisc] = OpticalDiscFactory.GenerateFromPath(path, options, autoPlay);
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
2021-03-19 17:07:27 -03:00
return;
2021-07-04 23:43:55 -07:00
// Add event handling for the optical disc
_opticalDiscs[CurrentDisc].PropertyChanged += OpticalDiscStateChanged;
2021-07-04 23:43:55 -07:00
// Initialize the sound output
_soundOutput.Init(_opticalDiscs[CurrentDisc], repeatMode, autoPlay);
if(_soundOutput == null || !_soundOutput.Initialized)
return;
2021-07-04 23:17:30 -07:00
// Add event handling for the sound output
_soundOutput.PropertyChanged += SoundOutputStateChanged;
// Mark the player as ready
2021-03-19 17:07:27 -03:00
Initialized = true;
2021-07-05 13:00:20 -07:00
2021-07-05 13:04:48 -07:00
// Force a refresh of the state information
2021-07-05 13:00:20 -07:00
OpticalDiscStateChanged(this, null);
2021-07-05 13:04:48 -07:00
SoundOutputStateChanged(this, null);
2021-03-19 17:07:27 -03:00
}
#region Playback
2021-03-19 17:07:27 -03:00
2021-07-04 23:17:30 -07:00
/// <summary>
/// Begin playback
/// </summary>
public void Play()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
2021-07-04 23:17:30 -07:00
return;
else if(_soundOutput == null)
return;
2021-08-05 21:05:20 -07:00
else if(_soundOutput.PlayerState != PlayerState.Paused && _soundOutput.PlayerState != PlayerState.Stopped)
2021-07-04 23:17:30 -07:00
return;
_soundOutput.Play();
_opticalDiscs[CurrentDisc].SetTotalIndexes();
2021-08-05 21:05:20 -07:00
PlayerState = PlayerState.Playing;
2021-07-04 23:17:30 -07:00
}
/// <summary>
/// Pause current playback
/// </summary>
public void Pause()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
2021-07-04 23:17:30 -07:00
return;
else if(_soundOutput == null)
return;
2021-08-05 21:05:20 -07:00
else if(_soundOutput.PlayerState != PlayerState.Playing)
2021-07-04 23:17:30 -07:00
return;
2021-08-05 21:05:20 -07:00
_soundOutput?.Pause();
PlayerState = PlayerState.Paused;
2021-07-04 23:17:30 -07:00
}
2021-07-12 10:41:11 -07:00
/// <summary>
/// Toggle current playback
/// </summary>
public void TogglePlayback()
{
2021-08-05 21:05:20 -07:00
switch(PlayerState)
{
case PlayerState.NoDisc:
break;
case PlayerState.Stopped:
Play();
break;
case PlayerState.Paused:
Play();
break;
case PlayerState.Playing:
Pause();
break;
}
2021-07-12 10:41:11 -07:00
}
2021-07-04 23:17:30 -07:00
/// <summary>
/// Stop current playback
/// </summary>
public void Stop()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
2021-07-04 23:17:30 -07:00
return;
else if(_soundOutput == null)
return;
2021-08-05 21:05:20 -07:00
else if(_soundOutput.PlayerState != PlayerState.Playing && _soundOutput.PlayerState != PlayerState.Paused)
2021-07-04 23:17:30 -07:00
return;
2021-08-05 21:05:20 -07:00
_soundOutput.Stop();
_opticalDiscs[CurrentDisc].LoadFirstTrack();
2021-08-05 21:05:20 -07:00
PlayerState = PlayerState.Stopped;
2021-07-04 23:17:30 -07:00
}
2021-08-24 22:11:25 -07:00
/// <summary>
/// Eject the currently loaded disc
/// </summary>
public void Eject()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
2021-08-24 22:11:25 -07:00
return;
else if(_soundOutput == null)
return;
Stop();
_soundOutput.Eject();
_opticalDiscs[CurrentDisc] = null;
2021-08-24 22:11:25 -07:00
PlayerState = PlayerState.NoDisc;
Initialized = false;
}
/// <summary>
/// Move to the next playable track
/// </summary>
public void NextTrack()
2021-03-19 17:07:27 -03:00
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
2021-03-19 17:07:27 -03:00
return;
2021-08-05 21:05:20 -07:00
PlayerState wasPlaying = PlayerState;
if(wasPlaying == PlayerState.Playing)
Pause();
_opticalDiscs[CurrentDisc].NextTrack();
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-07-04 23:17:30 -07:00
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-08-05 21:05:20 -07:00
if(wasPlaying == PlayerState.Playing)
Play();
}
/// <summary>
/// Move to the previous playable track
/// </summary>
2021-07-05 22:13:00 -07:00
public void PreviousTrack()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
return;
2021-08-05 21:05:20 -07:00
PlayerState wasPlaying = PlayerState;
if(wasPlaying == PlayerState.Playing)
Pause();
2021-07-04 23:17:30 -07:00
_opticalDiscs[CurrentDisc].PreviousTrack();
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-07-04 23:17:30 -07:00
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-08-05 21:05:20 -07:00
if(wasPlaying == PlayerState.Playing)
Play();
}
/// <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)
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
return;
2021-08-05 21:05:20 -07:00
PlayerState wasPlaying = PlayerState;
if(wasPlaying == PlayerState.Playing)
Pause();
_opticalDiscs[CurrentDisc].NextIndex(changeTrack);
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-07-04 23:17:30 -07:00
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-08-05 21:05:20 -07:00
if(wasPlaying == PlayerState.Playing)
Play();
}
/// <summary>
/// Move to the previous index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
2021-07-05 22:13:00 -07:00
public void PreviousIndex(bool changeTrack)
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
return;
2021-08-05 21:05:20 -07:00
PlayerState wasPlaying = PlayerState;
if(wasPlaying == PlayerState.Playing)
Pause();
2021-07-04 23:17:30 -07:00
_opticalDiscs[CurrentDisc].PreviousIndex(changeTrack);
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-07-04 23:17:30 -07:00
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-08-05 21:05:20 -07:00
if(wasPlaying == PlayerState.Playing)
Play();
}
/// <summary>
2021-08-05 21:05:20 -07:00
/// Fast-forward playback by 75 sectors
/// </summary>
public void FastForward()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
return;
_opticalDiscs[CurrentDisc].SetCurrentSector(_opticalDiscs[CurrentDisc].CurrentSector + 75);
}
/// <summary>
2021-08-05 21:05:20 -07:00
/// Rewind playback by 75 sectors
/// </summary>
public void Rewind()
{
if(_opticalDiscs[CurrentDisc] == null || !_opticalDiscs[CurrentDisc].Initialized)
return;
_opticalDiscs[CurrentDisc].SetCurrentSector(_opticalDiscs[CurrentDisc].CurrentSector - 75);
2021-03-19 17:07:27 -03:00
}
#endregion
2021-03-19 17:07:27 -03:00
2021-07-12 10:41:11 -07:00
#region Volume
/// <summary>
/// Increment the volume value
/// </summary>
public void VolumeUp() => SetVolume(Volume + 1);
/// <summary>
/// Decrement the volume value
/// </summary>
public void VolumeDown() => SetVolume(Volume + 1);
/// <summary>
/// Set the value for the volume
/// </summary>
/// <param name="volume">New volume value</param>
public void SetVolume(int volume) => _soundOutput?.SetVolume(volume);
/// <summary>
/// Temporarily mute playback
/// </summary>
public void ToggleMute()
{
if(_lastVolume == null)
{
_lastVolume = Volume;
SetVolume(0);
}
else
{
SetVolume(_lastVolume.Value);
_lastVolume = null;
}
}
#endregion
#region Emphasis
/// <summary>
/// Enable de-emphasis
/// </summary>
public void EnableDeEmphasis() => SetDeEmphasis(true);
/// <summary>
/// Disable de-emphasis
/// </summary>
public void DisableDeEmphasis() => SetDeEmphasis(false);
/// <summary>
/// Toggle de-emphasis
/// </summary>
public void ToggleDeEmphasis() => SetDeEmphasis(!ApplyDeEmphasis);
/// <summary>
2021-07-04 23:17:30 -07:00
/// Set de-emphasis status
/// </summary>
2021-07-04 23:17:30 -07:00
/// <param name="apply"></param>
2021-07-12 10:41:11 -07:00
private void SetDeEmphasis(bool apply) => _soundOutput?.SetDeEmphasis(apply);
#endregion
#region Helpers
2021-08-24 22:11:25 -07:00
/// <summary>
/// Extract a single track from the image to WAV
/// </summary>
/// <param name="trackNumber"></param>
/// <param name="outputDirectory">Output path to write data to</param>
public void ExtractSingleTrackToWav(uint trackNumber, string outputDirectory) => _opticalDiscs[CurrentDisc]?.ExtractTrackToWav(trackNumber, outputDirectory);
2021-08-24 22:11:25 -07:00
/// <summary>
/// Extract all tracks from the image to WAV
/// <param name="outputDirectory">Output path to write data to</param>
public void ExtractAllTracksToWav(string outputDirectory) => _opticalDiscs[CurrentDisc]?.ExtractAllTracksToWav(outputDirectory);
2021-08-24 22:11:25 -07:00
/// <summary>
2021-08-05 21:05:20 -07:00
/// Set data playback method [CompactDisc only]
/// </summary>
2021-08-05 21:05:20 -07:00
/// <param name="dataPlayback">New playback value</param>
public void SetDataPlayback(DataPlayback dataPlayback)
2021-07-12 10:57:52 -07:00
{
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-08-05 21:05:20 -07:00
compactDisc.DataPlayback = dataPlayback;
2021-07-12 10:57:52 -07:00
}
2021-07-05 22:13:00 -07:00
/// <summary>
/// Set the value for loading hidden tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading hidden tracks, false otherwise</param>
2021-07-12 10:57:52 -07:00
public void SetLoadHiddenTracks(bool load)
{
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-07-12 10:57:52 -07:00
compactDisc.LoadHiddenTracks = load;
}
2021-07-05 22:13:00 -07:00
2021-08-24 22:11:25 -07:00
/// <summary>
/// Set repeat mode
/// </summary>
/// <param name="repeatMode">New repeat mode value</param>
public void SetRepeatMode(RepeatMode repeatMode) => _soundOutput?.SetRepeatMode(repeatMode);
/// <summary>
/// Set the value for session handling [CompactDisc only]
/// </summary>
/// <param name="sessionHandling">New session handling value</param>
public void SetSessionHandling(SessionHandling sessionHandling)
{
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-08-24 22:11:25 -07:00
compactDisc.SessionHandling = sessionHandling;
}
2021-07-04 23:43:55 -07:00
/// <summary>
/// Update the player from the current OpticalDisc
/// </summary>
2021-07-05 13:00:20 -07:00
private void OpticalDiscStateChanged(object sender, PropertyChangedEventArgs e)
2021-07-04 23:17:30 -07:00
{
CurrentTrackNumber = _opticalDiscs[CurrentDisc].CurrentTrackNumber;
CurrentTrackIndex = _opticalDiscs[CurrentDisc].CurrentTrackIndex;
CurrentSector = _opticalDiscs[CurrentDisc].CurrentSector;
SectionStartSector = _opticalDiscs[CurrentDisc].SectionStartSector;
2021-07-04 23:17:30 -07:00
HiddenTrack = TimeOffset > 150;
if(_opticalDiscs[CurrentDisc] is CompactDisc compactDisc)
2021-07-04 23:17:30 -07:00
{
QuadChannel = compactDisc.QuadChannel;
IsDataTrack = compactDisc.IsDataTrack;
CopyAllowed = compactDisc.CopyAllowed;
TrackHasEmphasis = compactDisc.TrackHasEmphasis;
}
else
{
QuadChannel = false;
IsDataTrack = _opticalDiscs[CurrentDisc].TrackType != TrackType.Audio;
2021-07-04 23:17:30 -07:00
CopyAllowed = false;
TrackHasEmphasis = false;
}
}
2021-07-05 13:00:20 -07:00
/// <summary>
/// Update the player from the current SoundOutput
/// </summary>
private void SoundOutputStateChanged(object sender, PropertyChangedEventArgs e)
{
2021-08-05 21:05:20 -07:00
PlayerState = _soundOutput.PlayerState;
2021-08-24 22:11:25 -07:00
RepeatMode = _soundOutput.RepeatMode;
2021-07-05 13:00:20 -07:00
ApplyDeEmphasis = _soundOutput.ApplyDeEmphasis;
Volume = _soundOutput.Volume;
}
#endregion
2021-03-19 17:07:27 -03:00
}
2021-06-06 20:28:36 +01:00
}