General refactor and clean-up.

This commit is contained in:
2021-06-06 20:28:36 +01:00
parent 0702d1a69e
commit 5eae13c3a2
13 changed files with 446 additions and 551 deletions

View File

@@ -1,5 +1,4 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Application xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="RedBookPlayer.App">
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml" />

View File

@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using Avalonia;
using Avalonia.Controls;
@@ -11,10 +12,8 @@ namespace RedBookPlayer
{
public static Settings Settings;
static App()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
}
static App() =>
Directory.SetCurrentDirectory(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));
public override void Initialize()
{

View File

@@ -5,7 +5,12 @@ namespace RedBookPlayer
{
public class DeEmphasisFilter : BiQuadFilter
{
static double B0, B1, B2, A0, A1, A2;
static readonly double B0;
static readonly double B1;
static readonly double B2;
static readonly double A0;
static readonly double A1;
static readonly double A2;
static DeEmphasisFilter()
{
@@ -15,17 +20,17 @@ namespace RedBookPlayer
double w0 = 2 * Math.PI * fc / 44100;
double A = Math.Exp(gain / 40 * Math.Log(10));
double alpha = Math.Sin(w0) / 2 * Math.Sqrt((A + 1 / A) * (1 / slope - 1) + 2);
double alpha = Math.Sin(w0) / 2 * Math.Sqrt(((A + (1 / A)) * ((1 / slope) - 1)) + 2);
double cs = Math.Cos(w0);
double v = 2 * Math.Sqrt(A) * alpha;
B0 = A * ((A + 1) + (A - 1) * cs + v);
B1 = -2 * A * ((A - 1) + (A + 1) * cs);
B2 = A * ((A + 1) + (A - 1) * cs - v);
A0 = (A + 1) - (A - 1) * cs + v;
A1 = 2 * ((A - 1) - (A + 1) * cs);
A2 = (A + 1) - (A - 1) * cs - v;
B0 = A * (A + 1 + ((A - 1) * cs) + v);
B1 = -2 * A * (A - 1 + ((A + 1) * cs));
B2 = A * (A + 1 + ((A - 1) * cs) - v);
A0 = A + 1 - ((A - 1) * cs) + v;
A1 = 2 * (A - 1 - ((A + 1) * cs));
A2 = A + 1 - ((A - 1) * cs) - v;
B2 /= A0;
B1 /= A0;
@@ -35,8 +40,6 @@ namespace RedBookPlayer
A0 = 1;
}
public DeEmphasisFilter() : base(B0, B1, B2, A0, A1, A2)
{
}
public DeEmphasisFilter() : base(B0, B1, B2, A0, A1, A2) {}
}
}

View File

@@ -2,34 +2,35 @@ using System;
using System.Diagnostics;
using System.Threading;
namespace RedBookPlayer {
namespace RedBookPlayer
{
public class HiResTimer
{
private static readonly float tickFrequency = 1000f / Stopwatch.Frequency;
static readonly float tickFrequency = 1000f / Stopwatch.Frequency;
public event EventHandler<HiResTimerElapsedEventArgs> Elapsed;
volatile float interval;
volatile bool isRunning;
private volatile float interval;
private volatile bool isRunning;
public HiResTimer() : this(1f)
{
}
public HiResTimer() : this(1f) {}
public HiResTimer(float interval)
{
if (interval < 0f || Single.IsNaN(interval))
if(interval < 0f ||
float.IsNaN(interval))
throw new ArgumentOutOfRangeException(nameof(interval));
this.interval = interval;
}
public float Interval
{
get { return interval; }
get => interval;
set
{
if (value < 0f || Single.IsNaN(value))
if(value < 0f ||
float.IsNaN(value))
throw new ArgumentOutOfRangeException(nameof(value));
interval = value;
}
}
@@ -43,30 +44,29 @@ namespace RedBookPlayer {
else
Stop();
}
get { return isRunning; }
get => isRunning;
}
public event EventHandler<HiResTimerElapsedEventArgs> Elapsed;
public void Start()
{
if(isRunning)
return;
isRunning = true;
Thread thread = new Thread(ExecuteTimer);
var thread = new Thread(ExecuteTimer);
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
public void Stop()
{
isRunning = false;
}
public void Stop() => isRunning = false;
private void ExecuteTimer()
void ExecuteTimer()
{
float nextTrigger = 0f;
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
while(isRunning)
@@ -78,6 +78,7 @@ namespace RedBookPlayer {
{
elapsed = ElapsedHiRes(stopwatch);
float diff = nextTrigger - elapsed;
if(diff <= 0f)
break;
@@ -94,33 +95,26 @@ namespace RedBookPlayer {
return;
}
float delay = elapsed - nextTrigger;
Elapsed?.Invoke(this, new HiResTimerElapsedEventArgs(delay));
if (stopwatch.Elapsed.TotalHours >= 1d)
{
if(!(stopwatch.Elapsed.TotalHours >= 1d))
continue;
stopwatch.Restart();
nextTrigger = 0f;
}
}
stopwatch.Stop();
}
private static float ElapsedHiRes(Stopwatch stopwatch)
{
return stopwatch.ElapsedTicks * tickFrequency;
}
static float ElapsedHiRes(Stopwatch stopwatch) => stopwatch.ElapsedTicks * tickFrequency;
}
public class HiResTimerElapsedEventArgs : EventArgs
{
public float Delay { get; }
internal HiResTimerElapsedEventArgs(float delay) => Delay = delay;
internal HiResTimerElapsedEventArgs(float delay)
{
Delay = delay;
}
public float Delay { get; }
}
}

View File

@@ -1,10 +1,6 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="RedBookPlayer.MainWindow"
Title="RedBookPlayer"
SizeToContent="WidthAndHeight">
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="RedBookPlayer.MainWindow" Title="RedBookPlayer" SizeToContent="WidthAndHeight">
<ContentControl Name="Content" />
</Window>

View File

@@ -1,8 +1,9 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Input;
using System;
using System.IO;
using System.Xml;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
namespace RedBookPlayer
{
@@ -27,7 +28,7 @@ namespace RedBookPlayer
if(theme == "default")
{
MainWindow.Instance.ContentControl.Content = new PlayerView();
Instance.ContentControl.Content = new PlayerView();
}
else
{
@@ -36,25 +37,26 @@ namespace RedBookPlayer
if(!File.Exists(xamlPath))
{
Console.WriteLine($"Warning: specified theme doesn't exist, reverting to default");
Console.WriteLine("Warning: specified theme doesn't exist, reverting to default");
return;
}
try
{
MainWindow.Instance.ContentControl.Content = new PlayerView(
File.ReadAllText(xamlPath).Replace("Source=\"", $"Source=\"file://{themeDirectory}/")
);
Instance.ContentControl.Content =
new PlayerView(File.ReadAllText(xamlPath).
Replace("Source=\"", $"Source=\"file://{themeDirectory}/"));
}
catch (System.Xml.XmlException ex)
catch(XmlException ex)
{
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
MainWindow.Instance.ContentControl.Content = new PlayerView();
Instance.ContentControl.Content = new PlayerView();
}
}
MainWindow.Instance.Width = ((PlayerView)MainWindow.Instance.ContentControl.Content).Width;
MainWindow.Instance.Height = ((PlayerView)MainWindow.Instance.ContentControl.Content).Height;
Instance.Width = ((PlayerView)Instance.ContentControl.Content).Width;
Instance.Height = ((PlayerView)Instance.ContentControl.Content).Height;
}
public void OnKeyDown(object sender, KeyEventArgs e)
@@ -66,28 +68,29 @@ namespace RedBookPlayer
}
}
private void InitializeComponent()
void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
ContentControl = this.FindControl<ContentControl>("Content");
ContentControl.Content = new PlayerView();
MainWindow.Instance.MaxWidth = ((PlayerView)MainWindow.Instance.ContentControl.Content).Width;
MainWindow.Instance.MaxHeight = ((PlayerView)MainWindow.Instance.ContentControl.Content).Height;
Instance.MaxWidth = ((PlayerView)Instance.ContentControl.Content).Width;
Instance.MaxHeight = ((PlayerView)Instance.ContentControl.Content).Height;
ContentControl.Content = new PlayerView();
this.CanResize = false;
CanResize = false;
this.KeyDown += OnKeyDown;
this.Closing += (s, e) =>
KeyDown += OnKeyDown;
Closing += (s, e) =>
{
settingsWindow?.Close();
settingsWindow = null;
};
this.Closing += (e, f) =>
Closing += (e, f) =>
{
PlayerView.Player.Stop();
};

View File

@@ -1,17 +1,16 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Structs;
using Aaru.Decoders.CD;
using static Aaru.Decoders.CD.FullTOC;
using Aaru.DiscImages;
using Aaru.Helpers;
using CSCore.SoundOut;
using CSCore;
using CSCore.SoundOut;
using NWaves.Audio;
using NWaves.Filters.BiQuad;
using static Aaru.Decoders.CD.FullTOC;
using WaveFormat = CSCore.WaveFormat;
namespace RedBookPlayer
{
@@ -19,48 +18,49 @@ namespace RedBookPlayer
{
public enum TrackType
{
Audio,
Data
Audio, Data
}
public bool Initialized = false;
private int currentTrack = 0;
readonly object readingImage = new object();
ushort currentIndex = 1;
ulong currentSector;
int currentSectorReadPosition;
int currentTrack;
BiQuadFilter deEmphasisFilterLeft;
BiQuadFilter deEmphasisFilterRight;
public bool Initialized;
ALSoundOut soundOut;
PlayerSource source;
CDFullTOC toc;
int volume = 100;
public int CurrentTrack
{
get
{
return currentTrack;
}
get => currentTrack;
private set
{
if (Image != null)
{
if (value >= Image.Tracks.Count)
{
currentTrack = 0;
}
else if (value < 0)
{
currentTrack = Image.Tracks.Count - 1;
}
else
{
currentTrack = value;
}
if(Image == null)
return;
if(value >= Image.Tracks.Count)
currentTrack = 0;
else if(value < 0)
currentTrack = Image.Tracks.Count - 1;
else
currentTrack = value;
byte[] flagsData =
Image.ReadSectorTag(Image.Tracks[CurrentTrack].TrackSequence, SectorTagType.CdTrackFlags);
byte[] flagsData = Image.ReadSectorTag(Image.Tracks[CurrentTrack].TrackSequence, SectorTagType.CdTrackFlags);
ApplyDeEmphasis = ((CdFlags)flagsData[0]).HasFlag(CdFlags.PreEmphasis);
byte[] subchannel = Image.ReadSectorTag(
Image.Tracks[CurrentTrack].TrackStartSector,
SectorTagType.CdSectorSubchannel
);
byte[] subchannel = Image.ReadSectorTag(Image.Tracks[CurrentTrack].TrackStartSector,
SectorTagType.CdSectorSubchannel);
if(!ApplyDeEmphasis)
{
ApplyDeEmphasis = (subchannel[3] & 0b01000000) != 0;
}
CopyAllowed = (subchannel[2] & 0b01000000) != 0;
TrackType_ = (subchannel[1] & 0b01000000) != 0 ? TrackType.Data : TrackType.Audio;
@@ -71,14 +71,10 @@ namespace RedBookPlayer
CurrentIndex = Image.Tracks[CurrentTrack].Indexes.Keys.Min();
}
}
}
ushort currentIndex = 1;
public ushort CurrentIndex
{
get
{
return currentIndex;
}
get => currentIndex;
private set
{
@@ -88,86 +84,79 @@ namespace RedBookPlayer
TotalTime = Image.Tracks[CurrentTrack].TrackEndSector - Image.Tracks[CurrentTrack].TrackStartSector;
}
}
private ulong currentSector = 0;
private int currentSectorReadPosition = 0;
public ulong CurrentSector
{
get
{
return currentSector;
}
get => currentSector;
private set
{
currentSector = value;
if (Image != null)
{
if ((CurrentTrack < Image.Tracks.Count - 1 && CurrentSector >= Image.Tracks[CurrentTrack + 1].TrackStartSector)
|| (CurrentTrack > 0 && CurrentSector < Image.Tracks[CurrentTrack].TrackStartSector))
if(Image == null)
return;
if((CurrentTrack < Image.Tracks.Count - 1 &&
CurrentSector >= Image.Tracks[CurrentTrack + 1].TrackStartSector) ||
(CurrentTrack > 0 && CurrentSector < Image.Tracks[CurrentTrack].TrackStartSector))
{
foreach(Track track in Image.Tracks.ToArray().Reverse())
{
if (CurrentSector >= track.TrackStartSector)
{
if(CurrentSector < track.TrackStartSector)
continue;
CurrentTrack = (int)track.TrackSequence - 1;
break;
}
}
}
foreach (var item in Image.Tracks[CurrentTrack].Indexes.Reverse())
foreach((ushort key, int i) in Image.Tracks[CurrentTrack].Indexes.Reverse())
{
if ((int)CurrentSector >= item.Value)
{
CurrentIndex = item.Key;
if((int)CurrentSector < i)
continue;
CurrentIndex = key;
return;
}
}
CurrentIndex = 0;
}
}
}
public bool TrackHasEmphasis { get; private set; } = false;
public bool ApplyDeEmphasis { get; private set; } = false;
public bool CopyAllowed { get; private set; } = false;
public bool TrackHasEmphasis { get; private set; }
public bool ApplyDeEmphasis { get; private set; }
public bool CopyAllowed { get; private set; }
public TrackType? TrackType_ { get; private set; }
public ulong SectionStartSector { get; private set; }
public int TotalTracks { get; private set; } = 0;
public int TotalIndexes { get; private set; } = 0;
public ulong TimeOffset { get; private set; } = 0;
public ulong TotalTime { get; private set; } = 0;
int volume = 100;
public int TotalTracks { get; private set; }
public int TotalIndexes { get; private set; }
public ulong TimeOffset { get; private set; }
public ulong TotalTime { get; private set; }
public int Volume
{
get
{
return volume;
}
get => volume;
set
{
if (volume >= 0 && volume <= 100)
{
if(volume >= 0 &&
volume <= 100)
volume = value;
}
}
}
public AaruFormat Image { get; private set; }
FullTOC.CDFullTOC toc;
PlayerSource source;
ALSoundOut soundOut;
BiQuadFilter deEmphasisFilterLeft;
BiQuadFilter deEmphasisFilterRight;
object readingImage = new object();
public async void Init(AaruFormat image, bool autoPlay = false)
{
this.Image = image;
Image = image;
if(await Task.Run(() => image.Info.ReadableMediaTags?.Contains(MediaTagType.CD_FullTOC)) != true)
{
Console.WriteLine("Full TOC not found");
return;
}
@@ -176,6 +165,7 @@ namespace RedBookPlayer
if((tocBytes?.Length ?? 0) == 0)
{
Console.WriteLine("Error reading TOC from disc image");
return;
}
@@ -188,17 +178,18 @@ namespace RedBookPlayer
tocBytes = tmp;
}
FullTOC.CDFullTOC? nullableToc = await Task.Run(() => FullTOC.Decode(tocBytes));
CDFullTOC? nullableToc = await Task.Run(() => Decode(tocBytes));
if(nullableToc == null)
{
Console.WriteLine("Error decoding TOC");
return;
}
toc = nullableToc.Value;
Console.WriteLine(FullTOC.Prettify(toc));
Console.WriteLine(Prettify(toc));
if(deEmphasisFilterLeft == null)
{
@@ -219,25 +210,19 @@ namespace RedBookPlayer
soundOut.Initialize(source);
}
else
{
soundOut.Stop();
}
CurrentTrack = 0;
LoadTrack(0);
if(autoPlay)
{
soundOut.Play();
}
else
{
TotalIndexes = 0;
}
TotalTracks = image.Tracks.Count;
TrackDataDescriptor firstTrack = toc.TrackDescriptors.First(d => d.ADR == 1 && d.POINT == 1);
TimeOffset = (ulong)(firstTrack.PMIN * 60 * 75 + firstTrack.PSEC * 75 + firstTrack.PFRAME);
TimeOffset = (ulong)((firstTrack.PMIN * 60 * 75) + (firstTrack.PSEC * 75) + firstTrack.PFRAME);
TotalTime = TimeOffset + image.Tracks.Last().TrackEndSector;
Volume = App.Settings.Volume;
@@ -256,7 +241,7 @@ namespace RedBookPlayer
do
{
sectorsToRead = (ulong)count / 2352 + 2;
sectorsToRead = ((ulong)count / 2352) + 2;
zeroSectorsAmount = 0;
if(CurrentSector + sectorsToRead > Image.Info.Sectors)
@@ -266,14 +251,14 @@ namespace RedBookPlayer
zeroSectorsAmount = oldSectorsToRead - sectorsToRead;
}
if (sectorsToRead <= 0)
{
if(sectorsToRead > 0)
continue;
LoadTrack(0);
currentSectorReadPosition = 0;
}
} while(sectorsToRead <= 0);
byte[] zeroSectors = new Byte[zeroSectorsAmount * 2352];
byte[] zeroSectors = new byte[zeroSectorsAmount * 2352];
Array.Clear(zeroSectors, 0, zeroSectors.Length);
byte[] audioData;
@@ -285,9 +270,10 @@ namespace RedBookPlayer
{
return Image.ReadSectors(CurrentSector, (uint)sectorsToRead).Concat(zeroSectors).ToArray();
}
catch (System.ArgumentOutOfRangeException)
catch(ArgumentOutOfRangeException)
{
LoadTrack(0);
return Image.ReadSectors(CurrentSector, (uint)sectorsToRead).Concat(zeroSectors).ToArray();
}
}
@@ -300,6 +286,7 @@ namespace RedBookPlayer
else
{
Array.Clear(buffer, offset, count);
return count;
}
@@ -333,11 +320,12 @@ namespace RedBookPlayer
Array.Copy(audioDataSegment, 0, buffer, offset, count);
currentSectorReadPosition += count;
if (currentSectorReadPosition >= 2352)
{
if(currentSectorReadPosition < 2352)
return count;
CurrentSector += (ulong)currentSectorReadPosition / 2352;
currentSectorReadPosition %= 2352;
}
return count;
}
@@ -355,9 +343,7 @@ namespace RedBookPlayer
public void Play()
{
if(Image == null)
{
return;
}
soundOut.Play();
TotalIndexes = Image.Tracks[CurrentTrack].Indexes.Keys.Max();
@@ -366,9 +352,7 @@ namespace RedBookPlayer
public void Pause()
{
if(Image == null)
{
return;
}
soundOut.Stop();
}
@@ -376,9 +360,7 @@ namespace RedBookPlayer
public void Stop()
{
if(Image == null)
{
return;
}
soundOut.Stop();
LoadTrack(CurrentTrack);
@@ -387,18 +369,12 @@ namespace RedBookPlayer
public void NextTrack()
{
if(Image == null)
{
return;
}
if(CurrentTrack + 1 >= Image.Tracks.Count)
{
CurrentTrack = 0;
}
else
{
CurrentTrack++;
}
LoadTrack(CurrentTrack);
}
@@ -406,28 +382,22 @@ namespace RedBookPlayer
public void PreviousTrack()
{
if(Image == null)
{
return;
}
if(CurrentSector < (ulong)Image.Tracks[CurrentTrack].Indexes[1] + 75)
{
if (App.Settings.AllowSkipHiddenTrack && CurrentTrack == 0 && CurrentSector >= 75)
{
if(App.Settings.AllowSkipHiddenTrack &&
CurrentTrack == 0 &&
CurrentSector >= 75)
CurrentSector = 0;
}
else
{
if(CurrentTrack - 1 < 0)
{
CurrentTrack = Image.Tracks.Count - 1;
}
else
{
CurrentTrack--;
}
}
}
LoadTrack(CurrentTrack);
}
@@ -435,51 +405,41 @@ namespace RedBookPlayer
public void NextIndex(bool changeTrack)
{
if(Image == null)
{
return;
}
if(CurrentIndex + 1 > Image.Tracks[CurrentTrack].Indexes.Keys.Max())
{
if (changeTrack)
{
if(!changeTrack)
return;
NextTrack();
CurrentSector = (ulong)Image.Tracks[CurrentTrack].Indexes.Values.Min();
}
}
else
{
CurrentSector = (ulong)Image.Tracks[CurrentTrack].Indexes[++CurrentIndex];
}
}
public void PreviousIndex(bool changeTrack)
{
if(Image == null)
{
return;
}
if(CurrentIndex - 1 < Image.Tracks[CurrentTrack].Indexes.Keys.Min())
{
if (changeTrack)
{
if(!changeTrack)
return;
PreviousTrack();
CurrentSector = (ulong)Image.Tracks[CurrentTrack].Indexes.Values.Max();
}
}
else
{
CurrentSector = (ulong)Image.Tracks[CurrentTrack].Indexes[--CurrentIndex];
}
}
public void FastForward()
{
if(Image == null)
{
return;
}
CurrentSector = Math.Min(Image.Info.Sectors - 1, CurrentSector + 75);
}
@@ -487,67 +447,52 @@ namespace RedBookPlayer
public void Rewind()
{
if(Image == null)
{
return;
}
if(CurrentSector >= 75)
CurrentSector -= 75;
}
public void EnableDeEmphasis()
{
ApplyDeEmphasis = true;
}
public void EnableDeEmphasis() => ApplyDeEmphasis = true;
public void DisableDeEmphasis()
{
ApplyDeEmphasis = false;
}
public void DisableDeEmphasis() => ApplyDeEmphasis = false;
}
public class PlayerSource : IWaveSource
{
public CSCore.WaveFormat WaveFormat => new CSCore.WaveFormat();
bool IAudioSource.CanSeek => throw new NotImplementedException();
public long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public long Length => throw new NotImplementedException();
public bool Run = true;
private ReadFunction read;
public delegate int ReadFunction(byte[] buffer, int offset, int count);
public PlayerSource(ReadFunction read)
readonly ReadFunction read;
public bool Run = true;
public PlayerSource(ReadFunction read) => this.read = read;
public WaveFormat WaveFormat => new WaveFormat();
bool IAudioSource.CanSeek => throw new NotImplementedException();
public long Position
{
this.read = read;
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public long Length => throw new NotImplementedException();
public int Read(byte[] buffer, int offset, int count)
{
if (!Run)
{
if(Run)
return read(buffer, offset, count);
Array.Clear(buffer, offset, count);
return count;
}
else
{
return read(buffer, offset, count);
}
}
public void Start()
{
Run = true;
}
public void Dispose() {}
public void Stop()
{
Run = false;
}
public void Start() => Run = true;
public void Dispose()
{
}
public void Stop() => Run = false;
}
}

View File

@@ -1,10 +1,7 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<UserControl xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="RedBookPlayer.PlayerView"
Width="900" Height="400">
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="RedBookPlayer.PlayerView" Width="900" Height="400">
<StackPanel Margin="16" VerticalAlignment="Center">
<Button Click="LoadButton_Click" Margin="32,0,32,16">Load</Button>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,16">
@@ -24,28 +21,28 @@
<StackPanel Margin="0,0,32,0">
<TextBlock Margin="0,0,0,4">TRACK</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Name="TrackDigit1" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TrackDigit2" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TrackDigit1" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TrackDigit2" Width="42" Height="51" Source="/Assets/-.png" />
</StackPanel>
</StackPanel>
<StackPanel Margin="0,0,32,0">
<TextBlock Margin="0,0,0,4">INDEX</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Name="IndexDigit1" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="IndexDigit2" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="IndexDigit1" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="IndexDigit2" Width="42" Height="51" Source="/Assets/-.png" />
</StackPanel>
</StackPanel>
<StackPanel>
<TextBlock Margin="0,0,0,4">TIME</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Name="TimeDigit1" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TimeDigit2" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Width="11" Height="51" Source="/Assets/colon.png"></Image>
<Image Name="TimeDigit3" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TimeDigit4" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Width="11" Height="51" Source="/Assets/colon.png"></Image>
<Image Name="TimeDigit5" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TimeDigit6" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TimeDigit1" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TimeDigit2" Width="42" Height="51" Source="/Assets/-.png" />
<Image Width="11" Height="51" Source="/Assets/colon.png" />
<Image Name="TimeDigit3" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TimeDigit4" Width="42" Height="51" Source="/Assets/-.png" />
<Image Width="11" Height="51" Source="/Assets/colon.png" />
<Image Name="TimeDigit5" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TimeDigit6" Width="42" Height="51" Source="/Assets/-.png" />
</StackPanel>
</StackPanel>
</StackPanel>
@@ -53,34 +50,40 @@
<StackPanel Margin="0,0,32,0">
<TextBlock Margin="0,0,0,4">TRACKS</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Name="TotalTracksDigit1" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalTracksDigit2" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalTracksDigit1" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TotalTracksDigit2" Width="42" Height="51" Source="/Assets/-.png" />
</StackPanel>
</StackPanel>
<StackPanel Margin="0,0,32,0">
<TextBlock Margin="0,0,0,4">INDEXES</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Name="TotalIndexesDigit1" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalIndexesDigit2" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalIndexesDigit1" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TotalIndexesDigit2" Width="42" Height="51" Source="/Assets/-.png" />
</StackPanel>
</StackPanel>
<StackPanel>
<TextBlock Margin="0,0,0,4">TOTAL</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Name="TotalTimeDigit1" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalTimeDigit2" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Width="11" Height="51" Source="/Assets/colon.png"></Image>
<Image Name="TotalTimeDigit3" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalTimeDigit4" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Width="11" Height="51" Source="/Assets/colon.png"></Image>
<Image Name="TotalTimeDigit5" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalTimeDigit6" Width="42" Height="51" Source="/Assets/-.png"></Image>
<Image Name="TotalTimeDigit1" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TotalTimeDigit2" Width="42" Height="51" Source="/Assets/-.png" />
<Image Width="11" Height="51" Source="/Assets/colon.png" />
<Image Name="TotalTimeDigit3" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TotalTimeDigit4" Width="42" Height="51" Source="/Assets/-.png" />
<Image Width="11" Height="51" Source="/Assets/colon.png" />
<Image Name="TotalTimeDigit5" Width="42" Height="51" Source="/Assets/-.png" />
<Image Name="TotalTimeDigit6" Width="42" Height="51" Source="/Assets/-.png" />
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,16">
<Button Click="EnableDeEmphasisButton_Click" IsVisible="{Binding !ApplyDeEmphasis}" Width="200" Margin="0,0,16,0">Enable De-Emphasis</Button>
<Button Click="DisableDeEmphasisButton_Click" IsVisible="{Binding ApplyDeEmphasis}" Width="200" Margin="0,0,16,0">Disable De-Emphasis</Button>
<Button Click="EnableDeEmphasisButton_Click" IsVisible="{Binding !ApplyDeEmphasis}" Width="200"
Margin="0,0,16,0">
Enable De-Emphasis
</Button>
<Button Click="DisableDeEmphasisButton_Click" IsVisible="{Binding ApplyDeEmphasis}" Width="200"
Margin="0,0,16,0">
Disable De-Emphasis
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Margin="0,0,16,0" Foreground="LightGray" IsVisible="{Binding !IsAudioTrack}">AUDIO</TextBlock>

View File

@@ -20,21 +20,15 @@ namespace RedBookPlayer
{
public class PlayerView : UserControl
{
public PlayerView()
{
InitializeComponent(null);
}
public PlayerView(string xaml)
{
InitializeComponent(xaml);
}
public static Player Player = new Player();
TextBlock currentTrack;
Image[] digits;
Timer updateTimer;
public PlayerView() => InitializeComponent(null);
public PlayerView(string xaml) => InitializeComponent(xaml);
public async void LoadButton_Click(object sender, RoutedEventArgs e)
{
string path = await GetPath();
@@ -46,7 +40,7 @@ namespace RedBookPlayer
await Task.Run(() =>
{
AaruFormat image = new AaruFormat();
var image = new AaruFormat();
IFilter filter = new ZZZNoFilter();
filter.Open(path);
image.Open(filter);
@@ -57,86 +51,55 @@ namespace RedBookPlayer
await Dispatcher.UIThread.InvokeAsync(() =>
{
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last();
}
);
});
}
public async Task<string> GetPath()
{
OpenFileDialog dialog = new OpenFileDialog();
var dialog = new OpenFileDialog();
dialog.AllowMultiple = false;
List<string> knownExtensions = (new Aaru.DiscImages.AaruFormat()).KnownExtensions.ToList();
dialog.Filters.Add(new FileDialogFilter()
List<string> knownExtensions = new AaruFormat().KnownExtensions.ToList();
dialog.Filters.Add(new FileDialogFilter
{
Name = "Aaru Image Format (*" + string.Join(", *", knownExtensions) + ")",
Extensions = knownExtensions.ConvertAll(e => e.Substring(1))
}
);
});
return (await dialog.ShowAsync((Window)this.Parent.Parent))?.FirstOrDefault();
return (await dialog.ShowAsync((Window)Parent.Parent))?.FirstOrDefault();
}
public void PlayButton_Click(object sender, RoutedEventArgs e)
{
Player.Play();
}
public void PlayButton_Click(object sender, RoutedEventArgs e) => Player.Play();
public void PauseButton_Click(object sender, RoutedEventArgs e)
{
Player.Pause();
}
public void PauseButton_Click(object sender, RoutedEventArgs e) => Player.Pause();
public void StopButton_Click(object sender, RoutedEventArgs e)
{
Player.Stop();
}
public void StopButton_Click(object sender, RoutedEventArgs e) => Player.Stop();
public void NextTrackButton_Click(object sender, RoutedEventArgs e)
{
Player.NextTrack();
}
public void NextTrackButton_Click(object sender, RoutedEventArgs e) => Player.NextTrack();
public void PreviousTrackButton_Click(object sender, RoutedEventArgs e)
{
Player.PreviousTrack();
}
public void PreviousTrackButton_Click(object sender, RoutedEventArgs e) => Player.PreviousTrack();
public void NextIndexButton_Click(object sender, RoutedEventArgs e)
{
public void NextIndexButton_Click(object sender, RoutedEventArgs e) =>
Player.NextIndex(App.Settings.IndexButtonChangeTrack);
}
public void PreviousIndexButton_Click(object sender, RoutedEventArgs e)
{
public void PreviousIndexButton_Click(object sender, RoutedEventArgs e) =>
Player.PreviousIndex(App.Settings.IndexButtonChangeTrack);
}
public void FastForwardButton_Click(object sender, RoutedEventArgs e)
{
Player.FastForward();
}
public void FastForwardButton_Click(object sender, RoutedEventArgs e) => Player.FastForward();
public void RewindButton_Click(object sender, RoutedEventArgs e)
{
Player.Rewind();
}
public void RewindButton_Click(object sender, RoutedEventArgs e) => Player.Rewind();
public void EnableDeEmphasisButton_Click(object sender, RoutedEventArgs e)
{
Player.EnableDeEmphasis();
}
public void EnableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => Player.EnableDeEmphasis();
public void DisableDeEmphasisButton_Click(object sender, RoutedEventArgs e)
{
Player.DisableDeEmphasis();
}
public void DisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => Player.DisableDeEmphasis();
private void UpdateView(object sender, ElapsedEventArgs e)
void UpdateView(object sender, ElapsedEventArgs e)
{
if(Player.Initialized)
{
ulong sectorTime = Player.CurrentSector;
if(Player.SectionStartSector != 0)
{
sectorTime -= Player.SectionStartSector;
@@ -146,20 +109,14 @@ namespace RedBookPlayer
sectorTime += Player.TimeOffset;
}
int[] numbers = new int[]{
Player.CurrentTrack + 1,
Player.CurrentIndex,
(int)(sectorTime / (75 * 60)),
(int)((sectorTime / 75) % 60),
(int)(sectorTime % 75),
Player.TotalTracks,
Player.TotalIndexes,
(int)(Player.TotalTime / (75 * 60)),
(int)((Player.TotalTime / 75) % 60),
(int)(Player.TotalTime % 75),
int[] numbers =
{
Player.CurrentTrack + 1, Player.CurrentIndex, (int)(sectorTime / (75 * 60)),
(int)(sectorTime / 75 % 60), (int)(sectorTime % 75), Player.TotalTracks, Player.TotalIndexes,
(int)(Player.TotalTime / (75 * 60)), (int)(Player.TotalTime / 75 % 60), (int)(Player.TotalTime % 75)
};
string digitString = String.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
string digitString = string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
Dispatcher.UIThread.InvokeAsync(() =>
{
@@ -171,7 +128,7 @@ namespace RedBookPlayer
}
}
PlayerViewModel dataContext = (PlayerViewModel)DataContext;
var dataContext = (PlayerViewModel)DataContext;
dataContext.HiddenTrack = Player.TimeOffset > 150;
dataContext.ApplyDeEmphasis = Player.ApplyDeEmphasis;
dataContext.TrackHasEmphasis = Player.TrackHasEmphasis;
@@ -195,24 +152,25 @@ namespace RedBookPlayer
}
}
private Bitmap GetBitmap(char character)
Bitmap GetBitmap(char character)
{
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;
Bitmap bitmap;
using(FileStream stream = File.Open(themeDirectory + $"/{character}.png", FileMode.Open))
{
bitmap = new Bitmap(stream);
}
return bitmap;
}
}
public void Initialize()
{
@@ -247,7 +205,7 @@ namespace RedBookPlayer
currentTrack = this.FindControl<TextBlock>("CurrentTrack");
}
private void InitializeComponent(string xaml)
void InitializeComponent(string xaml)
{
DataContext = new PlayerViewModel();
@@ -263,6 +221,7 @@ namespace RedBookPlayer
Initialize();
updateTimer = new Timer(1000 / 60);
updateTimer.Elapsed += (sender, e) =>
{
try
@@ -274,6 +233,7 @@ namespace RedBookPlayer
Console.WriteLine(ex);
}
};
updateTimer.AutoReset = true;
updateTimer.Start();
}
@@ -281,37 +241,43 @@ namespace RedBookPlayer
public class PlayerViewModel : ReactiveObject
{
private bool applyDeEmphasis;
bool applyDeEmphasis;
bool copyAllowed;
bool hiddenTrack;
bool isAudioTrack;
bool isDataTrack;
bool trackHasEmphasis;
public bool ApplyDeEmphasis
{
get => applyDeEmphasis;
set => this.RaiseAndSetIfChanged(ref applyDeEmphasis, value);
}
private bool trackHasEmphasis;
public bool TrackHasEmphasis
{
get => trackHasEmphasis;
set => this.RaiseAndSetIfChanged(ref trackHasEmphasis, value);
}
private bool hiddenTrack;
public bool HiddenTrack
{
get => hiddenTrack;
set => this.RaiseAndSetIfChanged(ref hiddenTrack, value);
}
private bool copyAllowed;
public bool CopyAllowed
{
get => copyAllowed;
set => this.RaiseAndSetIfChanged(ref copyAllowed, value);
}
private bool isAudioTrack;
public bool IsAudioTrack
{
get => isAudioTrack;
set => this.RaiseAndSetIfChanged(ref isAudioTrack, value);
}
private bool isDataTrack;
public bool IsDataTrack
{
get => isDataTrack;

View File

@@ -1,10 +1,9 @@
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia;
using Avalonia.Logging.Serilog;
namespace RedBookPlayer
{
class Program
internal class Program
{
public static void Main(string[] args)
{
@@ -20,9 +19,6 @@ namespace RedBookPlayer
static extern bool AllocConsole();
#endif
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToDebug();
public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure<App>().UsePlatformDetect().LogToDebug();
}
}

View File

@@ -6,19 +6,17 @@ namespace RedBookPlayer
{
public class Settings
{
public bool AutoPlay { get; set; } = false;
public bool IndexButtonChangeTrack { get; set; } = false;
public bool AllowSkipHiddenTrack { get; set; } = false;
public int Volume { get; set; } = 100;
public string SelectedTheme { get; set; } = "default";
string filePath;
public Settings() {}
public Settings(string filePath)
{
this.filePath = filePath;
}
public Settings(string filePath) => this.filePath = filePath;
public bool AutoPlay { get; set; }
public bool IndexButtonChangeTrack { get; set; }
public bool AllowSkipHiddenTrack { get; set; }
public int Volume { get; set; } = 100;
public string SelectedTheme { get; set; } = "default";
public static Settings Load(string filePath)
{
@@ -36,18 +34,17 @@ namespace RedBookPlayer
catch(JsonException)
{
Console.WriteLine("Couldn't parse settings, reverting to default");
return new Settings(filePath);
}
}
else
{
return new Settings(filePath);
}
}
return new Settings(filePath);
}
public void Save()
{
JsonSerializerOptions options = new JsonSerializerOptions()
var options = new JsonSerializerOptions
{
WriteIndented = true
};

View File

@@ -1,11 +1,7 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="RedBookPlayer.SettingsWindow"
Title="Settings"
Width="450" Height="600">
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800"
d:DesignHeight="450" x:Class="RedBookPlayer.SettingsWindow" Title="Settings" Width="450" Height="600">
<DockPanel Margin="16">
<TextBlock DockPanel.Dock="Top" Margin="0,0,0,4">Themes</TextBlock>
<StackPanel DockPanel.Dock="Bottom">
@@ -24,8 +20,10 @@
<DockPanel Margin="0,0,0,16">
<TextBlock VerticalAlignment="Center" Margin="0,0,8,0" DockPanel.Dock="Left">Volume</TextBlock>
<TextBlock VerticalAlignment="Center" DockPanel.Dock="Right" Text="%" />
<TextBlock VerticalAlignment="Center" Margin="8,0,0,0" DockPanel.Dock="Right" Text="{Binding Volume}" Name="VolumeLabel"/>
<Slider Minimum="0" Maximum="100" SmallChange="1" LargeChange="10" Value="{Binding Volume}" Name="VolumeSlider"/>
<TextBlock VerticalAlignment="Center" Margin="8,0,0,0" DockPanel.Dock="Right" Text="{Binding Volume}"
Name="VolumeLabel" />
<Slider Minimum="0" Maximum="100" SmallChange="1" LargeChange="10" Value="{Binding Volume}"
Name="VolumeSlider" />
</DockPanel>
<Button Name="ApplyButton">Apply</Button>
</StackPanel>

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using Avalonia.Controls;
@@ -9,15 +8,15 @@ namespace RedBookPlayer
{
public class SettingsWindow : Window
{
Settings settings;
ListBox themeList;
readonly Settings settings;
string selectedTheme;
ListBox themeList;
public SettingsWindow() {}
public SettingsWindow(Settings settings)
{
this.DataContext = this.settings = settings;
DataContext = this.settings = settings;
InitializeComponent();
}
@@ -44,19 +43,16 @@ namespace RedBookPlayer
settings.Save();
}
public void UpdateView()
{
this.FindControl<TextBlock>("VolumeLabel").Text = settings.Volume.ToString();
}
public void UpdateView() => this.FindControl<TextBlock>("VolumeLabel").Text = settings.Volume.ToString();
private void InitializeComponent()
void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
themeList = this.FindControl<ListBox>("ThemeList");
themeList.SelectionChanged += ThemeList_SelectionChanged;
List<String> items = new List<String>();
List<string> items = new List<string>();
items.Add("default");
if(Directory.Exists("themes/"))