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