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 #include <Bela.h> // to schedule lower prio parallel process
|
robert@464
|
26 #include <rtdk.h>
|
robert@464
|
27 #include <cmath>
|
robert@464
|
28 #include <stdio.h>
|
robert@464
|
29 #include "SampleData.h"
|
robert@464
|
30
|
robert@464
|
31 SampleData gSampleData; // User defined structure to get complex data from main
|
robert@464
|
32 int gReadPtr; // Position of last read sample from file
|
robert@464
|
33
|
robert@464
|
34 // filter vars
|
robert@464
|
35 float gLastX[2];
|
robert@464
|
36 float gLastY[2];
|
robert@464
|
37 double lb0, lb1, lb2, la1, la2 = 0.0;
|
robert@464
|
38
|
robert@464
|
39 // communication vars between the 2 auxiliary tasks
|
robert@464
|
40 int gChangeCoeff = 0;
|
robert@464
|
41 int gFreqDelta = 0;
|
robert@464
|
42
|
robert@464
|
43 void initialise_filter(float freq);
|
robert@464
|
44
|
robert@464
|
45 void calculate_coeff(float cutFreq);
|
robert@464
|
46
|
robert@464
|
47 bool initialise_aux_tasks();
|
robert@464
|
48
|
robert@464
|
49 // Task for handling the update of the frequencies using the matrix
|
robert@464
|
50 AuxiliaryTask gChangeCoeffTask;
|
robert@464
|
51
|
robert@464
|
52 void check_coeff();
|
robert@464
|
53
|
robert@464
|
54 // Task for handling the update of the frequencies using the matrix
|
robert@464
|
55 AuxiliaryTask gInputTask;
|
robert@464
|
56
|
robert@464
|
57 void read_input();
|
robert@464
|
58
|
robert@464
|
59
|
robert@464
|
60 extern float gCutFreq;
|
robert@464
|
61
|
robert@464
|
62
|
robert@464
|
63 bool setup(BelaContext *context, void *userData)
|
robert@464
|
64 {
|
robert@464
|
65
|
robert@544
|
66 // Check that we have the same number of inputs and outputs.
|
robert@544
|
67 if(context->audioInChannels != context->audioOutChannels ||
|
robert@544
|
68 context->analogInChannels != context-> analogOutChannels){
|
robert@544
|
69 printf("Error: for this project, you need the same number of input and output channels.\n");
|
robert@544
|
70 return false;
|
robert@544
|
71 }
|
robert@544
|
72
|
robert@464
|
73 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
74 gSampleData = *(SampleData *)userData;
|
robert@464
|
75
|
robert@464
|
76 gReadPtr = -1;
|
robert@464
|
77
|
robert@464
|
78 initialise_filter(200);
|
robert@464
|
79
|
robert@464
|
80 // Initialise auxiliary tasks
|
robert@464
|
81 if(!initialise_aux_tasks())
|
robert@464
|
82 return false;
|
robert@464
|
83
|
robert@464
|
84 return true;
|
robert@464
|
85 }
|
robert@464
|
86
|
robert@464
|
87 void render(BelaContext *context, void *userData)
|
robert@464
|
88 {
|
robert@464
|
89 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
90 float sample = 0;
|
robert@464
|
91 float out = 0;
|
robert@464
|
92
|
robert@464
|
93 // If triggered...
|
robert@464
|
94 if(gReadPtr != -1)
|
robert@464
|
95 sample += gSampleData.samples[gReadPtr++]; // ...read each sample...
|
robert@464
|
96
|
robert@464
|
97 if(gReadPtr >= gSampleData.sampleLen)
|
robert@464
|
98 gReadPtr = -1;
|
robert@464
|
99
|
robert@464
|
100 out = lb0*sample+lb1*gLastX[0]+lb2*gLastX[1]-la1*gLastY[0]-la2*gLastY[1];
|
robert@464
|
101
|
robert@464
|
102 gLastX[1] = gLastX[0];
|
robert@464
|
103 gLastX[0] = out;
|
robert@464
|
104 gLastY[1] = gLastY[0];
|
robert@464
|
105 gLastY[0] = out;
|
robert@464
|
106
|
robert@544
|
107 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
|
robert@544
|
108 context->audioOut[n * context->audioOutChannels + channel] = out; // ...and put it in both left and right channel
|
robert@464
|
109
|
robert@464
|
110 }
|
robert@464
|
111
|
robert@464
|
112 // Request that the lower-priority tasks run at next opportunity
|
robert@464
|
113 Bela_scheduleAuxiliaryTask(gChangeCoeffTask);
|
robert@464
|
114 Bela_scheduleAuxiliaryTask(gInputTask);
|
robert@464
|
115 }
|
robert@464
|
116
|
robert@464
|
117 // First calculation of coefficients
|
robert@464
|
118
|
robert@464
|
119 void initialise_filter(float freq)
|
robert@464
|
120 {
|
robert@464
|
121 calculate_coeff(freq);
|
robert@464
|
122 }
|
robert@464
|
123
|
robert@464
|
124
|
robert@464
|
125 // Calculate the filter coefficients
|
robert@464
|
126 // second order low pass butterworth filter
|
robert@464
|
127
|
robert@464
|
128 void calculate_coeff(float cutFreq)
|
robert@464
|
129 {
|
robert@464
|
130 // Initialise any previous state (clearing buffers etc.)
|
robert@464
|
131 // to prepare for calls to render()
|
robert@464
|
132 float sampleRate = 44100;
|
robert@464
|
133 double f = 2*M_PI*cutFreq/sampleRate;
|
robert@464
|
134 double denom = 4+2*sqrt(2)*f+f*f;
|
robert@464
|
135 lb0 = f*f/denom;
|
robert@464
|
136 lb1 = 2*lb0;
|
robert@464
|
137 lb2 = lb0;
|
robert@464
|
138 la1 = (2*f*f-8)/denom;
|
robert@464
|
139 la2 = (f*f+4-2*sqrt(2)*f)/denom;
|
robert@464
|
140 gLastX[0] = gLastX [1] = 0;
|
robert@464
|
141 gLastY[0] = gLastY[1] = 0;
|
robert@464
|
142
|
robert@464
|
143 }
|
robert@464
|
144
|
robert@464
|
145
|
robert@464
|
146 // Initialise the auxiliary tasks
|
robert@464
|
147 // and print info
|
robert@464
|
148
|
robert@464
|
149 bool initialise_aux_tasks()
|
robert@464
|
150 {
|
robert@464
|
151 if((gChangeCoeffTask = Bela_createAuxiliaryTask(&check_coeff, 90, "bela-check-coeff")) == 0)
|
robert@464
|
152 return false;
|
robert@464
|
153
|
robert@464
|
154 if((gInputTask = Bela_createAuxiliaryTask(&read_input, 50, "bela-read-input")) == 0)
|
robert@464
|
155 return false;
|
robert@464
|
156
|
robert@464
|
157 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
|
robert@464
|
158 rt_printf("Press 'z' to low down cut-off freq of 100 Hz, 'x' to raise it up\n");
|
robert@464
|
159 rt_printf("Press 'q' to quit\n");
|
robert@464
|
160
|
robert@464
|
161 return true;
|
robert@464
|
162 }
|
robert@464
|
163
|
robert@464
|
164 // Check if cut-off freq has been changed
|
robert@464
|
165 // and new coefficients are needed
|
robert@464
|
166
|
robert@464
|
167 void check_coeff()
|
robert@464
|
168 {
|
robert@464
|
169 if(gChangeCoeff == 1)
|
robert@464
|
170 {
|
robert@464
|
171 gCutFreq += gFreqDelta;
|
robert@464
|
172 gCutFreq = gCutFreq < 0 ? 0 : gCutFreq;
|
robert@464
|
173 gCutFreq = gCutFreq > 22050 ? 22050 : gCutFreq;
|
robert@464
|
174
|
robert@464
|
175 rt_printf("Cut-off frequency: %f\n", gCutFreq);
|
robert@464
|
176
|
robert@464
|
177 calculate_coeff(gCutFreq);
|
robert@464
|
178 gChangeCoeff = 0;
|
robert@464
|
179 }
|
robert@464
|
180 }
|
robert@464
|
181
|
robert@464
|
182 // This is a lower-priority call to periodically read keyboard input
|
robert@464
|
183 // and trigger samples. By placing it at a lower priority,
|
robert@464
|
184 // it has minimal effect on the audio performance but it will take longer to
|
robert@464
|
185 // complete if the system is under heavy audio load.
|
robert@464
|
186
|
robert@464
|
187 void read_input()
|
robert@464
|
188 {
|
robert@464
|
189 // This is not a real-time task!
|
robert@464
|
190 // Cos getchar is a system call, not handled by Xenomai.
|
robert@464
|
191 // This task will be automatically down graded.
|
robert@464
|
192
|
robert@464
|
193 char keyStroke = '.';
|
robert@464
|
194
|
robert@464
|
195 keyStroke = getchar();
|
robert@464
|
196 while(getchar()!='\n'); // to read the first stroke
|
robert@464
|
197
|
robert@464
|
198 switch (keyStroke)
|
robert@464
|
199 {
|
robert@464
|
200 case 'a':
|
robert@464
|
201 gReadPtr = 0;
|
robert@464
|
202 break;
|
robert@464
|
203 case 's':
|
robert@464
|
204 gReadPtr = -1;
|
robert@464
|
205 break;
|
robert@464
|
206 case 'z':
|
robert@464
|
207 gChangeCoeff = 1;
|
robert@464
|
208 gFreqDelta = -100;
|
robert@464
|
209 break;
|
robert@464
|
210 case 'x':
|
robert@464
|
211 gChangeCoeff = 1;
|
robert@464
|
212 gFreqDelta = 100;
|
robert@464
|
213 break;
|
robert@464
|
214 case 'q':
|
robert@464
|
215 gShouldStop = true;
|
robert@464
|
216 break;
|
robert@464
|
217 default:
|
robert@464
|
218 break;
|
robert@464
|
219 }
|
robert@464
|
220 }
|
robert@464
|
221
|
robert@464
|
222
|
robert@464
|
223 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
224 {
|
robert@464
|
225 delete[] gSampleData.samples;
|
robert@464
|
226 }
|
robert@464
|
227
|
robert@464
|
228
|
robert@464
|
229 /**
|
robert@500
|
230 \example filter-IIR/render.cpp
|
robert@464
|
231
|
robert@464
|
232 Infinite Impulse Response Filter
|
robert@464
|
233 ------------------------------
|
robert@464
|
234
|
robert@464
|
235 This is an example of a infinite impulse response filter implementation.
|
robert@464
|
236 */
|