view 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
line wrap: on
line source
/*
 * render.cpp
 *
 *  Created on: Oct 24, 2014
 *      Author: parallels
 */


#include "../../include/render.h"
#include <rtdk.h>
#include <NE10.h>					// neon library
#include <cmath>

int gFftSize;
int gNumChannels;

int gReadPointer = 0;
int gWritePointer = 0;

// FFT vars
ne10_fft_cpx_float32_t* timeDomain;
ne10_fft_cpx_float32_t* frequencyDomain;
ne10_fft_cfg_float32_t cfg;

// initialise_render() is called once before the audio rendering starts.
// Use it to perform any initialisation and allocation which is dependent
// on the period size or sample rate.
//
// userData holds an opaque pointer to a data structure that was passed
// in from the call to initAudio().
//
// Return true on success; returning false halts the program.

bool initialise_render(int numChannels, int numMatrixFramesPerPeriod,
					   int numAudioFramesPerPeriod, float matrixSampleRate,
					   float audioSampleRate, void *userData)
{
	// Retrieve a parameter passed in from the initAudio() call
	gFftSize = *(int *)userData;
	gNumChannels = numChannels;

	//memset(outSamples, gFftSize, 0.0); // set all to 0

	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);

	return true;
}

// render() is called regularly at the highest priority by the audio engine.
// Input and output are given from the audio hardware and the other
// ADCs and DACs (if available). If only audio is available, numMatrixFrames
// will be 0.

void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
			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++;
	}

	if(gReadPointer>=gFftSize)
	{
		//FFT
		ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomain, cfg->twiddles, cfg->factors, gFftSize, 0);

		//Do frequency domain stuff

		//IFFT
		ne10_fft_c2c_1d_float32_neon (timeDomain, frequencyDomain, cfg->twiddles, cfg->factors, gFftSize, 1);

		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;
	}
}

// cleanup_render() is called once at the end, after the audio has stopped.
// Release any resources that were allocated in initialise_render().

void cleanup_render()
{
	NE10_FREE(timeDomain);
	NE10_FREE(frequencyDomain);
	NE10_FREE(cfg);
}