mirror of
https://github.com/aaru-dps/RedBookPlayer.git
synced 2025-12-16 19:24:41 +00:00
Port only MVVM work and related fixes
This commit is contained in:
@@ -26,7 +26,7 @@ namespace RedBookPlayer.Models.Discs
|
||||
return;
|
||||
|
||||
// Data tracks only and flag disabled means we can't do anything
|
||||
if(_image.Tracks.All(t => t.TrackType != TrackType.Audio) && !LoadDataTracks)
|
||||
if(_image.Tracks.All(t => t.TrackType != TrackType.Audio) && DataPlayback == DataPlayback.Skip)
|
||||
return;
|
||||
|
||||
// Cache the value and the current track number
|
||||
@@ -69,8 +69,10 @@ namespace RedBookPlayer.Models.Discs
|
||||
SetTrackFlags(track);
|
||||
|
||||
// If the track is playable, just return
|
||||
if(TrackType == TrackType.Audio || LoadDataTracks)
|
||||
if(TrackType == TrackType.Audio || DataPlayback != DataPlayback.Skip)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// If we're not playing the track, skip
|
||||
if(increment)
|
||||
@@ -143,26 +145,28 @@ namespace RedBookPlayer.Models.Discs
|
||||
if(_image == null)
|
||||
return;
|
||||
|
||||
// If the sector is over the end of the image, then loop
|
||||
ulong tempSector = value;
|
||||
if(tempSector > _image.Info.Sectors)
|
||||
tempSector = 0;
|
||||
else if(tempSector < 0)
|
||||
tempSector = _image.Info.Sectors - 1;
|
||||
|
||||
// Cache the current track for easy access
|
||||
Track track = GetTrack(CurrentTrackNumber);
|
||||
if(track == null)
|
||||
return;
|
||||
|
||||
this.RaiseAndSetIfChanged(ref _currentSector, value);
|
||||
this.RaiseAndSetIfChanged(ref _currentSector, tempSector);
|
||||
|
||||
if((CurrentTrackNumber < _image.Tracks.Count - 1 && CurrentSector >= (GetTrack(CurrentTrackNumber + 1)?.TrackStartSector ?? 0))
|
||||
|| (CurrentTrackNumber > 0 && CurrentSector < track.TrackStartSector))
|
||||
// If the current sector is outside of the last known track, seek to the right one
|
||||
if(CurrentSector < track.TrackStartSector || CurrentSector > track.TrackEndSector)
|
||||
{
|
||||
foreach(Track trackData in _image.Tracks.ToArray().Reverse())
|
||||
{
|
||||
if(CurrentSector >= trackData.TrackStartSector)
|
||||
{
|
||||
CurrentTrackNumber = (int)trackData.TrackSequence;
|
||||
break;
|
||||
}
|
||||
}
|
||||
track = _image.Tracks.Last(t => CurrentSector >= t.TrackStartSector);
|
||||
CurrentTrackNumber = (int)track.TrackSequence;
|
||||
}
|
||||
|
||||
// Set the new index, if necessary
|
||||
foreach((ushort key, int i) in track.Indexes.Reverse())
|
||||
{
|
||||
if((int)CurrentSector >= i)
|
||||
@@ -216,9 +220,9 @@ namespace RedBookPlayer.Models.Discs
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicate if data tracks should be loaded
|
||||
/// Indicate how data tracks should be handled
|
||||
/// </summary>
|
||||
public bool LoadDataTracks { get; set; } = false;
|
||||
public DataPlayback DataPlayback { get; set; } = DataPlayback.Skip;
|
||||
|
||||
/// <summary>
|
||||
/// Indicate if hidden tracks should be loaded
|
||||
@@ -264,14 +268,12 @@ namespace RedBookPlayer.Models.Discs
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="generateMissingToc">Generate a TOC if the disc is missing one</param>
|
||||
/// <param name="loadHiddenTracks">Load hidden tracks for playback</param>
|
||||
/// <param name="loadDataTracks">Load data tracks for playback</param>
|
||||
public CompactDisc(bool generateMissingToc, bool loadHiddenTracks, bool loadDataTracks)
|
||||
/// <param name="options">Set of options for a new disc</param>
|
||||
public CompactDisc(OpticalDiscOptions options)
|
||||
{
|
||||
_generateMissingToc = generateMissingToc;
|
||||
LoadHiddenTracks = loadHiddenTracks;
|
||||
LoadDataTracks = loadDataTracks;
|
||||
DataPlayback = options.DataPlayback;
|
||||
_generateMissingToc = options.GenerateMissingToc;
|
||||
LoadHiddenTracks = options.LoadHiddenTracks;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -392,7 +394,7 @@ namespace RedBookPlayer.Models.Discs
|
||||
}
|
||||
|
||||
// If the previous index has an invalid offset, change tracks if needed
|
||||
else if (track.Indexes[(ushort)(CurrentTrackIndex - 1)] < 0)
|
||||
else if(track.Indexes[(ushort)(CurrentTrackIndex - 1)] < 0)
|
||||
{
|
||||
if(changeTrack)
|
||||
{
|
||||
@@ -401,7 +403,7 @@ namespace RedBookPlayer.Models.Discs
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Otherwise, just move to the previous index
|
||||
else
|
||||
{
|
||||
@@ -416,23 +418,7 @@ namespace RedBookPlayer.Models.Discs
|
||||
#region Helpers
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void LoadFirstTrack()
|
||||
{
|
||||
CurrentTrackNumber = 1;
|
||||
LoadTrack(CurrentTrackNumber);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetTotalIndexes()
|
||||
{
|
||||
if(_image == null)
|
||||
return;
|
||||
|
||||
TotalIndexes = GetTrack(CurrentTrackNumber)?.Indexes.Keys.Max() ?? 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void LoadTrack(int trackNumber)
|
||||
public override void LoadTrack(int trackNumber)
|
||||
{
|
||||
if(_image == null)
|
||||
return;
|
||||
@@ -448,6 +434,33 @@ namespace RedBookPlayer.Models.Discs
|
||||
CurrentSector = (ulong)(track?.Indexes.OrderBy(kvp => kvp.Key).First(kvp => kvp.Value >= 0).Value ?? 0);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void LoadFirstTrack()
|
||||
{
|
||||
CurrentTrackNumber = 1;
|
||||
LoadTrack(CurrentTrackNumber);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override byte[] ReadSectors(uint sectorsToRead)
|
||||
{
|
||||
if(TrackType == TrackType.Audio || DataPlayback == DataPlayback.Play)
|
||||
return base.ReadSectors(sectorsToRead);
|
||||
else if(DataPlayback == DataPlayback.Blank)
|
||||
return new byte[sectorsToRead * BytesPerSector];
|
||||
else
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetTotalIndexes()
|
||||
{
|
||||
if(_image == null)
|
||||
return;
|
||||
|
||||
TotalIndexes = GetTrack(CurrentTrackNumber)?.Indexes.Keys.Max() ?? 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the track with the given sequence value, if possible
|
||||
/// </summary>
|
||||
@@ -580,4 +593,4 @@ namespace RedBookPlayer.Models.Discs
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,6 +122,12 @@ namespace RedBookPlayer.Models.Discs
|
||||
|
||||
#region Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Load the desired track, if possible
|
||||
/// </summary>
|
||||
/// <param name="track">Track number to load</param>
|
||||
public abstract void LoadTrack(int track);
|
||||
|
||||
/// <summary>
|
||||
/// Load the first valid track in the image
|
||||
/// </summary>
|
||||
@@ -132,7 +138,7 @@ namespace RedBookPlayer.Models.Discs
|
||||
/// </summary>
|
||||
/// <param name="sectorsToRead">Current number of sectors to read</param>
|
||||
/// <returns>Byte array representing the read sectors, if possible</returns>
|
||||
public byte[] ReadSectors(uint sectorsToRead) => _image.ReadSectors(CurrentSector, sectorsToRead);
|
||||
public virtual byte[] ReadSectors(uint sectorsToRead) => _image.ReadSectors(CurrentSector, sectorsToRead);
|
||||
|
||||
/// <summary>
|
||||
/// Set the total indexes from the current track
|
||||
@@ -145,12 +151,6 @@ namespace RedBookPlayer.Models.Discs
|
||||
/// <param name="sector">New sector number to use</param>
|
||||
public void SetCurrentSector(ulong sector) => CurrentSector = sector;
|
||||
|
||||
/// <summary>
|
||||
/// Load the desired track, if possible
|
||||
/// </summary>
|
||||
/// <param name="track">Track number to load</param>
|
||||
protected abstract void LoadTrack(int track);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
24
RedBookPlayer.Models/Discs/OpticalDiscOptions.cs
Normal file
24
RedBookPlayer.Models/Discs/OpticalDiscOptions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace RedBookPlayer.Models.Discs
|
||||
{
|
||||
public class OpticalDiscOptions
|
||||
{
|
||||
#region CompactDisc
|
||||
|
||||
/// <summary>
|
||||
/// Indicate how data tracks should be handled
|
||||
/// </summary>
|
||||
public DataPlayback DataPlayback { get; set; } = DataPlayback.Skip;
|
||||
|
||||
/// <summary>
|
||||
/// Indicate if a TOC should be generated if missing
|
||||
/// </summary>
|
||||
public bool GenerateMissingToc { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Indicate if hidden tracks should be loaded
|
||||
/// </summary>
|
||||
public bool LoadHiddenTracks { get; set; } = false;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
49
RedBookPlayer.Models/Enums.cs
Normal file
49
RedBookPlayer.Models/Enums.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
namespace RedBookPlayer.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Determine how to handle data tracks
|
||||
/// </summary>
|
||||
public enum DataPlayback
|
||||
{
|
||||
/// <summary>
|
||||
/// Skip playing all data tracks
|
||||
/// </summary>
|
||||
Skip = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Play silence for all data tracks
|
||||
/// </summary>
|
||||
Blank = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Play the data from all data tracks
|
||||
/// </summary>
|
||||
Play = 2,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Current player state
|
||||
/// </summary>
|
||||
public enum PlayerState
|
||||
{
|
||||
/// <summary>
|
||||
/// No disc is loaded
|
||||
/// </summary>
|
||||
NoDisc,
|
||||
|
||||
/// <summary>
|
||||
/// Disc is loaded, playback is stopped
|
||||
/// </summary>
|
||||
Stopped,
|
||||
|
||||
/// <summary>
|
||||
/// Disc is loaded, playback is paused
|
||||
/// </summary>
|
||||
Paused,
|
||||
|
||||
/// <summary>
|
||||
/// Disc is loaded, playback enabled
|
||||
/// </summary>
|
||||
Playing,
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,10 @@ namespace RedBookPlayer.Models.Factories
|
||||
/// Generate an OpticalDisc from an input path
|
||||
/// </summary>
|
||||
/// <param name="path">Path to load the image from</param>
|
||||
/// <param name="generateMissingToc">Generate a TOC if the disc is missing one [CompactDisc only]</param>
|
||||
/// <param name="loadHiddenTracks">Load hidden tracks for playback [CompactDisc only]</param>
|
||||
/// <param name="loadDataTracks">Load data tracks for playback [CompactDisc only]</param>
|
||||
/// <param name="options">Options to pass to the optical disc factory</param>
|
||||
/// <param name="autoPlay">True if the image should be playable immediately, false otherwise</param>
|
||||
/// <returns>Instantiated OpticalDisc, if possible</returns>
|
||||
public static OpticalDiscBase GenerateFromPath(string path, bool generateMissingToc, bool loadHiddenTracks, bool loadDataTracks, bool autoPlay)
|
||||
public static OpticalDiscBase GenerateFromPath(string path, OpticalDiscOptions options, bool autoPlay)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -34,7 +32,7 @@ namespace RedBookPlayer.Models.Factories
|
||||
image.Open(filter);
|
||||
|
||||
// Generate and instantiate the disc
|
||||
return GenerateFromImage(image, generateMissingToc, loadHiddenTracks, loadDataTracks, autoPlay);
|
||||
return GenerateFromImage(image, options, autoPlay);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -47,12 +45,10 @@ namespace RedBookPlayer.Models.Factories
|
||||
/// Generate an OpticalDisc from an input IOpticalMediaImage
|
||||
/// </summary>
|
||||
/// <param name="image">IOpticalMediaImage to create from</param>
|
||||
/// <param name="generateMissingToc">Generate a TOC if the disc is missing one [CompactDisc only]</param>
|
||||
/// <param name="loadHiddenTracks">Load hidden tracks for playback [CompactDisc only]</param>
|
||||
/// <param name="loadDataTracks">Load data tracks for playback [CompactDisc only]</param>
|
||||
/// <param name="options">Options to pass to the optical disc factory</param>
|
||||
/// <param name="autoPlay">True if the image should be playable immediately, false otherwise</param>
|
||||
/// <returns>Instantiated OpticalDisc, if possible</returns>
|
||||
public static OpticalDiscBase GenerateFromImage(IOpticalMediaImage image, bool generateMissingToc, bool loadHiddenTracks, bool loadDataTracks, bool autoPlay)
|
||||
public static OpticalDiscBase GenerateFromImage(IOpticalMediaImage image, OpticalDiscOptions options, bool autoPlay)
|
||||
{
|
||||
// If the image is not usable, we don't do anything
|
||||
if(!IsUsableImage(image))
|
||||
@@ -66,7 +62,7 @@ namespace RedBookPlayer.Models.Factories
|
||||
{
|
||||
case "Compact Disc":
|
||||
case "GD":
|
||||
opticalDisc = new CompactDisc(generateMissingToc, loadHiddenTracks, loadDataTracks);
|
||||
opticalDisc = new CompactDisc(options);
|
||||
break;
|
||||
default:
|
||||
opticalDisc = null;
|
||||
@@ -118,4 +114,4 @@ namespace RedBookPlayer.Models.Factories
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Aaru.CommonTypes.Enums;
|
||||
using ReactiveUI;
|
||||
@@ -12,7 +11,13 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// <summary>
|
||||
/// Indicate if the player is ready to be used
|
||||
/// </summary>
|
||||
public bool Initialized { get; private set; } = false;
|
||||
public bool Initialized
|
||||
{
|
||||
get => _initialized;
|
||||
private set => this.RaiseAndSetIfChanged(ref _initialized, value);
|
||||
}
|
||||
|
||||
private bool _initialized;
|
||||
|
||||
#region OpticalDisc Passthrough
|
||||
|
||||
@@ -100,27 +105,27 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// <summary>
|
||||
/// Represents the total tracks on the disc
|
||||
/// </summary>
|
||||
public int TotalTracks => _opticalDisc.TotalTracks;
|
||||
public int TotalTracks => _opticalDisc?.TotalTracks ?? 0;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the total indices on the disc
|
||||
/// </summary>
|
||||
public int TotalIndexes => _opticalDisc.TotalIndexes;
|
||||
public int TotalIndexes => _opticalDisc?.TotalIndexes ?? 0;
|
||||
|
||||
/// <summary>
|
||||
/// Total sectors in the image
|
||||
/// </summary>
|
||||
public ulong TotalSectors => _opticalDisc.TotalSectors;
|
||||
public ulong TotalSectors => _opticalDisc?.TotalSectors ?? 0;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the time adjustment offset for the disc
|
||||
/// </summary>
|
||||
public ulong TimeOffset => _opticalDisc.TimeOffset;
|
||||
public ulong TimeOffset => _opticalDisc?.TimeOffset ?? 0;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the total playing time for the disc
|
||||
/// </summary>
|
||||
public ulong TotalTime => _opticalDisc.TotalTime;
|
||||
public ulong TotalTime => _opticalDisc?.TotalTime ?? 0;
|
||||
|
||||
private int _currentTrackNumber;
|
||||
private ushort _currentTrackIndex;
|
||||
@@ -138,12 +143,21 @@ namespace RedBookPlayer.Models.Hardware
|
||||
#region SoundOutput Passthrough
|
||||
|
||||
/// <summary>
|
||||
/// Indicate if the output is playing
|
||||
/// Indicates the current player state
|
||||
/// </summary>
|
||||
public bool? Playing
|
||||
public PlayerState PlayerState
|
||||
{
|
||||
get => _playing;
|
||||
private set => this.RaiseAndSetIfChanged(ref _playing, value);
|
||||
get => _playerState;
|
||||
private set => this.RaiseAndSetIfChanged(ref _playerState, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates how to handle playback of data tracks
|
||||
/// </summary>
|
||||
public DataPlayback DataPlayback
|
||||
{
|
||||
get => _dataPlayback;
|
||||
private set => this.RaiseAndSetIfChanged(ref _dataPlayback, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -164,7 +178,8 @@ namespace RedBookPlayer.Models.Hardware
|
||||
private set => this.RaiseAndSetIfChanged(ref _volume, value);
|
||||
}
|
||||
|
||||
private bool? _playing;
|
||||
private PlayerState _playerState;
|
||||
private DataPlayback _dataPlayback;
|
||||
private bool _applyDeEmphasis;
|
||||
private int _volume;
|
||||
|
||||
@@ -180,7 +195,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// <summary>
|
||||
/// OpticalDisc object
|
||||
/// </summary>
|
||||
private readonly OpticalDiscBase _opticalDisc;
|
||||
private OpticalDiscBase _opticalDisc;
|
||||
|
||||
/// <summary>
|
||||
/// Last volume for mute toggling
|
||||
@@ -190,23 +205,29 @@ namespace RedBookPlayer.Models.Hardware
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Create a new Player from a given image path
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
|
||||
public Player(int defaultVolume)
|
||||
{
|
||||
Initialized = false;
|
||||
_soundOutput = new SoundOutput(defaultVolume);
|
||||
_soundOutput.SetDeEmphasis(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes 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="loadHiddenTracks">Load hidden tracks for playback [CompactDisc only]</param>
|
||||
/// <param name="loadDataTracks">Load data tracks for playback [CompactDisc only]</param>
|
||||
/// <param name="options">Options to pass to the optical disc factory</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 loadHiddenTracks, bool loadDataTracks, bool autoPlay, int defaultVolume)
|
||||
public void Init(string path, OpticalDiscOptions options, bool autoPlay)
|
||||
{
|
||||
// Set the internal state for initialization
|
||||
// Reset initialization
|
||||
Initialized = false;
|
||||
_soundOutput = new SoundOutput();
|
||||
_soundOutput.SetDeEmphasis(false);
|
||||
|
||||
// Initalize the disc
|
||||
_opticalDisc = OpticalDiscFactory.GenerateFromPath(path, generateMissingToc, loadHiddenTracks, loadDataTracks, autoPlay);
|
||||
_opticalDisc = OpticalDiscFactory.GenerateFromPath(path, options, autoPlay);
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
@@ -214,7 +235,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
_opticalDisc.PropertyChanged += OpticalDiscStateChanged;
|
||||
|
||||
// Initialize the sound output
|
||||
_soundOutput.Init(_opticalDisc, autoPlay, defaultVolume);
|
||||
_soundOutput.Init(_opticalDisc, autoPlay);
|
||||
if(_soundOutput == null || !_soundOutput.Initialized)
|
||||
return;
|
||||
|
||||
@@ -240,12 +261,12 @@ namespace RedBookPlayer.Models.Hardware
|
||||
return;
|
||||
else if(_soundOutput == null)
|
||||
return;
|
||||
else if(_soundOutput.Playing)
|
||||
else if(_soundOutput.PlayerState != PlayerState.Paused && _soundOutput.PlayerState != PlayerState.Stopped)
|
||||
return;
|
||||
|
||||
_soundOutput.Play();
|
||||
_opticalDisc.SetTotalIndexes();
|
||||
Playing = true;
|
||||
PlayerState = PlayerState.Playing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -257,11 +278,11 @@ namespace RedBookPlayer.Models.Hardware
|
||||
return;
|
||||
else if(_soundOutput == null)
|
||||
return;
|
||||
else if(!_soundOutput.Playing)
|
||||
else if(_soundOutput.PlayerState != PlayerState.Playing)
|
||||
return;
|
||||
|
||||
_soundOutput?.Stop();
|
||||
Playing = false;
|
||||
_soundOutput?.Pause();
|
||||
PlayerState = PlayerState.Paused;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -269,10 +290,20 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// </summary>
|
||||
public void TogglePlayback()
|
||||
{
|
||||
if(Playing == true)
|
||||
Pause();
|
||||
else
|
||||
Play();
|
||||
switch(PlayerState)
|
||||
{
|
||||
case PlayerState.NoDisc:
|
||||
break;
|
||||
case PlayerState.Stopped:
|
||||
Play();
|
||||
break;
|
||||
case PlayerState.Paused:
|
||||
Play();
|
||||
break;
|
||||
case PlayerState.Playing:
|
||||
Pause();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -284,12 +315,12 @@ namespace RedBookPlayer.Models.Hardware
|
||||
return;
|
||||
else if(_soundOutput == null)
|
||||
return;
|
||||
else if(!_soundOutput.Playing)
|
||||
else if(_soundOutput.PlayerState != PlayerState.Playing && _soundOutput.PlayerState != PlayerState.Paused)
|
||||
return;
|
||||
|
||||
_soundOutput?.Stop();
|
||||
_soundOutput.Stop();
|
||||
_opticalDisc.LoadFirstTrack();
|
||||
Playing = null;
|
||||
PlayerState = PlayerState.Stopped;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -300,14 +331,16 @@ namespace RedBookPlayer.Models.Hardware
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
bool? wasPlaying = Playing;
|
||||
if(wasPlaying == true) Pause();
|
||||
PlayerState wasPlaying = PlayerState;
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Pause();
|
||||
|
||||
_opticalDisc.NextTrack();
|
||||
if(_opticalDisc is CompactDisc compactDisc)
|
||||
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
|
||||
|
||||
if(wasPlaying == true) Play();
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Play();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -318,14 +351,16 @@ namespace RedBookPlayer.Models.Hardware
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
bool? wasPlaying = Playing;
|
||||
if(wasPlaying == true) Pause();
|
||||
PlayerState wasPlaying = PlayerState;
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Pause();
|
||||
|
||||
_opticalDisc.PreviousTrack();
|
||||
if(_opticalDisc is CompactDisc compactDisc)
|
||||
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
|
||||
|
||||
if(wasPlaying == true) Play();
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Play();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -337,14 +372,16 @@ namespace RedBookPlayer.Models.Hardware
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
bool? wasPlaying = Playing;
|
||||
if(wasPlaying == true) Pause();
|
||||
PlayerState wasPlaying = PlayerState;
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Pause();
|
||||
|
||||
_opticalDisc.NextIndex(changeTrack);
|
||||
if(_opticalDisc is CompactDisc compactDisc)
|
||||
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
|
||||
|
||||
if(wasPlaying == true) Play();
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Play();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -356,37 +393,38 @@ namespace RedBookPlayer.Models.Hardware
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
bool? wasPlaying = Playing;
|
||||
if(wasPlaying == true) Pause();
|
||||
PlayerState wasPlaying = PlayerState;
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Pause();
|
||||
|
||||
_opticalDisc.PreviousIndex(changeTrack);
|
||||
if(_opticalDisc is CompactDisc compactDisc)
|
||||
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
|
||||
|
||||
if(wasPlaying == true) Play();
|
||||
if(wasPlaying == PlayerState.Playing)
|
||||
Play();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fast-forward playback by 75 sectors, if possible
|
||||
/// Fast-forward playback by 75 sectors
|
||||
/// </summary>
|
||||
public void FastForward()
|
||||
{
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
_opticalDisc.SetCurrentSector(Math.Min(_opticalDisc.TotalSectors, _opticalDisc.CurrentSector + 75));
|
||||
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector + 75);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rewind playback by 75 sectors, if possible
|
||||
/// Rewind playback by 75 sectors
|
||||
/// </summary>
|
||||
public void Rewind()
|
||||
{
|
||||
if(_opticalDisc == null || !_opticalDisc.Initialized)
|
||||
return;
|
||||
|
||||
if(_opticalDisc.CurrentSector >= 75)
|
||||
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector - 75);
|
||||
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector - 75);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -456,13 +494,13 @@ namespace RedBookPlayer.Models.Hardware
|
||||
#region Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Set the value for loading data tracks [CompactDisc only]
|
||||
/// Set data playback method [CompactDisc only]
|
||||
/// </summary>
|
||||
/// <param name="load">True to enable loading data tracks, false otherwise</param>
|
||||
public void SetLoadDataTracks(bool load)
|
||||
/// <param name="dataPlayback">New playback value</param>
|
||||
public void SetDataPlayback(DataPlayback dataPlayback)
|
||||
{
|
||||
if(_opticalDisc is CompactDisc compactDisc)
|
||||
compactDisc.LoadDataTracks = load;
|
||||
compactDisc.DataPlayback = dataPlayback;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -508,7 +546,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// </summary>
|
||||
private void SoundOutputStateChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
Playing = _soundOutput.Playing;
|
||||
PlayerState = _soundOutput.PlayerState;
|
||||
ApplyDeEmphasis = _soundOutput.ApplyDeEmphasis;
|
||||
Volume = _soundOutput.Volume;
|
||||
}
|
||||
|
||||
@@ -16,15 +16,19 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// <summary>
|
||||
/// Indicate if the output is ready to be used
|
||||
/// </summary>
|
||||
public bool Initialized { get; private set; } = false;
|
||||
public bool Initialized
|
||||
{
|
||||
get => _initialized;
|
||||
private set => this.RaiseAndSetIfChanged(ref _initialized, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicate if the output is playing
|
||||
/// Indicates the current player state
|
||||
/// </summary>
|
||||
public bool Playing
|
||||
public PlayerState PlayerState
|
||||
{
|
||||
get => _playing;
|
||||
private set => this.RaiseAndSetIfChanged(ref _playing, value);
|
||||
get => _playerState;
|
||||
private set => this.RaiseAndSetIfChanged(ref _playerState, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -54,7 +58,8 @@ namespace RedBookPlayer.Models.Hardware
|
||||
}
|
||||
}
|
||||
|
||||
private bool _playing;
|
||||
private bool _initialized;
|
||||
private PlayerState _playerState;
|
||||
private bool _applyDeEmphasis;
|
||||
private int _volume;
|
||||
|
||||
@@ -102,13 +107,18 @@ namespace RedBookPlayer.Models.Hardware
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
|
||||
public SoundOutput(int defaultVolume = 100) => Volume = defaultVolume;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the output with a given image
|
||||
/// </summary>
|
||||
/// <param name="opticalDisc">OpticalDisc to load from</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(OpticalDiscBase opticalDisc, bool autoPlay = false, int defaultVolume = 100)
|
||||
public void Init(OpticalDiscBase opticalDisc, bool autoPlay)
|
||||
{
|
||||
// If we have an unusable disc, just return
|
||||
if(opticalDisc == null || !opticalDisc.Initialized)
|
||||
@@ -117,9 +127,6 @@ namespace RedBookPlayer.Models.Hardware
|
||||
// Save a reference to the disc
|
||||
_opticalDisc = opticalDisc;
|
||||
|
||||
// Set the initial playback volume
|
||||
Volume = defaultVolume;
|
||||
|
||||
// Enable de-emphasis for CDs, if necessary
|
||||
if(opticalDisc is CompactDisc compactDisc)
|
||||
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
|
||||
@@ -136,6 +143,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
|
||||
// Mark the output as ready
|
||||
Initialized = true;
|
||||
PlayerState = PlayerState.Stopped;
|
||||
|
||||
// Begin loading data
|
||||
_source.Start();
|
||||
@@ -163,57 +171,14 @@ namespace RedBookPlayer.Models.Hardware
|
||||
// Determine how many sectors we can read
|
||||
DetermineReadAmount(count, out ulong sectorsToRead, out ulong zeroSectorsAmount);
|
||||
|
||||
// Create padding data for overreads
|
||||
byte[] zeroSectors = new byte[(int)zeroSectorsAmount * _opticalDisc.BytesPerSector];
|
||||
byte[] audioData;
|
||||
|
||||
// Attempt to read the required number of sectors
|
||||
var readSectorTask = Task.Run(() =>
|
||||
{
|
||||
lock(_readingImage)
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _opticalDisc.ReadSectors((uint)sectorsToRead).Concat(zeroSectors).ToArray();
|
||||
}
|
||||
catch(ArgumentOutOfRangeException)
|
||||
{
|
||||
_opticalDisc.LoadFirstTrack();
|
||||
}
|
||||
}
|
||||
|
||||
return zeroSectors;
|
||||
}
|
||||
});
|
||||
|
||||
// Wait 100ms at longest for the read to occur
|
||||
if(readSectorTask.Wait(TimeSpan.FromMilliseconds(100)))
|
||||
{
|
||||
audioData = readSectorTask.Result;
|
||||
}
|
||||
else
|
||||
// Get data to return
|
||||
byte[] audioDataSegment = ReadData(count, sectorsToRead, zeroSectorsAmount);
|
||||
if(audioDataSegment == null)
|
||||
{
|
||||
Array.Clear(buffer, offset, count);
|
||||
return count;
|
||||
}
|
||||
|
||||
// Load only the requested audio segment
|
||||
byte[] audioDataSegment = new byte[count];
|
||||
int copyAmount = Math.Min(count, audioData.Length - _currentSectorReadPosition);
|
||||
if(Math.Max(0, copyAmount) == 0)
|
||||
{
|
||||
Array.Clear(buffer, offset, count);
|
||||
return count;
|
||||
}
|
||||
|
||||
Array.Copy(audioData, _currentSectorReadPosition, audioDataSegment, 0, copyAmount);
|
||||
|
||||
// Apply de-emphasis filtering, only if enabled
|
||||
if(ApplyDeEmphasis)
|
||||
ProcessDeEmphasis(audioDataSegment);
|
||||
|
||||
// Write out the audio data to the buffer
|
||||
Array.Copy(audioDataSegment, 0, buffer, offset, count);
|
||||
|
||||
@@ -221,6 +186,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
_currentSectorReadPosition += count;
|
||||
if(_currentSectorReadPosition >= _opticalDisc.BytesPerSector)
|
||||
{
|
||||
int currentTrack = _opticalDisc.CurrentTrackNumber;
|
||||
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector + (ulong)(_currentSectorReadPosition / _opticalDisc.BytesPerSector));
|
||||
_currentSectorReadPosition %= _opticalDisc.BytesPerSector;
|
||||
}
|
||||
@@ -235,10 +201,10 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// </summary>
|
||||
public void Play()
|
||||
{
|
||||
if (_soundOut.PlaybackState != PlaybackState.Playing)
|
||||
if(_soundOut.PlaybackState != PlaybackState.Playing)
|
||||
_soundOut.Play();
|
||||
|
||||
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
|
||||
PlayerState = PlayerState.Playing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -249,7 +215,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
if(_soundOut.PlaybackState != PlaybackState.Paused)
|
||||
_soundOut.Pause();
|
||||
|
||||
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
|
||||
PlayerState = PlayerState.Paused;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -260,7 +226,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
if(_soundOut.PlaybackState != PlaybackState.Stopped)
|
||||
_soundOut.Stop();
|
||||
|
||||
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
|
||||
PlayerState = PlayerState.Stopped;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -270,7 +236,7 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// <summary>
|
||||
/// Set de-emphasis status
|
||||
/// </summary>
|
||||
/// <param name="apply"></param>
|
||||
/// <param name="apply">New de-emphasis status</param>
|
||||
public void SetDeEmphasis(bool apply) => ApplyDeEmphasis = apply;
|
||||
|
||||
/// <summary>
|
||||
@@ -287,30 +253,19 @@ namespace RedBookPlayer.Models.Hardware
|
||||
/// <param name="zeroSectorsAmount">Number of zeroed sectors to concatenate</param>
|
||||
private void DetermineReadAmount(int count, out ulong sectorsToRead, out ulong zeroSectorsAmount)
|
||||
{
|
||||
do
|
||||
// Attempt to read 5 more sectors than requested
|
||||
sectorsToRead = ((ulong)count / (ulong)_opticalDisc.BytesPerSector) + 5;
|
||||
zeroSectorsAmount = 0;
|
||||
|
||||
// Avoid overreads by padding with 0-byte data at the end
|
||||
if(_opticalDisc.CurrentSector + sectorsToRead > _opticalDisc.TotalSectors)
|
||||
{
|
||||
// Attempt to read 2 more sectors than requested
|
||||
sectorsToRead = ((ulong)count / (ulong)_opticalDisc.BytesPerSector) + 2;
|
||||
zeroSectorsAmount = 0;
|
||||
ulong oldSectorsToRead = sectorsToRead;
|
||||
sectorsToRead = _opticalDisc.TotalSectors - _opticalDisc.CurrentSector;
|
||||
|
||||
// Avoid overreads by padding with 0-byte data at the end
|
||||
if(_opticalDisc.CurrentSector + sectorsToRead > _opticalDisc.TotalSectors)
|
||||
{
|
||||
ulong oldSectorsToRead = sectorsToRead;
|
||||
sectorsToRead = _opticalDisc.TotalSectors - _opticalDisc.CurrentSector;
|
||||
|
||||
int tempZeroSectorCount = (int)(oldSectorsToRead - sectorsToRead);
|
||||
zeroSectorsAmount = (ulong)(tempZeroSectorCount < 0 ? 0 : tempZeroSectorCount);
|
||||
}
|
||||
|
||||
// If we're reading past the last sector of the disc, wrap around
|
||||
// TODO: Have past-end reads looping back controlled by a flag instead (Repeat? Repeat All?)
|
||||
if(sectorsToRead <= 0)
|
||||
{
|
||||
_opticalDisc.LoadFirstTrack();
|
||||
_currentSectorReadPosition = 0;
|
||||
}
|
||||
} while(sectorsToRead <= 0);
|
||||
int tempZeroSectorCount = (int)(oldSectorsToRead - sectorsToRead);
|
||||
zeroSectorsAmount = (ulong)(tempZeroSectorCount < 0 ? 0 : tempZeroSectorCount);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -333,6 +288,58 @@ namespace RedBookPlayer.Models.Hardware
|
||||
ByteConverter.FromFloats16Bit(floatAudioData, audioData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the requested amount of data from an input
|
||||
/// </summary>
|
||||
/// <param name="count">Number of bytes to load</param>
|
||||
/// <param name="sectorsToRead">Number of sectors to read</param>
|
||||
/// <param name="zeroSectorsAmount">Number of zeroed sectors to concatenate</param>
|
||||
/// <returns>The requested amount of data, if possible</returns>
|
||||
private byte[] ReadData(int count, ulong sectorsToRead, ulong zeroSectorsAmount)
|
||||
{
|
||||
// Create padding data for overreads
|
||||
byte[] zeroSectors = new byte[(int)zeroSectorsAmount * _opticalDisc.BytesPerSector];
|
||||
byte[] audioData;
|
||||
|
||||
// Attempt to read the required number of sectors
|
||||
var readSectorTask = Task.Run(() =>
|
||||
{
|
||||
lock(_readingImage)
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _opticalDisc.ReadSectors((uint)sectorsToRead).Concat(zeroSectors).ToArray();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return zeroSectors;
|
||||
}
|
||||
});
|
||||
|
||||
// Wait 100ms at longest for the read to occur
|
||||
if(readSectorTask.Wait(TimeSpan.FromMilliseconds(100)))
|
||||
audioData = readSectorTask.Result;
|
||||
else
|
||||
return null;
|
||||
|
||||
// Load only the requested audio segment
|
||||
byte[] audioDataSegment = new byte[count];
|
||||
int copyAmount = Math.Min(count, audioData.Length - _currentSectorReadPosition);
|
||||
if(Math.Max(0, copyAmount) == 0)
|
||||
return null;
|
||||
|
||||
Array.Copy(audioData, _currentSectorReadPosition, audioDataSegment, 0, copyAmount);
|
||||
|
||||
// Apply de-emphasis filtering, only if enabled
|
||||
if(ApplyDeEmphasis)
|
||||
ProcessDeEmphasis(audioDataSegment);
|
||||
|
||||
return audioDataSegment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets or resets the de-emphasis filters
|
||||
/// </summary>
|
||||
@@ -369,4 +376,4 @@ namespace RedBookPlayer.Models.Hardware
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user