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