comparison projects/audio_in_FFT/render.cpp @ 14:06f93bef7dd2

Updated remaining examples to new API
author andrewm
date Fri, 23 Jan 2015 15:35:10 +0000
parents 09f03ac40fcc
children a6d223473ea2
comparison
equal deleted inserted replaced
13:6adb088196a7 14:06f93bef7dd2
11 #include <NE10.h> // neon library 11 #include <NE10.h> // neon library
12 #include <cmath> 12 #include <cmath>
13 13
14 int gFFTSize; 14 int gFFTSize;
15 float gFFTScaleFactor = 0; 15 float gFFTScaleFactor = 0;
16 int gNumChannels;
17 16
18 int gReadPointer = 0; 17 int gReadPointer = 0;
19 int gWritePointer = 0; 18 int gWritePointer = 0;
20 19
21 // FFT vars 20 // FFT vars
31 // userData holds an opaque pointer to a data structure that was passed 30 // userData holds an opaque pointer to a data structure that was passed
32 // in from the call to initAudio(). 31 // in from the call to initAudio().
33 // 32 //
34 // Return true on success; returning false halts the program. 33 // Return true on success; returning false halts the program.
35 34
36 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod, 35 bool initialise_render(int numMatrixChannels, int numAudioChannels,
37 int numAudioFramesPerPeriod, float matrixSampleRate, 36 int numMatrixFramesPerPeriod,
38 float audioSampleRate, void *userData) 37 int numAudioFramesPerPeriod,
38 float matrixSampleRate, float audioSampleRate,
39 void *userData)
39 { 40 {
40 // Retrieve a parameter passed in from the initAudio() call 41 // Retrieve a parameter passed in from the initAudio() call
41 gFFTSize = *(int *)userData; 42 gFFTSize = *(int *)userData;
42 gFFTScaleFactor = 1.0f / (float)gFFTSize; 43 gFFTScaleFactor = 1.0f / (float)gFFTSize;
43 gNumChannels = numChannels;
44 44
45 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t)); 45 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
46 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t)); 46 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
47 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t)); 47 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
48 cfg = ne10_fft_alloc_c2c_float32 (gFFTSize); 48 cfg = ne10_fft_alloc_c2c_float32 (gFFTSize);
59 59
60 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, 60 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
61 uint16_t *matrixIn, uint16_t *matrixOut) 61 uint16_t *matrixIn, uint16_t *matrixOut)
62 { 62 {
63 for(int n = 0; n < numAudioFrames; n++) { 63 for(int n = 0; n < numAudioFrames; n++) {
64 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumChannels] + audioIn[n*gNumChannels+1]) * 0.5); 64 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumAudioChannels] + audioIn[n*gNumAudioChannels+1]) * 0.5);
65 timeDomainIn[gReadPointer].i = 0; 65 timeDomainIn[gReadPointer].i = 0;
66 66
67 if(++gReadPointer >= gFFTSize) 67 if(++gReadPointer >= gFFTSize)
68 { 68 {
69 //FFT 69 //FFT
76 76
77 gReadPointer = 0; 77 gReadPointer = 0;
78 gWritePointer = 0; 78 gWritePointer = 0;
79 } 79 }
80 80
81 for(int channel = 0; channel < gNumChannels; channel++) 81 for(int channel = 0; channel < gNumAudioChannels; channel++)
82 audioOut[n * gNumChannels + channel] = (float) timeDomainOut[gWritePointer].r * gFFTScaleFactor; 82 audioOut[n * gNumAudioChannels + channel] = (float) timeDomainOut[gWritePointer].r * gFFTScaleFactor;
83 gWritePointer++; 83 gWritePointer++;
84 } 84 }
85 } 85 }
86 86
87 // cleanup_render() is called once at the end, after the audio has stopped. 87 // cleanup_render() is called once at the end, after the audio has stopped.