Debian Codegraph @

lib-src/libnyquist/nyquist/nyqstk/src/PitShift.cpp

raw
1 /***************************************************/
2 /*! \class PitShift
3 \brief STK simple pitch shifter effect class.
4
5 This class implements a simple pitch shifter
6 using delay lines.
7
8 by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
9 */
10 /***************************************************/
11
12 #include "PitShift.h"
13 #include <cmath>
14
15 using namespace Nyq;
16
17 const int maxDelay = 5024;
18
19 PitShift :: PitShift()
20 {
21 delayLength = maxDelay - 24;
22 halfLength = delayLength / 2;
23 delay_[0] = 12;
24 delay_[1] = maxDelay / 2;
25
26 delayLine_[0].setMaximumDelay( maxDelay );
27 delayLine_[0].setDelay( delay_[0] );
28 delayLine_[1].setMaximumDelay( maxDelay );
29 delayLine_[1].setDelay( delay_[1] );
30 effectMix_ = 0.5;
31 rate_ = 1.0;
32 }
33
34 PitShift :: ~PitShift()
35 {
36 }
37
38 void PitShift :: clear()
39 {
40 delayLine_[0].clear();
41 delayLine_[1].clear();
42 lastOutput_[0] = 0.0;
43 lastOutput_[1] = 0.0;
44 }
45
46 void PitShift :: setShift(StkFloat shift)
47 {
48 if (shift < 1.0) {
49 rate_ = 1.0 - shift;
50 }
51 else if (shift > 1.0) {
52 rate_ = 1.0 - shift;
53 }
54 else {
55 rate_ = 0.0;
56 delay_[0] = halfLength+12;
57 }
58 }
59
60 StkFloat PitShift :: computeSample(StkFloat input)
61 {
62 // Calculate the two delay length values, keeping them within the
63 // range 12 to maxDelay-12.
64 delay_[0] += rate_;
65 while (delay_[0] > maxDelay-12) delay_[0] -= delayLength;
66 while (delay_[0] < 12) delay_[0] += delayLength;
67
68 delay_[1] = delay_[0] + halfLength;
69 while (delay_[1] > maxDelay-12) delay_[1] -= delayLength;
70 while (delay_[1] < 12) delay_[1] += delayLength;
71
72 // Set the new delay line lengths.
73 delayLine_[0].setDelay((long)delay_[0]);
74 delayLine_[1].setDelay((long)delay_[1]);
75
76 // Calculate a triangular envelope.
77 env_[1] = fabs( (delay_[0] - halfLength + 12) * (1.0 / (halfLength+12) ) );
78 env_[0] = 1.0 - env_[1];
79
80 // Delay input and apply envelope.
81 lastOutput_[0] = env_[0] * delayLine_[0].tick(input);
82 lastOutput_[0] += env_[1] * delayLine_[1].tick(input);
83
84 // Compute effect mix and output.
85 lastOutput_[0] *= effectMix_;
86 lastOutput_[0] += (1.0 - effectMix_) * input;
87 lastOutput_[1] = lastOutput_[0];
88
89 return lastOutput_[0];
90 }