andrewm@0
|
1 /*
|
andrewm@0
|
2 This code accompanies the textbook:
|
andrewm@0
|
3
|
andrewm@0
|
4 Digital Audio Effects: Theory, Implementation and Application
|
andrewm@0
|
5 Joshua D. Reiss and Andrew P. McPherson
|
andrewm@0
|
6
|
andrewm@0
|
7 ---
|
andrewm@0
|
8
|
andrewm@0
|
9 PVOC Pitch Shift: pitch shifter using phase vocoder
|
andrewm@0
|
10 See textbook Chapter 8: The Phase Vocoder
|
andrewm@0
|
11
|
andrewm@0
|
12 Code by Andrew McPherson, Brecht De Man and Joshua Reiss
|
andrewm@0
|
13 Based on a project by Xinyuan Lai
|
andrewm@0
|
14
|
andrewm@0
|
15 This code requires the fftw library version 3 to compile:
|
andrewm@0
|
16 http://fftw.org
|
andrewm@0
|
17
|
andrewm@0
|
18 ---
|
andrewm@0
|
19
|
andrewm@0
|
20 This program is free software: you can redistribute it and/or modify
|
andrewm@0
|
21 it under the terms of the GNU General Public License as published by
|
andrewm@0
|
22 the Free Software Foundation, either version 3 of the License, or
|
andrewm@0
|
23 (at your option) any later version.
|
andrewm@0
|
24
|
andrewm@0
|
25 This program is distributed in the hope that it will be useful,
|
andrewm@0
|
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
andrewm@0
|
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
andrewm@0
|
28 GNU General Public License for more details.
|
andrewm@0
|
29
|
andrewm@0
|
30 You should have received a copy of the GNU General Public License
|
andrewm@0
|
31 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
andrewm@0
|
32 */
|
andrewm@0
|
33
|
andrewm@0
|
34 #ifndef __PLUGINPROCESSOR_H_4693CB6E__
|
andrewm@0
|
35 #define __PLUGINPROCESSOR_H_4693CB6E__
|
andrewm@0
|
36
|
andrewm@0
|
37 #include "../JuceLibraryCode/JuceHeader.h"
|
andrewm@0
|
38 #include <fftw3.h>
|
andrewm@0
|
39
|
andrewm@0
|
40 //==============================================================================
|
andrewm@0
|
41 /**
|
andrewm@0
|
42 */
|
andrewm@0
|
43 class PVOCPitchShiftAudioProcessor : public AudioProcessor
|
andrewm@0
|
44 {
|
andrewm@0
|
45 public:
|
andrewm@0
|
46 //==============================================================================
|
andrewm@0
|
47 PVOCPitchShiftAudioProcessor();
|
andrewm@0
|
48 ~PVOCPitchShiftAudioProcessor();
|
andrewm@0
|
49
|
andrewm@0
|
50 //==============================================================================
|
andrewm@0
|
51 void prepareToPlay (double sampleRate, int samplesPerBlock);
|
andrewm@0
|
52 void releaseResources();
|
andrewm@0
|
53
|
andrewm@0
|
54 void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
|
andrewm@0
|
55
|
andrewm@0
|
56 //==============================================================================
|
andrewm@0
|
57 AudioProcessorEditor* createEditor();
|
andrewm@0
|
58 bool hasEditor() const;
|
andrewm@0
|
59
|
andrewm@0
|
60 //==============================================================================
|
andrewm@0
|
61 const String getName() const;
|
andrewm@0
|
62
|
andrewm@0
|
63 int getNumParameters();
|
andrewm@0
|
64
|
andrewm@0
|
65 float getParameter (int index);
|
andrewm@0
|
66 void setParameter (int index, float newValue);
|
andrewm@0
|
67
|
andrewm@0
|
68 const String getParameterName (int index);
|
andrewm@0
|
69 const String getParameterText (int index);
|
andrewm@0
|
70
|
andrewm@0
|
71 const String getInputChannelName (int channelIndex) const;
|
andrewm@0
|
72 const String getOutputChannelName (int channelIndex) const;
|
andrewm@0
|
73 bool isInputChannelStereoPair (int index) const;
|
andrewm@0
|
74 bool isOutputChannelStereoPair (int index) const;
|
andrewm@0
|
75
|
andrewm@0
|
76 bool silenceInProducesSilenceOut() const;
|
andrewm@0
|
77 double getTailLengthSeconds() const;
|
andrewm@0
|
78 bool acceptsMidi() const;
|
andrewm@0
|
79 bool producesMidi() const;
|
andrewm@0
|
80
|
andrewm@0
|
81 //==============================================================================
|
andrewm@0
|
82 int getNumPrograms();
|
andrewm@0
|
83 int getCurrentProgram();
|
andrewm@0
|
84 void setCurrentProgram (int index);
|
andrewm@0
|
85 const String getProgramName (int index);
|
andrewm@0
|
86 void changeProgramName (int index, const String& newName);
|
andrewm@0
|
87
|
andrewm@0
|
88 //==============================================================================
|
andrewm@0
|
89 void getStateInformation (MemoryBlock& destData);
|
andrewm@0
|
90 void setStateInformation (const void* data, int sizeInBytes);
|
andrewm@0
|
91
|
andrewm@0
|
92 //==============================================================================
|
andrewm@0
|
93
|
andrewm@0
|
94 // these are used to persist the UI's size - the values are stored along with the
|
andrewm@0
|
95 // filter's other parameters, and the UI component will update them when it gets
|
andrewm@0
|
96 // resized.
|
andrewm@0
|
97 int lastUIWidth_, lastUIHeight_;
|
andrewm@0
|
98
|
andrewm@0
|
99 enum Parameters
|
andrewm@0
|
100 {
|
andrewm@0
|
101 kFFTSizeParam = 0,
|
andrewm@0
|
102 kHopSizeParam,
|
andrewm@0
|
103 kWindowTypeParam,
|
andrewm@0
|
104 kPitchShiftParam, // (⊙_⊙)
|
andrewm@0
|
105 kNumParameters
|
andrewm@0
|
106 };
|
andrewm@0
|
107
|
andrewm@0
|
108 enum Window
|
andrewm@0
|
109 {
|
andrewm@0
|
110 kWindowRectangular = 1,
|
andrewm@0
|
111 kWindowBartlett,
|
andrewm@0
|
112 kWindowHann,
|
andrewm@0
|
113 kWindowHamming
|
andrewm@0
|
114 };
|
andrewm@0
|
115
|
andrewm@0
|
116 enum HopSize
|
andrewm@0
|
117 {
|
andrewm@0
|
118 kHopSize1Window = 1,
|
andrewm@0
|
119 kHopSize1_2Window,
|
andrewm@0
|
120 kHopSize1_4Window,
|
andrewm@0
|
121 kHopSize1_8Window
|
andrewm@0
|
122 };
|
andrewm@0
|
123
|
andrewm@0
|
124 enum PitchShift // (⊙_⊙) pitch shift from 6 semitones rise to 6 semitones drop
|
andrewm@0
|
125 {
|
andrewm@0
|
126 kShift0 = 1,
|
andrewm@0
|
127 kShiftP1,
|
andrewm@0
|
128 kShiftP2,
|
andrewm@0
|
129 kShiftP3,
|
andrewm@0
|
130 kShiftP4,
|
andrewm@0
|
131 kShiftP5,
|
andrewm@0
|
132 kShiftP6,
|
andrewm@0
|
133 kShiftM1,
|
andrewm@0
|
134 kShiftM2,
|
andrewm@0
|
135 kShiftM3,
|
andrewm@0
|
136 kShiftM4,
|
andrewm@0
|
137 kShiftM5,
|
andrewm@0
|
138 kShiftM6
|
andrewm@0
|
139
|
andrewm@0
|
140 };
|
andrewm@0
|
141
|
andrewm@0
|
142 // This parameter indicates the FFT size for phase vocoder computation. It is selected
|
andrewm@0
|
143 // by the GUI and may temporarily differ from the actual size used in calculations.
|
andrewm@0
|
144 int fftSelectedSize_;
|
andrewm@0
|
145 int hopSelectedSize_; // Hop size, chosen from one of the options above
|
andrewm@0
|
146 int windowType_; // Type of window used
|
andrewm@0
|
147 int pitchSelectedShift_; // (⊙_⊙)semitones of pitch rise or drop
|
andrewm@0
|
148
|
andrewm@0
|
149 private:
|
andrewm@0
|
150 // Methods to initialise and de-initialise the FFT machinery
|
andrewm@0
|
151 void initFFT(int length);
|
andrewm@0
|
152 void deinitFFT();
|
andrewm@0
|
153
|
andrewm@0
|
154 // Methods to initialise and de-initialise the window
|
andrewm@0
|
155 void initWindow(int length, int windowType);
|
andrewm@0
|
156 void deinitWindow();
|
andrewm@0
|
157 void initSynthesisWindow(int length, int windowType);
|
andrewm@0
|
158 void deinitSynthesisWindow();
|
andrewm@0
|
159
|
andrewm@0
|
160 // Methods to update the buffering for the given hop size and the output scaling
|
andrewm@0
|
161 void updateHopSize();
|
andrewm@0
|
162 void updateScaleFactor();
|
andrewm@0
|
163
|
andrewm@0
|
164
|
andrewm@0
|
165 // (⊙_⊙)Method to update the pitch shift
|
andrewm@0
|
166 void updatePitchShift();
|
andrewm@0
|
167
|
andrewm@0
|
168 // (⊙_⊙)Method to compute the priciple phase argument
|
andrewm@0
|
169 double princArg(double phaseIn);
|
andrewm@0
|
170
|
andrewm@0
|
171
|
andrewm@0
|
172 // Whether the FFT has been initialised and is therefore ready to go
|
andrewm@0
|
173 bool fftInitialised_;
|
andrewm@0
|
174
|
andrewm@0
|
175 // Variables for calculating the FFT and IFFT: complex data structures and the
|
andrewm@0
|
176 // "plan" used by the fftw library to calculate the transforms.
|
andrewm@0
|
177 fftw_complex *fftTimeDomain_, *fftFrequencyDomain_;
|
andrewm@0
|
178 fftw_plan fftForwardPlan_, fftBackwardPlan_;
|
andrewm@0
|
179
|
andrewm@0
|
180 // Size of the FFT (generally a power of two) and the hop size (in samples, generally a fraction of FFT size)
|
andrewm@0
|
181 int fftActualTransformSize_;
|
andrewm@0
|
182 int hopActualSize_;
|
andrewm@0
|
183
|
andrewm@0
|
184 // (⊙_⊙) The actual pitch shift by ratios
|
andrewm@0
|
185 double pitchActualShift_; // the frequency ratio of original and shifted pitch
|
andrewm@0
|
186 double pitchActualShiftRec_; // the reciprocal of of pitchActualShift_, just for computing efficiency
|
andrewm@0
|
187 double actualRatio_;
|
andrewm@0
|
188 double omega_[2048]; // the arrays for storing the phases
|
andrewm@0
|
189 double phi_[2048][2];
|
andrewm@0
|
190 double phi0_[2048][2];
|
andrewm@0
|
191 double dphi_[2048][2];
|
andrewm@0
|
192 double psi_[2048][2];
|
andrewm@0
|
193
|
andrewm@0
|
194 // Amount by which to scale the inverse FFT to return to original amplitude: depends on the
|
andrewm@0
|
195 // transform size (because of fftw implementation) and the hop size (because of inherent overlap)
|
andrewm@0
|
196 double fftScaleFactor_;
|
andrewm@0
|
197
|
andrewm@0
|
198 // Circular buffer gathers audio samples from the input until enough are available
|
andrewm@0
|
199 // for the FFT calculation
|
andrewm@0
|
200 AudioSampleBuffer inputBuffer_;
|
andrewm@0
|
201 int inputBufferLength_;
|
andrewm@0
|
202 int inputBufferWritePosition_;
|
andrewm@0
|
203
|
andrewm@0
|
204 // Circular buffer that collects output samples from the FFT overlap-add process
|
andrewm@0
|
205 // before they are ready to be sent to the output stream
|
andrewm@0
|
206 AudioSampleBuffer outputBuffer_;
|
andrewm@0
|
207 int outputBufferLength_;
|
andrewm@0
|
208 int outputBufferReadPosition_, outputBufferWritePosition_;
|
andrewm@0
|
209
|
andrewm@0
|
210 // How many samples since the last FFT?
|
andrewm@0
|
211 int samplesSinceLastFFT_;
|
andrewm@0
|
212
|
andrewm@0
|
213 // Stored window function for pre-processing input frames
|
andrewm@0
|
214 double *windowBuffer_;
|
andrewm@0
|
215 double *synthesisWindowBuffer_;
|
andrewm@0
|
216 int windowBufferLength_;
|
andrewm@0
|
217 int synthesisWindowBufferLength_;
|
andrewm@0
|
218
|
andrewm@0
|
219 // Whether or not prepareToPlay() has been called, i.e. that resources are in use
|
andrewm@0
|
220 bool preparedToPlay_;
|
andrewm@0
|
221
|
andrewm@0
|
222 // Spin lock that prevents the FFT settings from changing in the middle of the audio
|
andrewm@0
|
223 // thread.
|
andrewm@0
|
224 SpinLock fftSpinLock_;
|
andrewm@0
|
225
|
andrewm@0
|
226 //==============================================================================
|
andrewm@0
|
227 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PVOCPitchShiftAudioProcessor);
|
andrewm@0
|
228 };
|
andrewm@0
|
229
|
andrewm@0
|
230 #endif // __PLUGINPROCESSOR_H_4693CB6E__
|