robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: #include // neon library robert@464: #include robert@464: robert@464: int gFFTSize; robert@464: robert@464: int gReadPointer = 0; robert@464: int gWritePointer = 0; robert@464: robert@464: // FFT vars robert@464: static ne10_fft_cpx_float32_t* timeDomainIn; robert@464: static ne10_fft_cpx_float32_t* timeDomainOut; robert@464: static ne10_fft_cpx_float32_t* frequencyDomain; robert@464: static ne10_fft_cfg_float32_t cfg; robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: // Retrieve a parameter passed in from the initAudio() call robert@464: gFFTSize = *(int *)userData; robert@464: robert@464: timeDomainIn = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t)); robert@464: timeDomainOut = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t)); robert@464: frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFFTSize * sizeof (ne10_fft_cpx_float32_t)); robert@464: cfg = ne10_fft_alloc_c2c_float32_neon (gFFTSize); robert@464: robert@464: memset(timeDomainOut, 0, gFFTSize * sizeof (ne10_fft_cpx_float32_t)); robert@464: robert@464: return true; robert@464: } robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioChannels] + robert@464: context->audioIn[n*context->audioChannels+1]) * 0.5); robert@464: timeDomainIn[gReadPointer].i = 0; robert@464: robert@464: if(++gReadPointer >= gFFTSize) robert@464: { robert@464: //FFT robert@464: ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomainIn, cfg, 0); robert@464: robert@464: //Do frequency domain stuff robert@464: robert@464: //IFFT robert@464: ne10_fft_c2c_1d_float32_neon (timeDomainOut, frequencyDomain, cfg, 1); robert@464: robert@464: gReadPointer = 0; robert@464: gWritePointer = 0; robert@464: } robert@464: robert@464: for(unsigned int channel = 0; channel < context->audioChannels; channel++) robert@464: context->audioOut[n * context->audioChannels + channel] = (float) timeDomainOut[gWritePointer].r; robert@464: gWritePointer++; robert@464: } robert@464: } robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: NE10_FREE(timeDomainIn); robert@464: NE10_FREE(timeDomainOut); robert@464: NE10_FREE(frequencyDomain); robert@464: NE10_FREE(cfg); robert@464: } robert@464: robert@464: robert@464: /** robert@500: \example FFT-audio-in/render.cpp robert@464: robert@464: Fast Fourier Transform robert@464: ---------------------- robert@464: robert@464: This sketch performs an FFT (Fast Fourier Transform) on incoming audio. It uses robert@464: the NE10 library, included at the top of the file. robert@464: robert@464: Read the documentation on the NE10 library [here](http://projectne10.github.io/Ne10/doc/annotated.html). robert@464: robert@464: The variables `timeDomainIn`, `timeDomainOut` and `frequencyDomain` are robert@464: 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). robert@464: These are declared at the top of the file, and memory is allocated robert@464: for them in `setup()`. robert@464: robert@464: In `render()` a `for` loop performs the FFT which is performed on each sample, robert@464: and the resulting output is placed on each channel. robert@464: */