Files
RedBookPlayer/RedBookPlayer/Player.cs

479 lines
16 KiB
C#
Raw Normal View History

2021-03-19 17:07:27 -03:00
using System;
using System.IO;
using System.Linq;
2021-03-19 17:07:27 -03:00
using System.Threading.Tasks;
using Aaru.CommonTypes.Enums;
using Aaru.DiscImages;
using Aaru.Filters;
2021-06-06 20:28:36 +01:00
using CSCore.SoundOut;
2021-03-19 17:07:27 -03:00
using NWaves.Audio;
using NWaves.Filters.BiQuad;
using RedBookPlayer.Discs;
2021-03-19 17:07:27 -03:00
namespace RedBookPlayer
{
public class Player
{
#region Public Fields
2021-04-14 20:36:34 -03:00
/// <summary>
/// Indicate if the player is ready to be used
/// </summary>
public bool Initialized { get; private set; } = false;
2021-06-06 20:28:36 +01:00
/// <summary>
/// Indicates if de-emphasis should be applied
/// </summary>
public bool ApplyDeEmphasis { get; private set; } = false;
/// <summary>
/// Indicate if the disc is playing
/// </summary>
public bool Playing => _soundOut.PlaybackState == PlaybackState.Playing;
#endregion
#region Private State Variables
/// <summary>
/// OpticalDisc object
/// </summary>
private OpticalDisc _opticalDisc;
/// <summary>
/// Current position in the sector
/// </summary>
private int _currentSectorReadPosition = 0;
/// <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>
/// Lock object for reading track data
/// </summary>
private readonly object _readingImage = new object();
#endregion
/// <summary>
/// Initialize the player with a given image path
/// </summary>
/// <param name="path">Path to the disc image</param>
/// <param name="autoPlay">True if playback should begin immediately, false otherwise</param>
public void Init(string path, bool autoPlay = false)
2021-03-19 17:07:27 -03:00
{
// Reset the internal state for initialization
Initialized = false;
ApplyDeEmphasis = false;
_opticalDisc = null;
try
{
// Validate the image exists
if(string.IsNullOrWhiteSpace(path) || !File.Exists(path))
return;
// Load the disc image to memory
var image = new AaruFormat();
var filter = new ZZZNoFilter();
filter.Open(path);
image.Open(filter);
// Generate and instantiate the disc
_opticalDisc = OpticalDiscFactory.GenerateFromImage(image, App.Settings.AutoPlay);
}
catch
{
// All errors mean an invalid image in some way
2021-03-19 17:07:27 -03:00
return;
}
2021-03-19 17:07:27 -03:00
// If we have an unusable disc, just return
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
// 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();
2021-03-19 17:07:27 -03:00
// Initialize playback, if necessary
2021-06-06 20:28:36 +01:00
if(autoPlay)
_soundOut.Play();
2021-04-15 19:16:34 -03:00
// Mark the player as ready
2021-03-19 17:07:27 -03:00
Initialized = true;
// Begin loading data
_source.Start();
2021-03-19 17:07:27 -03:00
}
/// <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>
2021-03-19 17:07:27 -03:00
public int ProviderRead(byte[] buffer, int offset, int count)
{
// Set the current volume
_soundOut.Volume = (float)App.Settings.Volume / 100;
// Determine how many sectors we can read
2021-03-19 17:07:27 -03:00
ulong sectorsToRead;
ulong zeroSectorsAmount;
do
{
// Attempt to read 2 more sectors than requested
sectorsToRead = ((ulong)(count / _opticalDisc.BytesPerSector)) + 2;
2021-03-19 17:07:27 -03:00
zeroSectorsAmount = 0;
// Avoid overreads by padding with 0-byte data at the end
if(_opticalDisc.CurrentSector + sectorsToRead > _opticalDisc.TotalSectors)
2021-03-19 17:07:27 -03:00
{
ulong oldSectorsToRead = sectorsToRead;
sectorsToRead = _opticalDisc.TotalSectors - _opticalDisc.CurrentSector;
zeroSectorsAmount = oldSectorsToRead - sectorsToRead;
2021-03-19 17:07:27 -03:00
}
// TODO: Figure out when this value could be negative
if(sectorsToRead <= 0)
{
_opticalDisc.LoadFirstTrack();
_currentSectorReadPosition = 0;
}
2021-06-06 20:28:36 +01:00
} while(sectorsToRead <= 0);
// Create padding data for overreads
byte[] zeroSectors = new byte[(int)zeroSectorsAmount * _opticalDisc.BytesPerSector];
2021-03-19 17:07:27 -03:00
byte[] audioData;
// Attempt to read the required number of sectors
var readSectorTask = Task.Run(() =>
2021-03-19 17:07:27 -03:00
{
lock(_readingImage)
2021-03-19 17:07:27 -03:00
{
try
{
return _opticalDisc.ReadSectors((uint)sectorsToRead).Concat(zeroSectors).ToArray();
}
2021-06-06 20:28:36 +01:00
catch(ArgumentOutOfRangeException)
{
_opticalDisc.LoadFirstTrack();
return _opticalDisc.ReadSectors((uint)sectorsToRead).Concat(zeroSectors).ToArray();
}
2021-03-19 17:07:27 -03:00
}
});
// Wait 100ms at longest for the read to occur
if(readSectorTask.Wait(TimeSpan.FromMilliseconds(100)))
2021-03-19 17:07:27 -03:00
{
audioData = readSectorTask.Result;
2021-03-19 17:07:27 -03:00
}
else
{
Array.Clear(buffer, offset, count);
return count;
}
// Load only the requested audio segment
2021-03-19 17:07:27 -03:00
byte[] audioDataSegment = new byte[count];
Array.Copy(audioData, _currentSectorReadPosition, audioDataSegment, 0, Math.Min(count, audioData.Length - _currentSectorReadPosition));
2021-03-19 17:07:27 -03:00
// Apply de-emphasis filtering, only if enabled
if(ApplyDeEmphasis)
2021-03-19 17:07:27 -03:00
{
float[][] floatAudioData = new float[2][];
floatAudioData[0] = new float[audioDataSegment.Length / 4];
floatAudioData[1] = new float[audioDataSegment.Length / 4];
ByteConverter.ToFloats16Bit(audioDataSegment, floatAudioData);
2021-06-06 20:28:36 +01:00
for(int i = 0; i < floatAudioData[0].Length; i++)
2021-03-19 17:07:27 -03:00
{
floatAudioData[0][i] = _deEmphasisFilterLeft.Process(floatAudioData[0][i]);
floatAudioData[1][i] = _deEmphasisFilterRight.Process(floatAudioData[1][i]);
2021-03-19 17:07:27 -03:00
}
ByteConverter.FromFloats16Bit(floatAudioData, audioDataSegment);
}
// Write out the audio data to the buffer
2021-03-19 17:07:27 -03:00
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;
}
2021-03-19 17:07:27 -03:00
return count;
}
#region Playback
2021-03-19 17:07:27 -03:00
/// <summary>
/// Toggle audio playback
/// </summary>
/// <param name="start">True to start playback, false to pause</param>
public void TogglePlayPause(bool start)
2021-03-19 17:07:27 -03:00
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
2021-03-19 17:07:27 -03:00
return;
if(start)
{
_soundOut.Play();
_opticalDisc.SetTotalIndexes();
}
else
{
_soundOut.Stop();
}
2021-03-19 17:07:27 -03:00
}
/// <summary>
/// Stop the current audio playback
/// </summary>
public void Stop()
2021-03-19 17:07:27 -03:00
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
2021-03-19 17:07:27 -03:00
return;
_soundOut.Stop();
_opticalDisc.LoadFirstTrack();
2021-03-19 17:07:27 -03:00
}
/// <summary>
/// Move to the next playable track
/// </summary>
public void NextTrack()
2021-03-19 17:07:27 -03:00
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
2021-03-19 17:07:27 -03:00
return;
bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false);
_opticalDisc.NextTrack();
if(_opticalDisc is CompactDisc compactDisc)
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true);
}
/// <summary>
/// Move to the previous playable track
/// </summary>
public void PreviousTrack()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false);
_opticalDisc.PreviousTrack();
if(_opticalDisc is CompactDisc compactDisc)
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true);
}
/// <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) TogglePlayPause(false);
_opticalDisc.NextIndex(changeTrack);
if(_opticalDisc is CompactDisc compactDisc)
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true);
}
/// <summary>
/// Move to the previous index
/// </summary>
/// <param name="changeTrack">True if index changes can trigger a track change, false otherwise</param>
public void PreviousIndex(bool changeTrack)
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false);
_opticalDisc.PreviousIndex(changeTrack);
if(_opticalDisc is CompactDisc compactDisc)
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true);
}
/// <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;
2021-03-19 17:07:27 -03:00
}
#endregion
2021-03-19 17:07:27 -03:00
#region Helpers
/// <summary>
/// Generate the digit string to be interpreted by the frontend
/// </summary>
/// <returns>String representing the digits for the frontend</returns>
public string GenerateDigitString()
{
// If the disc isn't initialized, return all '-' characters
if(_opticalDisc == null || !_opticalDisc.Initialized)
return string.Empty.PadLeft(20, '-');
// Otherwise, take the current time into account
ulong sectorTime = _opticalDisc.CurrentSector;
if(_opticalDisc.SectionStartSector != 0)
sectorTime -= _opticalDisc.SectionStartSector;
else
sectorTime += _opticalDisc.TimeOffset;
int[] numbers = new int[]
{
_opticalDisc.CurrentTrackNumber + 1,
_opticalDisc.CurrentTrackIndex,
(int)(sectorTime / (75 * 60)),
(int)(sectorTime / 75 % 60),
(int)(sectorTime % 75),
_opticalDisc.TotalTracks,
_opticalDisc.TotalIndexes,
(int)(_opticalDisc.TotalTime / (75 * 60)),
(int)(_opticalDisc.TotalTime / 75 % 60),
(int)(_opticalDisc.TotalTime % 75),
};
return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
}
/// <summary>
/// Toggle de-emphasis processing
/// </summary>
/// <param name="enable">True to apply de-emphasis, false otherwise</param>
public void ToggleDeEmphasis(bool enable) => ApplyDeEmphasis = enable;
/// <summary>
/// Update the data context for the frontend
/// </summary>
/// <param name="dataContext">Data context to be updated</param>
public void UpdateDataContext(PlayerViewModel dataContext)
{
if(!Initialized || dataContext == null)
return;
dataContext.HiddenTrack = _opticalDisc.TimeOffset > 150;
dataContext.ApplyDeEmphasis = ApplyDeEmphasis;
if(_opticalDisc is CompactDisc compactDisc)
{
dataContext.QuadChannel = compactDisc.QuadChannel;
dataContext.IsDataTrack = compactDisc.IsDataTrack;
dataContext.CopyAllowed = compactDisc.CopyAllowed;
dataContext.TrackHasEmphasis = compactDisc.TrackHasEmphasis;
}
else
{
dataContext.QuadChannel = false;
dataContext.IsDataTrack = _opticalDisc.TrackType != TrackType.Audio;
dataContext.CopyAllowed = false;
dataContext.TrackHasEmphasis = false;
}
}
/// <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();
}
}
2021-06-06 20:28:36 +01:00
/// <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();
}
}
2021-06-06 20:28:36 +01:00
#endregion
2021-03-19 17:07:27 -03:00
}
2021-06-06 20:28:36 +01:00
}