From 55aad5a4fa7ff556737cd677306f1b5ba4c7a8e9 Mon Sep 17 00:00:00 2001 From: filoe Date: Sun, 5 Feb 2017 17:13:26 +0100 Subject: [PATCH] Fixed some warnings. --- CSCore/CSCore.csproj | 7 + CSCore/Codecs/AIFF/AiffBinaryReader.cs | 4 +- CSCore/Codecs/FLAC/FlacFrame.cs | 5 +- CSCore/Codecs/WAV/WaveFileChunk.cs | 2 +- CSCore/DSP/BandpassFilter.cs | 38 ++++ CSCore/DSP/BiQuad.cs | 280 ------------------------- CSCore/DSP/HighShelfFilter.cs | 55 +++++ CSCore/DSP/HighpassFilter.cs | 38 ++++ CSCore/DSP/LowShelfFilter.cs | 55 +++++ CSCore/DSP/LowpassFilter.cs | 38 ++++ CSCore/DSP/NotchFilter.cs | 38 ++++ CSCore/DSP/PeakFilter.cs | 71 +++++++ CSCore/Win32/ComStream.cs | 1 + 13 files changed, 347 insertions(+), 285 deletions(-) create mode 100644 CSCore/DSP/BandpassFilter.cs create mode 100644 CSCore/DSP/HighShelfFilter.cs create mode 100644 CSCore/DSP/HighpassFilter.cs create mode 100644 CSCore/DSP/LowShelfFilter.cs create mode 100644 CSCore/DSP/LowpassFilter.cs create mode 100644 CSCore/DSP/NotchFilter.cs create mode 100644 CSCore/DSP/PeakFilter.cs diff --git a/CSCore/CSCore.csproj b/CSCore/CSCore.csproj index 684535dd..5c46e22d 100644 --- a/CSCore/CSCore.csproj +++ b/CSCore/CSCore.csproj @@ -286,12 +286,19 @@ + + + + + + + diff --git a/CSCore/Codecs/AIFF/AiffBinaryReader.cs b/CSCore/Codecs/AIFF/AiffBinaryReader.cs index c12e0720..e0273a59 100644 --- a/CSCore/Codecs/AIFF/AiffBinaryReader.cs +++ b/CSCore/Codecs/AIFF/AiffBinaryReader.cs @@ -81,11 +81,11 @@ private double ConvertFromIeeeExtended(byte[] bytes) hiMant = ((ulong) (bytes[2] & 0xFF) << 24) | ((ulong) (bytes[3] & 0xFF) << 16) | ((ulong) (bytes[4] & 0xFF) << 8) - | ((ulong) (bytes[5] & 0xFF)); + | (ulong) (bytes[5] & 0xFF); loMant = ((ulong) (bytes[6] & 0xFF) << 24) | ((ulong) (bytes[7] & 0xFF) << 16) | ((ulong) (bytes[8] & 0xFF) << 8) - | ((ulong) (bytes[9] & 0xFF)); + | (ulong) (bytes[9] & 0xFF); if (expon == 0 && hiMant == 0 && loMant == 0) f = 0; diff --git a/CSCore/Codecs/FLAC/FlacFrame.cs b/CSCore/Codecs/FLAC/FlacFrame.cs index a195f49b..9c8aefeb 100644 --- a/CSCore/Codecs/FLAC/FlacFrame.cs +++ b/CSCore/Codecs/FLAC/FlacFrame.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.IO; using System.Runtime.InteropServices; @@ -14,7 +13,9 @@ namespace CSCore.Codecs.FLAC public sealed partial class FlacFrame : IDisposable { private List _subFrameData; - private ReadOnlyCollection _subFrames; +#if FLAC_DEBUG + private System.Collections.ObjectModel.ReadOnlyCollection _subFrames; +#endif private Stream _stream; private FlacMetadataStreamInfo _streamInfo; diff --git a/CSCore/Codecs/WAV/WaveFileChunk.cs b/CSCore/Codecs/WAV/WaveFileChunk.cs index 1ead06c8..b505f455 100644 --- a/CSCore/Codecs/WAV/WaveFileChunk.cs +++ b/CSCore/Codecs/WAV/WaveFileChunk.cs @@ -38,7 +38,7 @@ public WaveFileChunk(BinaryReader reader) /// /// Gets the data size of the chunk. /// - public uint ChunkDataSize { get; private set; } + public long ChunkDataSize { get; private set; } /// /// Parses the and returns a . Note that the position of the diff --git a/CSCore/DSP/BandpassFilter.cs b/CSCore/DSP/BandpassFilter.cs new file mode 100644 index 00000000..0191fe33 --- /dev/null +++ b/CSCore/DSP/BandpassFilter.cs @@ -0,0 +1,38 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply a bandpass-filter to a signal. + /// + public class BandpassFilter : BiQuad + { + /// + /// Initializes a new instance of the class. + /// + /// The sample rate. + /// The filter's corner frequency. + public BandpassFilter(int sampleRate, double frequency) + : base(sampleRate, frequency) + { + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + double k = Math.Tan(Math.PI * Frequency / SampleRate); + double norm = 1 / (1 + k / Q + k * k); + A0 = k / Q * norm; + A1 = 0; + A2 = -A0; + B1 = 2 * (k * k - 1) * norm; + B2 = (1 - k / Q + k * k) * norm; + } + } +} \ No newline at end of file diff --git a/CSCore/DSP/BiQuad.cs b/CSCore/DSP/BiQuad.cs index 48ba8e19..2255d3b1 100644 --- a/CSCore/DSP/BiQuad.cs +++ b/CSCore/DSP/BiQuad.cs @@ -178,284 +178,4 @@ public void Process(float[] input) /// protected abstract void CalculateBiQuadCoefficients(); } - - /// - /// Used to apply a lowpass-filter to a signal. - /// - public class LowpassFilter : BiQuad - { - /// - /// Initializes a new instance of the class. - /// - /// The sample rate. - /// The filter's corner frequency. - public LowpassFilter(int sampleRate, double frequency) - : base(sampleRate, frequency) - { - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - double k = Math.Tan(Math.PI * Frequency / SampleRate); - var norm = 1 / (1 + k / Q + k * k); - A0 = k * k * norm; - A1 = 2 * A0; - A2 = A0; - B1 = 2 * (k * k - 1) * norm; - B2 = (1 - k / Q + k * k) * norm; - } - } - - /// - /// Used to apply a highpass-filter to a signal. - /// - public class HighpassFilter : BiQuad - { - private int p1; - private double p2; - - /// - /// Initializes a new instance of the class. - /// - /// The sample rate. - /// The filter's corner frequency. - public HighpassFilter(int sampleRate, double frequency) - : base(sampleRate, frequency) - { - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - double k = Math.Tan(Math.PI * Frequency / SampleRate); - var norm = 1 / (1 + k / Q + k * k); - A0 = 1 * norm; - A1 = -2 * A0; - A2 = A0; - B1 = 2 * (k * k - 1) * norm; - B2 = (1 - k / Q + k * k) * norm; - } - } - - /// - /// Used to apply a bandpass-filter to a signal. - /// - public class BandpassFilter : BiQuad - { - /// - /// Initializes a new instance of the class. - /// - /// The sample rate. - /// The filter's corner frequency. - public BandpassFilter(int sampleRate, double frequency) - : base(sampleRate, frequency) - { - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - double k = Math.Tan(Math.PI * Frequency / SampleRate); - double norm = 1 / (1 + k / Q + k * k); - A0 = k / Q * norm; - A1 = 0; - A2 = -A0; - B1 = 2 * (k * k - 1) * norm; - B2 = (1 - k / Q + k * k) * norm; - } - } - - /// - /// Used to apply a notch-filter to a signal. - /// - public class NotchFilter : BiQuad - { - /// - /// Initializes a new instance of the class. - /// - /// The sample rate. - /// The filter's corner frequency. - public NotchFilter(int sampleRate, double frequency) - : base(sampleRate, frequency) - { - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - double k = Math.Tan(Math.PI * Frequency / SampleRate); - double norm = 1 / (1 + k / Q + k * k); - A0 = (1 + k * k) * norm; - A1 = 2 * (k * k - 1) * norm; - A2 = A0; - B1 = A1; - B2 = (1 - k / Q + k * k) * norm; - } - } - - /// - /// Used to apply a lowshelf-filter to a signal. - /// - public class LowShelfFilter : BiQuad - { - /// - /// Initializes a new instance of the class. - /// - /// The sample rate. - /// The filter's corner frequency. - /// Gain value in dB. - public LowShelfFilter(int sampleRate, double frequency, double gainDB) - : base(sampleRate, frequency) - { - GainDB = gainDB; - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - const double sqrt2 = 1.4142135623730951; - double k = Math.Tan(Math.PI * Frequency / SampleRate); - double v = Math.Pow(10, Math.Abs(GainDB) / 20.0); - double norm; - if (GainDB >= 0) - { // boost - norm = 1 / (1 + sqrt2 * k + k * k); - A0 = (1 + Math.Sqrt(2 * v) * k + v * k * k) * norm; - A1 = 2 * (v * k * k - 1) * norm; - A2 = (1 - Math.Sqrt(2 * v) * k + v * k * k) * norm; - B1 = 2 * (k * k - 1) * norm; - B2 = (1 - sqrt2 * k + k * k) * norm; - } - else - { // cut - norm = 1 / (1 + Math.Sqrt(2 * v) * k + v * k * k); - A0 = (1 + sqrt2 * k + k * k) * norm; - A1 = 2 * (k * k - 1) * norm; - A2 = (1 - sqrt2 * k + k * k) * norm; - B1 = 2 * (v * k * k - 1) * norm; - B2 = (1 - Math.Sqrt(2 * v) * k + v * k * k) * norm; - } - } - } - - /// - /// Used to apply a highshelf-filter to a signal. - /// - public class HighShelfFilter : BiQuad - { - /// - /// Initializes a new instance of the class. - /// - /// The sample rate. - /// The filter's corner frequency. - /// Gain value in dB. - public HighShelfFilter(int sampleRate, double frequency, double gainDB) - : base(sampleRate, frequency) - { - GainDB = gainDB; - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - const double sqrt2 = 1.4142135623730951; - double k = Math.Tan(Math.PI * Frequency / SampleRate); - double v = Math.Pow(10, Math.Abs(GainDB) / 20.0); - double norm; - if (GainDB >= 0) - { // boost - norm = 1 / (1 + sqrt2 * k + k * k); - A0 = (v + Math.Sqrt(2 * v) * k + k * k) * norm; - A1 = 2 * (k * k - v) * norm; - A2 = (v - Math.Sqrt(2 * v) * k + k * k) * norm; - B1 = 2 * (k * k - 1) * norm; - B2 = (1 - sqrt2 * k + k * k) * norm; - } - else - { // cut - norm = 1 / (v + Math.Sqrt(2 * v) * k + k * k); - A0 = (1 + sqrt2 * k + k * k) * norm; - A1 = 2 * (k * k - 1) * norm; - A2 = (1 - sqrt2 * k + k * k) * norm; - B1 = 2 * (k * k - v) * norm; - B2 = (v - Math.Sqrt(2 * v) * k + k * k) * norm; - } - } - } - - /// - /// Used to apply an peak-filter to a signal. - /// - public class PeakFilter : BiQuad - { - /// - /// Gets or sets the bandwidth. - /// - public double BandWidth - { - get { return Q; } - set - { - if (value <= 0) - throw new ArgumentOutOfRangeException("value"); - Q = value; - } - } - - /// - /// Initializes a new instance of the class. - /// - /// The sampleRate of the audio data to process. - /// The center frequency to adjust. - /// The bandWidth. - /// The gain value in dB. - public PeakFilter(int sampleRate, double frequency, double bandWidth, double peakGainDB) - : base(sampleRate, frequency, bandWidth) - { - GainDB = peakGainDB; - } - - /// - /// Calculates all coefficients. - /// - protected override void CalculateBiQuadCoefficients() - { - double norm; - double v = Math.Pow(10, Math.Abs(GainDB) / 20.0); - double k = Math.Tan(Math.PI * Frequency / SampleRate); - double q = Q; - - if (GainDB >= 0) //boost - { - norm = 1 / (1 + 1 / q * k + k * k); - A0 = (1 + v / q * k + k * k) * norm; - A1 = 2 * (k * k - 1) * norm; - A2 = (1 - v / q * k + k * k) * norm; - B1 = A1; - B2 = (1 - 1 / q * k + k * k) * norm; - } - else //cut - { - norm = 1 / (1 + v / q * k + k * k); - A0 = (1 + 1 / q * k + k * k) * norm; - A1 = 2 * (k * k - 1) * norm; - A2 = (1 - 1 / q * k + k * k) * norm; - B1 = A1; - B2 = (1 - v / q * k + k * k) * norm; - } - } - } } diff --git a/CSCore/DSP/HighShelfFilter.cs b/CSCore/DSP/HighShelfFilter.cs new file mode 100644 index 00000000..f9b9d953 --- /dev/null +++ b/CSCore/DSP/HighShelfFilter.cs @@ -0,0 +1,55 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply a highshelf-filter to a signal. + /// + public class HighShelfFilter : BiQuad + { + /// + /// Initializes a new instance of the class. + /// + /// The sample rate. + /// The filter's corner frequency. + /// Gain value in dB. + public HighShelfFilter(int sampleRate, double frequency, double gainDB) + : base(sampleRate, frequency) + { + GainDB = gainDB; + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + const double sqrt2 = 1.4142135623730951; + double k = Math.Tan(Math.PI * Frequency / SampleRate); + double v = Math.Pow(10, Math.Abs(GainDB) / 20.0); + double norm; + if (GainDB >= 0) + { // boost + norm = 1 / (1 + sqrt2 * k + k * k); + A0 = (v + Math.Sqrt(2 * v) * k + k * k) * norm; + A1 = 2 * (k * k - v) * norm; + A2 = (v - Math.Sqrt(2 * v) * k + k * k) * norm; + B1 = 2 * (k * k - 1) * norm; + B2 = (1 - sqrt2 * k + k * k) * norm; + } + else + { // cut + norm = 1 / (v + Math.Sqrt(2 * v) * k + k * k); + A0 = (1 + sqrt2 * k + k * k) * norm; + A1 = 2 * (k * k - 1) * norm; + A2 = (1 - sqrt2 * k + k * k) * norm; + B1 = 2 * (k * k - v) * norm; + B2 = (v - Math.Sqrt(2 * v) * k + k * k) * norm; + } + } + } +} \ No newline at end of file diff --git a/CSCore/DSP/HighpassFilter.cs b/CSCore/DSP/HighpassFilter.cs new file mode 100644 index 00000000..a2936e44 --- /dev/null +++ b/CSCore/DSP/HighpassFilter.cs @@ -0,0 +1,38 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply a highpass-filter to a signal. + /// + public class HighpassFilter : BiQuad + { + /// + /// Initializes a new instance of the class. + /// + /// The sample rate. + /// The filter's corner frequency. + public HighpassFilter(int sampleRate, double frequency) + : base(sampleRate, frequency) + { + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + double k = Math.Tan(Math.PI * Frequency / SampleRate); + var norm = 1 / (1 + k / Q + k * k); + A0 = 1 * norm; + A1 = -2 * A0; + A2 = A0; + B1 = 2 * (k * k - 1) * norm; + B2 = (1 - k / Q + k * k) * norm; + } + } +} \ No newline at end of file diff --git a/CSCore/DSP/LowShelfFilter.cs b/CSCore/DSP/LowShelfFilter.cs new file mode 100644 index 00000000..a9efc45d --- /dev/null +++ b/CSCore/DSP/LowShelfFilter.cs @@ -0,0 +1,55 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply a lowshelf-filter to a signal. + /// + public class LowShelfFilter : BiQuad + { + /// + /// Initializes a new instance of the class. + /// + /// The sample rate. + /// The filter's corner frequency. + /// Gain value in dB. + public LowShelfFilter(int sampleRate, double frequency, double gainDB) + : base(sampleRate, frequency) + { + GainDB = gainDB; + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + const double sqrt2 = 1.4142135623730951; + double k = Math.Tan(Math.PI * Frequency / SampleRate); + double v = Math.Pow(10, Math.Abs(GainDB) / 20.0); + double norm; + if (GainDB >= 0) + { // boost + norm = 1 / (1 + sqrt2 * k + k * k); + A0 = (1 + Math.Sqrt(2 * v) * k + v * k * k) * norm; + A1 = 2 * (v * k * k - 1) * norm; + A2 = (1 - Math.Sqrt(2 * v) * k + v * k * k) * norm; + B1 = 2 * (k * k - 1) * norm; + B2 = (1 - sqrt2 * k + k * k) * norm; + } + else + { // cut + norm = 1 / (1 + Math.Sqrt(2 * v) * k + v * k * k); + A0 = (1 + sqrt2 * k + k * k) * norm; + A1 = 2 * (k * k - 1) * norm; + A2 = (1 - sqrt2 * k + k * k) * norm; + B1 = 2 * (v * k * k - 1) * norm; + B2 = (1 - Math.Sqrt(2 * v) * k + v * k * k) * norm; + } + } + } +} \ No newline at end of file diff --git a/CSCore/DSP/LowpassFilter.cs b/CSCore/DSP/LowpassFilter.cs new file mode 100644 index 00000000..6ce6a6ac --- /dev/null +++ b/CSCore/DSP/LowpassFilter.cs @@ -0,0 +1,38 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply a lowpass-filter to a signal. + /// + public class LowpassFilter : BiQuad + { + /// + /// Initializes a new instance of the class. + /// + /// The sample rate. + /// The filter's corner frequency. + public LowpassFilter(int sampleRate, double frequency) + : base(sampleRate, frequency) + { + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + double k = Math.Tan(Math.PI * Frequency / SampleRate); + var norm = 1 / (1 + k / Q + k * k); + A0 = k * k * norm; + A1 = 2 * A0; + A2 = A0; + B1 = 2 * (k * k - 1) * norm; + B2 = (1 - k / Q + k * k) * norm; + } + } +} \ No newline at end of file diff --git a/CSCore/DSP/NotchFilter.cs b/CSCore/DSP/NotchFilter.cs new file mode 100644 index 00000000..e00a3312 --- /dev/null +++ b/CSCore/DSP/NotchFilter.cs @@ -0,0 +1,38 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply a notch-filter to a signal. + /// + public class NotchFilter : BiQuad + { + /// + /// Initializes a new instance of the class. + /// + /// The sample rate. + /// The filter's corner frequency. + public NotchFilter(int sampleRate, double frequency) + : base(sampleRate, frequency) + { + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + double k = Math.Tan(Math.PI * Frequency / SampleRate); + double norm = 1 / (1 + k / Q + k * k); + A0 = (1 + k * k) * norm; + A1 = 2 * (k * k - 1) * norm; + A2 = A0; + B1 = A1; + B2 = (1 - k / Q + k * k) * norm; + } + } +} \ No newline at end of file diff --git a/CSCore/DSP/PeakFilter.cs b/CSCore/DSP/PeakFilter.cs new file mode 100644 index 00000000..6042d7eb --- /dev/null +++ b/CSCore/DSP/PeakFilter.cs @@ -0,0 +1,71 @@ +/* + * These implementations are based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/ + */ + +using System; + +namespace CSCore.DSP +{ + /// + /// Used to apply an peak-filter to a signal. + /// + public class PeakFilter : BiQuad + { + /// + /// Gets or sets the bandwidth. + /// + public double BandWidth + { + get { return Q; } + set + { + if (value <= 0) + throw new ArgumentOutOfRangeException("value"); + Q = value; + } + } + + /// + /// Initializes a new instance of the class. + /// + /// The sampleRate of the audio data to process. + /// The center frequency to adjust. + /// The bandWidth. + /// The gain value in dB. + public PeakFilter(int sampleRate, double frequency, double bandWidth, double peakGainDB) + : base(sampleRate, frequency, bandWidth) + { + GainDB = peakGainDB; + } + + /// + /// Calculates all coefficients. + /// + protected override void CalculateBiQuadCoefficients() + { + double norm; + double v = Math.Pow(10, Math.Abs(GainDB) / 20.0); + double k = Math.Tan(Math.PI * Frequency / SampleRate); + double q = Q; + + if (GainDB >= 0) //boost + { + norm = 1 / (1 + 1 / q * k + k * k); + A0 = (1 + v / q * k + k * k) * norm; + A1 = 2 * (k * k - 1) * norm; + A2 = (1 - v / q * k + k * k) * norm; + B1 = A1; + B2 = (1 - 1 / q * k + k * k) * norm; + } + else //cut + { + norm = 1 / (1 + v / q * k + k * k); + A0 = (1 + 1 / q * k + k * k) * norm; + A1 = 2 * (k * k - 1) * norm; + A2 = (1 - 1 / q * k + k * k) * norm; + B1 = A1; + B2 = (1 - v / q * k + k * k) * norm; + } + } + } +} \ No newline at end of file diff --git a/CSCore/Win32/ComStream.cs b/CSCore/Win32/ComStream.cs index 9118dac7..08f2bbb8 100644 --- a/CSCore/Win32/ComStream.cs +++ b/CSCore/Win32/ComStream.cs @@ -26,6 +26,7 @@ public ComStream(Stream stream) /// Initializes a new instance of the class. /// /// Underlying . + /// Indicates whether the underlying stream should be disposed on . public ComStream(Stream stream, bool disposeBaseStream) { if (stream == null)