2021-03-19 17:07:27 -03:00
|
|
|
using System;
|
2021-06-05 14:21:59 -07:00
|
|
|
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-05 13:20:06 -07:00
|
|
|
using RedBookPlayer.Common.Discs;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-07-05 13:20:06 -07:00
|
|
|
namespace RedBookPlayer.Common.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
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
#region Public Fields
|
2021-04-14 20:36:34 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
2021-06-29 21:16:43 -07:00
|
|
|
/// Indicate if the output is ready to be used
|
2021-06-06 21:43:47 -07:00
|
|
|
/// </summary>
|
|
|
|
|
public bool Initialized { get; private set; } = false;
|
2021-06-06 20:28:36 +01:00
|
|
|
|
2021-06-29 12:08:08 -07:00
|
|
|
/// <summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
/// Indicate if the output is playing
|
2021-06-29 12:08:08 -07:00
|
|
|
/// </summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
public bool Playing
|
|
|
|
|
{
|
|
|
|
|
get => _playing;
|
|
|
|
|
private set => this.RaiseAndSetIfChanged(ref _playing, value);
|
|
|
|
|
}
|
2021-06-29 12:08:08 -07:00
|
|
|
|
2021-06-28 23:11:16 -07:00
|
|
|
/// <summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
/// Indicates if de-emphasis should be applied
|
2021-06-28 23:11:16 -07:00
|
|
|
/// </summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
public bool ApplyDeEmphasis
|
|
|
|
|
{
|
|
|
|
|
get => _applyDeEmphasis;
|
|
|
|
|
private set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
|
|
|
|
|
}
|
2021-06-28 23:11:16 -07:00
|
|
|
|
2021-07-03 16:04:18 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Current playback volume
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Volume
|
|
|
|
|
{
|
|
|
|
|
get => _volume;
|
2021-07-05 16:23:50 -07:00
|
|
|
private set
|
2021-07-03 16:04:18 -07:00
|
|
|
{
|
2021-07-04 23:17:30 -07:00
|
|
|
int tempVolume = value;
|
2021-07-03 16:04:18 -07:00
|
|
|
if(value > 100)
|
2021-07-04 23:17:30 -07:00
|
|
|
tempVolume = 100;
|
2021-07-03 16:04:18 -07:00
|
|
|
else if(value < 0)
|
2021-07-04 23:17:30 -07:00
|
|
|
tempVolume = 0;
|
|
|
|
|
|
|
|
|
|
this.RaiseAndSetIfChanged(ref _volume, tempVolume);
|
2021-07-03 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
private bool _playing;
|
|
|
|
|
private bool _applyDeEmphasis;
|
|
|
|
|
private int _volume;
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private State Variables
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-06-29 15:45:13 -07:00
|
|
|
/// OpticalDisc from the parent player for easy access
|
2021-06-06 21:43:47 -07:00
|
|
|
/// </summary>
|
2021-06-29 15:53:48 -07:00
|
|
|
/// <remarks>
|
|
|
|
|
/// TODO: Can we remove the need for a local reference to OpticalDisc?
|
|
|
|
|
/// </remarks>
|
2021-06-29 15:45:13 -07:00
|
|
|
private OpticalDisc _opticalDisc;
|
2021-06-06 21:43:47 -07:00
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Lock object for reading track data
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly object _readingImage = new object();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-06-29 15:45:13 -07:00
|
|
|
/// Initialize the output with a given image
|
2021-06-06 21:43:47 -07:00
|
|
|
/// </summary>
|
2021-06-29 15:45:13 -07:00
|
|
|
/// <param name="opticalDisc">OpticalDisc to load from</param>
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <param name="autoPlay">True if playback should begin immediately, false otherwise</param>
|
2021-07-03 16:04:18 -07:00
|
|
|
/// <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)
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-06-29 12:08:08 -07:00
|
|
|
// If we have an unusable disc, just return
|
2021-06-29 15:45:13 -07:00
|
|
|
if(opticalDisc == null || !opticalDisc.Initialized)
|
2021-06-29 12:08:08 -07:00
|
|
|
return;
|
2021-04-08 21:41:31 -03:00
|
|
|
|
2021-06-29 15:45:13 -07:00
|
|
|
// Save a reference to the disc
|
|
|
|
|
_opticalDisc = opticalDisc;
|
|
|
|
|
|
2021-07-03 16:04:18 -07:00
|
|
|
// Set the initial playback volume
|
|
|
|
|
Volume = defaultVolume;
|
|
|
|
|
|
2021-06-29 12:08:08 -07:00
|
|
|
// Enable de-emphasis for CDs, if necessary
|
2021-06-29 15:45:13 -07:00
|
|
|
if(opticalDisc is CompactDisc compactDisc)
|
2021-06-29 12:08:08 -07:00
|
|
|
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
|
|
|
|
|
|
|
|
|
|
// Setup de-emphasis filters
|
2021-06-06 21:43:47 -07:00
|
|
|
SetupFilters();
|
2021-04-08 21:41:31 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Setup the audio output
|
|
|
|
|
SetupAudio();
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Initialize playback, if necessary
|
2021-06-06 20:28:36 +01:00
|
|
|
if(autoPlay)
|
2021-06-06 21:43:47 -07:00
|
|
|
_soundOut.Play();
|
2021-04-15 19:16:34 -03:00
|
|
|
|
2021-06-29 15:45:13 -07:00
|
|
|
// Mark the output as ready
|
2021-03-19 17:07:27 -03:00
|
|
|
Initialized = true;
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Begin loading data
|
|
|
|
|
_source.Start();
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:43:47 -07: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)
|
|
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
// Set the current volume
|
2021-07-03 16:04:18 -07:00
|
|
|
_soundOut.Volume = (float)Volume / 100;
|
2021-03-30 20:25:44 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Determine how many sectors we can read
|
2021-03-19 17:07:27 -03:00
|
|
|
ulong sectorsToRead;
|
|
|
|
|
ulong zeroSectorsAmount;
|
|
|
|
|
do
|
|
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
// Attempt to read 2 more sectors than requested
|
2021-06-29 13:49:12 -07:00
|
|
|
sectorsToRead = ((ulong)count / (ulong)_opticalDisc.BytesPerSector) + 2;
|
2021-03-19 17:07:27 -03:00
|
|
|
zeroSectorsAmount = 0;
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Avoid overreads by padding with 0-byte data at the end
|
2021-06-29 12:08:08 -07:00
|
|
|
if(_opticalDisc.CurrentSector + sectorsToRead > _opticalDisc.TotalSectors)
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
|
|
|
|
ulong oldSectorsToRead = sectorsToRead;
|
2021-06-29 12:08:08 -07:00
|
|
|
sectorsToRead = _opticalDisc.TotalSectors - _opticalDisc.CurrentSector;
|
2021-06-29 13:49:12 -07:00
|
|
|
|
|
|
|
|
int tempZeroSectorCount = (int)(oldSectorsToRead - sectorsToRead);
|
|
|
|
|
zeroSectorsAmount = (ulong)(tempZeroSectorCount < 0 ? 0 : tempZeroSectorCount);
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// TODO: Figure out when this value could be negative
|
|
|
|
|
if(sectorsToRead <= 0)
|
|
|
|
|
{
|
2021-06-29 12:08:08 -07:00
|
|
|
_opticalDisc.LoadFirstTrack();
|
2021-06-06 21:43:47 -07:00
|
|
|
_currentSectorReadPosition = 0;
|
|
|
|
|
}
|
2021-06-06 20:28:36 +01:00
|
|
|
} while(sectorsToRead <= 0);
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Create padding data for overreads
|
2021-06-29 12:08:08 -07:00
|
|
|
byte[] zeroSectors = new byte[(int)zeroSectorsAmount * _opticalDisc.BytesPerSector];
|
2021-03-19 17:07:27 -03:00
|
|
|
byte[] audioData;
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Attempt to read the required number of sectors
|
|
|
|
|
var readSectorTask = Task.Run(() =>
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
lock(_readingImage)
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-06-29 15:45:13 -07:00
|
|
|
for(int i = 0; i < 4; i++)
|
2021-06-05 10:57:20 -07:00
|
|
|
{
|
2021-06-29 13:49:12 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _opticalDisc.ReadSectors((uint)sectorsToRead).Concat(zeroSectors).ToArray();
|
|
|
|
|
}
|
|
|
|
|
catch(ArgumentOutOfRangeException)
|
|
|
|
|
{
|
|
|
|
|
_opticalDisc.LoadFirstTrack();
|
|
|
|
|
}
|
2021-06-05 10:57:20 -07:00
|
|
|
}
|
2021-06-29 13:49:12 -07:00
|
|
|
|
|
|
|
|
return zeroSectors;
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Wait 100ms at longest for the read to occur
|
|
|
|
|
if(readSectorTask.Wait(TimeSpan.FromMilliseconds(100)))
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
audioData = readSectorTask.Result;
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Array.Clear(buffer, offset, count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Load only the requested audio segment
|
2021-03-19 17:07:27 -03:00
|
|
|
byte[] audioDataSegment = new byte[count];
|
2021-06-29 13:49:12 -07:00
|
|
|
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);
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Apply de-emphasis filtering, only if enabled
|
2021-06-29 12:08:08 -07:00
|
|
|
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
|
|
|
{
|
2021-06-06 21:43:47 -07: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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Write out the audio data to the buffer
|
2021-03-19 17:07:27 -03:00
|
|
|
Array.Copy(audioDataSegment, 0, buffer, offset, count);
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
// Set the read position in the sector for easier access
|
|
|
|
|
_currentSectorReadPosition += count;
|
2021-06-29 12:08:08 -07:00
|
|
|
if(_currentSectorReadPosition >= _opticalDisc.BytesPerSector)
|
2021-06-06 21:43:47 -07:00
|
|
|
{
|
2021-07-05 16:30:38 -07:00
|
|
|
_opticalDisc.SetCurrentSector(_opticalDisc.CurrentSector + (ulong)(_currentSectorReadPosition / _opticalDisc.BytesPerSector));
|
2021-06-29 12:08:08 -07:00
|
|
|
_currentSectorReadPosition %= _opticalDisc.BytesPerSector;
|
2021-06-06 21:43:47 -07:00
|
|
|
}
|
2021-03-19 17:07:27 -03:00
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 21:35:09 -07:00
|
|
|
#region Playback
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
2021-06-29 15:45:13 -07:00
|
|
|
/// Start audio playback
|
2021-06-29 12:08:08 -07:00
|
|
|
/// </summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2021-06-29 12:08:08 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
2021-06-29 15:45:13 -07:00
|
|
|
/// Stop audio playback
|
2021-06-29 12:08:08 -07:00
|
|
|
/// </summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
if(_soundOut.PlaybackState != PlaybackState.Stopped)
|
|
|
|
|
_soundOut.Stop();
|
|
|
|
|
|
|
|
|
|
Playing = _soundOut.PlaybackState == PlaybackState.Playing;
|
|
|
|
|
}
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#endregion
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#region Helpers
|
|
|
|
|
|
2021-06-29 12:08:08 -07:00
|
|
|
/// <summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
/// Set de-emphasis status
|
2021-06-29 12:08:08 -07:00
|
|
|
/// </summary>
|
2021-07-04 23:17:30 -07:00
|
|
|
/// <param name="apply"></param>
|
|
|
|
|
public void SetDeEmphasis(bool apply) => ApplyDeEmphasis = apply;
|
2021-06-29 12:08:08 -07:00
|
|
|
|
2021-07-05 16:20:34 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Set the value for the volume
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="volume">New volume value</param>
|
|
|
|
|
public void SetVolume(int volume) => Volume = volume;
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <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
|
|
|
|
2021-06-06 21:43:47 -07: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
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#endregion
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
2021-06-29 15:45:13 -07:00
|
|
|
}
|