annotate projects/audio_in_FFT/render.cpp @ 39:638bc1ae2500 staging

Improved readibility of the DIGITAL code in the PRU, using register names instead of aliases and expanding some of the macros, removing unused macros. Binaries were not modified
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 13 May 2015 12:18:10 +0100
parents 06f93bef7dd2
children a6d223473ea2
rev   line source
victor@4 1 /*
victor@4 2 * render.cpp
victor@4 3 *
victor@4 4 * Created on: Oct 24, 2014
victor@4 5 * Author: parallels
victor@4 6 */
victor@4 7
victor@4 8
victor@4 9 #include "../../include/render.h"
victor@4 10 #include <rtdk.h>
victor@4 11 #include <NE10.h> // neon library
victor@4 12 #include <cmath>
victor@4 13
andrewm@5 14 int gFFTSize;
andrewm@5 15 float gFFTScaleFactor = 0;
victor@4 16
victor@4 17 int gReadPointer = 0;
victor@4 18 int gWritePointer = 0;
victor@4 19
victor@4 20 // FFT vars
andrewm@5 21 ne10_fft_cpx_float32_t* timeDomainIn;
andrewm@5 22 ne10_fft_cpx_float32_t* timeDomainOut;
victor@4 23 ne10_fft_cpx_float32_t* frequencyDomain;
victor@4 24 ne10_fft_cfg_float32_t cfg;
victor@4 25
victor@4 26 // initialise_render() is called once before the audio rendering starts.
victor@4 27 // Use it to perform any initialisation and allocation which is dependent
victor@4 28 // on the period size or sample rate.
victor@4 29 //
victor@4 30 // userData holds an opaque pointer to a data structure that was passed
victor@4 31 // in from the call to initAudio().
victor@4 32 //
victor@4 33 // Return true on success; returning false halts the program.
victor@4 34
andrewm@14 35 bool initialise_render(int numMatrixChannels, int numAudioChannels,
andrewm@14 36 int numMatrixFramesPerPeriod,
andrewm@14 37 int numAudioFramesPerPeriod,
andrewm@14 38 float matrixSampleRate, float audioSampleRate,
andrewm@14 39 void *userData)
victor@4 40 {
victor@4 41 // Retrieve a parameter passed in from the initAudio() call
andrewm@5 42 gFFTSize = *(int *)userData;
andrewm@5 43 gFFTScaleFactor = 1.0f / (float)gFFTSize;
victor@4 44
andrewm@5 45 timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
andrewm@5 46 timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
andrewm@5 47 frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t));
andrewm@5 48 cfg = ne10_fft_alloc_c2c_float32 (gFFTSize);
victor@4 49
andrewm@5 50 memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t));
victor@4 51
victor@4 52 return true;
victor@4 53 }
victor@4 54
victor@4 55 // render() is called regularly at the highest priority by the audio engine.
victor@4 56 // Input and output are given from the audio hardware and the other
victor@4 57 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
victor@4 58 // will be 0.
victor@4 59
victor@4 60 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
victor@4 61 uint16_t *matrixIn, uint16_t *matrixOut)
victor@4 62 {
victor@4 63 for(int n = 0; n < numAudioFrames; n++) {
andrewm@14 64 timeDomainIn[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumAudioChannels] + audioIn[n*gNumAudioChannels+1]) * 0.5);
andrewm@5 65 timeDomainIn[gReadPointer].i = 0;
victor@4 66
andrewm@5 67 if(++gReadPointer >= gFFTSize)
andrewm@5 68 {
andrewm@5 69 //FFT
andrewm@5 70 ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg->twiddles, cfg->factors, gFFTSize, 0);
victor@4 71
andrewm@5 72 //Do frequency domain stuff
victor@4 73
andrewm@5 74 //IFFT
andrewm@5 75 ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg->twiddles, cfg->factors, gFFTSize, 1);
victor@4 76
andrewm@5 77 gReadPointer = 0;
andrewm@5 78 gWritePointer = 0;
andrewm@5 79 }
victor@4 80
andrewm@14 81 for(int channel = 0; channel < gNumAudioChannels; channel++)
andrewm@14 82 audioOut[n * gNumAudioChannels + channel] = (float) timeDomainOut[gWritePointer].r * gFFTScaleFactor;
andrewm@5 83 gWritePointer++;
victor@4 84 }
victor@4 85 }
victor@4 86
victor@4 87 // cleanup_render() is called once at the end, after the audio has stopped.
victor@4 88 // Release any resources that were allocated in initialise_render().
victor@4 89
victor@4 90 void cleanup_render()
victor@4 91 {
andrewm@5 92 NE10_FREE(timeDomainIn);
andrewm@5 93 NE10_FREE(timeDomainOut);
victor@4 94 NE10_FREE(frequencyDomain);
victor@4 95 NE10_FREE(cfg);
victor@4 96 }