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@464
|
66 // Retrieve a parameter passed in from the initAudio() call
|
robert@464
|
67 gSampleData = *(SampleData *)userData;
|
robert@464
|
68
|
robert@464
|
69 gReadPtr = -1;
|
robert@464
|
70
|
robert@464
|
71 initialise_filter(200);
|
robert@464
|
72
|
robert@464
|
73 // Initialise auxiliary tasks
|
robert@464
|
74 if(!initialise_aux_tasks())
|
robert@464
|
75 return false;
|
robert@464
|
76
|
robert@464
|
77 return true;
|
robert@464
|
78 }
|
robert@464
|
79
|
robert@464
|
80 void render(BelaContext *context, void *userData)
|
robert@464
|
81 {
|
robert@464
|
82 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
83 float sample = 0;
|
robert@464
|
84 float out = 0;
|
robert@464
|
85
|
robert@464
|
86 // If triggered...
|
robert@464
|
87 if(gReadPtr != -1)
|
robert@464
|
88 sample += gSampleData.samples[gReadPtr++]; // ...read each sample...
|
robert@464
|
89
|
robert@464
|
90 if(gReadPtr >= gSampleData.sampleLen)
|
robert@464
|
91 gReadPtr = -1;
|
robert@464
|
92
|
robert@464
|
93 out = lb0*sample+lb1*gLastX[0]+lb2*gLastX[1]-la1*gLastY[0]-la2*gLastY[1];
|
robert@464
|
94
|
robert@464
|
95 gLastX[1] = gLastX[0];
|
robert@464
|
96 gLastX[0] = out;
|
robert@464
|
97 gLastY[1] = gLastY[0];
|
robert@464
|
98 gLastY[0] = out;
|
robert@464
|
99
|
robert@464
|
100 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
robert@464
|
101 context->audioOut[n * context->audioChannels + channel] = out; // ...and put it in both left and right channel
|
robert@464
|
102
|
robert@464
|
103 }
|
robert@464
|
104
|
robert@464
|
105 // Request that the lower-priority tasks run at next opportunity
|
robert@464
|
106 Bela_scheduleAuxiliaryTask(gChangeCoeffTask);
|
robert@464
|
107 Bela_scheduleAuxiliaryTask(gInputTask);
|
robert@464
|
108 }
|
robert@464
|
109
|
robert@464
|
110 // First calculation of coefficients
|
robert@464
|
111
|
robert@464
|
112 void initialise_filter(float freq)
|
robert@464
|
113 {
|
robert@464
|
114 calculate_coeff(freq);
|
robert@464
|
115 }
|
robert@464
|
116
|
robert@464
|
117
|
robert@464
|
118 // Calculate the filter coefficients
|
robert@464
|
119 // second order low pass butterworth filter
|
robert@464
|
120
|
robert@464
|
121 void calculate_coeff(float cutFreq)
|
robert@464
|
122 {
|
robert@464
|
123 // Initialise any previous state (clearing buffers etc.)
|
robert@464
|
124 // to prepare for calls to render()
|
robert@464
|
125 float sampleRate = 44100;
|
robert@464
|
126 double f = 2*M_PI*cutFreq/sampleRate;
|
robert@464
|
127 double denom = 4+2*sqrt(2)*f+f*f;
|
robert@464
|
128 lb0 = f*f/denom;
|
robert@464
|
129 lb1 = 2*lb0;
|
robert@464
|
130 lb2 = lb0;
|
robert@464
|
131 la1 = (2*f*f-8)/denom;
|
robert@464
|
132 la2 = (f*f+4-2*sqrt(2)*f)/denom;
|
robert@464
|
133 gLastX[0] = gLastX [1] = 0;
|
robert@464
|
134 gLastY[0] = gLastY[1] = 0;
|
robert@464
|
135
|
robert@464
|
136 }
|
robert@464
|
137
|
robert@464
|
138
|
robert@464
|
139 // Initialise the auxiliary tasks
|
robert@464
|
140 // and print info
|
robert@464
|
141
|
robert@464
|
142 bool initialise_aux_tasks()
|
robert@464
|
143 {
|
robert@464
|
144 if((gChangeCoeffTask = Bela_createAuxiliaryTask(&check_coeff, 90, "bela-check-coeff")) == 0)
|
robert@464
|
145 return false;
|
robert@464
|
146
|
robert@464
|
147 if((gInputTask = Bela_createAuxiliaryTask(&read_input, 50, "bela-read-input")) == 0)
|
robert@464
|
148 return false;
|
robert@464
|
149
|
robert@464
|
150 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
|
robert@464
|
151 rt_printf("Press 'z' to low down cut-off freq of 100 Hz, 'x' to raise it up\n");
|
robert@464
|
152 rt_printf("Press 'q' to quit\n");
|
robert@464
|
153
|
robert@464
|
154 return true;
|
robert@464
|
155 }
|
robert@464
|
156
|
robert@464
|
157 // Check if cut-off freq has been changed
|
robert@464
|
158 // and new coefficients are needed
|
robert@464
|
159
|
robert@464
|
160 void check_coeff()
|
robert@464
|
161 {
|
robert@464
|
162 if(gChangeCoeff == 1)
|
robert@464
|
163 {
|
robert@464
|
164 gCutFreq += gFreqDelta;
|
robert@464
|
165 gCutFreq = gCutFreq < 0 ? 0 : gCutFreq;
|
robert@464
|
166 gCutFreq = gCutFreq > 22050 ? 22050 : gCutFreq;
|
robert@464
|
167
|
robert@464
|
168 rt_printf("Cut-off frequency: %f\n", gCutFreq);
|
robert@464
|
169
|
robert@464
|
170 calculate_coeff(gCutFreq);
|
robert@464
|
171 gChangeCoeff = 0;
|
robert@464
|
172 }
|
robert@464
|
173 }
|
robert@464
|
174
|
robert@464
|
175 // This is a lower-priority call to periodically read keyboard input
|
robert@464
|
176 // and trigger samples. By placing it at a lower priority,
|
robert@464
|
177 // it has minimal effect on the audio performance but it will take longer to
|
robert@464
|
178 // complete if the system is under heavy audio load.
|
robert@464
|
179
|
robert@464
|
180 void read_input()
|
robert@464
|
181 {
|
robert@464
|
182 // This is not a real-time task!
|
robert@464
|
183 // Cos getchar is a system call, not handled by Xenomai.
|
robert@464
|
184 // This task will be automatically down graded.
|
robert@464
|
185
|
robert@464
|
186 char keyStroke = '.';
|
robert@464
|
187
|
robert@464
|
188 keyStroke = getchar();
|
robert@464
|
189 while(getchar()!='\n'); // to read the first stroke
|
robert@464
|
190
|
robert@464
|
191 switch (keyStroke)
|
robert@464
|
192 {
|
robert@464
|
193 case 'a':
|
robert@464
|
194 gReadPtr = 0;
|
robert@464
|
195 break;
|
robert@464
|
196 case 's':
|
robert@464
|
197 gReadPtr = -1;
|
robert@464
|
198 break;
|
robert@464
|
199 case 'z':
|
robert@464
|
200 gChangeCoeff = 1;
|
robert@464
|
201 gFreqDelta = -100;
|
robert@464
|
202 break;
|
robert@464
|
203 case 'x':
|
robert@464
|
204 gChangeCoeff = 1;
|
robert@464
|
205 gFreqDelta = 100;
|
robert@464
|
206 break;
|
robert@464
|
207 case 'q':
|
robert@464
|
208 gShouldStop = true;
|
robert@464
|
209 break;
|
robert@464
|
210 default:
|
robert@464
|
211 break;
|
robert@464
|
212 }
|
robert@464
|
213 }
|
robert@464
|
214
|
robert@464
|
215
|
robert@464
|
216 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
217 {
|
robert@464
|
218 delete[] gSampleData.samples;
|
robert@464
|
219 }
|
robert@464
|
220
|
robert@464
|
221
|
robert@464
|
222 /**
|
robert@500
|
223 \example filter-IIR/render.cpp
|
robert@464
|
224
|
robert@464
|
225 Infinite Impulse Response Filter
|
robert@464
|
226 ------------------------------
|
robert@464
|
227
|
robert@464
|
228 This is an example of a infinite impulse response filter implementation.
|
robert@464
|
229 */
|