robert@464
|
1 /*
|
robert@464
|
2 ____ _____ _ _
|
robert@464
|
3 | __ )| ____| | / \
|
robert@464
|
4 | _ \| _| | | / _ \
|
robert@464
|
5 | |_) | |___| |___ / ___ \
|
robert@464
|
6 |____/|_____|_____/_/ \_\
|
robert@464
|
7
|
robert@464
|
8 The platform for ultra-low latency audio and sensor processing
|
robert@464
|
9
|
robert@464
|
10 http://bela.io
|
robert@464
|
11
|
robert@464
|
12 A project of the Augmented Instruments Laboratory within the
|
robert@464
|
13 Centre for Digital Music at Queen Mary University of London.
|
robert@464
|
14 http://www.eecs.qmul.ac.uk/~andrewm
|
robert@464
|
15
|
robert@464
|
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
|
robert@464
|
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
|
robert@464
|
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
|
robert@464
|
19
|
robert@464
|
20 The Bela software is distributed under the GNU Lesser General Public License
|
robert@464
|
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
|
robert@464
|
22 */
|
robert@464
|
23
|
robert@464
|
24
|
robert@464
|
25 #include <Bela.h>
|
robert@464
|
26 #include <rtdk.h>
|
robert@464
|
27 #include <ne10/NE10.h> // neon library
|
robert@464
|
28 #include <cmath>
|
robert@464
|
29
|
robert@464
|
30 int gFFTSize;
|
robert@464
|
31
|
robert@464
|
32 int gReadPointer = 0;
|
robert@464
|
33 int gWritePointer = 0;
|
robert@464
|
34
|
robert@464
|
35 // FFT vars
|
robert@464
|
36 static ne10_fft_cpx_float32_t* timeDomainIn;
|
robert@464
|
37 static ne10_fft_cpx_float32_t* timeDomainOut;
|
robert@464
|
38 static ne10_fft_cpx_float32_t* frequencyDomain;
|
robert@464
|
39 static ne10_fft_cfg_float32_t cfg;
|
robert@464
|
40
|
robert@464
|
41 bool setup(BelaContext *context, void *userData)
|
robert@464
|
42 {
|
robert@464
|
43 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
44 gFFTSize = *(int *)userData;
|
robert@464
|
45
|
robert@464
|
46 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
47 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
48 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
49 cfg = ne10_fft_alloc_c2c_float32_neon (gFFTSize);
|
robert@464
|
50
|
robert@464
|
51 memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
52
|
robert@464
|
53 return true;
|
robert@464
|
54 }
|
robert@464
|
55
|
robert@464
|
56 void render(BelaContext *context, void *userData)
|
robert@464
|
57 {
|
robert@464
|
58 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
59 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioChannels] +
|
robert@464
|
60 context->audioIn[n*context->audioChannels+1]) * 0.5);
|
robert@464
|
61 timeDomainIn[gReadPointer].i = 0;
|
robert@464
|
62
|
robert@464
|
63 if(++gReadPointer >= gFFTSize)
|
robert@464
|
64 {
|
robert@464
|
65 //FFT
|
robert@464
|
66 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg, 0);
|
robert@464
|
67
|
robert@464
|
68 //Do frequency domain stuff
|
robert@464
|
69
|
robert@464
|
70 //IFFT
|
robert@464
|
71 ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg, 1);
|
robert@464
|
72
|
robert@464
|
73 gReadPointer = 0;
|
robert@464
|
74 gWritePointer = 0;
|
robert@464
|
75 }
|
robert@464
|
76
|
robert@464
|
77 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
robert@464
|
78 context->audioOut[n * context->audioChannels + channel] = (float) timeDomainOut[gWritePointer].r;
|
robert@464
|
79 gWritePointer++;
|
robert@464
|
80 }
|
robert@464
|
81 }
|
robert@464
|
82
|
robert@464
|
83 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
84 {
|
robert@464
|
85 NE10_FREE(timeDomainIn);
|
robert@464
|
86 NE10_FREE(timeDomainOut);
|
robert@464
|
87 NE10_FREE(frequencyDomain);
|
robert@464
|
88 NE10_FREE(cfg);
|
robert@464
|
89 }
|
robert@464
|
90
|
robert@464
|
91
|
robert@464
|
92 /**
|
robert@500
|
93 \example FFT-audio-in/render.cpp
|
robert@464
|
94
|
robert@464
|
95 Fast Fourier Transform
|
robert@464
|
96 ----------------------
|
robert@464
|
97
|
robert@464
|
98 This sketch performs an FFT (Fast Fourier Transform) on incoming audio. It uses
|
robert@464
|
99 the NE10 library, included at the top of the file.
|
robert@464
|
100
|
robert@464
|
101 Read the documentation on the NE10 library [here](http://projectne10.github.io/Ne10/doc/annotated.html).
|
robert@464
|
102
|
robert@464
|
103 The variables `timeDomainIn`, `timeDomainOut` and `frequencyDomain` are
|
robert@464
|
104 variables of the struct `ne10_fft_cpx_float32_t` [http://projectne10.github.io/Ne10/doc/structne10__fft__cpx__float32__t.html](http://projectne10.github.io/Ne10/doc/structne10__fft__cpx__float32__t.html).
|
robert@464
|
105 These are declared at the top of the file, and memory is allocated
|
robert@464
|
106 for them in `setup()`.
|
robert@464
|
107
|
robert@464
|
108 In `render()` a `for` loop performs the FFT which is performed on each sample,
|
robert@464
|
109 and the resulting output is placed on each channel.
|
robert@464
|
110 */
|