Arduino STK 4.6.2
LentPitShift.h
1#ifndef STK_LENTPITSHIFT_H
2#define STK_LENTPITSHIFT_H
3
4#include "Effect.h"
5#include "Delay.h"
6
7namespace stk {
8
9/***************************************************/
18/***************************************************/
19
20class LentPitShift : public Effect
21{
22 public:
24 LentPitShift( StkFloat periodRatio = 1.0, int tMax = RT_BUFFER_SIZE );
25
26 ~LentPitShift( void ) {
27 delete window;
28 delete dt;
29 delete dpt;
30 delete cumDt;
31 }
32
34 void clear( void );
35
37 void setShift( StkFloat shift );
38
40 StkFloat tick( StkFloat input );
41
42 // prevent compile errors
43 StkFloat tick( StkFloat input, unsigned int channel) { return tick(input); }
44
46
54 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
55
57
65 StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
66
67 protected:
68
70
74 void process( );
75
76 // Frame storage vectors for process function
77 StkFrames inputFrames;
78 StkFrames outputFrames;
79 int ptrFrames; // writing pointer
80
81 // Input delay line
82 Delay inputLine_;
83 int inputPtr;
84
85 // Output delay line
86 Delay outputLine_;
87 double outputPtr;
88
89 // Pitch tracker variables
90 unsigned long tMax_; // Maximal period measurable by the pitch tracker.
91 // It is also the size of the window used by the pitch tracker and
92 // the size of the frames that can be computed by the tick function
93
94 StkFloat threshold_; // Threshold of detection for the pitch tracker
95 unsigned long lastPeriod_; // Result of the last pitch tracking loop
96 StkFloat* dt; // Array containing the euclidian distance coefficients
97 StkFloat* cumDt; // Array containing the cumulative sum of the coefficients in dt
98 StkFloat* dpt; // Array containing the pitch tracking function coefficients
99
100 // Pitch shifter variables
101 StkFloat env[2]; // Coefficients for the linear interpolation when modifying the output samples
102 StkFloat* window; // Hamming window used for the input portion extraction
103 StkFloat periodRatio_; // Ratio of modification of the signal period
104 StkFrames zeroFrame; // Frame of tMax_ zero samples
105
106
107 // Coefficient delay line that could be used for a dynamic calculation of the pitch
108 //Delay* coeffLine_;
109
110};
111
113{
114 StkFloat x_t; // input coefficient
115 StkFloat x_t_T; // previous input coefficient at T samples
116 StkFloat coeff; // new coefficient for the difference function
117
118 unsigned long alternativePitch = tMax_; // Global minimum storage
119 lastPeriod_ = tMax_+1; // Storage of the lowest local minimum under the threshold
120
121 // Loop variables
122 unsigned long delay_;
123 unsigned int n;
124
125 // Initialization of the dt coefficients. Since the
126 // frames are of tMax_ length, there is no overlapping
127 // between the successive windows where pitch tracking
128 // is performed.
129 for ( delay_=1; delay_<=tMax_; delay_++ )
130 dt[delay_] = 0.;
131
132 // Calculation of the dt coefficients and update of the input delay line.
133 for ( n=0; n<inputFrames.size(); n++ ) {
134 x_t = inputLine_.tick( inputFrames[ n ] );
135 for ( delay_=1; delay_<= tMax_; delay_++ ) {
136 x_t_T = inputLine_.tapOut( delay_ );
137 coeff = x_t - x_t_T;
138 dt[delay_] += coeff * coeff;
139 }
140 }
141
142 // Calculation of the pitch tracking function and test for the minima.
143 for ( delay_=1; delay_<=tMax_; delay_++ ) {
144 cumDt[delay_] = dt[delay_] + cumDt[delay_-1];
145 dpt[delay_] = dt[delay_] * delay_ / cumDt[delay_];
146
147 // Look for a minimum
148 if ( dpt[delay_-1]-dpt[delay_-2] < 0 && dpt[delay_]-dpt[delay_-1] > 0 ) {
149 // Check if the minimum is under the threshold
150 if ( dpt[delay_-1] < threshold_ ){
151 lastPeriod_ = delay_-1;
152 // If a minimum is found, we can stop the loop
153 break;
154 }
155 else if ( dpt[alternativePitch] > dpt[delay_-1] )
156 // Otherwise we store it if it is the current global minimum
157 alternativePitch = delay_-1;
158 }
159 }
160
161 // Test for the last period length.
162 if ( dpt[delay_]-dpt[delay_-1] < 0 ) {
163 if ( dpt[delay_] < threshold_ )
164 lastPeriod_ = delay_;
165 else if ( dpt[alternativePitch] > dpt[delay_] )
166 alternativePitch = delay_;
167 }
168
169 if ( lastPeriod_ == tMax_+1 )
170 // No period has been under the threshold so we used the global minimum
171 lastPeriod_ = alternativePitch;
172
173 // We put the new zero output coefficients in the output delay line and
174 // we get the previous calculated coefficients
175 outputLine_.tick( zeroFrame, outputFrames );
176
177 // Initialization of the Hamming window used in the algorithm
178 for ( int n=-(int)lastPeriod_; n<(int)lastPeriod_; n++ )
179 window[n+lastPeriod_] = (1 + std::cos(STK_PI*n/lastPeriod_)) / 2 ;
180
181 long M; // Index of reading in the input delay line
182 long N; // Index of writing in the output delay line
183 StkFloat sample; // Temporary storage for the new coefficient
184
185 // We loop for all the frames of length lastPeriod_ presents between inputPtr and tMax_
186 for ( ; inputPtr<(int)(tMax_-lastPeriod_); inputPtr+=lastPeriod_ ) {
187 // Test for the decision of compression/expansion
188 while ( outputPtr < inputPtr ) {
189 // Coefficients for the linear interpolation
190 env[1] = std::fmod( outputPtr + tMax_, 1.0 );
191 env[0] = 1.0f - env[1];
192 M = tMax_ - inputPtr + lastPeriod_ - 1; // New reading pointer
193 N = 2*tMax_ - (unsigned long)std::floor(outputPtr + tMax_) + lastPeriod_ - 1; // New writing pointer
194 for ( unsigned int j=0; j<2*lastPeriod_; j++,M--,N-- ) {
195 sample = inputLine_.tapOut(M) * window[j] / 2.f;
196 // Linear interpolation
197 outputLine_.addTo(env[0] * sample, N);
198 outputLine_.addTo(env[1] * sample, N-1);
199 }
200 outputPtr = outputPtr + lastPeriod_ * periodRatio_; // new output pointer
201 }
202 }
203 // Shifting of the pointers waiting for the new frame of length tMax_.
204 outputPtr -= tMax_;
205 inputPtr -= tMax_;
206}
207
208
209inline StkFloat LentPitShift :: tick( StkFloat input )
210{
211 StkFloat sample;
212
213 inputFrames[ptrFrames] = input;
214
215 sample = outputFrames[ptrFrames++];
216
217 // Check for end condition
218 if ( ptrFrames == (int) inputFrames.size() ){
219 ptrFrames = 0;
220 process( );
221 }
222
223 return sample;
224}
225
226inline StkFrames& LentPitShift :: tick( StkFrames& frames, unsigned int channel )
227{
228#if defined(_STK_DEBUG_)
229 if ( channel >= frames.channels() ) {
230 oStream_ << "LentPitShift::tick(): channel and StkFrames arguments are incompatible!";
231 handleError( StkError::FUNCTION_ARGUMENT );
232 }
233#endif
234
235 StkFloat *samples = &frames[channel];
236 unsigned int hop = frames.channels();
237 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
238 *samples = tick( *samples );
239 }
240
241 return frames;
242}
243
244inline StkFrames& LentPitShift :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
245{
246#if defined(_STK_DEBUG_)
247 if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
248 oStream_ << "LentPitShift::tick(): channel and StkFrames arguments are incompatible!";
249 handleError( StkError::FUNCTION_ARGUMENT );
250 }
251#endif
252
253 StkFloat *iSamples = &iFrames[iChannel];
254 StkFloat *oSamples = &oFrames[oChannel];
255 unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
256 for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
257 *oSamples = tick( *iSamples );
258 }
259
260 return iFrames;
261}
262
263} // stk namespace
264
265#endif
266
STK non-interpolating delay line class.
Definition Delay.h:25
Effect(void)
Class constructor.
Definition Effect.h:25
LentPitShift(StkFloat periodRatio=1.0, int tMax=RT_BUFFER_SIZE)
Class constructor.
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition LentPitShift.h:209
void clear(void)
Reset and clear all internal state.
void process()
Apply the effect on the input samples and store it.
Definition LentPitShift.h:112
void setShift(StkFloat shift)
Set the pitch shift factor (1.0 produces no shift).
An STK class to handle vectorized audio data.
Definition Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition ADSR.h:8