FIFOSamplePipe.h 8.5 KB

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