FIFOSamplePipe.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// 'FIFOSamplePipe' : An abstract base class for classes that manipulate sound
  4. /// samples by operating like a first-in-first-out pipe: New samples are fed
  5. /// into one end of the pipe with the 'putSamples' function, and the processed
  6. /// samples are received from the other end with the 'receiveSamples' function.
  7. ///
  8. /// 'FIFOProcessor' : A base class for classes the do signal processing with
  9. /// the samples while operating like a first-in-first-out pipe. When samples
  10. /// are input with the 'putSamples' function, the class processes them
  11. /// and moves the processed samples to the given 'output' pipe object, which
  12. /// may be either another processing stage, or a fifo sample buffer object.
  13. ///
  14. /// Author : Copyright (c) Olli Parviainen
  15. /// Author e-mail : oparviai 'at' iki.fi
  16. /// SoundTouch WWW: http://www.surina.net/soundtouch
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////
  19. //
  20. // License :
  21. //
  22. // SoundTouch audio processing library
  23. // Copyright (c) Olli Parviainen
  24. //
  25. // This library is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser General Public
  27. // License as published by the Free Software Foundation; either
  28. // version 2.1 of the License, or (at your option) any later version.
  29. //
  30. // This library is distributed in the hope that it will be useful,
  31. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. // Lesser General Public License for more details.
  34. //
  35. // You should have received a copy of the GNU Lesser General Public
  36. // License along with this library; if not, write to the Free Software
  37. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  38. //
  39. ////////////////////////////////////////////////////////////////////////////////
  40. #ifndef FIFOSamplePipe_H
  41. #define FIFOSamplePipe_H
  42. #include <assert.h>
  43. #include <stdlib.h>
  44. #include "STTypes.h"
  45. namespace soundtouch
  46. {
  47. /// Abstract base class for FIFO (first-in-first-out) sample processing classes.
  48. class FIFOSamplePipe
  49. {
  50. protected:
  51. bool verifyNumberOfChannels(int nChannels) const
  52. {
  53. if ((nChannels > 0) && (nChannels <= SOUNDTOUCH_MAX_CHANNELS))
  54. {
  55. return true;
  56. }
  57. ST_THROW_RT_ERROR("Error: Illegal number of channels");
  58. return false;
  59. }
  60. public:
  61. // virtual default destructor
  62. virtual ~FIFOSamplePipe() {}
  63. /// Returns a pointer to the beginning of the output samples.
  64. /// This function is provided for accessing the output samples directly.
  65. /// Please be careful for not to corrupt the book-keeping!
  66. ///
  67. /// When using this function to output samples, also remember to 'remove' the
  68. /// output samples from the buffer by calling the
  69. /// 'receiveSamples(numSamples)' function
  70. virtual SAMPLETYPE *ptrBegin() = 0;
  71. /// Adds 'numSamples' pcs of samples from the 'samples' memory position to
  72. /// the sample buffer.
  73. virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples.
  74. uint numSamples ///< Number of samples to insert.
  75. ) = 0;
  76. // Moves samples from the 'other' pipe instance to this instance.
  77. void moveSamples(FIFOSamplePipe &other ///< Other pipe instance where from the receive the data.
  78. )
  79. {
  80. int oNumSamples = other.numSamples();
  81. putSamples(other.ptrBegin(), oNumSamples);
  82. other.receiveSamples(oNumSamples);
  83. };
  84. /// Output samples from beginning of the sample buffer. Copies requested samples to
  85. /// output buffer and removes them from the sample buffer. If there are less than
  86. /// 'numsample' samples in the buffer, returns all that available.
  87. ///
  88. /// \return Number of samples returned.
  89. virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
  90. uint maxSamples ///< How many samples to receive at max.
  91. ) = 0;
  92. /// Adjusts book-keeping so that given number of samples are removed from beginning of the
  93. /// sample buffer without copying them anywhere.
  94. ///
  95. /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
  96. /// with 'ptrBegin' function.
  97. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
  98. ) = 0;
  99. /// Returns number of samples currently available.
  100. virtual uint numSamples() const = 0;
  101. // Returns nonzero if there aren't any samples available for outputting.
  102. virtual int isEmpty() const = 0;
  103. /// Clears all the samples.
  104. virtual void clear() = 0;
  105. /// allow trimming (downwards) amount of samples in pipeline.
  106. /// Returns adjusted amount of samples
  107. virtual uint adjustAmountOfSamples(uint numSamples) = 0;
  108. };
  109. /// Base-class for sound processing routines working in FIFO principle. With this base
  110. /// class it's easy to implement sound processing stages that can be chained together,
  111. /// so that samples that are fed into beginning of the pipe automatically go through
  112. /// all the processing stages.
  113. ///
  114. /// When samples are input to this class, they're first processed and then put to
  115. /// the FIFO pipe that's defined as output of this class. This output pipe can be
  116. /// either other processing stage or a FIFO sample buffer.
  117. class FIFOProcessor :public FIFOSamplePipe
  118. {
  119. protected:
  120. /// Internal pipe where processed samples are put.
  121. FIFOSamplePipe *output;
  122. /// Sets output pipe.
  123. void setOutPipe(FIFOSamplePipe *pOutput)
  124. {
  125. assert(output == nullptr);
  126. assert(pOutput != nullptr);
  127. output = pOutput;
  128. }
  129. /// Constructor. Doesn't define output pipe; it has to be set be
  130. /// 'setOutPipe' function.
  131. FIFOProcessor()
  132. {
  133. output = nullptr;
  134. }
  135. /// Constructor. Configures output pipe.
  136. FIFOProcessor(FIFOSamplePipe *pOutput ///< Output pipe.
  137. )
  138. {
  139. output = pOutput;
  140. }
  141. /// Destructor.
  142. virtual ~FIFOProcessor() override
  143. {
  144. }
  145. /// Returns a pointer to the beginning of the output samples.
  146. /// This function is provided for accessing the output samples directly.
  147. /// Please be careful for not to corrupt the book-keeping!
  148. ///
  149. /// When using this function to output samples, also remember to 'remove' the
  150. /// output samples from the buffer by calling the
  151. /// 'receiveSamples(numSamples)' function
  152. virtual SAMPLETYPE *ptrBegin() override
  153. {
  154. return output->ptrBegin();
  155. }
  156. public:
  157. /// Output samples from beginning of the sample buffer. Copies requested samples to
  158. /// output buffer and removes them from the sample buffer. If there are less than
  159. /// 'numsample' samples in the buffer, returns all that available.
  160. ///
  161. /// \return Number of samples returned.
  162. virtual uint receiveSamples(SAMPLETYPE *outBuffer, ///< Buffer where to copy output samples.
  163. uint maxSamples ///< How many samples to receive at max.
  164. ) override
  165. {
  166. return output->receiveSamples(outBuffer, maxSamples);
  167. }
  168. /// Adjusts book-keeping so that given number of samples are removed from beginning of the
  169. /// sample buffer without copying them anywhere.
  170. ///
  171. /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
  172. /// with 'ptrBegin' function.
  173. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
  174. ) override
  175. {
  176. return output->receiveSamples(maxSamples);
  177. }
  178. /// Returns number of samples currently available.
  179. virtual uint numSamples() const override
  180. {
  181. return output->numSamples();
  182. }
  183. /// Returns nonzero if there aren't any samples available for outputting.
  184. virtual int isEmpty() const override
  185. {
  186. return output->isEmpty();
  187. }
  188. /// allow trimming (downwards) amount of samples in pipeline.
  189. /// Returns adjusted amount of samples
  190. virtual uint adjustAmountOfSamples(uint numSamples) override
  191. {
  192. return output->adjustAmountOfSamples(numSamples);
  193. }
  194. };
  195. }
  196. #endif