1 ////////////////////////////////////////////////////////////////////////////////
3 /// A buffer class for temporarily storaging sound samples, operates as a
4 /// first-in-first-out pipe.
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.
12 /// Author : Copyright (c) Olli Parviainen
13 /// Author e-mail : oparviai @ iki.fi
14 /// SoundTouch WWW: http://www.iki.fi/oparviai/soundtouch
16 ////////////////////////////////////////////////////////////////////////////////
18 // Last changed : $Date$
19 // File revision : $Revision$
23 ////////////////////////////////////////////////////////////////////////////////
27 // SoundTouch audio processing library
28 // Copyright (c) Olli Parviainen
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.
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.
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
44 ////////////////////////////////////////////////////////////////////////////////
46 #ifndef FIFOSampleBuffer_H
47 #define FIFOSampleBuffer_H
49 #include "FIFOSamplePipe.h"
54 /// Sample buffer working in FIFO (first-in-first-out) principle. The class takes
55 /// care of storage size adjustment and data moving during input/output operations.
57 /// Notice that in case of stereo audio, one sample is considered to consist of
58 /// both channel data.
59 class FIFOSampleBuffer : public FIFOSamplePipe
65 // Raw unaligned buffer memory. 'buffer' is made aligned by pointing it to first
66 // 16-byte aligned location of this buffer
67 SAMPLETYPE *bufferUnaligned;
69 /// Sample buffer size in bytes
72 /// How many samples are currently in buffer.
75 /// Channels, 1=mono, 2=stereo.
78 /// Current position pointer to the buffer. This pointer is increased when samples are
79 /// removed from the pipe so that it's necessary to actually rewind buffer (move data)
80 /// only new data when is put to the pipe.
83 /// Rewind the buffer by moving data from position pointed by 'bufferPos' to real
84 /// beginning of the buffer.
87 /// Ensures that the buffer has capacity for at least this many samples.
88 void ensureCapacity(const uint capacityRequirement);
90 /// Returns current capacity.
91 uint getCapacity() const;
96 FIFOSampleBuffer(uint numChannels = 2 ///< Number of channels, 1=mono, 2=stereo.
97 ///< Default is stereo.
101 virtual ~FIFOSampleBuffer();
103 /// Returns a pointer to the beginning of the output samples.
104 /// This function is provided for accessing the output samples directly.
105 /// Please be careful for not to corrupt the book-keeping!
107 /// When using this function to output samples, also remember to 'remove' the
108 /// output samples from the buffer by calling the
109 /// 'receiveSamples(numSamples)' function
110 virtual SAMPLETYPE *ptrBegin() const;
112 /// Returns a pointer to the end of the used part of the sample buffer (i.e.
113 /// where the new samples are to be inserted). This function may be used for
114 /// inserting new samples into the sample buffer directly. Please be careful
115 /// not corrupt the book-keeping!
117 /// When using this function as means for inserting new samples, also remember
118 /// to increase the sample count afterwards, by calling the
119 /// 'putSamples(numSamples)' function.
121 uint slackCapacity ///< How much free capacity (in samples) there _at least_
122 ///< should be so that the caller can succesfully insert the
123 ///< desired samples to the buffer. If necessary, the function
124 ///< grows the buffer size to comply with this requirement.
127 /// Adds 'numSamples' pcs of samples from the 'samples' memory position to
128 /// the sample buffer.
129 virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples.
130 uint numSamples ///< Number of samples to insert.
133 /// Adjusts the book-keeping to increase number of samples in the buffer without
134 /// copying any actual samples.
136 /// This function is used to update the number of samples in the sample buffer
137 /// when accessing the buffer directly with 'ptrEnd' function. Please be
139 virtual void putSamples(uint numSamples ///< Number of samples been inserted.
142 /// Output samples from beginning of the sample buffer. Copies requested samples to
143 /// output buffer and removes them from the sample buffer. If there are less than
144 /// 'numsample' samples in the buffer, returns all that available.
146 /// \return Number of samples returned.
147 virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
148 uint maxSamples ///< How many samples to receive at max.
151 /// Adjusts book-keeping so that given number of samples are removed from beginning of the
152 /// sample buffer without copying them anywhere.
154 /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
155 /// with 'ptrBegin' function.
156 virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
159 /// Returns number of samples currently available.
160 virtual uint numSamples() const;
162 /// Sets number of channels, 1 = mono, 2 = stereo.
163 void setChannels(uint numChannels);
165 /// Returns nonzero if there aren't any samples available for outputting.
166 virtual int isEmpty() const;
168 /// Clears all the samples.
169 virtual void clear();