Files
RedBookPlayer/RedBookPlayer.GUI/ViewModels/PlayerViewModel.cs

672 lines
22 KiB
C#
Raw Normal View History

2021-07-12 15:40:56 -07:00
using System;
2021-07-12 12:38:33 -07:00
using System.Collections.Generic;
2021-07-04 23:17:30 -07:00
using System.ComponentModel;
2021-07-12 15:40:56 -07:00
using System.IO;
2021-07-12 10:52:50 -07:00
using System.Linq;
2021-07-12 10:41:11 -07:00
using System.Reactive;
2021-07-12 12:38:33 -07:00
using System.Threading.Tasks;
2021-07-12 15:40:56 -07:00
using Avalonia;
2021-07-12 12:38:33 -07:00
using Avalonia.Controls;
2021-07-12 15:40:56 -07:00
using Avalonia.Media.Imaging;
using Avalonia.Platform;
2021-07-12 12:38:33 -07:00
using Avalonia.Threading;
2021-06-06 21:42:14 -07:00
using ReactiveUI;
using RedBookPlayer.GUI.Views;
2021-07-12 15:40:56 -07:00
using RedBookPlayer.Models.Hardware;
2021-06-06 21:42:14 -07:00
2021-07-12 10:41:11 -07:00
namespace RedBookPlayer.GUI.ViewModels
2021-06-06 21:42:14 -07:00
{
public class PlayerViewModel : ReactiveObject
{
/// <summary>
/// Player representing the internal state
/// </summary>
private Player _player;
2021-07-12 15:40:56 -07:00
/// <summary>
/// Set of images representing the digits for the UI
/// </summary>
private Image[] _digits;
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-05 00:48:14 -07:00
/// <summary>
/// Represents the sector starting the section
/// </summary>
public ulong SectionStartSector
{
get => _sectionStartSector;
2021-07-05 16:23:50 -07:00
private set => this.RaiseAndSetIfChanged(ref _sectionStartSector, value);
2021-07-05 00:48:14 -07:00
}
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;
2021-07-05 00:48:14 -07:00
private ulong _sectionStartSector;
2021-07-04 23:36:09 -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 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;
private 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
2021-07-12 10:41:11 -07:00
#region Commands
2021-07-12 12:38:33 -07:00
/// <summary>
/// Command for loading a disc
/// </summary>
public ReactiveCommand<Unit, Unit> LoadCommand { get; }
2021-07-12 10:41:11 -07:00
#region Playback
/// <summary>
/// Command for beginning playback
/// </summary>
public ReactiveCommand<Unit, Unit> PlayCommand { get; }
/// <summary>
/// Command for pausing playback
/// </summary>
public ReactiveCommand<Unit, Unit> PauseCommand { get; }
/// <summary>
/// Command for pausing playback
/// </summary>
public ReactiveCommand<Unit, Unit> TogglePlayPauseCommand { get; }
/// <summary>
/// Command for stopping playback
/// </summary>
public ReactiveCommand<Unit, Unit> StopCommand { get; }
/// <summary>
/// Command for moving to the next track
/// </summary>
public ReactiveCommand<Unit, Unit> NextTrackCommand { get; }
/// <summary>
/// Command for moving to the previous track
/// </summary>
public ReactiveCommand<Unit, Unit> PreviousTrackCommand { get; }
/// <summary>
/// Command for moving to the next index
/// </summary>
public ReactiveCommand<Unit, Unit> NextIndexCommand { get; }
/// <summary>
/// Command for moving to the previous index
/// </summary>
public ReactiveCommand<Unit, Unit> PreviousIndexCommand { get; }
/// <summary>
/// Command for fast forwarding
/// </summary>
public ReactiveCommand<Unit, Unit> FastForwardCommand { get; }
/// <summary>
/// Command for rewinding
/// </summary>
public ReactiveCommand<Unit, Unit> RewindCommand { get; }
#endregion
#region Volume
/// <summary>
/// Command for incrementing volume
/// </summary>
public ReactiveCommand<Unit, Unit> VolumeUpCommand { get; }
/// <summary>
/// Command for decrementing volume
/// </summary>
public ReactiveCommand<Unit, Unit> VolumeDownCommand { get; }
/// <summary>
/// Command for toggling mute
/// </summary>
public ReactiveCommand<Unit, Unit> ToggleMuteCommand { get; }
#endregion
#region Emphasis
/// <summary>
/// Command for enabling de-emphasis
/// </summary>
public ReactiveCommand<Unit, Unit> EnableDeEmphasisCommand { get; }
/// <summary>
/// Command for disabling de-emphasis
/// </summary>
public ReactiveCommand<Unit, Unit> DisableDeEmphasisCommand { get; }
/// <summary>
/// Command for toggling de-emphasis
/// </summary>
public ReactiveCommand<Unit, Unit> ToggleDeEmphasisCommand { get; }
#endregion
#endregion
/// <summary>
/// Constructor
/// </summary>
public PlayerViewModel()
{
2021-07-12 12:38:33 -07:00
LoadCommand = ReactiveCommand.Create(ExecuteLoad);
2021-07-12 10:41:11 -07:00
PlayCommand = ReactiveCommand.Create(ExecutePlay);
PauseCommand = ReactiveCommand.Create(ExecutePause);
TogglePlayPauseCommand = ReactiveCommand.Create(ExecuteTogglePlayPause);
StopCommand = ReactiveCommand.Create(ExecuteStop);
NextTrackCommand = ReactiveCommand.Create(ExecuteNextTrack);
PreviousTrackCommand = ReactiveCommand.Create(ExecutePreviousTrack);
NextIndexCommand = ReactiveCommand.Create(ExecuteNextIndex);
PreviousIndexCommand = ReactiveCommand.Create(ExecutePreviousIndex);
FastForwardCommand = ReactiveCommand.Create(ExecuteFastForward);
RewindCommand = ReactiveCommand.Create(ExecuteRewind);
VolumeUpCommand = ReactiveCommand.Create(ExecuteVolumeUp);
VolumeDownCommand = ReactiveCommand.Create(ExecuteVolumeDown);
ToggleMuteCommand = ReactiveCommand.Create(ExecuteToggleMute);
EnableDeEmphasisCommand = ReactiveCommand.Create(ExecuteEnableDeEmphasis);
DisableDeEmphasisCommand = ReactiveCommand.Create(ExecuteDisableDeEmphasis);
ToggleDeEmphasisCommand = ReactiveCommand.Create(ExecuteToggleDeEmphasis);
}
/// <summary>
/// Initialize the view model with 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>
2021-07-05 22:13:00 -07:00
/// <param name="loadHiddenTracks">Load hidden tracks for playback [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>
2021-07-05 22:13:00 -07:00
public void Init(string path, bool generateMissingToc, bool loadHiddenTracks, bool loadDataTracks, bool autoPlay, int defaultVolume)
{
2021-07-03 16:25:56 -07:00
// Stop current playback, if necessary
2021-07-12 10:41:11 -07:00
if(Playing != null) ExecuteStop();
2021-07-03 16:25:56 -07:00
// Create and attempt to initialize new Player
2021-07-05 22:13:00 -07:00
_player = new Player(path, generateMissingToc, loadHiddenTracks, loadDataTracks, 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>
2021-07-12 10:41:11 -07:00
public void ExecutePlay() => _player?.Play();
2021-07-04 23:17:30 -07:00
/// <summary>
/// Pause current playback
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecutePause() => _player?.Pause();
/// <summary>
/// Toggle playback
/// </summary>
public void ExecuteTogglePlayPause() => _player?.TogglePlayback();
2021-07-04 23:17:30 -07:00
/// <summary>
/// Stop current playback
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteStop() => _player?.Stop();
2021-07-04 23:17:30 -07:00
/// <summary>
/// Move to the next playable track
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteNextTrack() => _player?.NextTrack();
/// <summary>
/// Move to the previous playable track
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecutePreviousTrack() => _player?.PreviousTrack();
/// <summary>
/// Move to the next index
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteNextIndex() => _player?.NextIndex(App.Settings.IndexButtonChangeTrack);
/// <summary>
/// Move to the previous index
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecutePreviousIndex() => _player?.PreviousIndex(App.Settings.IndexButtonChangeTrack);
/// <summary>
/// Fast-forward playback by 75 sectors, if possible
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteFastForward() => _player?.FastForward();
/// <summary>
/// Rewind playback by 75 sectors, if possible
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteRewind() => _player?.Rewind();
#endregion
2021-07-12 10:41:11 -07:00
#region Volume
2021-07-04 23:17:30 -07:00
/// <summary>
2021-07-12 10:41:11 -07:00
/// Increment the volume value
2021-07-04 23:17:30 -07:00
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteVolumeUp() => _player?.VolumeUp();
2021-07-04 23:17:30 -07:00
/// <summary>
2021-07-12 10:41:11 -07:00
/// Decrement the volume value
2021-07-05 22:13:00 -07:00
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteVolumeDown() => _player?.VolumeDown();
2021-07-05 22:13:00 -07:00
/// <summary>
/// Set the value for the volume
/// </summary>
/// <param name="volume">New volume value</param>
2021-07-12 10:41:11 -07:00
public void ExecuteSetVolume(int volume) => _player?.SetVolume(volume);
2021-07-03 16:21:14 -07:00
/// <summary>
/// Temporarily mute playback
/// </summary>
2021-07-12 10:41:11 -07:00
public void ExecuteToggleMute() => _player?.ToggleMute();
#endregion
#region Emphasis
/// <summary>
/// Enable de-emphasis
/// </summary>
public void ExecuteEnableDeEmphasis() => _player?.EnableDeEmphasis();
/// <summary>
/// Disable de-emphasis
/// </summary>
public void ExecuteDisableDeEmphasis() => _player?.DisableDeEmphasis();
/// <summary>
/// Toggle de-emphasis
/// </summary>
public void ExecuteToggleDeEmphasis() => _player?.ToggleDeEmphasis();
#endregion
#region Helpers
2021-07-12 15:40:56 -07:00
/// <summary>
/// Load a disc image from a selection box
/// </summary>
public async void ExecuteLoad()
{
string path = await GetPath();
if(path == null)
return;
await LoadImage(path);
}
/// <summary>
/// Initialize the displayed digits array
/// </summary>
public void InitializeDigits()
{
PlayerView playerView = MainWindow.Instance.ContentControl.Content as PlayerView;
_digits = new Image[]
{
playerView.FindControl<Image>("TrackDigit1"),
playerView.FindControl<Image>("TrackDigit2"),
playerView.FindControl<Image>("IndexDigit1"),
playerView.FindControl<Image>("IndexDigit2"),
playerView.FindControl<Image>("TimeDigit1"),
playerView.FindControl<Image>("TimeDigit2"),
playerView.FindControl<Image>("TimeDigit3"),
playerView.FindControl<Image>("TimeDigit4"),
playerView.FindControl<Image>("TimeDigit5"),
playerView.FindControl<Image>("TimeDigit6"),
playerView.FindControl<Image>("TotalTracksDigit1"),
playerView.FindControl<Image>("TotalTracksDigit2"),
playerView.FindControl<Image>("TotalIndexesDigit1"),
playerView.FindControl<Image>("TotalIndexesDigit2"),
playerView.FindControl<Image>("TotalTimeDigit1"),
playerView.FindControl<Image>("TotalTimeDigit2"),
playerView.FindControl<Image>("TotalTimeDigit3"),
playerView.FindControl<Image>("TotalTimeDigit4"),
playerView.FindControl<Image>("TotalTimeDigit5"),
playerView.FindControl<Image>("TotalTimeDigit6"),
};
}
/// <summary>
/// Load an image from the path
/// </summary>
/// <param name="path">Path to the image to load</param>
public async Task<bool> LoadImage(string path)
{
return await Dispatcher.UIThread.InvokeAsync(() =>
{
Init(path, App.Settings.GenerateMissingTOC, App.Settings.PlayHiddenTracks, App.Settings.PlayDataTracks, App.Settings.AutoPlay, App.Settings.Volume);
if(Initialized)
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last();
return Initialized;
});
}
/// <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) => _player?.SetLoadDataTracks(load);
/// <summary>
/// Set the value for loading hidden tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading hidden tracks, false otherwise</param>
public void SetLoadHiddenTracks(bool load) => _player?.SetLoadHiddenTracks(load);
2021-07-12 10:52:50 -07:00
/// <summary>
/// Generate the digit string to be interpreted by the frontend
/// </summary>
/// <returns>String representing the digits for the frontend</returns>
2021-07-12 15:40:56 -07:00
private string GenerateDigitString()
2021-07-12 10:52:50 -07:00
{
// If the disc isn't initialized, return all '-' characters
if(Initialized != true)
return string.Empty.PadLeft(20, '-');
int usableTrackNumber = CurrentTrackNumber;
if(usableTrackNumber < 0)
usableTrackNumber = 0;
else if(usableTrackNumber > 99)
usableTrackNumber = 99;
// Otherwise, take the current time into account
ulong sectorTime = GetCurrentSectorTime();
int[] numbers = new int[]
{
usableTrackNumber,
CurrentTrackIndex,
(int)(sectorTime / (75 * 60)),
(int)(sectorTime / 75 % 60),
(int)(sectorTime % 75),
TotalTracks,
TotalIndexes,
(int)(TotalTime / (75 * 60)),
(int)(TotalTime / 75 % 60),
(int)(TotalTime % 75),
};
return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
}
2021-07-12 12:38:33 -07:00
/// <summary>
2021-07-12 15:40:56 -07:00
/// Load the png image for a given character based on the theme
2021-07-12 12:38:33 -07:00
/// </summary>
2021-07-12 15:40:56 -07:00
/// <param name="character">Character to load the image for</param>
/// <returns>Bitmap representing the loaded image</returns>
private Bitmap GetBitmap(char character)
2021-07-12 12:38:33 -07:00
{
2021-07-12 15:40:56 -07:00
try
2021-07-12 12:38:33 -07:00
{
2021-07-12 15:40:56 -07:00
if(App.Settings.SelectedTheme == "default")
{
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
}
else
{
string themeDirectory = $"{Directory.GetCurrentDirectory()}/themes/{App.Settings.SelectedTheme}";
using FileStream stream = File.Open($"{themeDirectory}/{character}.png", FileMode.Open);
return new Bitmap(stream);
}
}
catch
{
return null;
}
2021-07-12 12:38:33 -07:00
}
2021-07-12 10:52:50 -07:00
/// <summary>
/// Get current sector time, accounting for offsets
/// </summary>
/// <returns>ulong representing the current sector time</returns>
private ulong GetCurrentSectorTime()
{
ulong sectorTime = CurrentSector;
if(SectionStartSector != 0)
sectorTime -= SectionStartSector;
else if(CurrentTrackNumber > 0)
sectorTime += TimeOffset;
return sectorTime;
}
2021-07-12 12:38:33 -07:00
/// <summary>
/// Generate a path selection dialog box
/// </summary>
/// <returns>User-selected path, if possible</returns>
private async Task<string> GetPath()
{
2021-07-12 15:40:56 -07:00
return await Dispatcher.UIThread.InvokeAsync(async () =>
2021-07-12 12:38:33 -07:00
{
2021-07-12 15:40:56 -07:00
var dialog = new OpenFileDialog { AllowMultiple = false };
List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList();
dialog.Filters.Add(new FileDialogFilter()
{
Name = "Aaru Image Format (*" + string.Join(", *", knownExtensions) + ")",
Extensions = knownExtensions.ConvertAll(e => e.TrimStart('.'))
});
return (await dialog.ShowAsync(MainWindow.Instance))?.FirstOrDefault();
2021-07-12 12:38:33 -07:00
});
}
/// <summary>
2021-07-04 23:43:55 -07:00
/// Update the view-model from the 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-05 12:06:34 -07:00
SectionStartSector = _player.SectionStartSector;
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;
2021-07-12 15:40:56 -07:00
2021-07-12 16:37:12 -07:00
UpdateDigits();
}
/// <summary>
/// Update UI
/// </summary>
private void UpdateDigits()
{
Dispatcher.UIThread.Post(() =>
2021-07-12 15:40:56 -07:00
{
string digitString = GenerateDigitString() ?? string.Empty.PadLeft(20, '-');
for(int i = 0; i < _digits.Length; i++)
{
Bitmap digitImage = GetBitmap(digitString[i]);
if(_digits[i] != null && digitImage != null)
_digits[i].Source = digitImage;
}
});
}
#endregion
2021-06-06 21:42:14 -07:00
}
}