The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

Public Member Functions | Protected Member Functions | List of all members
stk::LentPitShift Class Reference

Pitch shifter effect class based on the Lent algorithm. More...

`#include <LentPitShift.h>`

Inheritance diagram for stk::LentPitShift:

## Public Member Functions
| LentPitShift (StkFloat periodRatio=1.0, int tMax=RT_BUFFER_SIZE)
| Class constructor.
void | clear (void)
| Reset and clear all internal state.
void | setShift (StkFloat shift)
| Set the pitch shift factor (1.0 produces no shift).
StkFloat | tick (StkFloat input)
| Input one sample to the filter and return one output.
StkFrames & | tick (StkFrames &frames, unsigned int channel=0)
| Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs. More...
StkFrames & | tick (StkFrames &iFrames, StkFrames &oFrames, unsigned int iChannel=0, unsigned int oChannel=0)
| Take a channel of the `iFrames` object as inputs to the filter and write outputs to the `oFrames` object. More...
Public Member Functions inherited from stk::Effect
| Effect (void)
| Class constructor.
unsigned int | channelsOut (void) const
| Return the number of output channels for the class.
const StkFrames & | lastFrame (void) const
| Return an StkFrames reference to the last output sample frame.
virtual void | setEffectMix (StkFloat mix)
| Set the mixture of input and "effected" levels in the output (0.0 = input only, 1.0 = effect only).
Public Member Functions inherited from stk::Stk
void | ignoreSampleRateChange (bool ignore=true)
| A function to enable/disable the automatic updating of class data when the STK sample rate changes. More...

## Protected Member Functions
void | process ()
| Apply the effect on the input samples and store it. More...

## Additional Inherited Members
Static Public Member Functions inherited from stk::Stk
static StkFloat | sampleRate (void)
| Static method that returns the current STK sample rate.
static void | setSampleRate (StkFloat rate)
| Static method that sets the STK sample rate. More...
static void | clear_alertList ()
| Static method that frees memory from alertList_.
static std::string | rawwavePath (void)
| Static method that returns the current rawwave path.
static void | setRawwavePath (std::string path)
| Static method that sets the STK rawwave path.
static void | swap16 (unsigned char *ptr)
| Static method that byte-swaps a 16-bit data type.
static void | swap32 (unsigned char *ptr)
| Static method that byte-swaps a 32-bit data type.
static void | swap64 (unsigned char *ptr)
| Static method that byte-swaps a 64-bit data type.
static void | sleep (unsigned long milliseconds)
| Static cross-platform method to sleep for a number of milliseconds.
static bool | inRange (StkFloat value, StkFloat min, StkFloat max)
| Static method to check whether a value is within a specified range.
static void | handleError (const char *message, StkError::Type type)
| Static function for error reporting and handling using c-strings.
static void | handleError (std::string message, StkError::Type type)
| Static function for error reporting and handling using c++ strings.
static void | showWarnings (bool status)
| Toggle display of WARNING and STATUS messages.
static void | printErrors (bool status)
| Toggle display of error messages before throwing exceptions.
Static Public Attributes inherited from stk::Stk
static const StkFormat | STK_SINT8
static const StkFormat | STK_SINT16
static const StkFormat | STK_SINT24
static const StkFormat | STK_SINT32
static const StkFormat | STK_FLOAT32
static const StkFormat | STK_FLOAT64

## Detailed Description

Pitch shifter effect class based on the Lent algorithm.

This class implements a pitch shifter using pitch tracking and sample windowing and shifting.

by Francois Germain, 2009.

## Member Function Documentation

StkFrames & stk::LentPitShift::tick | ( | StkFrames & | frames,
|  | unsigned int | channel = `0`
| ) |  |
| inline

Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.

The StkFrames argument reference is returned. The `channel` argument must be less than the number of channels in the StkFrames argument (the first channel is specified by 0). However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

224 {
225 #if defined(_STK_DEBUG_)
226 if ( channel >= frames.channels() ) {
227 oStream_ << "LentPitShift::tick(): channel and StkFrames arguments are incompatible!";
228 handleError( StkError::FUNCTION_ARGUMENT );
229 }
230 #endif
231
232 StkFloat *samples = &frames[channel];
233 unsigned int hop = frames.channels();
234 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
235 *samples = tick( *samples );
236 }
237
238 return frames;
239 }

StkFrames & stk::LentPitShift::tick | ( | StkFrames & | iFrames,
|  | StkFrames & | oFrames,
|  | unsigned int | iChannel = `0`,
|  | unsigned int | oChannel = `0`
| ) |  |
| inline

Take a channel of the `iFrames` object as inputs to the filter and write outputs to the `oFrames` object.

The `iFrames` object reference is returned. Each channel argument must be less than the number of channels in the corresponding StkFrames argument (the first channel is specified by 0). However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

242 {
243 #if defined(_STK_DEBUG_)
244 if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
245 oStream_ << "LentPitShift::tick(): channel and StkFrames arguments are incompatible!";
246 handleError( StkError::FUNCTION_ARGUMENT );
247 }
248 #endif
249
250 StkFloat *iSamples = &iFrames[iChannel];
251 StkFloat *oSamples = &oFrames[oChannel];
252 unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
253 for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
254 *oSamples = tick( *iSamples );
255 }
256
257 return iFrames;
258 }

