tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FIFOSampleBuffer.h (7137B)


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