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@544
|
43
|
robert@544
|
44 // Check that we have the same number of inputs and outputs.
|
robert@544
|
45 if(context->audioInChannels != context->audioOutChannels ||
|
robert@544
|
46 context->analogInChannels != context-> analogOutChannels){
|
robert@544
|
47 printf("Error: for this project, you need the same number of input and output channels.\n");
|
robert@544
|
48 return false;
|
robert@544
|
49 }
|
robert@544
|
50
|
robert@464
|
51 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
52 gFFTSize = *(int *)userData;
|
robert@464
|
53
|
robert@464
|
54 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
55 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
56 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
57 cfg = ne10_fft_alloc_c2c_float32_neon (gFFTSize);
|
robert@464
|
58
|
robert@464
|
59 memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
robert@464
|
60
|
robert@464
|
61 return true;
|
robert@464
|
62 }
|
robert@464
|
63
|
robert@464
|
64 void render(BelaContext *context, void *userData)
|
robert@464
|
65 {
|
robert@464
|
66 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@544
|
67 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioInChannels] +
|
robert@544
|
68 context->audioIn[n*context->audioOutChannels+1]) * 0.5);
|
robert@464
|
69 timeDomainIn[gReadPointer].i = 0;
|
robert@464
|
70
|
robert@464
|
71 if(++gReadPointer >= gFFTSize)
|
robert@464
|
72 {
|
robert@464
|
73 //FFT
|
robert@464
|
74 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg, 0);
|
robert@464
|
75
|
robert@464
|
76 //Do frequency domain stuff
|
robert@464
|
77
|
robert@464
|
78 //IFFT
|
robert@464
|
79 ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg, 1);
|
robert@464
|
80
|
robert@464
|
81 gReadPointer = 0;
|
robert@464
|
82 gWritePointer = 0;
|
robert@464
|
83 }
|
robert@464
|
84
|
robert@544
|
85 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
|
robert@544
|
86 context->audioOut[n * context->audioOutChannels + channel] = (float) timeDomainOut[gWritePointer].r;
|
robert@464
|
87 gWritePointer++;
|
robert@464
|
88 }
|
robert@464
|
89 }
|
robert@464
|
90
|
robert@464
|
91 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
92 {
|
robert@464
|
93 NE10_FREE(timeDomainIn);
|
robert@464
|
94 NE10_FREE(timeDomainOut);
|
robert@464
|
95 NE10_FREE(frequencyDomain);
|
robert@464
|
96 NE10_FREE(cfg);
|
robert@464
|
97 }
|
robert@464
|
98
|
robert@464
|
99
|
robert@464
|
100 /**
|
robert@500
|
101 \example FFT-audio-in/render.cpp
|
robert@464
|
102
|
robert@464
|
103 Fast Fourier Transform
|
robert@464
|
104 ----------------------
|
robert@464
|
105
|
robert@464
|
106 This sketch performs an FFT (Fast Fourier Transform) on incoming audio. It uses
|
robert@464
|
107 the NE10 library, included at the top of the file.
|
robert@464
|
108
|
robert@464
|
109 Read the documentation on the NE10 library [here](http://projectne10.github.io/Ne10/doc/annotated.html).
|
robert@464
|
110
|
robert@464
|
111 The variables `timeDomainIn`, `timeDomainOut` and `frequencyDomain` are
|
robert@464
|
112 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
|
113 These are declared at the top of the file, and memory is allocated
|
robert@464
|
114 for them in `setup()`.
|
robert@464
|
115
|
robert@464
|
116 In `render()` a `for` loop performs the FFT which is performed on each sample,
|
robert@464
|
117 and the resulting output is placed on each channel.
|
robert@464
|
118 */
|