michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: /// michael@0: /// Sample rate transposer. Changes sample rate by using linear interpolation michael@0: /// together with anti-alias filtering (first order interpolation with anti- michael@0: /// alias filtering should be quite adequate for this application). michael@0: /// michael@0: /// Use either of the derived classes of 'RateTransposerInteger' or michael@0: /// 'RateTransposerFloat' for corresponding integer/floating point tranposing michael@0: /// algorithm implementation. michael@0: /// michael@0: /// Author : Copyright (c) Olli Parviainen michael@0: /// Author e-mail : oparviai 'at' iki.fi michael@0: /// SoundTouch WWW: http://www.surina.net/soundtouch michael@0: /// michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // Last changed : $Date: 2014-04-06 10:57:21 -0500 (Sun, 06 Apr 2014) $ michael@0: // File revision : $Revision: 4 $ michael@0: // michael@0: // $Id: RateTransposer.h 195 2014-04-06 15:57:21Z oparviai $ michael@0: // michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // License : michael@0: // michael@0: // SoundTouch audio processing library michael@0: // Copyright (c) Olli Parviainen michael@0: // michael@0: // This library is free software; you can redistribute it and/or michael@0: // modify it under the terms of the GNU Lesser General Public michael@0: // License as published by the Free Software Foundation; either michael@0: // version 2.1 of the License, or (at your option) any later version. michael@0: // michael@0: // This library is distributed in the hope that it will be useful, michael@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: // Lesser General Public License for more details. michael@0: // michael@0: // You should have received a copy of the GNU Lesser General Public michael@0: // License along with this library; if not, write to the Free Software michael@0: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA michael@0: // michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifndef RateTransposer_H michael@0: #define RateTransposer_H michael@0: michael@0: #include michael@0: #include "AAFilter.h" michael@0: #include "FIFOSamplePipe.h" michael@0: #include "FIFOSampleBuffer.h" michael@0: michael@0: #include "STTypes.h" michael@0: michael@0: namespace soundtouch michael@0: { michael@0: michael@0: /// Abstract base class for transposer implementations (linear, advanced vs integer, float etc) michael@0: class TransposerBase michael@0: { michael@0: public: michael@0: enum ALGORITHM { michael@0: LINEAR = 0, michael@0: CUBIC, michael@0: SHANNON michael@0: }; michael@0: michael@0: protected: michael@0: virtual void resetRegisters() = 0; michael@0: michael@0: virtual int transposeMono(SAMPLETYPE *dest, michael@0: const SAMPLETYPE *src, michael@0: int &srcSamples) = 0; michael@0: virtual int transposeStereo(SAMPLETYPE *dest, michael@0: const SAMPLETYPE *src, michael@0: int &srcSamples) = 0; michael@0: virtual int transposeMulti(SAMPLETYPE *dest, michael@0: const SAMPLETYPE *src, michael@0: int &srcSamples) = 0; michael@0: michael@0: static ALGORITHM algorithm; michael@0: michael@0: public: michael@0: float rate; michael@0: int numChannels; michael@0: michael@0: TransposerBase(); michael@0: virtual ~TransposerBase(); michael@0: michael@0: virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src); michael@0: virtual void setRate(float newRate); michael@0: virtual void setChannels(int channels); michael@0: michael@0: // static factory function michael@0: static TransposerBase *newInstance(); michael@0: michael@0: // static function to set interpolation algorithm michael@0: static void setAlgorithm(ALGORITHM a); michael@0: }; michael@0: michael@0: michael@0: /// A common linear samplerate transposer class. michael@0: /// michael@0: class RateTransposer : public FIFOProcessor michael@0: { michael@0: protected: michael@0: /// Anti-alias filter object michael@0: AAFilter *pAAFilter; michael@0: TransposerBase *pTransposer; michael@0: michael@0: /// Buffer for collecting samples to feed the anti-alias filter between michael@0: /// two batches michael@0: FIFOSampleBuffer inputBuffer; michael@0: michael@0: /// Buffer for keeping samples between transposing & anti-alias filter michael@0: FIFOSampleBuffer midBuffer; michael@0: michael@0: /// Output sample buffer michael@0: FIFOSampleBuffer outputBuffer; michael@0: michael@0: bool bUseAAFilter; michael@0: michael@0: michael@0: /// Transposes sample rate by applying anti-alias filter to prevent folding. michael@0: /// Returns amount of samples returned in the "dest" buffer. michael@0: /// The maximum amount of samples that can be returned at a time is set by michael@0: /// the 'set_returnBuffer_size' function. michael@0: void processSamples(const SAMPLETYPE *src, michael@0: uint numSamples); michael@0: michael@0: public: michael@0: RateTransposer(); michael@0: virtual ~RateTransposer(); michael@0: michael@0: /// Operator 'new' is overloaded so that it automatically creates a suitable instance michael@0: /// depending on if we're to use integer or floating point arithmetics. michael@0: // static void *operator new(size_t s); michael@0: michael@0: /// Use this function instead of "new" operator to create a new instance of this class. michael@0: /// This function automatically chooses a correct implementation, depending on if michael@0: /// integer ot floating point arithmetics are to be used. michael@0: // static RateTransposer *newInstance(); michael@0: michael@0: /// Returns the output buffer object michael@0: FIFOSamplePipe *getOutput() { return &outputBuffer; }; michael@0: michael@0: /// Returns the store buffer object michael@0: // FIFOSamplePipe *getStore() { return &storeBuffer; }; michael@0: michael@0: /// Return anti-alias filter object michael@0: AAFilter *getAAFilter(); michael@0: michael@0: /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable michael@0: void enableAAFilter(bool newMode); michael@0: michael@0: /// Returns nonzero if anti-alias filter is enabled. michael@0: bool isAAFilterEnabled() const; michael@0: michael@0: /// Sets new target rate. Normal rate = 1.0, smaller values represent slower michael@0: /// rate, larger faster rates. michael@0: virtual void setRate(float newRate); michael@0: michael@0: /// Sets the number of channels, 1 = mono, 2 = stereo michael@0: void setChannels(int channels); michael@0: michael@0: /// Adds 'numSamples' pcs of samples from the 'samples' memory position into michael@0: /// the input of the object. michael@0: void putSamples(const SAMPLETYPE *samples, uint numSamples); michael@0: michael@0: /// Clears all the samples in the object michael@0: void clear(); michael@0: michael@0: /// Returns nonzero if there aren't any samples available for outputting. michael@0: int isEmpty() const; michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif