comparison examples/04-Audio/FFT-audio-in/render.cpp @ 464:8fcfbfb32aa0 prerelease

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