FIRFilter.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// General FIR digital filter routines with MMX optimization.
  4. ///
  5. /// Note : MMX optimized functions reside in a separate, platform-specific file,
  6. /// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
  7. ///
  8. /// Author : Copyright (c) Olli Parviainen
  9. /// Author e-mail : oparviai 'at' iki.fi
  10. /// SoundTouch WWW: http://www.surina.net/soundtouch
  11. ///
  12. ////////////////////////////////////////////////////////////////////////////////
  13. //
  14. // License :
  15. //
  16. // SoundTouch audio processing library
  17. // Copyright (c) Olli Parviainen
  18. //
  19. // This library is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Lesser General Public
  21. // License as published by the Free Software Foundation; either
  22. // version 2.1 of the License, or (at your option) any later version.
  23. //
  24. // This library is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. // Lesser General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU Lesser General Public
  30. // License along with this library; if not, write to the Free Software
  31. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. //
  33. ////////////////////////////////////////////////////////////////////////////////
  34. #ifndef FIRFilter_H
  35. #define FIRFilter_H
  36. #include <stddef.h>
  37. #include "STTypes.h"
  38. namespace soundtouch
  39. {
  40. class FIRFilter
  41. {
  42. protected:
  43. // Number of FIR filter taps
  44. uint length;
  45. // Number of FIR filter taps divided by 8
  46. uint lengthDiv8;
  47. // Result divider factor in 2^k format
  48. uint resultDivFactor;
  49. // Result divider value.
  50. SAMPLETYPE resultDivider;
  51. // Memory for filter coefficients
  52. SAMPLETYPE *filterCoeffs;
  53. SAMPLETYPE *filterCoeffsStereo;
  54. virtual uint evaluateFilterStereo(SAMPLETYPE *dest,
  55. const SAMPLETYPE *src,
  56. uint numSamples) const;
  57. virtual uint evaluateFilterMono(SAMPLETYPE *dest,
  58. const SAMPLETYPE *src,
  59. uint numSamples) const;
  60. virtual uint evaluateFilterMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels);
  61. public:
  62. FIRFilter();
  63. virtual ~FIRFilter();
  64. /// Operator 'new' is overloaded so that it automatically creates a suitable instance
  65. /// depending on if we've a MMX-capable CPU available or not.
  66. static void * operator new(size_t s);
  67. static FIRFilter *newInstance();
  68. /// Applies the filter to the given sequence of samples.
  69. /// Note : The amount of outputted samples is by value of 'filter_length'
  70. /// smaller than the amount of input samples.
  71. ///
  72. /// \return Number of samples copied to 'dest'.
  73. uint evaluate(SAMPLETYPE *dest,
  74. const SAMPLETYPE *src,
  75. uint numSamples,
  76. uint numChannels);
  77. uint getLength() const;
  78. virtual void setCoefficients(const SAMPLETYPE *coeffs,
  79. uint newLength,
  80. uint uResultDivFactor);
  81. };
  82. // Optional subclasses that implement CPU-specific optimizations:
  83. #ifdef SOUNDTOUCH_ALLOW_MMX
  84. /// Class that implements MMX optimized functions exclusive for 16bit integer samples type.
  85. class FIRFilterMMX : public FIRFilter
  86. {
  87. protected:
  88. short *filterCoeffsUnalign;
  89. short *filterCoeffsAlign;
  90. virtual uint evaluateFilterStereo(short *dest, const short *src, uint numSamples) const override;
  91. public:
  92. FIRFilterMMX();
  93. ~FIRFilterMMX();
  94. virtual void setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor) override;
  95. };
  96. #endif // SOUNDTOUCH_ALLOW_MMX
  97. #ifdef SOUNDTOUCH_ALLOW_SSE
  98. /// Class that implements SSE optimized functions exclusive for floating point samples type.
  99. class FIRFilterSSE : public FIRFilter
  100. {
  101. protected:
  102. float *filterCoeffsUnalign;
  103. float *filterCoeffsAlign;
  104. virtual uint evaluateFilterStereo(float *dest, const float *src, uint numSamples) const override;
  105. public:
  106. FIRFilterSSE();
  107. ~FIRFilterSSE();
  108. virtual void setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor) override;
  109. };
  110. #endif // SOUNDTOUCH_ALLOW_SSE
  111. }
  112. #endif // FIRFilter_H