comparison projects/audio_in_FFT/render.cpp @ 4:f34c63568523

_new FFT example [still noisy]
author Victor Zappi <victor.zappi@qmul.ac.uk>
date Thu, 06 Nov 2014 19:02:48 +0000
parents
children 09f03ac40fcc
comparison
equal deleted inserted replaced
3:6810f166482f 4:f34c63568523
1 /*
2 * render.cpp
3 *
4 * Created on: Oct 24, 2014
5 * Author: parallels
6 */
7
8
9 #include "../../include/render.h"
10 #include <rtdk.h>
11 #include <NE10.h> // neon library
12 #include <cmath>
13
14 int gFftSize;
15 int gNumChannels;
16
17 int gReadPointer = 0;
18 int gWritePointer = 0;
19
20 // FFT vars
21 ne10_fft_cpx_float32_t* timeDomain;
22 ne10_fft_cpx_float32_t* frequencyDomain;
23 ne10_fft_cfg_float32_t cfg;
24
25 // initialise_render() is called once before the audio rendering starts.
26 // Use it to perform any initialisation and allocation which is dependent
27 // on the period size or sample rate.
28 //
29 // userData holds an opaque pointer to a data structure that was passed
30 // in from the call to initAudio().
31 //
32 // Return true on success; returning false halts the program.
33
34 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod,
35 int numAudioFramesPerPeriod, float matrixSampleRate,
36 float audioSampleRate, void *userData)
37 {
38 // Retrieve a parameter passed in from the initAudio() call
39 gFftSize = *(int *)userData;
40 gNumChannels = numChannels;
41
42 //memset(outSamples, gFftSize, 0.0); // set all to 0
43
44 timeDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFftSize * sizeof (ne10_fft_cpx_float32_t));
45 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFftSize * sizeof (ne10_fft_cpx_float32_t));
46 cfg = ne10_fft_alloc_c2c_float32 (gFftSize);
47
48 return true;
49 }
50
51 // render() is called regularly at the highest priority by the audio engine.
52 // Input and output are given from the audio hardware and the other
53 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
54 // will be 0.
55
56 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
57 uint16_t *matrixIn, uint16_t *matrixOut)
58 {
59 for(int n = 0; n < numAudioFrames; n++) {
60 timeDomain[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumChannels] + audioIn[n*gNumChannels+1])/2);
61 timeDomain[gReadPointer].i = 0;
62 gReadPointer++;
63 }
64
65 if(gReadPointer>=gFftSize)
66 {
67 //FFT
68 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomain, cfg->twiddles, cfg->factors, gFftSize, 0);
69
70 //Do frequency domain stuff
71
72 //IFFT
73 ne10_fft_c2c_1d_float32_neon (timeDomain, frequencyDomain, cfg->twiddles, cfg->factors, gFftSize, 1);
74
75 gReadPointer = 0;
76 gWritePointer = 0;
77 }
78
79 for(int n = 0; n < numAudioFrames; n++) {
80 for(int channel = 0; channel < gNumChannels; channel++)
81 audioOut[n * gNumChannels + channel] = (float) timeDomain[gWritePointer++].r/gFftSize;
82 }
83 }
84
85 // cleanup_render() is called once at the end, after the audio has stopped.
86 // Release any resources that were allocated in initialise_render().
87
88 void cleanup_render()
89 {
90 NE10_FREE(timeDomain);
91 NE10_FREE(frequencyDomain);
92 NE10_FREE(cfg);
93 }