Split common and GUI code into separate projects

This commit is contained in:
Matt Nadareski
2021-07-05 13:20:06 -07:00
parent 30e4573be6
commit 4efc58d0ef
35 changed files with 54 additions and 23 deletions

View File

@@ -0,0 +1,48 @@
using System;
using NWaves.Filters.BiQuad;
namespace RedBookPlayer.Common.Hardware
{
/// <summary>
/// Filter for applying de-emphasis to audio
/// </summary>
public class DeEmphasisFilter : BiQuadFilter
{
private static readonly double B0;
private static readonly double B1;
private static readonly double B2;
private static readonly double A0;
private static readonly double A1;
private static readonly double A2;
static DeEmphasisFilter()
{
double fc = 5277;
double slope = 0.4850;
double gain = -9.465;
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 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;
B2 /= A0;
B1 /= A0;
B0 /= A0;
A2 /= A0;
A1 /= A0;
A0 = 1;
}
public DeEmphasisFilter() : base(B0, B1, B2, A0, A1, A2) {}
}
}

View File

@@ -0,0 +1,432 @@
using System;
using System.ComponentModel;
using Aaru.CommonTypes.Enums;
using ReactiveUI;
using RedBookPlayer.Common.Discs;
namespace RedBookPlayer.Common.Hardware
{
public class Player : ReactiveObject
{
/// <summary>
/// Indicate if the player is ready to be used
/// </summary>
public bool Initialized { get; private set; } = false;
#region OpticalDisc Passthrough
/// <summary>
/// Current track number
/// </summary>
public int CurrentTrackNumber
{
get => _currentTrackNumber;
private set => this.RaiseAndSetIfChanged(ref _currentTrackNumber, value);
}
/// <summary>
/// Current track index
/// </summary>
public ushort CurrentTrackIndex
{
get => _currentTrackIndex;
private set => this.RaiseAndSetIfChanged(ref _currentTrackIndex, value);
}
/// <summary>
/// Current sector number
/// </summary>
public ulong CurrentSector
{
get => _currentSector;
private set => this.RaiseAndSetIfChanged(ref _currentSector, value);
}
/// <summary>
/// Represents the sector starting the section
/// </summary>
public ulong SectionStartSector
{
get => _sectionStartSector;
protected set => this.RaiseAndSetIfChanged(ref _sectionStartSector, value);
}
/// <summary>
/// Represents if the disc has a hidden track
/// </summary>
public bool HiddenTrack
{
get => _hasHiddenTrack;
private set => this.RaiseAndSetIfChanged(ref _hasHiddenTrack, value);
}
/// <summary>
/// Represents the 4CH flag [CompactDisc only]
/// </summary>
public bool QuadChannel
{
get => _quadChannel;
private set => this.RaiseAndSetIfChanged(ref _quadChannel, value);
}
/// <summary>
/// Represents the DATA flag [CompactDisc only]
/// </summary>
public bool IsDataTrack
{
get => _isDataTrack;
private set => this.RaiseAndSetIfChanged(ref _isDataTrack, value);
}
/// <summary>
/// Represents the DCP flag [CompactDisc only]
/// </summary>
public bool CopyAllowed
{
get => _copyAllowed;
private set => this.RaiseAndSetIfChanged(ref _copyAllowed, value);
}
/// <summary>
/// Represents the PRE flag [CompactDisc only]
/// </summary>
public bool TrackHasEmphasis
{
get => _trackHasEmphasis;
private set => this.RaiseAndSetIfChanged(ref _trackHasEmphasis, value);
}
/// <summary>
/// Represents the total tracks on the disc
/// </summary>
public int TotalTracks => _opticalDisc.TotalTracks;
/// <summary>
/// Represents the total indices on the disc
/// </summary>
public int TotalIndexes => _opticalDisc.TotalIndexes;
/// <summary>
/// Total sectors in the image
/// </summary>
public ulong TotalSectors => _opticalDisc.TotalSectors;
/// <summary>
/// Represents the time adjustment offset for the disc
/// </summary>
public ulong TimeOffset => _opticalDisc.TimeOffset;
/// <summary>
/// Represents the total playing time for the disc
/// </summary>
public ulong TotalTime => _opticalDisc.TotalTime;
private int _currentTrackNumber;
private ushort _currentTrackIndex;
private ulong _currentSector;
private ulong _sectionStartSector;
private bool _hasHiddenTrack;
private bool _quadChannel;
private bool _isDataTrack;
private bool _copyAllowed;
private bool _trackHasEmphasis;
#endregion
#region SoundOutput Passthrough
/// <summary>
/// Indicate if the output is playing
/// </summary>
public bool? Playing
{
get => _playing;
private set => this.RaiseAndSetIfChanged(ref _playing, value);
}
/// <summary>
/// Indicates if de-emphasis should be applied
/// </summary>
public bool ApplyDeEmphasis
{
get => _applyDeEmphasis;
private set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
/// <summary>
/// Current playback volume
/// </summary>
public int Volume
{
get => _volume;
set => this.RaiseAndSetIfChanged(ref _volume, value);
}
private bool? _playing;
private bool _applyDeEmphasis;
private int _volume;
#endregion
#region Private State Variables
/// <summary>
/// Sound output handling class
/// </summary>
private readonly SoundOutput _soundOutput;
/// <summary>
/// OpticalDisc object
/// </summary>
private readonly OpticalDisc _opticalDisc;
#endregion
/// <summary>
/// Create a new Player from a given image path
/// </summary>
/// <param name="path">Path to the disc image</param>
/// <param name="generateMissingToc">Generate a TOC if the disc is missing one [CompactDisc only]</param>
/// <param name="loadDataTracks">Load data tracks for playback [CompactDisc only]</param>
/// <param name="autoPlay">True if playback should begin immediately, false otherwise</param>
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
public Player(string path, bool generateMissingToc, bool loadDataTracks, bool autoPlay, int defaultVolume)
{
// Set the internal state for initialization
Initialized = false;
_soundOutput = new SoundOutput();
_soundOutput.SetDeEmphasis(false);
// Initalize the disc
_opticalDisc = OpticalDiscFactory.GenerateFromPath(path, generateMissingToc, loadDataTracks, autoPlay);
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
// Add event handling for the optical disc
_opticalDisc.PropertyChanged += OpticalDiscStateChanged;
// Initialize the sound output
_soundOutput.Init(_opticalDisc, autoPlay, defaultVolume);
if(_soundOutput == null || !_soundOutput.Initialized)
return;
// Add event handling for the sound output
_soundOutput.PropertyChanged += SoundOutputStateChanged;
// Mark the player as ready
Initialized = true;
// Force a refresh of the state information
OpticalDiscStateChanged(this, null);
SoundOutputStateChanged(this, null);
}
#region Playback
/// <summary>
/// Begin playback
/// </summary>
public void Play()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
else if(_soundOutput == null)
return;
else if(_soundOutput.Playing)
return;
_soundOutput.Play();
_opticalDisc.SetTotalIndexes();
Playing = true;
}
/// <summary>
/// Pause current playback
/// </summary>
public void Pause()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
else if(_soundOutput == null)
return;
else if(!_soundOutput.Playing)
return;
_soundOutput?.Stop();
Playing = false;
}
/// <summary>
/// Stop current playback
/// </summary>
public void Stop()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
else if(_soundOutput == null)
return;
else if(!_soundOutput.Playing)
return;
_soundOutput?.Stop();
_opticalDisc.LoadFirstTrack();
Playing = null;
}
/// <summary>
/// Move to the next playable track
/// </summary>
public void NextTrack()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
if(wasPlaying == true) Pause();
_opticalDisc.NextTrack();
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
if(wasPlaying == true) Play();
}
/// <summary>
/// Move to the previous playable track
/// </summary>
/// <param name="playHiddenTrack">True to play the hidden track, if it exists</param>
public void PreviousTrack(bool playHiddenTrack)
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
if(wasPlaying == true) Pause();
_opticalDisc.PreviousTrack(playHiddenTrack);
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
if(wasPlaying == true) Play();
}
/// <summary>
/// Move to the next index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
public void NextIndex(bool changeTrack)
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
if(wasPlaying == true) Pause();
_opticalDisc.NextIndex(changeTrack);
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
if(wasPlaying == true) Play();
}
/// <summary>
/// Move to the previous index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
/// <param name="playHiddenTrack">True to play the hidden track, if it exists</param>
public void PreviousIndex(bool changeTrack, bool playHiddenTrack)
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool? wasPlaying = Playing;
if(wasPlaying == true) Pause();
_opticalDisc.PreviousIndex(changeTrack, playHiddenTrack);
if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.SetDeEmphasis(compactDisc.TrackHasEmphasis);
if(wasPlaying == true) Play();
}
/// <summary>
/// Fast-forward playback by 75 sectors, if possible
/// </summary>
public void FastForward()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
_opticalDisc.CurrentSector = Math.Min(_opticalDisc.TotalSectors, _opticalDisc.CurrentSector + 75);
}
/// <summary>
/// Rewind playback by 75 sectors, if possible
/// </summary>
public void Rewind()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
if(_opticalDisc.CurrentSector >= 75)
_opticalDisc.CurrentSector -= 75;
}
#endregion
#region Helpers
/// <summary>
/// Set de-emphasis status
/// </summary>
/// <param name="apply"></param>
public void SetDeEmphasis(bool apply) => _soundOutput?.SetDeEmphasis(apply);
/// <summary>
/// Set the value for loading data tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading data tracks, false otherwise</param>
public void SetLoadDataTracks(bool load) => (_opticalDisc as CompactDisc)?.SetLoadDataTracks(load);
/// <summary>
/// Update the player from the current OpticalDisc
/// </summary>
private void OpticalDiscStateChanged(object sender, PropertyChangedEventArgs e)
{
CurrentTrackNumber = _opticalDisc.CurrentTrackNumber;
CurrentTrackIndex = _opticalDisc.CurrentTrackIndex;
CurrentSector = _opticalDisc.CurrentSector;
SectionStartSector = _opticalDisc.SectionStartSector;
HiddenTrack = TimeOffset > 150;
if(_opticalDisc is CompactDisc compactDisc)
{
QuadChannel = compactDisc.QuadChannel;
IsDataTrack = compactDisc.IsDataTrack;
CopyAllowed = compactDisc.CopyAllowed;
TrackHasEmphasis = compactDisc.TrackHasEmphasis;
}
else
{
QuadChannel = false;
IsDataTrack = _opticalDisc.TrackType != TrackType.Audio;
CopyAllowed = false;
TrackHasEmphasis = false;
}
}
/// <summary>
/// Update the player from the current SoundOutput
/// </summary>
private void SoundOutputStateChanged(object sender, PropertyChangedEventArgs e)
{
Playing = _soundOutput.Playing;
ApplyDeEmphasis = _soundOutput.ApplyDeEmphasis;
Volume = _soundOutput.Volume;
}
#endregion
}
}

