annotate examples/04-Audio/filter-FIR/render.cpp @ 500:b935f890e512 prerelease

Updated Doxy to 1.8.11 Amended example titles in render.cpp and file locations in Doxy file.
author Robert Jack <robert.h.jack@gmail.com>
date Wed, 22 Jun 2016 00:27:13 +0100
parents 8fcfbfb32aa0
children 1cec96845a23
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #define ENABLE_NE10_FIR_FLOAT_NEON // Define needed for Ne10 library
robert@464 26
robert@464 27 #include <Bela.h>
robert@464 28 #include <cmath>
robert@464 29 #include <ne10/NE10.h> // neon library
robert@464 30 #include "SampleData.h"
robert@464 31 #include "FIRfilter.h"
robert@464 32
robert@464 33 SampleData gSampleData; // User defined structure to get complex data from main
robert@464 34 int gReadPtr; // Position of last read sample from file
robert@464 35
robert@464 36 // filter vars
robert@464 37 ne10_fir_instance_f32_t gFIRfilter;
robert@464 38 ne10_float32_t *gFIRfilterIn;
robert@464 39 ne10_float32_t *gFIRfilterOut;
robert@464 40 ne10_uint32_t blockSize;
robert@464 41 ne10_float32_t *gFIRfilterState;
robert@464 42
robert@464 43 void initialise_filter(BelaContext *context);
robert@464 44
robert@464 45 // Task for handling the update of the frequencies using the matrix
robert@464 46 AuxiliaryTask gTriggerSamplesTask;
robert@464 47
robert@464 48 bool initialise_trigger();
robert@464 49 void trigger_samples();
robert@464 50
robert@464 51 bool setup(BelaContext *context, void *userData)
robert@464 52 {
robert@464 53
robert@464 54 // Retrieve a parameter passed in from the initAudio() call
robert@464 55 gSampleData = *(SampleData *)userData;
robert@464 56
robert@464 57 gReadPtr = -1;
robert@464 58
robert@464 59 initialise_filter(context);
robert@464 60
robert@464 61 // Initialise auxiliary tasks
robert@464 62 if(!initialise_trigger())
robert@464 63 return false;
robert@464 64
robert@464 65 return true;
robert@464 66 }
robert@464 67
robert@464 68 void render(BelaContext *context, void *userData)
robert@464 69 {
robert@464 70 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 71 float in = 0;
robert@464 72
robert@464 73 // If triggered...
robert@464 74 if(gReadPtr != -1)
robert@464 75 in += gSampleData.samples[gReadPtr++]; // ...read each sample...
robert@464 76
robert@464 77 if(gReadPtr >= gSampleData.sampleLen)
robert@464 78 gReadPtr = -1;
robert@464 79
robert@464 80 gFIRfilterIn[n] = in;
robert@464 81 }
robert@464 82
robert@464 83 ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize);
robert@464 84
robert@464 85 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 86 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
robert@464 87 context->audioOut[n * context->audioChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel
robert@464 88 }
robert@464 89
robert@464 90
robert@464 91 // Request that the lower-priority task run at next opportunity
robert@464 92 Bela_scheduleAuxiliaryTask(gTriggerSamplesTask);
robert@464 93 }
robert@464 94
robert@464 95 // Initialise NE10 data structures to define FIR filter
robert@464 96
robert@464 97 void initialise_filter(BelaContext *context)
robert@464 98 {
robert@464 99 blockSize = context->audioFrames;
robert@464 100 gFIRfilterState = (ne10_float32_t *) NE10_MALLOC ((FILTER_TAP_NUM+blockSize-1) * sizeof (ne10_float32_t));
robert@464 101 gFIRfilterIn = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t));
robert@464 102 gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t));
robert@464 103 ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize);
robert@464 104 }
robert@464 105
robert@464 106
robert@464 107 // Initialise the auxiliary task
robert@464 108 // and print info
robert@464 109
robert@464 110 bool initialise_trigger()
robert@464 111 {
robert@464 112 if((gTriggerSamplesTask = Bela_createAuxiliaryTask(&trigger_samples, 50, "bela-trigger-samples")) == 0)
robert@464 113 return false;
robert@464 114
robert@464 115 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
robert@464 116 rt_printf("Press 'q' to quit\n");
robert@464 117
robert@464 118 return true;
robert@464 119 }
robert@464 120
robert@464 121 // This is a lower-priority call to periodically read keyboard input
robert@464 122 // and trigger samples. By placing it at a lower priority,
robert@464 123 // it has minimal effect on the audio performance but it will take longer to
robert@464 124 // complete if the system is under heavy audio load.
robert@464 125
robert@464 126 void trigger_samples()
robert@464 127 {
robert@464 128 // This is not a real-time task!
robert@464 129 // Cos getchar is a system call, not handled by Xenomai.
robert@464 130 // This task will be automatically down graded.
robert@464 131
robert@464 132 char keyStroke = '.';
robert@464 133
robert@464 134 keyStroke = getchar();
robert@464 135 while(getchar()!='\n'); // to read the first stroke
robert@464 136
robert@464 137 switch (keyStroke)
robert@464 138 {
robert@464 139 case 'a':
robert@464 140 gReadPtr = 0;
robert@464 141 break;
robert@464 142 case 's':
robert@464 143 gReadPtr = -1;
robert@464 144 break;
robert@464 145 case 'q':
robert@464 146 gShouldStop = true;
robert@464 147 break;
robert@464 148 default:
robert@464 149 break;
robert@464 150 }
robert@464 151 }
robert@464 152
robert@464 153
robert@464 154 void cleanup(BelaContext *context, void *userData)
robert@464 155 {
robert@464 156 delete[] gSampleData.samples;
robert@464 157
robert@464 158 NE10_FREE(gFIRfilterState);
robert@464 159 NE10_FREE(gFIRfilterIn);
robert@464 160 NE10_FREE(gFIRfilterOut);
robert@464 161 }
robert@464 162
robert@464 163 /* ------------ Project Explantation ------------ */
robert@464 164
robert@464 165 /**
robert@500 166 \example filter-FIR/render.cpp
robert@464 167
robert@464 168 Finite Impulse Response Filter
robert@464 169 ------------------------------
robert@464 170
robert@464 171 This is an example of a finite impulse response filter implementation.
robert@464 172 */