RateTransposer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Last changed : $Date: 2009-02-21 18:00:14 +0200 (Sat, 21 Feb 2009) $
  18. // File revision : $Revision: 4 $
  19. //
  20. // $Id: RateTransposer.h 63 2009-02-21 16:00:14Z oparviai $
  21. //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //
  24. // License :
  25. //
  26. // SoundTouch audio processing library
  27. // Copyright (c) Olli Parviainen
  28. //
  29. // This library is free software; you can redistribute it and/or
  30. // modify it under the terms of the GNU Lesser General Public
  31. // License as published by the Free Software Foundation; either
  32. // version 2.1 of the License, or (at your option) any later version.
  33. //
  34. // This library is distributed in the hope that it will be useful,
  35. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. // Lesser General Public License for more details.
  38. //
  39. // You should have received a copy of the GNU Lesser General Public
  40. // License along with this library; if not, write to the Free Software
  41. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  42. //
  43. ////////////////////////////////////////////////////////////////////////////////
  44. #ifndef RateTransposer_H
  45. #define RateTransposer_H
  46. #include <stddef.h>
  47. #include "AAFilter.h"
  48. #include "FIFOSamplePipe.h"
  49. #include "FIFOSampleBuffer.h"
  50. #include "STTypes.h"
  51. namespace soundtouch
  52. {
  53. /// A common linear samplerate transposer class.
  54. ///
  55. /// Note: Use function "RateTransposer::newInstance()" to create a new class
  56. /// instance instead of the "new" operator; that function automatically
  57. /// chooses a correct implementation depending on if integer or floating
  58. /// arithmetics are to be used.
  59. class RateTransposer : public FIFOProcessor
  60. {
  61. protected:
  62. /// Anti-alias filter object
  63. AAFilter *pAAFilter;
  64. float fRate;
  65. int numChannels;
  66. /// Buffer for collecting samples to feed the anti-alias filter between
  67. /// two batches
  68. FIFOSampleBuffer storeBuffer;
  69. /// Buffer for keeping samples between transposing & anti-alias filter
  70. FIFOSampleBuffer tempBuffer;
  71. /// Output sample buffer
  72. FIFOSampleBuffer outputBuffer;
  73. SBOOL bUseAAFilter;
  74. virtual void resetRegisters() = 0;
  75. virtual uint transposeStereo(SAMPLETYPE *dest,
  76. const SAMPLETYPE *src,
  77. uint numSamples) = 0;
  78. virtual uint transposeMono(SAMPLETYPE *dest,
  79. const SAMPLETYPE *src,
  80. uint numSamples) = 0;
  81. inline uint transpose(SAMPLETYPE *dest,
  82. const SAMPLETYPE *src,
  83. uint numSamples);
  84. void downsample(const SAMPLETYPE *src,
  85. uint numSamples);
  86. void upsample(const SAMPLETYPE *src,
  87. uint numSamples);
  88. /// Transposes sample rate by applying anti-alias filter to prevent folding.
  89. /// Returns amount of samples returned in the "dest" buffer.
  90. /// The maximum amount of samples that can be returned at a time is set by
  91. /// the 'set_returnBuffer_size' function.
  92. void processSamples(const SAMPLETYPE *src,
  93. uint numSamples);
  94. public:
  95. RateTransposer();
  96. virtual ~RateTransposer();
  97. /// Operator 'new' is overloaded so that it automatically creates a suitable instance
  98. /// depending on if we're to use integer or floating point arithmetics.
  99. static void *operator new(size_t s);
  100. /// Use this function instead of "new" operator to create a new instance of this class.
  101. /// This function automatically chooses a correct implementation, depending on if
  102. /// integer ot floating point arithmetics are to be used.
  103. static RateTransposer *newInstance();
  104. /// Returns the output buffer object
  105. FIFOSamplePipe *getOutput() { return &outputBuffer; };
  106. /// Returns the store buffer object
  107. FIFOSamplePipe *getStore() { return &storeBuffer; };
  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(SBOOL newMode);
  112. /// Returns nonzero if anti-alias filter is enabled.
  113. SBOOL isAAFilterEnabled() const;
  114. /// Sets new target rate. Normal rate = 1.0, smaller values represent slower
  115. /// rate, larger faster rates.
  116. virtual void setRate(float 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);
  122. /// Clears all the samples in the object
  123. void clear();
  124. /// Returns nonzero if there aren't any samples available for outputting.
  125. int isEmpty() const;
  126. };
  127. }
  128. #endif