View File

@@ -0,0 +1,44 @@
using System;
using CSCore;
using WaveFormat = CSCore.WaveFormat;
namespace RedBookPlayer.Common.Hardware
{
public class PlayerSource : IWaveSource
{
public delegate int ReadFunction(byte[] buffer, int offset, int count);
readonly ReadFunction _read;
public bool Run = true;
public PlayerSource(ReadFunction read) => _read = read;
public WaveFormat WaveFormat => new 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 int Read(byte[] buffer, int offset, int count)
{
if(Run)
return _read(buffer, offset, count);
Array.Clear(buffer, offset, count);
return count;
}
public void Dispose() {}
public void Start() => Run = true;
public void Stop() => Run = false;
}
}

View File

@@ -0,0 +1,342 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using CSCore.SoundOut;
using NWaves.Audio;
using NWaves.Filters.BiQuad;
using ReactiveUI;
using RedBookPlayer.Common.Discs;
namespace RedBookPlayer.Common.Hardware
{
public class SoundOutput : ReactiveObject
{
#region Public Fields
/// <summary>
/// Indicate if the output is ready to be used
/// </summary>
public bool Initialized { get; private set; } = 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
{
get => _applyDeEmphasis;
private set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
/// <summary>
/// Current playback volume
/// </summary>
public int Volume
{
get => _volume;
set
{
int tempVolume = value;
if(value > 100)
tempVolume = 100;
else if(value < 0)
tempVolume = 0;
this.RaiseAndSetIfChanged(ref _volume, tempVolume);
}
}
private bool _playing;
private bool _applyDeEmphasis;
private int _volume;
#endregion
#region Private State Variables
/// <summary>
/// OpticalDisc from the parent player for easy access
/// </summary>
/// <remarks>
/// TODO: Can we remove the need for a local reference to OpticalDisc?
/// </remarks>
private OpticalDisc _opticalDisc;
/// <summary>
/// Data provider for sound output
/// </summary>
private PlayerSource _source;
/// <summary>
/// Sound output instance
/// </summary>
private ALSoundOut _soundOut;
/// <summary>
/// Left channel de-emphasis filter
/// </summary>
private BiQuadFilter _deEmphasisFilterLeft;
/// <summary>
/// Right channel de-emphasis filter
/// </summary>
private BiQuadFilter _deEmphasisFilterRight;
/// <summary>
/// Current position in the sector
/// </summary>
private int _currentSectorReadPosition = 0;
/// <summary>
/// Lock object for reading track data
/// </summary>
private readonly object _readingImage = new object();
#endregion
/// <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(OpticalDisc opticalDisc, bool autoPlay = false, int defaultVolume = 100)
{
// If we have an unusable disc, just return
if(opticalDisc == null || !opticalDisc.Initialized)
return;
// 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;
// Setup de-emphasis filters
SetupFilters();
// Setup the audio output
SetupAudio();
// Initialize playback, if necessary
if(autoPlay)
_soundOut.Play();
// Mark the output as ready
Initialized = true;
// Begin loading data
_source.Start();
}
/// <summary>
/// Fill the current byte buffer with playable data
/// </summary>
/// <param name="buffer">Buffer to load data into</param>
/// <param name="offset">Offset in the buffer to load at</param>
/// <param name="count">Number of bytes to load</param>
/// <returns>Number of bytes read</returns>
public int ProviderRead(byte[] buffer, int offset, int count)
{
// Set the current volume
_soundOut.Volume = (float)Volume / 100;
// Determine how many sectors we can read
ulong sectorsToRead;
ulong zeroSectorsAmount;
do
{
// Attempt to read 2 more sectors than requested
sectorsToRead = ((ulong)count / (ulong)_opticalDisc.BytesPerSector) + 2;
zeroSectorsAmount = 0;
// 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);
}
// TODO: Figure out when this value could be negative
if(sectorsToRead <= 0)
{
_opticalDisc.LoadFirstTrack();
_currentSectorReadPosition = 0;
}
} while(sectorsToRead <= 0);
// 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
{
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)
{
float[][] floatAudioData = new float[2][];
floatAudioData[0] = new float[audioDataSegment.Length / 4];
floatAudioData[1] = new float[audioDataSegment.Length / 4];
ByteConverter.ToFloats16Bit(audioDataSegment, floatAudioData);
for(int i = 0; i < floatAudioData[0].Length; i++)
{
floatAudioData[0][i] = _deEmphasisFilterLeft.Process(floatAudioData[0][i]);
floatAudioData[1][i] = _deEmphasisFilterRight.Process(floatAudioData[1][i]);
}
ByteConverter.FromFloats16Bit(floatAudioData, audioDataSegment);
}
// Write out the audio data to the buffer
Array.Copy(audioDataSegment, 0, buffer, offset, count);
// Set the read position in the sector for easier access
_currentSectorReadPosition += count;
if(_currentSectorReadPosition >= _opticalDisc.BytesPerSector)
{
_opticalDisc.CurrentSector += (ulong)(_currentSectorReadPosition / _opticalDisc.BytesPerSector);
_currentSectorReadPosition %= _opticalDisc.BytesPerSector;
}
return count;
}
#region Playback
/// <summary>
/// Start audio playback
/// </summary>
public void Play()
{
if (_soundOut.PlaybackState != PlaybackState.Playing)
_soundOut.Play();
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
}
/// <summary>
/// Pause audio playback
/// </summary>
public void Pause()
{
if(_soundOut.PlaybackState != PlaybackState.Paused)
_soundOut.Pause();
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
}
/// <summary>
/// Stop audio playback
/// </summary>
public void Stop()
{
if(_soundOut.PlaybackState != PlaybackState.Stopped)
_soundOut.Stop();
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
}
#endregion
#region Helpers
/// <summary>
/// Set de-emphasis status
/// </summary>
/// <param name="apply"></param>
public void SetDeEmphasis(bool apply) => ApplyDeEmphasis = apply;
/// <summary>
/// Sets or resets the de-emphasis filters
/// </summary>
private void SetupFilters()
{
if(_deEmphasisFilterLeft == null)
{
_deEmphasisFilterLeft = new DeEmphasisFilter();
_deEmphasisFilterRight = new DeEmphasisFilter();
}
else
{
_deEmphasisFilterLeft.Reset();
_deEmphasisFilterRight.Reset();
}
}
/// <summary>
/// Sets or resets the audio playback objects
/// </summary>
private void SetupAudio()
{
if(_source == null)
{
_source = new PlayerSource(ProviderRead);
_soundOut = new ALSoundOut(100);
_soundOut.Initialize(_source);
}
else
{
_soundOut.Stop();
}
}
#endregion
}
}