media/libsoundtouch/src/FIFOSampleBuffer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libsoundtouch/src/FIFOSampleBuffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +////////////////////////////////////////////////////////////////////////////////
     1.5 +///
     1.6 +/// A buffer class for temporarily storaging sound samples, operates as a 
     1.7 +/// first-in-first-out pipe.
     1.8 +///
     1.9 +/// Samples are added to the end of the sample buffer with the 'putSamples' 
    1.10 +/// function, and are received from the beginning of the buffer by calling
    1.11 +/// the 'receiveSamples' function. The class automatically removes the 
    1.12 +/// output samples from the buffer as well as grows the storage size 
    1.13 +/// whenever necessary.
    1.14 +///
    1.15 +/// Author        : Copyright (c) Olli Parviainen
    1.16 +/// Author e-mail : oparviai 'at' iki.fi
    1.17 +/// SoundTouch WWW: http://www.surina.net/soundtouch
    1.18 +///
    1.19 +////////////////////////////////////////////////////////////////////////////////
    1.20 +//
    1.21 +// Last changed  : $Date: 2014-01-05 15:40:22 -0600 (Sun, 05 Jan 2014) $
    1.22 +// File revision : $Revision: 4 $
    1.23 +//
    1.24 +// $Id: FIFOSampleBuffer.h 177 2014-01-05 21:40:22Z oparviai $
    1.25 +//
    1.26 +////////////////////////////////////////////////////////////////////////////////
    1.27 +//
    1.28 +// License :
    1.29 +//
    1.30 +//  SoundTouch audio processing library
    1.31 +//  Copyright (c) Olli Parviainen
    1.32 +//
    1.33 +//  This library is free software; you can redistribute it and/or
    1.34 +//  modify it under the terms of the GNU Lesser General Public
    1.35 +//  License as published by the Free Software Foundation; either
    1.36 +//  version 2.1 of the License, or (at your option) any later version.
    1.37 +//
    1.38 +//  This library is distributed in the hope that it will be useful,
    1.39 +//  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.40 +//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.41 +//  Lesser General Public License for more details.
    1.42 +//
    1.43 +//  You should have received a copy of the GNU Lesser General Public
    1.44 +//  License along with this library; if not, write to the Free Software
    1.45 +//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.46 +//
    1.47 +////////////////////////////////////////////////////////////////////////////////
    1.48 +
    1.49 +#ifndef FIFOSampleBuffer_H
    1.50 +#define FIFOSampleBuffer_H
    1.51 +
    1.52 +#include "FIFOSamplePipe.h"
    1.53 +
    1.54 +namespace soundtouch
    1.55 +{
    1.56 +
    1.57 +/// Sample buffer working in FIFO (first-in-first-out) principle. The class takes
    1.58 +/// care of storage size adjustment and data moving during input/output operations.
    1.59 +///
    1.60 +/// Notice that in case of stereo audio, one sample is considered to consist of 
    1.61 +/// both channel data.
    1.62 +class FIFOSampleBuffer : public FIFOSamplePipe
    1.63 +{
    1.64 +private:
    1.65 +    /// Sample buffer.
    1.66 +    SAMPLETYPE *buffer;
    1.67 +
    1.68 +    // Raw unaligned buffer memory. 'buffer' is made aligned by pointing it to first
    1.69 +    // 16-byte aligned location of this buffer
    1.70 +    SAMPLETYPE *bufferUnaligned;
    1.71 +
    1.72 +    /// Sample buffer size in bytes
    1.73 +    uint sizeInBytes;
    1.74 +
    1.75 +    /// How many samples are currently in buffer.
    1.76 +    uint samplesInBuffer;
    1.77 +
    1.78 +    /// Channels, 1=mono, 2=stereo.
    1.79 +    uint channels;
    1.80 +
    1.81 +    /// Current position pointer to the buffer. This pointer is increased when samples are 
    1.82 +    /// removed from the pipe so that it's necessary to actually rewind buffer (move data)
    1.83 +    /// only new data when is put to the pipe.
    1.84 +    uint bufferPos;
    1.85 +
    1.86 +    /// Rewind the buffer by moving data from position pointed by 'bufferPos' to real 
    1.87 +    /// beginning of the buffer.
    1.88 +    void rewind();
    1.89 +
    1.90 +    /// Ensures that the buffer has capacity for at least this many samples.
    1.91 +    void ensureCapacity(uint capacityRequirement);
    1.92 +
    1.93 +    /// Returns current capacity.
    1.94 +    uint getCapacity() const;
    1.95 +
    1.96 +public:
    1.97 +
    1.98 +    /// Constructor
    1.99 +    FIFOSampleBuffer(int numChannels = 2     ///< Number of channels, 1=mono, 2=stereo.
   1.100 +                                              ///< Default is stereo.
   1.101 +                     );
   1.102 +
   1.103 +    /// destructor
   1.104 +    ~FIFOSampleBuffer();
   1.105 +
   1.106 +    /// Returns a pointer to the beginning of the output samples. 
   1.107 +    /// This function is provided for accessing the output samples directly. 
   1.108 +    /// Please be careful for not to corrupt the book-keeping!
   1.109 +    ///
   1.110 +    /// When using this function to output samples, also remember to 'remove' the
   1.111 +    /// output samples from the buffer by calling the 
   1.112 +    /// 'receiveSamples(numSamples)' function
   1.113 +    virtual SAMPLETYPE *ptrBegin();
   1.114 +
   1.115 +    /// Returns a pointer to the end of the used part of the sample buffer (i.e. 
   1.116 +    /// where the new samples are to be inserted). This function may be used for 
   1.117 +    /// inserting new samples into the sample buffer directly. Please be careful
   1.118 +    /// not corrupt the book-keeping!
   1.119 +    ///
   1.120 +    /// When using this function as means for inserting new samples, also remember 
   1.121 +    /// to increase the sample count afterwards, by calling  the 
   1.122 +    /// 'putSamples(numSamples)' function.
   1.123 +    SAMPLETYPE *ptrEnd(
   1.124 +                uint slackCapacity   ///< How much free capacity (in samples) there _at least_ 
   1.125 +                                     ///< should be so that the caller can succesfully insert the 
   1.126 +                                     ///< desired samples to the buffer. If necessary, the function 
   1.127 +                                     ///< grows the buffer size to comply with this requirement.
   1.128 +                );
   1.129 +
   1.130 +    /// Adds 'numSamples' pcs of samples from the 'samples' memory position to
   1.131 +    /// the sample buffer.
   1.132 +    virtual void putSamples(const SAMPLETYPE *samples,  ///< Pointer to samples.
   1.133 +                            uint numSamples                         ///< Number of samples to insert.
   1.134 +                            );
   1.135 +
   1.136 +    /// Adjusts the book-keeping to increase number of samples in the buffer without 
   1.137 +    /// copying any actual samples.
   1.138 +    ///
   1.139 +    /// This function is used to update the number of samples in the sample buffer
   1.140 +    /// when accessing the buffer directly with 'ptrEnd' function. Please be 
   1.141 +    /// careful though!
   1.142 +    virtual void putSamples(uint numSamples   ///< Number of samples been inserted.
   1.143 +                            );
   1.144 +
   1.145 +    /// Output samples from beginning of the sample buffer. Copies requested samples to 
   1.146 +    /// output buffer and removes them from the sample buffer. If there are less than 
   1.147 +    /// 'numsample' samples in the buffer, returns all that available.
   1.148 +    ///
   1.149 +    /// \return Number of samples returned.
   1.150 +    virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
   1.151 +                                uint maxSamples                 ///< How many samples to receive at max.
   1.152 +                                );
   1.153 +
   1.154 +    /// Adjusts book-keeping so that given number of samples are removed from beginning of the 
   1.155 +    /// sample buffer without copying them anywhere. 
   1.156 +    ///
   1.157 +    /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
   1.158 +    /// with 'ptrBegin' function.
   1.159 +    virtual uint receiveSamples(uint maxSamples   ///< Remove this many samples from the beginning of pipe.
   1.160 +                                );
   1.161 +
   1.162 +    /// Returns number of samples currently available.
   1.163 +    virtual uint numSamples() const;
   1.164 +
   1.165 +    /// Sets number of channels, 1 = mono, 2 = stereo.
   1.166 +    void setChannels(int numChannels);
   1.167 +
   1.168 +    /// Get number of channels
   1.169 +    int getChannels() 
   1.170 +    {
   1.171 +        return channels;
   1.172 +    }
   1.173 +
   1.174 +    /// Returns nonzero if there aren't any samples available for outputting.
   1.175 +    virtual int isEmpty() const;
   1.176 +
   1.177 +    /// Clears all the samples.
   1.178 +    virtual void clear();
   1.179 +
   1.180 +    /// allow trimming (downwards) amount of samples in pipeline.
   1.181 +    /// Returns adjusted amount of samples
   1.182 +    uint adjustAmountOfSamples(uint numSamples);
   1.183 +};
   1.184 +
   1.185 +}
   1.186 +
   1.187 +#endif

mercurial