view examples/audio_in_FFT/render.cpp @ 373:3bed6b09223c prerelease

Updated NE10 library to the latest version; needs a corresponding update to the /usr/include/ne10 header files on the SD image. Updated examples to compile against new version, and reordered D-Box channels to account for new PRU-based DAC channel reordering.
author andrewm
date Thu, 09 Jun 2016 20:03:09 +0100
parents db2fe4e1b88e
children 24c3a0663d54
line wrap: on
line source
/*
 ____  _____ _        _    
| __ )| ____| |      / \   
|  _ \|  _| | |     / _ \  
| |_) | |___| |___ / ___ \ 
|____/|_____|_____/_/   \_\.io

 */

/*
 * render.cpp
 *
 *  Created on: Oct 24, 2014
 *      Author: Andrew McPherson, C4DM, QMUL
 */

/**
\example 4_audio_FFT

Fast Fourier Transform
----------------------

This sketch performs an FFT (Fast Fourier Transform) on incoming audio. It uses 
the NE10 library, included at the top of the file (line 11).

Read the documentation on the NE10 library [here](http://projectne10.github.io/Ne10/doc/annotated.html).

The variables `timeDomainIn`, `timeDomainOut` and `frequencyDomain` are 
variables of the struct `ne10_fft_cpx_float32_t` [http://projectne10.github.io/Ne10/doc/structne10__fft__cpx__float32__t.html](http://projectne10.github.io/Ne10/doc/structne10__fft__cpx__float32__t.html). 
These are declared at the top of the file (line 21), and memory is allocated 
for them in `setup()` (line 41).

In `render()` a `for` loop performs the FFT which is performed on each sample, 
and the resulting output is placed on each channel.
*/

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

int gFFTSize;

int gReadPointer = 0;
int gWritePointer = 0;

// FFT vars
static ne10_fft_cpx_float32_t* timeDomainIn;
static ne10_fft_cpx_float32_t* timeDomainOut;
static ne10_fft_cpx_float32_t* frequencyDomain;
static ne10_fft_cfg_float32_t cfg;

// setup() 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 setup(BelaContext *context, void *userData)
{
	// Retrieve a parameter passed in from the initAudio() call
	gFFTSize = *(int *)userData;

	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_neon (gFFTSize);

	memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));

	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(BelaContext *context, void *userData)
{
	for(unsigned int n = 0; n < context->audioFrames; n++) {
		timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioChannels] +
															context->audioIn[n*context->audioChannels+1]) * 0.5);
		timeDomainIn[gReadPointer].i = 0;

		if(++gReadPointer >= gFFTSize)
		{
			//FFT
			ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg, 0);

			//Do frequency domain stuff

			//IFFT
			ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg, 1);

			gReadPointer = 0;
			gWritePointer = 0;
		}

		for(unsigned int channel = 0; channel < context->audioChannels; channel++)
			context->audioOut[n * context->audioChannels + channel] = (float) timeDomainOut[gWritePointer].r;
		gWritePointer++;
	}
}

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

void cleanup(BelaContext *context, void *userData)
{
	NE10_FREE(timeDomainIn);
	NE10_FREE(timeDomainOut);
	NE10_FREE(frequencyDomain);
	NE10_FREE(cfg);
}