Files
RedBookPlayer/RedBookPlayer.Models/Hardware/SoundOutput.cs

420 lines
14 KiB
C#
Raw Normal View History

2021-03-19 17:07:27 -03:00
using System;
using System.Linq;
2021-03-19 17:07:27 -03:00
using System.Threading.Tasks;
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;
2021-07-04 23:17:30 -07:00
using ReactiveUI;
2021-07-12 15:40:56 -07:00
using RedBookPlayer.Models.Discs;
2021-03-19 17:07:27 -03:00
2021-07-12 15:40:56 -07:00
namespace RedBookPlayer.Models.Hardware
2021-03-19 17:07:27 -03:00
{
2021-07-04 23:17:30 -07:00
public class SoundOutput : ReactiveObject
2021-03-19 17:07:27 -03:00
{
#region Public Fields
2021-04-14 20:36:34 -03:00
/// <summary>
2021-06-29 21:16:43 -07:00
/// Indicate if the output is ready to be used
/// </summary>
2021-08-05 21:05:20 -07:00
public bool Initialized
{
get => _initialized;
private set => this.RaiseAndSetIfChanged(ref _initialized, value);
}
2021-06-06 20:28:36 +01:00
/// <summary>
2021-08-05 21:05:20 -07:00
/// Indicates the current player state
/// </summary>
2021-08-05 21:05:20 -07:00
public PlayerState PlayerState
2021-07-04 23:17:30 -07:00
{
2021-08-05 21:05:20 -07:00
get => _playerState;
private set => this.RaiseAndSetIfChanged(ref _playerState, value);
2021-07-04 23:17:30 -07:00
}
2021-08-24 22:11:25 -07:00
/// <summary>
/// Indicates the repeat mode
/// </summary>
public RepeatMode RepeatMode
{
get => _repeatMode;
private set => this.RaiseAndSetIfChanged(ref _repeatMode, value);
}
/// <summary>
2021-07-04 23:17:30 -07:00
/// Indicates if de-emphasis should be applied
/// </summary>
2021-07-04 23:17:30 -07:00
public bool ApplyDeEmphasis
{
get => _applyDeEmphasis;
private set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
/// <summary>
/// Current playback volume
/// </summary>
public int Volume
{
get => _volume;
2021-07-05 16:23:50 -07:00
private set
{
2021-07-04 23:17:30 -07:00
int tempVolume = value;
if(value > 100)
2021-07-04 23:17:30 -07:00
tempVolume = 100;
else if(value < 0)
2021-07-04 23:17:30 -07:00
tempVolume = 0;
this.RaiseAndSetIfChanged(ref _volume, tempVolume);
}
}
2021-08-05 21:05:20 -07:00
private bool _initialized;
private PlayerState _playerState;
2021-08-24 22:11:25 -07:00
private RepeatMode _repeatMode;
2021-07-04 23:17:30 -07:00
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>
2021-07-12 10:52:50 -07:00
private OpticalDiscBase _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;
2021-07-04 23:17:30 -07:00
/// <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
2021-08-05 21:05:20 -07:00
/// <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>
2021-08-24 22:11:25 -07:00
/// <param name="repeatMode">RepeatMode for sound output</param>
/// <param name="autoPlay">True if playback should begin immediately, false otherwise</param>
2021-08-24 22:11:25 -07:00
public void Init(OpticalDiscBase opticalDisc, RepeatMode repeatMode, bool autoPlay)
2021-03-19 17:07:27 -03:00
{
// If we have an unusable disc, just return
if(opticalDisc == null || !opticalDisc.Initialized)
return;
// Save a reference to the disc
_opticalDisc = opticalDisc;
// 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
2021-08-24 22:11:25 -07:00
// Setup the repeat mode
RepeatMode = repeatMode;
// 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 output as ready
2021-03-19 17:07:27 -03:00
Initialized = true;
2021-08-05 21:05:20 -07:00
PlayerState = PlayerState.Stopped;
2021-03-19 17:07:27 -03:00
// Begin loading data
_source.Start();
2021-03-19 17:07:27 -03:00
}
2021-08-24 22:11:25 -07:00
/// <summary>
/// Reset the current internal state
/// </summary>
public void Reset()
{
_soundOut.Stop();
_opticalDisc = null;
Initialized = false;
PlayerState = PlayerState.NoDisc;
}
/// <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)Volume / 100;
2021-07-06 09:54:43 -07:00
// If we have an unreadable track, just return
2021-07-07 10:40:44 -07:00
if(_opticalDisc.BytesPerSector <= 0)
2021-07-06 09:54:43 -07:00
{
Array.Clear(buffer, offset, count);
return count;
}
// Determine how many sectors we can read
2021-07-07 10:40:44 -07:00
DetermineReadAmount(count, out ulong sectorsToRead, out ulong zeroSectorsAmount);
2021-06-06 20:28:36 +01:00
2021-08-05 21:05:20 -07:00
// Get data to return
byte[] audioDataSegment = ReadData(count, sectorsToRead, zeroSectorsAmount);
if(audioDataSegment == null)
2021-06-29 13:49:12 -07:00
{
Array.Clear(buffer, offset, count);
return count;
}
// 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)
{
2021-08-05 21:05:20 -07:00
int currentTrack = _opticalDisc.CurrentTrackNumber;
2021-07-05 16:30:38 -07:00
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector + (ulong)(_currentSectorReadPosition / _opticalDisc.BytesPerSector));
_currentSectorReadPosition %= _opticalDisc.BytesPerSector;
2021-08-24 22:11:25 -07:00
if(RepeatMode == RepeatMode.None && _opticalDisc.CurrentTrackNumber < currentTrack)
Stop();
else if(RepeatMode == RepeatMode.Single && _opticalDisc.CurrentTrackNumber != currentTrack)
_opticalDisc.LoadTrack(currentTrack);
}
2021-03-19 17:07:27 -03:00
return count;
}
#region Playback
2021-03-19 17:07:27 -03:00
/// <summary>
/// Start audio playback
/// </summary>
2021-07-04 23:17:30 -07:00
public void Play()
{
2021-08-05 21:05:20 -07:00
if(_soundOut.PlaybackState != PlaybackState.Playing)
2021-07-04 23:17:30 -07:00
_soundOut.Play();
2021-08-05 21:05:20 -07:00
PlayerState = PlayerState.Playing;
2021-07-04 23:17:30 -07:00
}
/// <summary>
/// Pause audio playback
/// </summary>
public void Pause()
{
if(_soundOut.PlaybackState != PlaybackState.Paused)
_soundOut.Pause();
2021-08-05 21:05:20 -07:00
PlayerState = PlayerState.Paused;
2021-07-04 23:17:30 -07:00
}
/// <summary>
/// Stop audio playback
/// </summary>
2021-07-04 23:17:30 -07:00
public void Stop()
{
if(_soundOut.PlaybackState != PlaybackState.Stopped)
_soundOut.Stop();
2021-08-05 21:05:20 -07:00
PlayerState = PlayerState.Stopped;
2021-07-04 23:17:30 -07:00
}
2021-03-19 17:07:27 -03:00
2021-08-24 22:11:25 -07:00
/// <summary>
/// Eject the currently loaded disc
/// </summary>
public void Eject() => Reset();
#endregion
2021-03-19 17:07:27 -03:00
#region Helpers
/// <summary>
2021-07-04 23:17:30 -07:00
/// Set de-emphasis status
/// </summary>
2021-08-05 21:05:20 -07:00
/// <param name="apply">New de-emphasis status</param>
2021-07-04 23:17:30 -07:00
public void SetDeEmphasis(bool apply) => ApplyDeEmphasis = apply;
2021-08-24 22:11:25 -07:00
/// <summary>
/// Set repeat mode
/// </summary>
/// <param name="repeatMode">New repeat mode value</param>
public void SetRepeatMode(RepeatMode repeatMode) => RepeatMode = repeatMode;
/// <summary>
/// Set the value for the volume
/// </summary>
/// <param name="volume">New volume value</param>
public void SetVolume(int volume) => Volume = volume;
2021-07-07 10:40:44 -07:00
/// <summary>
/// Determine the number of real and zero sectors to read
/// </summary>
/// <param name="count">Number of requested bytes to read</param>
/// <param name="sectorsToRead">Number of sectors to read</param>
/// <param name="zeroSectorsAmount">Number of zeroed sectors to concatenate</param>
private void DetermineReadAmount(int count, out ulong sectorsToRead, out ulong zeroSectorsAmount)
{
2021-08-24 22:11:25 -07:00
// Attempt to read 10 more sectors than requested
sectorsToRead = ((ulong)count / (ulong)_opticalDisc.BytesPerSector) + 10;
2021-08-05 21:05:20 -07:00
zeroSectorsAmount = 0;
2021-07-07 10:40:44 -07:00
2021-08-05 21:05:20 -07:00
// 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;
2021-07-07 10:40:44 -07:00
2021-08-05 21:05:20 -07:00
int tempZeroSectorCount = (int)(oldSectorsToRead - sectorsToRead);
zeroSectorsAmount = (ulong)(tempZeroSectorCount < 0 ? 0 : tempZeroSectorCount);
}
2021-07-07 10:40:44 -07:00
}
2021-07-07 10:21:23 -07:00
/// <summary>
/// Process de-emphasis of audio data
/// </summary>
/// <param name="audioData">Audio data to process</param>
private void ProcessDeEmphasis(byte[] audioData)
{
float[][] floatAudioData = new float[2][];
floatAudioData[0] = new float[audioData.Length / 4];
floatAudioData[1] = new float[audioData.Length / 4];
ByteConverter.ToFloats16Bit(audioData, 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, audioData);
}
2021-08-05 21:05:20 -07:00
/// <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>
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-08-05 21:05:20 -07:00
}