1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libsoundtouch/src/RateTransposer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +//////////////////////////////////////////////////////////////////////////////// 1.5 +/// 1.6 +/// Sample rate transposer. Changes sample rate by using linear interpolation 1.7 +/// together with anti-alias filtering (first order interpolation with anti- 1.8 +/// alias filtering should be quite adequate for this application). 1.9 +/// 1.10 +/// Use either of the derived classes of 'RateTransposerInteger' or 1.11 +/// 'RateTransposerFloat' for corresponding integer/floating point tranposing 1.12 +/// algorithm implementation. 1.13 +/// 1.14 +/// Author : Copyright (c) Olli Parviainen 1.15 +/// Author e-mail : oparviai 'at' iki.fi 1.16 +/// SoundTouch WWW: http://www.surina.net/soundtouch 1.17 +/// 1.18 +//////////////////////////////////////////////////////////////////////////////// 1.19 +// 1.20 +// Last changed : $Date: 2014-04-06 10:57:21 -0500 (Sun, 06 Apr 2014) $ 1.21 +// File revision : $Revision: 4 $ 1.22 +// 1.23 +// $Id: RateTransposer.h 195 2014-04-06 15:57:21Z oparviai $ 1.24 +// 1.25 +//////////////////////////////////////////////////////////////////////////////// 1.26 +// 1.27 +// License : 1.28 +// 1.29 +// SoundTouch audio processing library 1.30 +// Copyright (c) Olli Parviainen 1.31 +// 1.32 +// This library is free software; you can redistribute it and/or 1.33 +// modify it under the terms of the GNU Lesser General Public 1.34 +// License as published by the Free Software Foundation; either 1.35 +// version 2.1 of the License, or (at your option) any later version. 1.36 +// 1.37 +// This library is distributed in the hope that it will be useful, 1.38 +// but WITHOUT ANY WARRANTY; without even the implied warranty of 1.39 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.40 +// Lesser General Public License for more details. 1.41 +// 1.42 +// You should have received a copy of the GNU Lesser General Public 1.43 +// License along with this library; if not, write to the Free Software 1.44 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.45 +// 1.46 +//////////////////////////////////////////////////////////////////////////////// 1.47 + 1.48 +#ifndef RateTransposer_H 1.49 +#define RateTransposer_H 1.50 + 1.51 +#include <stddef.h> 1.52 +#include "AAFilter.h" 1.53 +#include "FIFOSamplePipe.h" 1.54 +#include "FIFOSampleBuffer.h" 1.55 + 1.56 +#include "STTypes.h" 1.57 + 1.58 +namespace soundtouch 1.59 +{ 1.60 + 1.61 +/// Abstract base class for transposer implementations (linear, advanced vs integer, float etc) 1.62 +class TransposerBase 1.63 +{ 1.64 +public: 1.65 + enum ALGORITHM { 1.66 + LINEAR = 0, 1.67 + CUBIC, 1.68 + SHANNON 1.69 + }; 1.70 + 1.71 +protected: 1.72 + virtual void resetRegisters() = 0; 1.73 + 1.74 + virtual int transposeMono(SAMPLETYPE *dest, 1.75 + const SAMPLETYPE *src, 1.76 + int &srcSamples) = 0; 1.77 + virtual int transposeStereo(SAMPLETYPE *dest, 1.78 + const SAMPLETYPE *src, 1.79 + int &srcSamples) = 0; 1.80 + virtual int transposeMulti(SAMPLETYPE *dest, 1.81 + const SAMPLETYPE *src, 1.82 + int &srcSamples) = 0; 1.83 + 1.84 + static ALGORITHM algorithm; 1.85 + 1.86 +public: 1.87 + float rate; 1.88 + int numChannels; 1.89 + 1.90 + TransposerBase(); 1.91 + virtual ~TransposerBase(); 1.92 + 1.93 + virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src); 1.94 + virtual void setRate(float newRate); 1.95 + virtual void setChannels(int channels); 1.96 + 1.97 + // static factory function 1.98 + static TransposerBase *newInstance(); 1.99 + 1.100 + // static function to set interpolation algorithm 1.101 + static void setAlgorithm(ALGORITHM a); 1.102 +}; 1.103 + 1.104 + 1.105 +/// A common linear samplerate transposer class. 1.106 +/// 1.107 +class RateTransposer : public FIFOProcessor 1.108 +{ 1.109 +protected: 1.110 + /// Anti-alias filter object 1.111 + AAFilter *pAAFilter; 1.112 + TransposerBase *pTransposer; 1.113 + 1.114 + /// Buffer for collecting samples to feed the anti-alias filter between 1.115 + /// two batches 1.116 + FIFOSampleBuffer inputBuffer; 1.117 + 1.118 + /// Buffer for keeping samples between transposing & anti-alias filter 1.119 + FIFOSampleBuffer midBuffer; 1.120 + 1.121 + /// Output sample buffer 1.122 + FIFOSampleBuffer outputBuffer; 1.123 + 1.124 + bool bUseAAFilter; 1.125 + 1.126 + 1.127 + /// Transposes sample rate by applying anti-alias filter to prevent folding. 1.128 + /// Returns amount of samples returned in the "dest" buffer. 1.129 + /// The maximum amount of samples that can be returned at a time is set by 1.130 + /// the 'set_returnBuffer_size' function. 1.131 + void processSamples(const SAMPLETYPE *src, 1.132 + uint numSamples); 1.133 + 1.134 +public: 1.135 + RateTransposer(); 1.136 + virtual ~RateTransposer(); 1.137 + 1.138 + /// Operator 'new' is overloaded so that it automatically creates a suitable instance 1.139 + /// depending on if we're to use integer or floating point arithmetics. 1.140 +// static void *operator new(size_t s); 1.141 + 1.142 + /// Use this function instead of "new" operator to create a new instance of this class. 1.143 + /// This function automatically chooses a correct implementation, depending on if 1.144 + /// integer ot floating point arithmetics are to be used. 1.145 +// static RateTransposer *newInstance(); 1.146 + 1.147 + /// Returns the output buffer object 1.148 + FIFOSamplePipe *getOutput() { return &outputBuffer; }; 1.149 + 1.150 + /// Returns the store buffer object 1.151 +// FIFOSamplePipe *getStore() { return &storeBuffer; }; 1.152 + 1.153 + /// Return anti-alias filter object 1.154 + AAFilter *getAAFilter(); 1.155 + 1.156 + /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable 1.157 + void enableAAFilter(bool newMode); 1.158 + 1.159 + /// Returns nonzero if anti-alias filter is enabled. 1.160 + bool isAAFilterEnabled() const; 1.161 + 1.162 + /// Sets new target rate. Normal rate = 1.0, smaller values represent slower 1.163 + /// rate, larger faster rates. 1.164 + virtual void setRate(float newRate); 1.165 + 1.166 + /// Sets the number of channels, 1 = mono, 2 = stereo 1.167 + void setChannels(int channels); 1.168 + 1.169 + /// Adds 'numSamples' pcs of samples from the 'samples' memory position into 1.170 + /// the input of the object. 1.171 + void putSamples(const SAMPLETYPE *samples, uint numSamples); 1.172 + 1.173 + /// Clears all the samples in the object 1.174 + void clear(); 1.175 + 1.176 + /// Returns nonzero if there aren't any samples available for outputting. 1.177 + int isEmpty() const; 1.178 +}; 1.179 + 1.180 +} 1.181 + 1.182 +#endif