FIFOSampleBuffer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// A buffer class for temporarily storaging sound samples, operates as a
  4. /// first-in-first-out pipe.
  5. ///
  6. /// Samples are added to the end of the sample buffer with the 'putSamples'
  7. /// function, and are received from the beginning of the buffer by calling
  8. /// the 'receiveSamples' function. The class automatically removes the
  9. /// output samples from the buffer as well as grows the storage size
  10. /// whenever necessary.
  11. ///
  12. /// Author : Copyright (c) Olli Parviainen
  13. /// Author e-mail : oparviai 'at' iki.fi
  14. /// SoundTouch WWW: http://www.surina.net/soundtouch
  15. ///
  16. ////////////////////////////////////////////////////////////////////////////////
  17. //
  18. // Last changed : $Date: 2012-06-13 22:29:53 +0300 (Wed, 13 Jun 2012) $
  19. // File revision : $Revision: 4 $
  20. //
  21. // $Id: FIFOSampleBuffer.h 143 2012-06-13 19:29:53Z oparviai $
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////
  24. //
  25. // License :
  26. //
  27. // SoundTouch audio processing library
  28. // Copyright (c) Olli Parviainen
  29. //
  30. // This library is free software; you can redistribute it and/or
  31. // modify it under the terms of the GNU Lesser General Public
  32. // License as published by the Free Software Foundation; either
  33. // version 2.1 of the License, or (at your option) any later version.
  34. //
  35. // This library is distributed in the hope that it will be useful,
  36. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  38. // Lesser General Public License for more details.
  39. //
  40. // You should have received a copy of the GNU Lesser General Public
  41. // License along with this library; if not, write to the Free Software
  42. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  43. //
  44. ////////////////////////////////////////////////////////////////////////////////
  45. #ifndef FIFOSampleBuffer_H
  46. #define FIFOSampleBuffer_H
  47. #include "FIFOSamplePipe.h"
  48. namespace soundtouch
  49. {
  50. /// Sample buffer working in FIFO (first-in-first-out) principle. The class takes
  51. /// care of storage size adjustment and data moving during input/output operations.
  52. ///
  53. /// Notice that in case of stereo audio, one sample is considered to consist of
  54. /// both channel data.
  55. class FIFOSampleBuffer : public FIFOSamplePipe
  56. {
  57. private:
  58. /// Sample buffer.
  59. SAMPLETYPE *buffer;
  60. // Raw unaligned buffer memory. 'buffer' is made aligned by pointing it to first
  61. // 16-byte aligned location of this buffer
  62. SAMPLETYPE *bufferUnaligned;
  63. /// Sample buffer size in bytes
  64. uint sizeInBytes;
  65. /// How many samples are currently in buffer.
  66. uint samplesInBuffer;
  67. /// Channels, 1=mono, 2=stereo.
  68. uint channels;
  69. /// Current position pointer to the buffer. This pointer is increased when samples are
  70. /// removed from the pipe so that it's necessary to actually rewind buffer (move data)
  71. /// only new data when is put to the pipe.
  72. uint bufferPos;
  73. /// Rewind the buffer by moving data from position pointed by 'bufferPos' to real
  74. /// beginning of the buffer.
  75. void rewind();
  76. /// Ensures that the buffer has capacity for at least this many samples.
  77. void ensureCapacity(uint capacityRequirement);
  78. /// Returns current capacity.
  79. uint getCapacity() const;
  80. public:
  81. /// Constructor
  82. FIFOSampleBuffer(int numChannels = 2 ///< Number of channels, 1=mono, 2=stereo.
  83. ///< Default is stereo.
  84. );
  85. /// destructor
  86. ~FIFOSampleBuffer();
  87. /// Returns a pointer to the beginning of the output samples.
  88. /// This function is provided for accessing the output samples directly.
  89. /// Please be careful for not to corrupt the book-keeping!
  90. ///
  91. /// When using this function to output samples, also remember to 'remove' the
  92. /// output samples from the buffer by calling the
  93. /// 'receiveSamples(numSamples)' function
  94. virtual SAMPLETYPE *ptrBegin();
  95. /// Returns a pointer to the end of the used part of the sample buffer (i.e.
  96. /// where the new samples are to be inserted). This function may be used for
  97. /// inserting new samples into the sample buffer directly. Please be careful
  98. /// not corrupt the book-keeping!
  99. ///
  100. /// When using this function as means for inserting new samples, also remember
  101. /// to increase the sample count afterwards, by calling the
  102. /// 'putSamples(numSamples)' function.
  103. SAMPLETYPE *ptrEnd(
  104. uint slackCapacity ///< How much free capacity (in samples) there _at least_
  105. ///< should be so that the caller can succesfully insert the
  106. ///< desired samples to the buffer. If necessary, the function
  107. ///< grows the buffer size to comply with this requirement.
  108. );
  109. /// Adds 'numSamples' pcs of samples from the 'samples' memory position to
  110. /// the sample buffer.
  111. virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples.
  112. uint numSamples ///< Number of samples to insert.
  113. );
  114. /// Adjusts the book-keeping to increase number of samples in the buffer without
  115. /// copying any actual samples.
  116. ///
  117. /// This function is used to update the number of samples in the sample buffer
  118. /// when accessing the buffer directly with 'ptrEnd' function. Please be
  119. /// careful though!
  120. virtual void putSamples(uint numSamples ///< Number of samples been inserted.
  121. );
  122. /// Output samples from beginning of the sample buffer. Copies requested samples to
  123. /// output buffer and removes them from the sample buffer. If there are less than
  124. /// 'numsample' samples in the buffer, returns all that available.
  125. ///
  126. /// \return Number of samples returned.
  127. virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
  128. uint maxSamples ///< How many samples to receive at max.
  129. );
  130. /// Adjusts book-keeping so that given number of samples are removed from beginning of the
  131. /// sample buffer without copying them anywhere.
  132. ///
  133. /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
  134. /// with 'ptrBegin' function.
  135. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
  136. );
  137. /// Returns number of samples currently available.
  138. virtual uint numSamples() const;
  139. /// Sets number of channels, 1 = mono, 2 = stereo.
  140. void setChannels(int numChannels);
  141. /// Returns nonzero if there aren't any samples available for outputting.
  142. virtual int isEmpty() const;
  143. /// Clears all the samples.
  144. virtual void clear();
  145. /// allow trimming (downwards) amount of samples in pipeline.
  146. /// Returns adjusted amount of samples
  147. uint adjustAmountOfSamples(uint numSamples);
  148. };
  149. }
  150. #endif