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
|
victor@4
|
10 /*
|
victor@4
|
11 * render.cpp
|
victor@4
|
12 *
|
victor@4
|
13 * Created on: Oct 24, 2014
|
victor@4
|
14 * Author: parallels
|
victor@4
|
15 */
|
victor@4
|
16
|
robert@372
|
17 /**
|
robert@372
|
18 \example 4_audio_FFT
|
robert@372
|
19
|
robert@372
|
20 Fast Fourier Transform
|
robert@372
|
21 ----------------------
|
robert@372
|
22
|
robert@372
|
23 This sketch performs an FFT (Fast Fourier Transform) on incoming audio. It uses
|
robert@372
|
24 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
|
robert@372
|
28 The variables `timeDomainIn`, `timeDomainOut` and `frequencyDomain` are
|
robert@372
|
29 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@372
|
30 These are declared at the top of the file (line 21), and memory is allocated
|
robert@372
|
31 for them in `setup()` (line 41).
|
robert@372
|
32
|
robert@372
|
33 In `render()` a `for` loop performs the FFT which is performed on each sample,
|
robert@372
|
34 and the resulting output is placed on each channel.
|
robert@372
|
35 */
|
victor@4
|
36
|
giuliomoro@301
|
37 #include <Bela.h>
|
victor@4
|
38 #include <rtdk.h>
|
victor@4
|
39 #include <NE10.h> // neon library
|
victor@4
|
40 #include <cmath>
|
victor@4
|
41
|
andrewm@5
|
42 int gFFTSize;
|
andrewm@5
|
43 float gFFTScaleFactor = 0;
|
victor@4
|
44
|
victor@4
|
45 int gReadPointer = 0;
|
victor@4
|
46 int gWritePointer = 0;
|
victor@4
|
47
|
victor@4
|
48 // FFT vars
|
andrewm@5
|
49 ne10_fft_cpx_float32_t* timeDomainIn;
|
andrewm@5
|
50 ne10_fft_cpx_float32_t* timeDomainOut;
|
victor@4
|
51 ne10_fft_cpx_float32_t* frequencyDomain;
|
victor@4
|
52 ne10_fft_cfg_float32_t cfg;
|
victor@4
|
53
|
andrewm@56
|
54 // setup() is called once before the audio rendering starts.
|
victor@4
|
55 // Use it to perform any initialisation and allocation which is dependent
|
victor@4
|
56 // on the period size or sample rate.
|
victor@4
|
57 //
|
victor@4
|
58 // userData holds an opaque pointer to a data structure that was passed
|
victor@4
|
59 // in from the call to initAudio().
|
victor@4
|
60 //
|
victor@4
|
61 // Return true on success; returning false halts the program.
|
victor@4
|
62
|
giuliomoro@301
|
63 bool setup(BelaContext *context, void *userData)
|
victor@4
|
64 {
|
victor@4
|
65 // Retrieve a parameter passed in from the initAudio() call
|
andrewm@5
|
66 gFFTSize = *(int *)userData;
|
andrewm@5
|
67 gFFTScaleFactor = 1.0f / (float)gFFTSize;
|
victor@4
|
68
|
andrewm@5
|
69 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
andrewm@5
|
70 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
andrewm@5
|
71 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
andrewm@5
|
72 cfg = ne10_fft_alloc_c2c_float32 (gFFTSize);
|
victor@4
|
73
|
andrewm@5
|
74 memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
victor@4
|
75
|
victor@4
|
76 return true;
|
victor@4
|
77 }
|
victor@4
|
78
|
victor@4
|
79 // render() is called regularly at the highest priority by the audio engine.
|
victor@4
|
80 // Input and output are given from the audio hardware and the other
|
victor@4
|
81 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
victor@4
|
82 // will be 0.
|
victor@4
|
83
|
giuliomoro@301
|
84 void render(BelaContext *context, void *userData)
|
victor@4
|
85 {
|
andrewm@52
|
86 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
andrewm@52
|
87 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioChannels] +
|
andrewm@52
|
88 context->audioIn[n*context->audioChannels+1]) * 0.5);
|
andrewm@5
|
89 timeDomainIn[gReadPointer].i = 0;
|
victor@4
|
90
|
andrewm@5
|
91 if(++gReadPointer >= gFFTSize)
|
andrewm@5
|
92 {
|
andrewm@5
|
93 //FFT
|
andrewm@5
|
94 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg->twiddles, cfg->factors, gFFTSize, 0);
|
victor@4
|
95
|
andrewm@5
|
96 //Do frequency domain stuff
|
victor@4
|
97
|
andrewm@5
|
98 //IFFT
|
andrewm@5
|
99 ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg->twiddles, cfg->factors, gFFTSize, 1);
|
victor@4
|
100
|
andrewm@5
|
101 gReadPointer = 0;
|
andrewm@5
|
102 gWritePointer = 0;
|
andrewm@5
|
103 }
|
victor@4
|
104
|
andrewm@56
|
105 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
andrewm@52
|
106 context->audioOut[n * context->audioChannels + channel] = (float) timeDomainOut[gWritePointer].r * gFFTScaleFactor;
|
andrewm@5
|
107 gWritePointer++;
|
victor@4
|
108 }
|
victor@4
|
109 }
|
victor@4
|
110
|
andrewm@56
|
111 // cleanup() is called once at the end, after the audio has stopped.
|
andrewm@56
|
112 // Release any resources that were allocated in setup().
|
victor@4
|
113
|
giuliomoro@301
|
114 void cleanup(BelaContext *context, void *userData)
|
victor@4
|
115 {
|
andrewm@5
|
116 NE10_FREE(timeDomainIn);
|
andrewm@5
|
117 NE10_FREE(timeDomainOut);
|
victor@4
|
118 NE10_FREE(frequencyDomain);
|
victor@4
|
119 NE10_FREE(cfg);
|
victor@4
|
120 }
|