void stk::LentPitShift::process | ( |  | ) |
| inlineprotected

Apply the effect on the input samples and store it.

The samples stored in the input frame vector are processed and the delayed result are stored in the output frame vector.

110 {
111 StkFloat x_t; // input coefficient
112 StkFloat x_t_T; // previous input coefficient at T samples
113 StkFloat coeff; // new coefficient for the difference function
114
115 unsigned long alternativePitch = tMax_; // Global minimum storage
116 lastPeriod_ = tMax_+1; // Storage of the lowest local minimum under the threshold
117
118 // Loop variables
119 unsigned long delay_;
120 unsigned int n;
121
122 // Initialization of the dt coefficients. Since the
123 // frames are of tMax_ length, there is no overlapping
124 // between the successive windows where pitch tracking
125 // is performed.
126 for ( delay_=1; delay_<=tMax_; delay_++ )
127 dt[delay_] = 0.;
128
129 // Calculation of the dt coefficients and update of the input delay line.
130 for ( n=0; n<inputFrames.size(); n++ ) {
131 x_t = inputLine_.tick( inputFrames[ n ] );
132 for ( delay_=1; delay_<= tMax_; delay_++ ) {
133 x_t_T = inputLine_.tapOut( delay_ );
134 coeff = x_t - x_t_T;
135 dt[delay_] += coeff * coeff;
136 }
137 }
138
139 // Calculation of the pitch tracking function and test for the minima.
140 for ( delay_=1; delay_<=tMax_; delay_++ ) {
141 cumDt[delay_] = dt[delay_] + cumDt[delay_-1];
142 dpt[delay_] = dt[delay_] * delay_ / cumDt[delay_];
143
144 // Look for a minimum
145 if ( dpt[delay_-1]-dpt[delay_-2] < 0 && dpt[delay_]-dpt[delay_-1] > 0 ) {
146 // Check if the minimum is under the threshold
147 if ( dpt[delay_-1] < threshold_ ){
148 lastPeriod_ = delay_-1;
149 // If a minimum is found, we can stop the loop
150 break;
151 }
152 else if ( dpt[alternativePitch] > dpt[delay_-1] )
153 // Otherwise we store it if it is the current global minimum
154 alternativePitch = delay_-1;
155 }
156 }
157
158 // Test for the last period length.
159 if ( dpt[delay_]-dpt[delay_-1] < 0 ) {
160 if ( dpt[delay_] < threshold_ )
161 lastPeriod_ = delay_;
162 else if ( dpt[alternativePitch] > dpt[delay_] )
163 alternativePitch = delay_;
164 }
165
166 if ( lastPeriod_ == tMax_+1 )
167 // No period has been under the threshold so we used the global minimum
168 lastPeriod_ = alternativePitch;
169
170 // We put the new zero output coefficients in the output delay line and
171 // we get the previous calculated coefficients
172 outputLine_.tick( zeroFrame, outputFrames );
173
174 // Initialization of the Hamming window used in the algorithm
175 for ( int n=-(int)lastPeriod_; n<(int)lastPeriod_; n++ )
176 window[n+lastPeriod_] = (1 + cos(PI*n/lastPeriod_)) / 2 ;
177
178 long M; // Index of reading in the input delay line
179 long N; // Index of writing in the output delay line
180 double sample; // Temporary storage for the new coefficient
181
182 // We loop for all the frames of length lastPeriod_ presents between inputPtr and tMax_
183 for ( ; inputPtr<(int)(tMax_-lastPeriod_); inputPtr+=lastPeriod_ ) {
184 // Test for the decision of compression/expansion
185 while ( outputPtr < inputPtr ) {
186 // Coefficients for the linear interpolation
187 env[1] = fmod( outputPtr + tMax_, 1.0 );
188 env[0] = 1.0 - env[1];
189 M = tMax_ - inputPtr + lastPeriod_ - 1; // New reading pointer
190 N = 2*tMax_ - (unsigned long)floor(outputPtr + tMax_) + lastPeriod_ - 1; // New writing pointer
191 for ( unsigned int j=0; j<2*lastPeriod_; j++,M--,N-- ) {
192 sample = inputLine_.tapOut(M) * window[j] / 2.;
193 // Linear interpolation
194 outputLine_.addTo(env[0] * sample, N);
195 outputLine_.addTo(env[1] * sample, N-1);
196 }
197 outputPtr = outputPtr + lastPeriod_ * periodRatio_; // new output pointer
198 }
199 }
200 // Shifting of the pointers waiting for the new frame of length tMax_.
201 outputPtr -= tMax_;
202 inputPtr -= tMax_;
203 }

The documentation for this class was generated from the following file:

- LentPitShift.h

The Synthesis ToolKit in C++ (STK)
©1995--2017 Perry R. Cook and Gary P. Scavone. All Rights Reserved.