Files
RedBookPlayer/RedBookPlayer.Common/Hardware/Player.cs

438 lines
14 KiB
C#
Raw Normal View History

2021-03-19 17:07:27 -03:00
using System;
2021-07-04 23:17:30 -07:00
using System.ComponentModel;
using Aaru.CommonTypes.Enums;
using ReactiveUI;
using RedBookPlayer.Common.Discs;
2021-03-19 17:07:27 -03:00
namespace RedBookPlayer.Common.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>
public bool Initialized { get; private set; } = false;
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-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 => _opticalDisc.TotalTracks;
/// <summary>
/// Represents the total indices on the disc
/// </summary>
public int TotalIndexes => _opticalDisc.TotalIndexes;
/// <summary>
/// Total sectors in the image
/// </summary>
public ulong TotalSectors => _opticalDisc.TotalSectors;
/// <summary>
/// Represents the time adjustment offset for the disc
/// </summary>
public ulong TimeOffset => _opticalDisc.TimeOffset;
/// <summary>
/// Represents the total playing time for the disc
/// </summary>
public ulong TotalTime => _opticalDisc.TotalTime;
private int _currentTrackNumber;
private ushort _currentTrackIndex;
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>
/// Indicate if the output is playing
/// </summary>
public bool? Playing
{
get => _playing;
private set => this.RaiseAndSetIfChanged(ref _playing, 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
}
private bool? _playing;
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>
private readonly OpticalDisc _opticalDisc;
#endregion
/// <summary>
/// Create a new Player from a given image path
/// </summary>
/// <param name="path">Path to the disc image</param>
/// <param name="generateMissingToc">Generate a TOC if the disc is missing one [CompactDisc only]</param>
/// <param name="loadDataTracks">Load data tracks for playback [CompactDisc only]</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 Player(string path, bool generateMissingToc, bool loadDataTracks, bool autoPlay, int defaultVolume)
2021-03-19 17:07:27 -03:00
{
// Set the internal state for initialization
Initialized = false;
_soundOutput = new SoundOutput();
2021-07-04 23:17:30 -07:00
_soundOutput.SetDeEmphasis(false);
2021-07-05 00:31:19 -07:00
// Initalize the disc
_opticalDisc = OpticalDiscFactory.GenerateFromPath(path, generateMissingToc, loadDataTracks, autoPlay);
2021-07-05 00:31:19 -07:00
if(_opticalDisc == null || !_opticalDisc.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
2021-07-05 00:31:19 -07:00
_opticalDisc.PropertyChanged += OpticalDiscStateChanged;
2021-07-04 23:43:55 -07:00
// Initialize the sound output
2021-07-04 23:17:30 -07:00
_soundOutput.Init(_opticalDisc, autoPlay, defaultVolume);
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(_opticalDisc == null || !_opticalDisc.Initialized)
return;
else if(_soundOutput == null)
return;
else if(_soundOutput.Playing)
return;
_soundOutput.Play();
_opticalDisc.SetTotalIndexes();
Playing = true;
}
/// <summary>
/// Pause current playback
/// </summary>
public void Pause()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
else if(_soundOutput == null)
return;
else if(!_soundOutput.Playing)
return;
_soundOutput?.Stop();
Playing = false;
}
/// <summary>
/// Stop current playback
/// </summary>
public void Stop()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
else if(_soundOutput == null)
return;
else if(!_soundOutput.Playing)
return;
_soundOutput?.Stop();
_opticalDisc.LoadFirstTrack();
Playing = null;
}
/// <summary>
/// Move to the next playable track
/// </summary>
public void NextTrack()
2021-03-19 17:07:27 -03:00
{
2021-07-04 23:17:30 -07:00
if(_opticalDisc == null || !_opticalDisc.Initialized)
2021-03-19 17:07:27 -03:00
return;
bool? wasPlaying = Playing;
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Pause();
2021-07-04 23:17:30 -07:00
_opticalDisc.NextTrack();
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Play();
}
/// <summary>
/// Move to the previous playable track
/// </summary>
/// <param name="playHiddenTrack">True to play the hidden track, if it exists</param>
public void PreviousTrack(bool playHiddenTrack)
{
2021-07-04 23:17:30 -07:00
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Pause();
_opticalDisc.PreviousTrack(playHiddenTrack);
2021-07-04 23:17:30 -07:00
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) 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)
{
2021-07-04 23:17:30 -07:00
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Pause();
2021-07-04 23:17:30 -07:00
_opticalDisc.NextIndex(changeTrack);
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Play();
}
/// <summary>
/// Move to the previous index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
/// <param name="playHiddenTrack">True to play the hidden track, if it exists</param>
public void PreviousIndex(bool changeTrack, bool playHiddenTrack)
{
2021-07-04 23:17:30 -07:00
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Pause();
_opticalDisc.PreviousIndex(changeTrack, playHiddenTrack);
2021-07-04 23:17:30 -07:00
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
2021-07-04 23:17:30 -07:00
if(wasPlaying == true) Play();
}
/// <summary>
/// Fast-forward playback by 75 sectors, if possible
/// </summary>
public void FastForward()
{
2021-07-04 23:17:30 -07:00
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
2021-07-05 16:30:38 -07:00
_opticalDisc.SetCurrentSector(Math.Min(_opticalDisc.TotalSectors, _opticalDisc.CurrentSector + 75));
}
/// <summary>
/// Rewind playback by 75 sectors, if possible
/// </summary>
public void Rewind()
{
2021-07-04 23:17:30 -07:00
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
2021-07-04 23:17:30 -07:00
if(_opticalDisc.CurrentSector >= 75)
2021-07-05 16:30:38 -07:00
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector - 75);
2021-03-19 17:07:27 -03:00
}
#endregion
2021-03-19 17:07:27 -03:00
#region Helpers
/// <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>
public void SetDeEmphasis(bool apply) => _soundOutput?.SetDeEmphasis(apply);
/// <summary>
/// Set the value for loading data tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading data tracks, false otherwise</param>
public void SetLoadDataTracks(bool load) => (_opticalDisc as CompactDisc)?.SetLoadDataTracks(load);
/// <summary>
/// Set the value for the volume
/// </summary>
/// <param name="volume">New volume value</param>
public void SetVolume(int volume) => _soundOutput?.SetVolume(volume);
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 = _opticalDisc.CurrentTrackNumber;
CurrentTrackIndex = _opticalDisc.CurrentTrackIndex;
CurrentSector = _opticalDisc.CurrentSector;
2021-07-05 00:48:14 -07:00
SectionStartSector = _opticalDisc.SectionStartSector;
2021-07-04 23:17:30 -07:00
HiddenTrack = TimeOffset > 150;
if(_opticalDisc is CompactDisc compactDisc)
{
QuadChannel = compactDisc.QuadChannel;
IsDataTrack = compactDisc.IsDataTrack;
CopyAllowed = compactDisc.CopyAllowed;
TrackHasEmphasis = compactDisc.TrackHasEmphasis;
}
else
{
QuadChannel = false;
IsDataTrack = _opticalDisc.TrackType != TrackType.Audio;
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)
{
Playing = _soundOutput.Playing;
ApplyDeEmphasis = _soundOutput.ApplyDeEmphasis;
Volume = _soundOutput.Volume;
}
#endregion
2021-03-19 17:07:27 -03:00
}
2021-06-06 20:28:36 +01:00
}