mirror of
https://github.com/aaru-dps/RedBookPlayer.git
synced 2025-12-16 19:24:41 +00:00
Extract filtering to new file
This commit is contained in:
58
RedBookPlayer.Models/Hardware/FilterStage.cs
Normal file
58
RedBookPlayer.Models/Hardware/FilterStage.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using NWaves.Audio;
|
||||||
|
using NWaves.Filters.BiQuad;
|
||||||
|
|
||||||
|
namespace RedBookPlayer.Models.Hardware
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Output stage that represents all filters on the audio
|
||||||
|
/// </summary>
|
||||||
|
public class FilterStage
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Left channel de-emphasis filter
|
||||||
|
/// </summary>
|
||||||
|
private BiQuadFilter _deEmphasisFilterLeft;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Right channel de-emphasis filter
|
||||||
|
/// </summary>
|
||||||
|
private BiQuadFilter _deEmphasisFilterRight;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process audio data with internal filters
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="audioData">Audio data to process</param>
|
||||||
|
public void ProcessAudioData(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets or resets the output filters
|
||||||
|
/// </summary>
|
||||||
|
public void SetupFilters()
|
||||||
|
{
|
||||||
|
if(_deEmphasisFilterLeft == null)
|
||||||
|
{
|
||||||
|
_deEmphasisFilterLeft = new DeEmphasisFilter();
|
||||||
|
_deEmphasisFilterRight = new DeEmphasisFilter();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_deEmphasisFilterLeft.Reset();
|
||||||
|
_deEmphasisFilterRight.Reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ using System.Linq;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CSCore.SoundOut;
|
using CSCore.SoundOut;
|
||||||
using NWaves.Audio;
|
using NWaves.Audio;
|
||||||
using NWaves.Filters.BiQuad;
|
|
||||||
using ReactiveUI;
|
using ReactiveUI;
|
||||||
using RedBookPlayer.Models.Discs;
|
using RedBookPlayer.Models.Discs;
|
||||||
|
|
||||||
@@ -96,14 +95,9 @@ namespace RedBookPlayer.Models.Hardware
|
|||||||
private ALSoundOut _soundOut;
|
private ALSoundOut _soundOut;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Left channel de-emphasis filter
|
/// Filtering stage for audio output
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private BiQuadFilter _deEmphasisFilterLeft;
|
private FilterStage _filterStage;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Right channel de-emphasis filter
|
|
||||||
/// </summary>
|
|
||||||
private BiQuadFilter _deEmphasisFilterRight;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Current position in the sector
|
/// Current position in the sector
|
||||||
@@ -121,7 +115,12 @@ namespace RedBookPlayer.Models.Hardware
|
|||||||
/// Constructor
|
/// Constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
|
/// <param name="defaultVolume">Default volume between 0 and 100 to use when starting playback</param>
|
||||||
public SoundOutput(int defaultVolume = 100) => Volume = defaultVolume;
|
public SoundOutput(int defaultVolume = 100)
|
||||||
|
{
|
||||||
|
Volume = defaultVolume;
|
||||||
|
_filterStage = new FilterStage();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize the output with a given image
|
/// Initialize the output with a given image
|
||||||
@@ -143,7 +142,7 @@ namespace RedBookPlayer.Models.Hardware
|
|||||||
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
|
ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
|
||||||
|
|
||||||
// Setup de-emphasis filters
|
// Setup de-emphasis filters
|
||||||
SetupFilters();
|
_filterStage.SetupFilters();
|
||||||
|
|
||||||
// Setup the audio output
|
// Setup the audio output
|
||||||
SetupAudio();
|
SetupAudio();
|
||||||
@@ -309,26 +308,6 @@ namespace RedBookPlayer.Models.Hardware
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read the requested amount of data from an input
|
/// Read the requested amount of data from an input
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -376,28 +355,11 @@ namespace RedBookPlayer.Models.Hardware
|
|||||||
|
|
||||||
// Apply de-emphasis filtering, only if enabled
|
// Apply de-emphasis filtering, only if enabled
|
||||||
if(ApplyDeEmphasis)
|
if(ApplyDeEmphasis)
|
||||||
ProcessDeEmphasis(audioDataSegment);
|
_filterStage.ProcessAudioData(audioDataSegment);
|
||||||
|
|
||||||
return 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets or resets the audio playback objects
|
/// Sets or resets the audio playback objects
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user