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@544
|
54 // Check that we have the same number of inputs and outputs.
|
robert@544
|
55 if(context->audioInChannels != context->audioOutChannels ||
|
robert@544
|
56 context->analogInChannels != context-> analogOutChannels){
|
robert@544
|
57 printf("Error: for this project, you need the same number of input and output channels.\n");
|
robert@544
|
58 return false;
|
robert@544
|
59 }
|
robert@544
|
60
|
robert@464
|
61 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
62 gSampleData = *(SampleData *)userData;
|
robert@464
|
63
|
robert@464
|
64 gReadPtr = -1;
|
robert@464
|
65
|
robert@464
|
66 initialise_filter(context);
|
robert@464
|
67
|
robert@464
|
68 // Initialise auxiliary tasks
|
robert@464
|
69 if(!initialise_trigger())
|
robert@464
|
70 return false;
|
robert@464
|
71
|
robert@464
|
72 return true;
|
robert@464
|
73 }
|
robert@464
|
74
|
robert@464
|
75 void render(BelaContext *context, void *userData)
|
robert@464
|
76 {
|
robert@464
|
77 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
78 float in = 0;
|
robert@464
|
79
|
robert@464
|
80 // If triggered...
|
robert@464
|
81 if(gReadPtr != -1)
|
robert@464
|
82 in += gSampleData.samples[gReadPtr++]; // ...read each sample...
|
robert@464
|
83
|
robert@464
|
84 if(gReadPtr >= gSampleData.sampleLen)
|
robert@464
|
85 gReadPtr = -1;
|
robert@464
|
86
|
robert@464
|
87 gFIRfilterIn[n] = in;
|
robert@464
|
88 }
|
robert@464
|
89
|
robert@464
|
90 ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize);
|
robert@464
|
91
|
robert@464
|
92 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@544
|
93 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
|
robert@544
|
94 context->audioOut[n * context->audioOutChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel
|
robert@464
|
95 }
|
robert@464
|
96
|
robert@464
|
97
|
robert@464
|
98 // Request that the lower-priority task run at next opportunity
|
robert@464
|
99 Bela_scheduleAuxiliaryTask(gTriggerSamplesTask);
|
robert@464
|
100 }
|
robert@464
|
101
|
robert@464
|
102 // Initialise NE10 data structures to define FIR filter
|
robert@464
|
103
|
robert@464
|
104 void initialise_filter(BelaContext *context)
|
robert@464
|
105 {
|
robert@464
|
106 blockSize = context->audioFrames;
|
robert@464
|
107 gFIRfilterState = (ne10_float32_t *) NE10_MALLOC ((FILTER_TAP_NUM+blockSize-1) * sizeof (ne10_float32_t));
|
robert@464
|
108 gFIRfilterIn = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t));
|
robert@464
|
109 gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t));
|
robert@464
|
110 ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize);
|
robert@464
|
111 }
|
robert@464
|
112
|
robert@464
|
113
|
robert@464
|
114 // Initialise the auxiliary task
|
robert@464
|
115 // and print info
|
robert@464
|
116
|
robert@464
|
117 bool initialise_trigger()
|
robert@464
|
118 {
|
robert@464
|
119 if((gTriggerSamplesTask = Bela_createAuxiliaryTask(&trigger_samples, 50, "bela-trigger-samples")) == 0)
|
robert@464
|
120 return false;
|
robert@464
|
121
|
robert@464
|
122 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
|
robert@464
|
123 rt_printf("Press 'q' to quit\n");
|
robert@464
|
124
|
robert@464
|
125 return true;
|
robert@464
|
126 }
|
robert@464
|
127
|
robert@464
|
128 // This is a lower-priority call to periodically read keyboard input
|
robert@464
|
129 // and trigger samples. By placing it at a lower priority,
|
robert@464
|
130 // it has minimal effect on the audio performance but it will take longer to
|
robert@464
|
131 // complete if the system is under heavy audio load.
|
robert@464
|
132
|
robert@464
|
133 void trigger_samples()
|
robert@464
|
134 {
|
robert@464
|
135 // This is not a real-time task!
|
robert@464
|
136 // Cos getchar is a system call, not handled by Xenomai.
|
robert@464
|
137 // This task will be automatically down graded.
|
robert@464
|
138
|
robert@464
|
139 char keyStroke = '.';
|
robert@464
|
140
|
robert@464
|
141 keyStroke = getchar();
|
robert@464
|
142 while(getchar()!='\n'); // to read the first stroke
|
robert@464
|
143
|
robert@464
|
144 switch (keyStroke)
|
robert@464
|
145 {
|
robert@464
|
146 case 'a':
|
robert@464
|
147 gReadPtr = 0;
|
robert@464
|
148 break;
|
robert@464
|
149 case 's':
|
robert@464
|
150 gReadPtr = -1;
|
robert@464
|
151 break;
|
robert@464
|
152 case 'q':
|
robert@464
|
153 gShouldStop = true;
|
robert@464
|
154 break;
|
robert@464
|
155 default:
|
robert@464
|
156 break;
|
robert@464
|
157 }
|
robert@464
|
158 }
|
robert@464
|
159
|
robert@464
|
160
|
robert@464
|
161 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
162 {
|
robert@464
|
163 delete[] gSampleData.samples;
|
robert@464
|
164
|
robert@464
|
165 NE10_FREE(gFIRfilterState);
|
robert@464
|
166 NE10_FREE(gFIRfilterIn);
|
robert@464
|
167 NE10_FREE(gFIRfilterOut);
|
robert@464
|
168 }
|
robert@464
|
169
|
robert@464
|
170
|
robert@464
|
171 /**
|
robert@500
|
172 \example filter-FIR/render.cpp
|
robert@464
|
173
|
robert@464
|
174 Finite Impulse Response Filter
|
robert@464
|
175 ------------------------------
|
robert@464
|
176
|
robert@464
|
177 This is an example of a finite impulse response filter implementation.
|
robert@464
|
178 */
|