* bugfix from http://tracker.ardour.org/view.php?id=2155 thanks to kristian: fix...
[ardour.git] / libs / soundtouch / FIFOSampleBuffer.h
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 @ iki.fi
14 /// SoundTouch WWW: http://www.iki.fi/oparviai/soundtouch
15 ///
16 ////////////////////////////////////////////////////////////////////////////////
17 //
18 // Last changed  : $Date$
19 // File revision : $Revision$
20 //
21 // $Id$
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
46 #ifndef FIFOSampleBuffer_H
47 #define FIFOSampleBuffer_H
48
49 #include "FIFOSamplePipe.h"
50
51 namespace soundtouch
52 {
53
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.
56 ///
57 /// Notice that in case of stereo audio, one sample is considered to consist of 
58 /// both channel data.
59 class FIFOSampleBuffer : public FIFOSamplePipe
60 {
61 private:
62     /// Sample buffer.
63     SAMPLETYPE *buffer;
64
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;
68
69     /// Sample buffer size in bytes
70     uint sizeInBytes;
71
72     /// How many samples are currently in buffer.
73     uint samplesInBuffer;
74
75     /// Channels, 1=mono, 2=stereo.
76     uint channels;
77
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.
81     uint bufferPos;
82
83     /// Rewind the buffer by moving data from position pointed by 'bufferPos' to real 
84     /// beginning of the buffer.
85     void rewind();
86
87     /// Ensures that the buffer has capacity for at least this many samples.
88     void ensureCapacity(const uint capacityRequirement);
89
90     /// Returns current capacity.
91     uint getCapacity() const;
92  
93 public:
94
95     /// Constructor
96     FIFOSampleBuffer(uint numChannels = 2     ///< Number of channels, 1=mono, 2=stereo.
97                                               ///< Default is stereo.
98                      );
99
100     /// destructor
101     virtual ~FIFOSampleBuffer();
102
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!
106     ///
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;
111
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!
116     ///
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.
120     SAMPLETYPE *ptrEnd(
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.
125                 );
126
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.
131                             );
132
133     /// Adjusts the book-keeping to increase number of samples in the buffer without 
134     /// copying any actual samples.
135     ///
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 
138     /// careful though!
139     virtual void putSamples(uint numSamples   ///< Number of samples been inserted.
140                             );
141
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.
145     ///
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.
149                                 );
150
151     /// Adjusts book-keeping so that given number of samples are removed from beginning of the 
152     /// sample buffer without copying them anywhere. 
153     ///
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.
157                                 );
158
159     /// Returns number of samples currently available.
160     virtual uint numSamples() const;
161
162     /// Sets number of channels, 1 = mono, 2 = stereo.
163     void setChannels(uint numChannels);
164
165     /// Returns nonzero if there aren't any samples available for outputting.
166     virtual int isEmpty() const;
167
168     /// Clears all the samples.
169     virtual void clear();
170 };
171
172 }
173
174 #endif