diff projects/audio_in_FFT/render.cpp @ 5:09f03ac40fcc

API improvements and cleanups. Now all common audio command-line options can be parsed automatically.
author andrewm
date Sat, 08 Nov 2014 16:16:55 +0100
parents f34c63568523
children 06f93bef7dd2
line wrap: on
line diff
--- a/projects/audio_in_FFT/render.cpp	Thu Nov 06 19:02:48 2014 +0000
+++ b/projects/audio_in_FFT/render.cpp	Sat Nov 08 16:16:55 2014 +0100
@@ -11,14 +11,16 @@
 #include <NE10.h>					// neon library
 #include <cmath>
 
-int gFftSize;
+int gFFTSize;
+float gFFTScaleFactor = 0;
 int gNumChannels;
 
 int gReadPointer = 0;
 int gWritePointer = 0;
 
 // FFT vars
-ne10_fft_cpx_float32_t* timeDomain;
+ne10_fft_cpx_float32_t* timeDomainIn;
+ne10_fft_cpx_float32_t* timeDomainOut;
 ne10_fft_cpx_float32_t* frequencyDomain;
 ne10_fft_cfg_float32_t cfg;
 
@@ -36,14 +38,16 @@
 					   float audioSampleRate, void *userData)
 {
 	// Retrieve a parameter passed in from the initAudio() call
-	gFftSize = *(int *)userData;
+	gFFTSize = *(int *)userData;
+	gFFTScaleFactor = 1.0f / (float)gFFTSize;
 	gNumChannels = numChannels;
 
-	//memset(outSamples, gFftSize, 0.0); // set all to 0
+	timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
+	timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
+	frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
+	cfg = ne10_fft_alloc_c2c_float32 (gFFTSize);
 
-	timeDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFftSize * sizeof (ne10_fft_cpx_float32_t));
-	frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFftSize * sizeof (ne10_fft_cpx_float32_t));
-	cfg = ne10_fft_alloc_c2c_float32 (gFftSize);
+	memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
 
 	return true;
 }
@@ -57,28 +61,26 @@
 			uint16_t *matrixIn, uint16_t *matrixOut)
 {
 	for(int n = 0; n < numAudioFrames; n++) {
-		timeDomain[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumChannels] + audioIn[n*gNumChannels+1])/2);
-		timeDomain[gReadPointer].i = 0;
-		gReadPointer++;
-	}
+		timeDomainIn[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumChannels] + audioIn[n*gNumChannels+1]) * 0.5);
+		timeDomainIn[gReadPointer].i = 0;
 
-	if(gReadPointer>=gFftSize)
-	{
-		//FFT
-		ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomain, cfg->twiddles, cfg->factors, gFftSize, 0);
+		if(++gReadPointer >= gFFTSize)
+		{
+			//FFT
+			ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg->twiddles, cfg->factors, gFFTSize, 0);
 
-		//Do frequency domain stuff
+			//Do frequency domain stuff
 
-		//IFFT
-		ne10_fft_c2c_1d_float32_neon (timeDomain, frequencyDomain, cfg->twiddles, cfg->factors, gFftSize, 1);
+			//IFFT
+			ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg->twiddles, cfg->factors, gFFTSize, 1);
 
-		gReadPointer = 0;
-		gWritePointer = 0;
-	}
+			gReadPointer = 0;
+			gWritePointer = 0;
+		}
 
-	for(int n = 0; n < numAudioFrames; n++) {
 		for(int channel = 0; channel < gNumChannels; channel++)
-			audioOut[n * gNumChannels + channel] = (float) timeDomain[gWritePointer++].r/gFftSize;
+			audioOut[n * gNumChannels + channel] = (float) timeDomainOut[gWritePointer].r * gFFTScaleFactor;
+		gWritePointer++;
 	}
 }
 
@@ -87,7 +89,8 @@
 
 void cleanup_render()
 {
-	NE10_FREE(timeDomain);
+	NE10_FREE(timeDomainIn);
+	NE10_FREE(timeDomainOut);
 	NE10_FREE(frequencyDomain);
 	NE10_FREE(cfg);
 }