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: #define ENABLE_NE10_FIR_FLOAT_NEON // Define needed for Ne10 library robert@464: robert@464: #include robert@464: #include robert@464: #include // neon library robert@464: #include "SampleData.h" robert@464: #include "FIRfilter.h" robert@464: robert@464: SampleData gSampleData; // User defined structure to get complex data from main robert@464: int gReadPtr; // Position of last read sample from file robert@464: robert@464: // filter vars robert@464: ne10_fir_instance_f32_t gFIRfilter; robert@464: ne10_float32_t *gFIRfilterIn; robert@464: ne10_float32_t *gFIRfilterOut; robert@464: ne10_uint32_t blockSize; robert@464: ne10_float32_t *gFIRfilterState; robert@464: robert@464: void initialise_filter(BelaContext *context); robert@464: robert@464: // Task for handling the update of the frequencies using the matrix robert@464: AuxiliaryTask gTriggerSamplesTask; robert@464: robert@464: bool initialise_trigger(); robert@464: void trigger_samples(); robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: // Retrieve a parameter passed in from the initAudio() call robert@464: gSampleData = *(SampleData *)userData; robert@464: robert@464: gReadPtr = -1; robert@464: robert@464: initialise_filter(context); robert@464: robert@464: // Initialise auxiliary tasks robert@464: if(!initialise_trigger()) robert@464: return false; 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: float in = 0; robert@464: robert@464: // If triggered... robert@464: if(gReadPtr != -1) robert@464: in += gSampleData.samples[gReadPtr++]; // ...read each sample... robert@464: robert@464: if(gReadPtr >= gSampleData.sampleLen) robert@464: gReadPtr = -1; robert@464: robert@464: gFIRfilterIn[n] = in; robert@464: } robert@464: robert@464: ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize); robert@464: robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: for(unsigned int channel = 0; channel < context->audioChannels; channel++) robert@464: context->audioOut[n * context->audioChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel robert@464: } robert@464: robert@464: robert@464: // Request that the lower-priority task run at next opportunity robert@464: Bela_scheduleAuxiliaryTask(gTriggerSamplesTask); robert@464: } robert@464: robert@464: // Initialise NE10 data structures to define FIR filter robert@464: robert@464: void initialise_filter(BelaContext *context) robert@464: { robert@464: blockSize = context->audioFrames; robert@464: gFIRfilterState = (ne10_float32_t *) NE10_MALLOC ((FILTER_TAP_NUM+blockSize-1) * sizeof (ne10_float32_t)); robert@464: gFIRfilterIn = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); robert@464: gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); robert@464: ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize); robert@464: } robert@464: robert@464: robert@464: // Initialise the auxiliary task robert@464: // and print info robert@464: robert@464: bool initialise_trigger() robert@464: { robert@464: if((gTriggerSamplesTask = Bela_createAuxiliaryTask(&trigger_samples, 50, "bela-trigger-samples")) == 0) robert@464: return false; robert@464: robert@464: rt_printf("Press 'a' to trigger sample, 's' to stop\n"); robert@464: rt_printf("Press 'q' to quit\n"); robert@464: robert@464: return true; robert@464: } robert@464: robert@464: // This is a lower-priority call to periodically read keyboard input robert@464: // and trigger samples. By placing it at a lower priority, robert@464: // it has minimal effect on the audio performance but it will take longer to robert@464: // complete if the system is under heavy audio load. robert@464: robert@464: void trigger_samples() robert@464: { robert@464: // This is not a real-time task! robert@464: // Cos getchar is a system call, not handled by Xenomai. robert@464: // This task will be automatically down graded. robert@464: robert@464: char keyStroke = '.'; robert@464: robert@464: keyStroke = getchar(); robert@464: while(getchar()!='\n'); // to read the first stroke robert@464: robert@464: switch (keyStroke) robert@464: { robert@464: case 'a': robert@464: gReadPtr = 0; robert@464: break; robert@464: case 's': robert@464: gReadPtr = -1; robert@464: break; robert@464: case 'q': robert@464: gShouldStop = true; robert@464: break; robert@464: default: robert@464: break; robert@464: } robert@464: } robert@464: robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: delete[] gSampleData.samples; robert@464: robert@464: NE10_FREE(gFIRfilterState); robert@464: NE10_FREE(gFIRfilterIn); robert@464: NE10_FREE(gFIRfilterOut); robert@464: } robert@464: robert@464: robert@464: /** robert@500: \example filter-FIR/render.cpp robert@464: robert@464: Finite Impulse Response Filter robert@464: ------------------------------ robert@464: robert@464: This is an example of a finite impulse response filter implementation. robert@464: */