robert@372
|
1 /*
|
robert@372
|
2 ____ _____ _ _
|
robert@372
|
3 | __ )| ____| | / \
|
robert@372
|
4 | _ \| _| | | / _ \
|
robert@372
|
5 | |_) | |___| |___ / ___ \
|
robert@372
|
6 |____/|_____|_____/_/ \_\.io
|
robert@372
|
7
|
robert@372
|
8 */
|
robert@372
|
9
|
giuliomoro@250
|
10 /*
|
giuliomoro@250
|
11 * render.cpp
|
giuliomoro@250
|
12 *
|
giuliomoro@250
|
13 * Created on: Oct 24, 2014
|
giuliomoro@250
|
14 * Author: parallels
|
giuliomoro@250
|
15 */
|
giuliomoro@250
|
16
|
robert@372
|
17 /**
|
robert@372
|
18 \example 4_audio_FFT_phase_vocoder
|
robert@372
|
19
|
robert@372
|
20 Phase Vocoder
|
robert@372
|
21 ----------------------
|
robert@372
|
22
|
robert@372
|
23 This sketch shows an implementation of a phase vocoder and builds on the previous FFT example.
|
robert@372
|
24 Again it uses the NE10 library, included at the top of the file (line 11).
|
robert@372
|
25
|
robert@372
|
26 Read the documentation on the NE10 library [here](http://projectne10.github.io/Ne10/doc/annotated.html).
|
robert@372
|
27 */
|
giuliomoro@250
|
28
|
giuliomoro@301
|
29 #include <Bela.h>
|
giuliomoro@250
|
30 #include <rtdk.h>
|
giuliomoro@250
|
31 #include <NE10.h> // NEON FFT library
|
giuliomoro@250
|
32 #include <cmath>
|
giuliomoro@250
|
33 #include "SampleData.h"
|
giuliomoro@250
|
34 #include <Midi.h>
|
giuliomoro@250
|
35
|
giuliomoro@250
|
36 #define BUFFER_SIZE 16384
|
giuliomoro@250
|
37
|
giuliomoro@250
|
38 // TODO: your buffer and counter go here!
|
giuliomoro@250
|
39 float gInputBuffer[BUFFER_SIZE];
|
giuliomoro@250
|
40 int gInputBufferPointer = 0;
|
giuliomoro@250
|
41 float gOutputBuffer[BUFFER_SIZE];
|
giuliomoro@250
|
42 int gOutputBufferWritePointer = 0;
|
giuliomoro@250
|
43 int gOutputBufferReadPointer = 0;
|
giuliomoro@250
|
44 int gSampleCount = 0;
|
giuliomoro@250
|
45
|
giuliomoro@250
|
46 float *gWindowBuffer;
|
giuliomoro@250
|
47
|
giuliomoro@250
|
48 // -----------------------------------------------
|
giuliomoro@250
|
49 // These variables used internally in the example:
|
giuliomoro@250
|
50 int gFFTSize = 2048;
|
giuliomoro@250
|
51 int gHopSize = 512;
|
giuliomoro@250
|
52 int gPeriod = 512;
|
giuliomoro@250
|
53 float gFFTScaleFactor = 0;
|
giuliomoro@250
|
54
|
giuliomoro@250
|
55 // FFT vars
|
giuliomoro@250
|
56 ne10_fft_cpx_float32_t* timeDomainIn;
|
giuliomoro@250
|
57 ne10_fft_cpx_float32_t* timeDomainOut;
|
giuliomoro@250
|
58 ne10_fft_cpx_float32_t* frequencyDomain;
|
giuliomoro@250
|
59 ne10_fft_cfg_float32_t cfg;
|
giuliomoro@250
|
60
|
giuliomoro@250
|
61 // Sample info
|
giuliomoro@250
|
62 SampleData gSampleData; // User defined structure to get complex data from main
|
giuliomoro@250
|
63 int gReadPtr = 0; // Position of last read sample from file
|
giuliomoro@250
|
64
|
giuliomoro@250
|
65 // Auxiliary task for calculating FFT
|
giuliomoro@250
|
66 AuxiliaryTask gFFTTask;
|
giuliomoro@250
|
67 int gFFTInputBufferPointer = 0;
|
giuliomoro@250
|
68 int gFFTOutputBufferPointer = 0;
|
giuliomoro@250
|
69
|
giuliomoro@250
|
70 void process_fft_background();
|
giuliomoro@250
|
71
|
giuliomoro@250
|
72
|
giuliomoro@250
|
73 int gEffect = 0; // change this here or with midi CC
|
giuliomoro@250
|
74 enum{
|
giuliomoro@250
|
75 kBypass,
|
giuliomoro@250
|
76 kRobot,
|
giuliomoro@250
|
77 kWhisper,
|
giuliomoro@250
|
78 };
|
giuliomoro@250
|
79
|
giuliomoro@250
|
80 float gDryWet = 1; // mix between the unprocessed and processed sound
|
giuliomoro@250
|
81 float gPlaybackLive = 0.5f; // mix between the file playback and the live audio input
|
giuliomoro@250
|
82 float gGain = 1; // overall gain
|
giuliomoro@250
|
83 Midi midi;
|
giuliomoro@250
|
84 void midiCallback(MidiChannelMessage message, void* arg){
|
giuliomoro@250
|
85 if(message.getType() == kmmNoteOn){
|
giuliomoro@250
|
86 if(message.getDataByte(1) > 0){
|
giuliomoro@250
|
87 int note = message.getDataByte(0);
|
giuliomoro@250
|
88 float frequency = powf(2, (note-69)/12.f)*440;
|
giuliomoro@250
|
89 gPeriod = (int)(44100 / frequency + 0.5);
|
giuliomoro@250
|
90 printf("\nnote: %d, frequency: %f, hop: %d\n", note, frequency, gPeriod);
|
giuliomoro@250
|
91 }
|
giuliomoro@250
|
92 }
|
giuliomoro@250
|
93
|
giuliomoro@250
|
94 bool shouldPrint = false;
|
giuliomoro@250
|
95 if(message.getType() == kmmControlChange){
|
giuliomoro@250
|
96 float data = message.getDataByte(1) / 127.0f;
|
giuliomoro@250
|
97 switch (message.getDataByte(0)){
|
giuliomoro@250
|
98 case 2 :
|
giuliomoro@250
|
99 gEffect = (int)(data * 2 + 0.5); // CC2 selects an effect between 0,1,2
|
giuliomoro@250
|
100 break;
|
giuliomoro@250
|
101 case 3 :
|
giuliomoro@250
|
102 gPlaybackLive = data;
|
giuliomoro@250
|
103 break;
|
giuliomoro@250
|
104 case 4 :
|
giuliomoro@250
|
105 gDryWet = data;
|
giuliomoro@250
|
106 break;
|
giuliomoro@250
|
107 case 5:
|
giuliomoro@250
|
108 gGain = data*10;
|
giuliomoro@250
|
109 break;
|
giuliomoro@250
|
110 default:
|
giuliomoro@250
|
111 shouldPrint = true;
|
giuliomoro@250
|
112 }
|
giuliomoro@250
|
113 }
|
giuliomoro@250
|
114 if(shouldPrint){
|
giuliomoro@250
|
115 message.prettyPrint();
|
giuliomoro@250
|
116 }
|
giuliomoro@250
|
117 }
|
giuliomoro@250
|
118
|
giuliomoro@250
|
119 // userData holds an opaque pointer to a data structure that was passed
|
giuliomoro@250
|
120 // in from the call to initAudio().
|
giuliomoro@250
|
121 //
|
giuliomoro@250
|
122 // Return true on success; returning false halts the program.
|
giuliomoro@301
|
123 bool setup(BelaContext* context, void* userData)
|
giuliomoro@250
|
124 {
|
giuliomoro@250
|
125 midi.readFrom(0);
|
giuliomoro@250
|
126 midi.setParserCallback(midiCallback);
|
giuliomoro@250
|
127 // Retrieve a parameter passed in from the initAudio() call
|
giuliomoro@250
|
128 gSampleData = *(SampleData *)userData;
|
giuliomoro@250
|
129
|
giuliomoro@250
|
130 gFFTScaleFactor = 1.0f / (float)gFFTSize;
|
giuliomoro@250
|
131 gOutputBufferWritePointer += gHopSize;
|
giuliomoro@250
|
132
|
giuliomoro@250
|
133 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
giuliomoro@250
|
134 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
giuliomoro@250
|
135 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
giuliomoro@250
|
136 cfg = ne10_fft_alloc_c2c_float32 (gFFTSize);
|
giuliomoro@250
|
137
|
giuliomoro@250
|
138 memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
giuliomoro@250
|
139 memset(gOutputBuffer, 0, BUFFER_SIZE * sizeof(float));
|
giuliomoro@250
|
140
|
giuliomoro@250
|
141 // Allocate the window buffer based on the FFT size
|
giuliomoro@250
|
142 gWindowBuffer = (float *)malloc(gFFTSize * sizeof(float));
|
giuliomoro@250
|
143 if(gWindowBuffer == 0)
|
giuliomoro@250
|
144 return false;
|
giuliomoro@250
|
145
|
giuliomoro@250
|
146 // Calculate a Hann window
|
giuliomoro@250
|
147 for(int n = 0; n < gFFTSize; n++) {
|
giuliomoro@250
|
148 gWindowBuffer[n] = 0.5f * (1.0f - cosf(2.0 * M_PI * n / (float)(gFFTSize - 1)));
|
giuliomoro@250
|
149 }
|
giuliomoro@250
|
150
|
giuliomoro@250
|
151 // Initialise auxiliary tasks
|
giuliomoro@301
|
152 if((gFFTTask = Bela_createAuxiliaryTask(&process_fft_background, 90, "fft-calculation")) == 0)
|
giuliomoro@250
|
153 return false;
|
giuliomoro@251
|
154 rt_printf("You are listening to an FFT phase-vocoder with overlap-and-add.\n"
|
giuliomoro@250
|
155 "Use Midi Control Change to control:\n"
|
giuliomoro@251
|
156 "CC 2: effect type (bypass/robotization/whisperization)\n"
|
giuliomoro@251
|
157 "CC 3: mix between recorded sample and live audio input\n"
|
giuliomoro@251
|
158 "CC 4: mix between the unprocessed and processed sound\n"
|
giuliomoro@251
|
159 "CC 5: gain\n"
|
giuliomoro@250
|
160 );
|
giuliomoro@250
|
161 return true;
|
giuliomoro@250
|
162 }
|
giuliomoro@250
|
163
|
giuliomoro@250
|
164 // This function handles the FFT processing in this example once the buffer has
|
giuliomoro@250
|
165 // been assembled.
|
giuliomoro@250
|
166 void process_fft(float *inBuffer, int inWritePointer, float *outBuffer, int outWritePointer)
|
giuliomoro@250
|
167 {
|
giuliomoro@250
|
168 // Copy buffer into FFT input
|
giuliomoro@250
|
169 int pointer = (inWritePointer - gFFTSize + BUFFER_SIZE) % BUFFER_SIZE;
|
giuliomoro@250
|
170 for(int n = 0; n < gFFTSize; n++) {
|
giuliomoro@250
|
171 timeDomainIn[n].r = (ne10_float32_t) inBuffer[pointer] * gWindowBuffer[n];
|
giuliomoro@250
|
172 timeDomainIn[n].i = 0;
|
giuliomoro@250
|
173
|
giuliomoro@250
|
174 pointer++;
|
giuliomoro@250
|
175 if(pointer >= BUFFER_SIZE)
|
giuliomoro@250
|
176 pointer = 0;
|
giuliomoro@250
|
177 }
|
giuliomoro@250
|
178
|
giuliomoro@250
|
179 // Run the FFT
|
giuliomoro@250
|
180 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg->twiddles, cfg->factors, gFFTSize, 0);
|
giuliomoro@250
|
181
|
giuliomoro@250
|
182 switch (gEffect){
|
giuliomoro@250
|
183 case kRobot :
|
giuliomoro@250
|
184 // Robotise the output
|
giuliomoro@250
|
185 for(int n = 0; n < gFFTSize; n++) {
|
giuliomoro@250
|
186 float amplitude = sqrtf(frequencyDomain[n].r * frequencyDomain[n].r + frequencyDomain[n].i * frequencyDomain[n].i);
|
giuliomoro@250
|
187 frequencyDomain[n].r = amplitude;
|
giuliomoro@250
|
188 frequencyDomain[n].i = 0;
|
giuliomoro@250
|
189 }
|
giuliomoro@250
|
190 break;
|
giuliomoro@250
|
191 case kWhisper :
|
giuliomoro@250
|
192 for(int n = 0; n < gFFTSize; n++) {
|
giuliomoro@250
|
193 float amplitude = sqrtf(frequencyDomain[n].r * frequencyDomain[n].r + frequencyDomain[n].i * frequencyDomain[n].i);
|
giuliomoro@250
|
194 float phase = rand()/(float)RAND_MAX * 2 * M_PI;
|
giuliomoro@250
|
195 frequencyDomain[n].r = cosf(phase) * amplitude;
|
giuliomoro@250
|
196 frequencyDomain[n].i = sinf(phase) * amplitude;
|
giuliomoro@250
|
197 }
|
giuliomoro@250
|
198 break;
|
giuliomoro@250
|
199 case kBypass:
|
giuliomoro@250
|
200 //bypass
|
giuliomoro@250
|
201 break;
|
giuliomoro@250
|
202 }
|
giuliomoro@250
|
203
|
giuliomoro@250
|
204 // Run the inverse FFT
|
giuliomoro@250
|
205 ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg->twiddles, cfg->factors, gFFTSize, 1);
|
giuliomoro@250
|
206 // Overlap-and-add timeDomainOut into the output buffer
|
giuliomoro@250
|
207 pointer = outWritePointer;
|
giuliomoro@250
|
208 for(int n = 0; n < gFFTSize; n++) {
|
giuliomoro@250
|
209 outBuffer[pointer] += (timeDomainOut[n].r) * gFFTScaleFactor;
|
giuliomoro@250
|
210 if(isnan(outBuffer[pointer]))
|
giuliomoro@250
|
211 rt_printf("outBuffer OLA\n");
|
giuliomoro@250
|
212 pointer++;
|
giuliomoro@250
|
213 if(pointer >= BUFFER_SIZE)
|
giuliomoro@250
|
214 pointer = 0;
|
giuliomoro@250
|
215 }
|
giuliomoro@250
|
216 }
|
giuliomoro@250
|
217
|
giuliomoro@250
|
218 // Function to process the FFT in a thread at lower priority
|
giuliomoro@250
|
219 void process_fft_background() {
|
giuliomoro@250
|
220 process_fft(gInputBuffer, gFFTInputBufferPointer, gOutputBuffer, gFFTOutputBufferPointer);
|
giuliomoro@250
|
221 }
|
giuliomoro@250
|
222
|
giuliomoro@250
|
223 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@250
|
224 // Input and output are given from the audio hardware and the other
|
giuliomoro@250
|
225 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
giuliomoro@250
|
226 // will be 0.
|
giuliomoro@301
|
227 void render(BelaContext* context, void* userData)
|
giuliomoro@250
|
228 {
|
giuliomoro@250
|
229 float* audioIn = context->audioIn;
|
giuliomoro@250
|
230 float* audioOut = context->audioOut;
|
giuliomoro@250
|
231 int numAudioFrames = context->audioFrames;
|
giuliomoro@250
|
232 int numAudioChannels = context->audioChannels;
|
giuliomoro@250
|
233 // ------ this code internal to the demo; leave as is ----------------
|
giuliomoro@250
|
234
|
giuliomoro@250
|
235 // Prep the "input" to be the sound file played in a loop
|
giuliomoro@250
|
236 for(int n = 0; n < numAudioFrames; n++) {
|
giuliomoro@250
|
237 if(gReadPtr < gSampleData.sampleLen)
|
giuliomoro@250
|
238 audioIn[2*n] = audioIn[2*n+1] = gSampleData.samples[gReadPtr]*(1-gPlaybackLive) +
|
andrewm@308
|
239 gPlaybackLive*0.5f*(audioRead(context,n,0)+audioRead(context,n,1));
|
giuliomoro@250
|
240 else
|
giuliomoro@250
|
241 audioIn[2*n] = audioIn[2*n+1] = 0;
|
giuliomoro@250
|
242 if(++gReadPtr >= gSampleData.sampleLen)
|
giuliomoro@250
|
243 gReadPtr = 0;
|
giuliomoro@250
|
244 }
|
giuliomoro@250
|
245 // -------------------------------------------------------------------
|
giuliomoro@250
|
246
|
giuliomoro@250
|
247 for(int n = 0; n < numAudioFrames; n++) {
|
giuliomoro@250
|
248 gInputBuffer[gInputBufferPointer] = ((audioIn[n*numAudioChannels] + audioIn[n*numAudioChannels+1]) * 0.5);
|
giuliomoro@250
|
249
|
giuliomoro@250
|
250 // Copy output buffer to output
|
giuliomoro@250
|
251 for(int channel = 0; channel < numAudioChannels; channel++){
|
giuliomoro@250
|
252 audioOut[n * numAudioChannels + channel] = gOutputBuffer[gOutputBufferReadPointer] * gGain * gDryWet + (1 - gDryWet) * audioIn[n * numAudioChannels + channel];
|
giuliomoro@250
|
253 }
|
giuliomoro@250
|
254
|
giuliomoro@250
|
255 // Clear the output sample in the buffer so it is ready for the next overlap-add
|
giuliomoro@250
|
256 gOutputBuffer[gOutputBufferReadPointer] = 0;
|
giuliomoro@250
|
257 gOutputBufferReadPointer++;
|
giuliomoro@250
|
258 if(gOutputBufferReadPointer >= BUFFER_SIZE)
|
giuliomoro@250
|
259 gOutputBufferReadPointer = 0;
|
giuliomoro@250
|
260 gOutputBufferWritePointer++;
|
giuliomoro@250
|
261 if(gOutputBufferWritePointer >= BUFFER_SIZE)
|
giuliomoro@250
|
262 gOutputBufferWritePointer = 0;
|
giuliomoro@250
|
263
|
giuliomoro@250
|
264 gInputBufferPointer++;
|
giuliomoro@250
|
265 if(gInputBufferPointer >= BUFFER_SIZE)
|
giuliomoro@250
|
266 gInputBufferPointer = 0;
|
giuliomoro@250
|
267
|
giuliomoro@250
|
268 gSampleCount++;
|
giuliomoro@250
|
269 if(gSampleCount >= gHopSize) {
|
giuliomoro@250
|
270 //process_fft(gInputBuffer, gInputBufferPointer, gOutputBuffer, gOutputBufferPointer);
|
giuliomoro@250
|
271 gFFTInputBufferPointer = gInputBufferPointer;
|
giuliomoro@250
|
272 gFFTOutputBufferPointer = gOutputBufferWritePointer;
|
giuliomoro@301
|
273 Bela_scheduleAuxiliaryTask(gFFTTask);
|
giuliomoro@250
|
274
|
giuliomoro@250
|
275 gSampleCount = 0;
|
giuliomoro@250
|
276 }
|
giuliomoro@250
|
277 }
|
giuliomoro@250
|
278 gHopSize = gPeriod;
|
giuliomoro@250
|
279 }
|
giuliomoro@250
|
280
|
giuliomoro@250
|
281 // cleanup_render() is called once at the end, after the audio has stopped.
|
giuliomoro@250
|
282 // Release any resources that were allocated in initialise_render().
|
giuliomoro@250
|
283
|
giuliomoro@301
|
284 void cleanup(BelaContext* context, void* userData)
|
giuliomoro@250
|
285 {
|
giuliomoro@250
|
286 NE10_FREE(timeDomainIn);
|
giuliomoro@250
|
287 NE10_FREE(timeDomainOut);
|
giuliomoro@250
|
288 NE10_FREE(frequencyDomain);
|
giuliomoro@250
|
289 NE10_FREE(cfg);
|
giuliomoro@250
|
290 free(gWindowBuffer);
|
giuliomoro@250
|
291 }
|