You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.7KB

  1. #ifndef STK_LENTPITSHIFT_H
  2. #define STK_LENTPITSHIFT_H
  3. #include "Effect.h"
  4. #include "Delay.h"
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class LentPitShift
  8. \brief Pitch shifter effect class based on the Lent algorithm.
  9. This class implements a pitch shifter using pitch
  10. tracking and sample windowing and shifting.
  11. by Francois Germain, 2009.
  12. */
  13. /***************************************************/
  14. class LentPitShift : public Effect
  15. {
  16. public:
  17. //! Class constructor.
  18. LentPitShift( StkFloat periodRatio = 1.0, int tMax = RT_BUFFER_SIZE );
  19. ~LentPitShift( void ) {
  20. delete window;
  21. delete dt;
  22. delete dpt;
  23. delete cumDt;
  24. }
  25. //! Reset and clear all internal state.
  26. void clear( void );
  27. //! Set the pitch shift factor (1.0 produces no shift).
  28. void setShift( StkFloat shift );
  29. //! Input one sample to the filter and return one output.
  30. StkFloat tick( StkFloat input );
  31. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  32. /*!
  33. The StkFrames argument reference is returned. The \c channel
  34. argument must be less than the number of channels in the
  35. StkFrames argument (the first channel is specified by 0).
  36. However, range checking is only performed if _STK_DEBUG_ is
  37. defined during compilation, in which case an out-of-range value
  38. will trigger an StkError exception.
  39. */
  40. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  41. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  42. /*!
  43. The \c iFrames object reference is returned. Each channel
  44. argument must be less than the number of channels in the
  45. corresponding StkFrames argument (the first channel is specified
  46. by 0). However, range checking is only performed if _STK_DEBUG_
  47. is defined during compilation, in which case an out-of-range value
  48. will trigger an StkError exception.
  49. */
  50. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  51. protected:
  52. //! Apply the effect on the input samples and store it.
  53. /*!
  54. The samples stored in the input frame vector are processed
  55. and the delayed result are stored in the output frame vector.
  56. */
  57. void process( );
  58. // Frame storage vectors for process function
  59. StkFrames inputFrames;
  60. StkFrames outputFrames;
  61. int ptrFrames; // writing pointer
  62. // Input delay line
  63. Delay inputLine_;
  64. int inputPtr;
  65. // Output delay line
  66. Delay outputLine_;
  67. double outputPtr;
  68. // Pitch tracker variables
  69. unsigned long tMax_; // Maximal period measurable by the pitch tracker.
  70. // It is also the size of the window used by the pitch tracker and
  71. // the size of the frames that can be computed by the tick function
  72. StkFloat threshold_; // Threshold of detection for the pitch tracker
  73. unsigned long lastPeriod_; // Result of the last pitch tracking loop
  74. StkFloat* dt; // Array containing the euclidian distance coefficients
  75. StkFloat* cumDt; // Array containing the cumulative sum of the coefficients in dt
  76. StkFloat* dpt; // Array containing the pitch tracking function coefficients
  77. // Pitch shifter variables
  78. StkFloat env[2]; // Coefficients for the linear interpolation when modifying the output samples
  79. StkFloat* window; // Hamming window used for the input portion extraction
  80. double periodRatio_; // Ratio of modification of the signal period
  81. StkFrames zeroFrame; // Frame of tMax_ zero samples
  82. // Coefficient delay line that could be used for a dynamic calculation of the pitch
  83. //Delay* coeffLine_;
  84. };
  85. inline void LentPitShift::process()
  86. {
  87. StkFloat x_t; // input coefficient
  88. StkFloat x_t_T; // previous input coefficient at T samples
  89. StkFloat coeff; // new coefficient for the difference function
  90. unsigned long alternativePitch = tMax_; // Global minimum storage
  91. lastPeriod_ = tMax_+1; // Storage of the lowest local minimum under the threshold
  92. // Loop variables
  93. unsigned long delay_;
  94. unsigned int n;
  95. // Initialization of the dt coefficients. Since the
  96. // frames are of tMax_ length, there is no overlapping
  97. // between the successive windows where pitch tracking
  98. // is performed.
  99. for ( delay_=1; delay_<=tMax_; delay_++ )
  100. dt[delay_] = 0.;
  101. // Calculation of the dt coefficients and update of the input delay line.
  102. for ( n=0; n<inputFrames.size(); n++ ) {
  103. x_t = inputLine_.tick( inputFrames[ n ] );
  104. for ( delay_=1; delay_<= tMax_; delay_++ ) {
  105. x_t_T = inputLine_.tapOut( delay_ );
  106. coeff = x_t - x_t_T;
  107. dt[delay_] += coeff * coeff;
  108. }
  109. }
  110. // Calculation of the pitch tracking function and test for the minima.
  111. for ( delay_=1; delay_<=tMax_; delay_++ ) {
  112. cumDt[delay_] = dt[delay_] + cumDt[delay_-1];
  113. dpt[delay_] = dt[delay_] * delay_ / cumDt[delay_];
  114. // Look for a minimum
  115. if ( dpt[delay_-1]-dpt[delay_-2] < 0 && dpt[delay_]-dpt[delay_-1] > 0 ) {
  116. // Check if the minimum is under the threshold
  117. if ( dpt[delay_-1] < threshold_ ){
  118. lastPeriod_ = delay_-1;
  119. // If a minimum is found, we can stop the loop
  120. break;
  121. }
  122. else if ( dpt[alternativePitch] > dpt[delay_-1] )
  123. // Otherwise we store it if it is the current global minimum
  124. alternativePitch = delay_-1;
  125. }
  126. }
  127. // Test for the last period length.
  128. if ( dpt[delay_]-dpt[delay_-1] < 0 ) {
  129. if ( dpt[delay_] < threshold_ )
  130. lastPeriod_ = delay_;
  131. else if ( dpt[alternativePitch] > dpt[delay_] )
  132. alternativePitch = delay_;
  133. }
  134. if ( lastPeriod_ == tMax_+1 )
  135. // No period has been under the threshold so we used the global minimum
  136. lastPeriod_ = alternativePitch;
  137. // We put the new zero output coefficients in the output delay line and
  138. // we get the previous calculated coefficients
  139. outputLine_.tick( zeroFrame, outputFrames );
  140. // Initialization of the Hamming window used in the algorithm
  141. for ( int n=-(int)lastPeriod_; n<(int)lastPeriod_; n++ )
  142. window[n+lastPeriod_] = (1 + cos(PI*n/lastPeriod_)) / 2 ;
  143. long M; // Index of reading in the input delay line
  144. long N; // Index of writing in the output delay line
  145. double sample; // Temporary storage for the new coefficient
  146. // We loop for all the frames of length lastPeriod_ presents between inputPtr and tMax_
  147. for ( ; inputPtr<(int)(tMax_-lastPeriod_); inputPtr+=lastPeriod_ ) {
  148. // Test for the decision of compression/expansion
  149. while ( outputPtr < inputPtr ) {
  150. // Coefficients for the linear interpolation
  151. env[1] = fmod( outputPtr + tMax_, 1.0 );
  152. env[0] = 1.0 - env[1];
  153. M = tMax_ - inputPtr + lastPeriod_ - 1; // New reading pointer
  154. N = 2*tMax_ - (unsigned long)floor(outputPtr + tMax_) + lastPeriod_ - 1; // New writing pointer
  155. for ( unsigned int j=0; j<2*lastPeriod_; j++,M--,N-- ) {
  156. sample = inputLine_.tapOut(M) * window[j] / 2.;
  157. // Linear interpolation
  158. outputLine_.addTo(env[0] * sample, N);
  159. outputLine_.addTo(env[1] * sample, N-1);
  160. }
  161. outputPtr = outputPtr + lastPeriod_ * periodRatio_; // new output pointer
  162. }
  163. }
  164. // Shifting of the pointers waiting for the new frame of length tMax_.
  165. outputPtr -= tMax_;
  166. inputPtr -= tMax_;
  167. }
  168. inline StkFloat LentPitShift :: tick( StkFloat input )
  169. {
  170. StkFloat sample;
  171. inputFrames[ptrFrames] = input;
  172. sample = outputFrames[ptrFrames++];
  173. // Check for end condition
  174. if ( ptrFrames == (int) inputFrames.size() ){
  175. ptrFrames = 0;
  176. process( );
  177. }
  178. return sample;
  179. }
  180. inline StkFrames& LentPitShift :: tick( StkFrames& frames, unsigned int channel )
  181. {
  182. #if defined(_STK_DEBUG_)
  183. if ( channel >= frames.channels() ) {
  184. oStream_ << "LentPitShift::tick(): channel and StkFrames arguments are incompatible!";
  185. handleError( StkError::FUNCTION_ARGUMENT );
  186. }
  187. #endif
  188. StkFloat *samples = &frames[channel];
  189. unsigned int hop = frames.channels();
  190. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  191. *samples = tick( *samples );
  192. }
  193. return frames;
  194. }
  195. inline StkFrames& LentPitShift :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  196. {
  197. #if defined(_STK_DEBUG_)
  198. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  199. oStream_ << "LentPitShift::tick(): channel and StkFrames arguments are incompatible!";
  200. handleError( StkError::FUNCTION_ARGUMENT );
  201. }
  202. #endif
  203. StkFloat *iSamples = &iFrames[iChannel];
  204. StkFloat *oSamples = &oFrames[oChannel];
  205. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  206. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  207. *oSamples = tick( *iSamples );
  208. }
  209. return iFrames;
  210. }
  211. } // stk namespace
  212. #endif