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