victor@4
|
1 /*
|
victor@4
|
2 * render.cpp
|
victor@4
|
3 *
|
victor@4
|
4 * Created on: Oct 24, 2014
|
victor@4
|
5 * Author: parallels
|
victor@4
|
6 */
|
victor@4
|
7
|
victor@4
|
8
|
andrewm@56
|
9 #include <BeagleRT.h>
|
victor@4
|
10 #include <rtdk.h>
|
victor@4
|
11 #include <NE10.h> // neon library
|
victor@4
|
12 #include <cmath>
|
victor@4
|
13
|
andrewm@5
|
14 int gFFTSize;
|
andrewm@5
|
15 float gFFTScaleFactor = 0;
|
victor@4
|
16
|
victor@4
|
17 int gReadPointer = 0;
|
victor@4
|
18 int gWritePointer = 0;
|
victor@4
|
19
|
victor@4
|
20 // FFT vars
|
andrewm@5
|
21 ne10_fft_cpx_float32_t* timeDomainIn;
|
andrewm@5
|
22 ne10_fft_cpx_float32_t* timeDomainOut;
|
victor@4
|
23 ne10_fft_cpx_float32_t* frequencyDomain;
|
victor@4
|
24 ne10_fft_cfg_float32_t cfg;
|
victor@4
|
25
|
andrewm@56
|
26 // setup() is called once before the audio rendering starts.
|
victor@4
|
27 // Use it to perform any initialisation and allocation which is dependent
|
victor@4
|
28 // on the period size or sample rate.
|
victor@4
|
29 //
|
victor@4
|
30 // userData holds an opaque pointer to a data structure that was passed
|
victor@4
|
31 // in from the call to initAudio().
|
victor@4
|
32 //
|
victor@4
|
33 // Return true on success; returning false halts the program.
|
victor@4
|
34
|
andrewm@56
|
35 bool setup(BeagleRTContext *context, void *userData)
|
victor@4
|
36 {
|
victor@4
|
37 // Retrieve a parameter passed in from the initAudio() call
|
andrewm@5
|
38 gFFTSize = *(int *)userData;
|
andrewm@5
|
39 gFFTScaleFactor = 1.0f / (float)gFFTSize;
|
victor@4
|
40
|
andrewm@5
|
41 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
andrewm@5
|
42 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
andrewm@5
|
43 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
andrewm@5
|
44 cfg = ne10_fft_alloc_c2c_float32 (gFFTSize);
|
victor@4
|
45
|
andrewm@5
|
46 memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
|
victor@4
|
47
|
victor@4
|
48 return true;
|
victor@4
|
49 }
|
victor@4
|
50
|
victor@4
|
51 // render() is called regularly at the highest priority by the audio engine.
|
victor@4
|
52 // Input and output are given from the audio hardware and the other
|
victor@4
|
53 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
victor@4
|
54 // will be 0.
|
victor@4
|
55
|
andrewm@52
|
56 void render(BeagleRTContext *context, void *userData)
|
victor@4
|
57 {
|
andrewm@52
|
58 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
andrewm@52
|
59 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioChannels] +
|
andrewm@52
|
60 context->audioIn[n*context->audioChannels+1]) * 0.5);
|
andrewm@5
|
61 timeDomainIn[gReadPointer].i = 0;
|
victor@4
|
62
|
andrewm@5
|
63 if(++gReadPointer >= gFFTSize)
|
andrewm@5
|
64 {
|
andrewm@5
|
65 //FFT
|
andrewm@5
|
66 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg->twiddles, cfg->factors, gFFTSize, 0);
|
victor@4
|
67
|
andrewm@5
|
68 //Do frequency domain stuff
|
victor@4
|
69
|
andrewm@5
|
70 //IFFT
|
andrewm@5
|
71 ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg->twiddles, cfg->factors, gFFTSize, 1);
|
victor@4
|
72
|
andrewm@5
|
73 gReadPointer = 0;
|
andrewm@5
|
74 gWritePointer = 0;
|
andrewm@5
|
75 }
|
victor@4
|
76
|
andrewm@56
|
77 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
andrewm@52
|
78 context->audioOut[n * context->audioChannels + channel] = (float) timeDomainOut[gWritePointer].r * gFFTScaleFactor;
|
andrewm@5
|
79 gWritePointer++;
|
victor@4
|
80 }
|
victor@4
|
81 }
|
victor@4
|
82
|
andrewm@56
|
83 // cleanup() is called once at the end, after the audio has stopped.
|
andrewm@56
|
84 // Release any resources that were allocated in setup().
|
victor@4
|
85
|
andrewm@56
|
86 void cleanup(BeagleRTContext *context, void *userData)
|
victor@4
|
87 {
|
andrewm@5
|
88 NE10_FREE(timeDomainIn);
|
andrewm@5
|
89 NE10_FREE(timeDomainOut);
|
victor@4
|
90 NE10_FREE(frequencyDomain);
|
victor@4
|
91 NE10_FREE(cfg);
|
victor@4
|
92 }
|