RateTransposer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// Sample rate transposer. Changes sample rate by using linear interpolation
  4. /// together with anti-alias filtering (first order interpolation with anti-
  5. /// alias filtering should be quite adequate for this application).
  6. ///
  7. /// Use either of the derived classes of 'RateTransposerInteger' or
  8. /// 'RateTransposerFloat' for corresponding integer/floating point tranposing
  9. /// algorithm implementation.
  10. ///
  11. /// Author : Copyright (c) Olli Parviainen
  12. /// Author e-mail : oparviai 'at' iki.fi
  13. /// SoundTouch WWW: http://www.surina.net/soundtouch
  14. ///
  15. ////////////////////////////////////////////////////////////////////////////////
  16. //
  17. // License :
  18. //
  19. // SoundTouch audio processing library
  20. // Copyright (c) Olli Parviainen
  21. //
  22. // This library is free software; you can redistribute it and/or
  23. // modify it under the terms of the GNU Lesser General Public
  24. // License as published by the Free Software Foundation; either
  25. // version 2.1 of the License, or (at your option) any later version.
  26. //
  27. // This library is distributed in the hope that it will be useful,
  28. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. // Lesser General Public License for more details.
  31. //
  32. // You should have received a copy of the GNU Lesser General Public
  33. // License along with this library; if not, write to the Free Software
  34. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  35. //
  36. ////////////////////////////////////////////////////////////////////////////////
  37. #ifndef RateTransposer_H
  38. #define RateTransposer_H
  39. #include <stddef.h>
  40. #include "AAFilter.h"
  41. #include "FIFOSamplePipe.h"
  42. #include "FIFOSampleBuffer.h"
  43. #include "STTypes.h"
  44. namespace soundtouch
  45. {
  46. /// Abstract base class for transposer implementations (linear, advanced vs integer, float etc)
  47. class TransposerBase
  48. {
  49. public:
  50. enum ALGORITHM {
  51. LINEAR = 0,
  52. CUBIC,
  53. SHANNON
  54. };
  55. protected:
  56. virtual int transposeMono(SAMPLETYPE *dest,
  57. const SAMPLETYPE *src,
  58. int &srcSamples) = 0;
  59. virtual int transposeStereo(SAMPLETYPE *dest,
  60. const SAMPLETYPE *src,
  61. int &srcSamples) = 0;
  62. virtual int transposeMulti(SAMPLETYPE *dest,
  63. const SAMPLETYPE *src,
  64. int &srcSamples) = 0;
  65. static ALGORITHM algorithm;
  66. public:
  67. double rate;
  68. int numChannels;
  69. TransposerBase();
  70. virtual ~TransposerBase();
  71. virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src);
  72. virtual void setRate(double newRate);
  73. virtual void setChannels(int channels);
  74. virtual int getLatency() const = 0;
  75. virtual void resetRegisters() = 0;
  76. // static factory function
  77. static TransposerBase *newInstance();
  78. // static function to set interpolation algorithm
  79. static void setAlgorithm(ALGORITHM a);
  80. };
  81. /// A common linear samplerate transposer class.
  82. ///
  83. class RateTransposer : public FIFOProcessor
  84. {
  85. protected:
  86. /// Anti-alias filter object
  87. AAFilter *pAAFilter;
  88. TransposerBase *pTransposer;
  89. /// Buffer for collecting samples to feed the anti-alias filter between
  90. /// two batches
  91. FIFOSampleBuffer inputBuffer;
  92. /// Buffer for keeping samples between transposing & anti-alias filter
  93. FIFOSampleBuffer midBuffer;
  94. /// Output sample buffer
  95. FIFOSampleBuffer outputBuffer;
  96. bool bUseAAFilter;
  97. /// Transposes sample rate by applying anti-alias filter to prevent folding.
  98. /// Returns amount of samples returned in the "dest" buffer.
  99. /// The maximum amount of samples that can be returned at a time is set by
  100. /// the 'set_returnBuffer_size' function.
  101. void processSamples(const SAMPLETYPE *src,
  102. uint numSamples);
  103. public:
  104. RateTransposer();
  105. virtual ~RateTransposer() override;
  106. /// Returns the output buffer object
  107. FIFOSamplePipe *getOutput() { return &outputBuffer; };
  108. /// Return anti-alias filter object
  109. AAFilter *getAAFilter();
  110. /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable
  111. void enableAAFilter(bool newMode);
  112. /// Returns nonzero if anti-alias filter is enabled.
  113. bool isAAFilterEnabled() const;
  114. /// Sets new target rate. Normal rate = 1.0, smaller values represent slower
  115. /// rate, larger faster rates.
  116. virtual void setRate(double newRate);
  117. /// Sets the number of channels, 1 = mono, 2 = stereo
  118. void setChannels(int channels);
  119. /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
  120. /// the input of the object.
  121. void putSamples(const SAMPLETYPE *samples, uint numSamples) override;
  122. /// Clears all the samples in the object
  123. void clear() override;
  124. /// Returns nonzero if there aren't any samples available for outputting.
  125. int isEmpty() const override;
  126. /// Return approximate initial input-output latency
  127. int getLatency() const;
  128. };
  129. }
  130. #endif