diff --git a/RedBookPlayer.Models/Hardware/FilterStage.cs b/RedBookPlayer.Models/Hardware/FilterStage.cs new file mode 100644 index 0000000..b9189ec --- /dev/null +++ b/RedBookPlayer.Models/Hardware/FilterStage.cs @@ -0,0 +1,58 @@ +using NWaves.Audio; +using NWaves.Filters.BiQuad; + +namespace RedBookPlayer.Models.Hardware +{ + /// + /// Output stage that represents all filters on the audio + /// + public class FilterStage + { + /// + /// Left channel de-emphasis filter + /// + private BiQuadFilter _deEmphasisFilterLeft; + + /// + /// Right channel de-emphasis filter + /// + private BiQuadFilter _deEmphasisFilterRight; + + /// + /// Process audio data with internal filters + /// + /// Audio data to process + 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); + } + + /// + /// Sets or resets the output filters + /// + public void SetupFilters() + { + if(_deEmphasisFilterLeft == null) + { + _deEmphasisFilterLeft = new DeEmphasisFilter(); + _deEmphasisFilterRight = new DeEmphasisFilter(); + } + else + { + _deEmphasisFilterLeft.Reset(); + _deEmphasisFilterRight.Reset(); + } + } + } +} \ No newline at end of file diff --git a/RedBookPlayer.Models/Hardware/SoundOutput.cs b/RedBookPlayer.Models/Hardware/SoundOutput.cs index 94b2935..c89a223 100644 --- a/RedBookPlayer.Models/Hardware/SoundOutput.cs +++ b/RedBookPlayer.Models/Hardware/SoundOutput.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Threading.Tasks; using CSCore.SoundOut; using NWaves.Audio; -using NWaves.Filters.BiQuad; using ReactiveUI; using RedBookPlayer.Models.Discs; @@ -96,14 +95,9 @@ namespace RedBookPlayer.Models.Hardware private ALSoundOut _soundOut; /// - /// Left channel de-emphasis filter + /// Filtering stage for audio output /// - private BiQuadFilter _deEmphasisFilterLeft; - - /// - /// Right channel de-emphasis filter - /// - private BiQuadFilter _deEmphasisFilterRight; + private FilterStage _filterStage; /// /// Current position in the sector @@ -121,7 +115,12 @@ namespace RedBookPlayer.Models.Hardware /// Constructor /// /// Default volume between 0 and 100 to use when starting playback - public SoundOutput(int defaultVolume = 100) => Volume = defaultVolume; + public SoundOutput(int defaultVolume = 100) + { + Volume = defaultVolume; + _filterStage = new FilterStage(); + } + /// /// Initialize the output with a given image @@ -143,7 +142,7 @@ namespace RedBookPlayer.Models.Hardware ApplyDeEmphasis = compactDisc.TrackHasEmphasis; // Setup de-emphasis filters - SetupFilters(); + _filterStage.SetupFilters(); // Setup the audio output SetupAudio(); @@ -309,26 +308,6 @@ namespace RedBookPlayer.Models.Hardware } } - /// - /// Process de-emphasis of audio data - /// - /// Audio data to process - 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); - } - /// /// Read the requested amount of data from an input /// @@ -376,28 +355,11 @@ namespace RedBookPlayer.Models.Hardware // Apply de-emphasis filtering, only if enabled if(ApplyDeEmphasis) - ProcessDeEmphasis(audioDataSegment); + _filterStage.ProcessAudioData(audioDataSegment); return audioDataSegment; } - /// - /// Sets or resets the de-emphasis filters - /// - private void SetupFilters() - { - if(_deEmphasisFilterLeft == null) - { - _deEmphasisFilterLeft = new DeEmphasisFilter(); - _deEmphasisFilterRight = new DeEmphasisFilter(); - } - else - { - _deEmphasisFilterLeft.Reset(); - _deEmphasisFilterRight.Reset(); - } - } - /// /// Sets or resets the audio playback objects ///