tor-browser

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

RateTransposer.h (5102B)


      1 ////////////////////////////////////////////////////////////////////////////////
      2 /// 
      3 /// Sample rate transposer. Changes sample rate by using linear interpolation 
      4 /// together with anti-alias filtering (first order interpolation with anti-
      5 /// alias filtering should be quite adequate for this application).
      6 ///
      7 /// Use either of the derived classes of 'RateTransposerInteger' or 
      8 /// 'RateTransposerFloat' for corresponding integer/floating point tranposing
      9 /// algorithm implementation.
     10 ///
     11 /// Author        : Copyright (c) Olli Parviainen
     12 /// Author e-mail : oparviai 'at' iki.fi
     13 /// SoundTouch WWW: http://www.surina.net/soundtouch
     14 ///
     15 ////////////////////////////////////////////////////////////////////////////////
     16 //
     17 // License :
     18 //
     19 //  SoundTouch audio processing library
     20 //  Copyright (c) Olli Parviainen
     21 //
     22 //  This library is free software; you can redistribute it and/or
     23 //  modify it under the terms of the GNU Lesser General Public
     24 //  License as published by the Free Software Foundation; either
     25 //  version 2.1 of the License, or (at your option) any later version.
     26 //
     27 //  This library is distributed in the hope that it will be useful,
     28 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
     29 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     30 //  Lesser General Public License for more details.
     31 //
     32 //  You should have received a copy of the GNU Lesser General Public
     33 //  License along with this library; if not, write to the Free Software
     34 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     35 //
     36 ////////////////////////////////////////////////////////////////////////////////
     37 
     38 #ifndef RateTransposer_H
     39 #define RateTransposer_H
     40 
     41 #include <stddef.h>
     42 #include "AAFilter.h"
     43 #include "FIFOSamplePipe.h"
     44 #include "FIFOSampleBuffer.h"
     45 
     46 #include "STTypes.h"
     47 
     48 namespace soundtouch
     49 {
     50 
     51 /// Abstract base class for transposer implementations (linear, advanced vs integer, float etc)
     52 class TransposerBase
     53 {
     54 public:
     55        enum ALGORITHM {
     56        LINEAR = 0,
     57        CUBIC,
     58        SHANNON
     59    };
     60 
     61 protected:
     62    virtual int transposeMono(SAMPLETYPE *dest, 
     63                        const SAMPLETYPE *src, 
     64                        int &srcSamples)  = 0;
     65    virtual int transposeStereo(SAMPLETYPE *dest, 
     66                        const SAMPLETYPE *src, 
     67                        int &srcSamples) = 0;
     68    virtual int transposeMulti(SAMPLETYPE *dest, 
     69                        const SAMPLETYPE *src, 
     70                        int &srcSamples) = 0;
     71 
     72    static ALGORITHM algorithm;
     73 
     74 public:
     75    double rate;
     76    int numChannels;
     77 
     78    TransposerBase();
     79    virtual ~TransposerBase();
     80 
     81    virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src);
     82    virtual void setRate(double newRate);
     83    virtual void setChannels(int channels);
     84    virtual int getLatency() const = 0;
     85 
     86    virtual void resetRegisters() = 0;
     87 
     88    // static factory function
     89    static TransposerBase *newInstance();
     90 
     91    // static function to set interpolation algorithm
     92    static void setAlgorithm(ALGORITHM a);
     93 };
     94 
     95 
     96 /// A common linear samplerate transposer class.
     97 ///
     98 class RateTransposer : public FIFOProcessor
     99 {
    100 protected:
    101    /// Anti-alias filter object
    102    AAFilter *pAAFilter;
    103    TransposerBase *pTransposer;
    104 
    105    /// Buffer for collecting samples to feed the anti-alias filter between
    106    /// two batches
    107    FIFOSampleBuffer inputBuffer;
    108 
    109    /// Buffer for keeping samples between transposing & anti-alias filter
    110    FIFOSampleBuffer midBuffer;
    111 
    112    /// Output sample buffer
    113    FIFOSampleBuffer outputBuffer;
    114 
    115    bool bUseAAFilter;
    116 
    117 
    118    /// Transposes sample rate by applying anti-alias filter to prevent folding. 
    119    /// Returns amount of samples returned in the "dest" buffer.
    120    /// The maximum amount of samples that can be returned at a time is set by
    121    /// the 'set_returnBuffer_size' function.
    122    void processSamples(const SAMPLETYPE *src, 
    123                        uint numSamples);
    124 
    125 public:
    126    RateTransposer();
    127    virtual ~RateTransposer();
    128 
    129    /// Returns the output buffer object
    130    FIFOSamplePipe *getOutput() { return &outputBuffer; };
    131 
    132    /// Return anti-alias filter object
    133    AAFilter *getAAFilter();
    134 
    135    /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable
    136    void enableAAFilter(bool newMode);
    137 
    138    /// Returns nonzero if anti-alias filter is enabled.
    139    bool isAAFilterEnabled() const;
    140 
    141    /// Sets new target rate. Normal rate = 1.0, smaller values represent slower 
    142    /// rate, larger faster rates.
    143    virtual void setRate(double newRate);
    144 
    145    /// Sets the number of channels, 1 = mono, 2 = stereo
    146    void setChannels(int channels);
    147 
    148    /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
    149    /// the input of the object.
    150    void putSamples(const SAMPLETYPE *samples, uint numSamples);
    151 
    152    /// Clears all the samples in the object
    153    void clear();
    154 
    155    /// Returns nonzero if there aren't any samples available for outputting.
    156    int isEmpty() const;
    157 
    158    /// Return approximate initial input-output latency
    159    int getLatency() const;
    160 };
    161 
    162 }
    163 
    164 #endif