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>
|
robert@464
|
26 #include <cmath>
|
robert@464
|
27
|
robert@464
|
28 int gBufferSize = 8192;
|
robert@464
|
29
|
robert@464
|
30 // Double buffers to hold samples for noise analysis
|
robert@464
|
31 float *gReadBuffers[10], *gWriteBuffers[10];
|
robert@464
|
32 float *gBuffers0[10], *gBuffers1[10];
|
robert@464
|
33
|
robert@464
|
34 int gWriteBufferPointers[10], gReadBufferPointers[10];
|
robert@464
|
35
|
robert@464
|
36 // Task to analyse and print results which would otherwise be too slow for render()
|
robert@464
|
37 AuxiliaryTask gAnalysisTask;
|
robert@464
|
38
|
robert@464
|
39 void analyseResults();
|
robert@464
|
40
|
robert@464
|
41 // setup() is called once before the audio rendering starts.
|
robert@464
|
42 // Use it to perform any initialisation and allocation which is dependent
|
robert@464
|
43 // on the period size or sample rate.
|
robert@464
|
44 //
|
robert@464
|
45 // userData holds an opaque pointer to a data structure that was passed
|
robert@464
|
46 // in from the call to initAudio().
|
robert@464
|
47 //
|
robert@464
|
48 // Return true on success; returning false halts the program.
|
robert@464
|
49
|
robert@464
|
50 bool setup(BelaContext *context, void *userData)
|
robert@464
|
51 {
|
robert@464
|
52 // Clear the filter data structures
|
robert@464
|
53 for(int i = 0; i < 10; i++) {
|
robert@464
|
54 gReadBufferPointers[i] = gWriteBufferPointers[i] = 0;
|
robert@464
|
55 gBuffers0[i] = new float[gBufferSize];
|
robert@464
|
56 gBuffers1[i] = new float[gBufferSize];
|
robert@464
|
57 gWriteBuffers[i] = gBuffers0[i];
|
robert@464
|
58 gReadBuffers[i] = gBuffers1[i];
|
robert@464
|
59 if(gBuffers0[i] == 0 || gBuffers1[i] == 0) {
|
robert@464
|
60 rt_printf("Error allocating buffer %d\n", i);
|
robert@464
|
61 return false;
|
robert@464
|
62 }
|
robert@464
|
63 }
|
robert@464
|
64
|
robert@464
|
65 gAnalysisTask = Bela_createAuxiliaryTask(analyseResults, 50, "bela-analyse-results");
|
robert@464
|
66
|
robert@464
|
67 return true;
|
robert@464
|
68 }
|
robert@464
|
69
|
robert@464
|
70 // render() is called regularly at the highest priority by the audio engine.
|
robert@464
|
71 // Input and output are given from the audio hardware and the other
|
robert@464
|
72 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
robert@464
|
73 // will be 0.
|
robert@464
|
74
|
robert@464
|
75 void render(BelaContext *context, void *userData)
|
robert@464
|
76 {
|
robert@464
|
77 bool bufferIsFull = false; // Whether at least one buffer has filled
|
robert@464
|
78
|
robert@464
|
79 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
80 // Store audio inputs in buffer
|
robert@464
|
81 for(unsigned int ch = 0; ch < context->audioChannels; ch++) {
|
robert@464
|
82 if(gWriteBufferPointers[ch] < gBufferSize) {
|
robert@464
|
83 gWriteBuffers[ch][gWriteBufferPointers[ch]] =
|
robert@464
|
84 context->audioIn[n * context->audioChannels + ch];
|
robert@464
|
85 gWriteBufferPointers[ch]++;
|
robert@464
|
86 if(gWriteBufferPointers[ch] >= gBufferSize)
|
robert@464
|
87 bufferIsFull = true;
|
robert@464
|
88 }
|
robert@464
|
89 }
|
robert@464
|
90 }
|
robert@464
|
91
|
robert@464
|
92 if(context->analogChannels != 0) {
|
robert@464
|
93 for(unsigned int n = 0; n < context->analogFrames; n++) {
|
robert@464
|
94 // Store analog inputs in buffer, starting at channel 2
|
robert@464
|
95 for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
|
robert@464
|
96 if(gWriteBufferPointers[ch + 2] < gBufferSize) {
|
robert@464
|
97 gWriteBuffers[ch + 2][gWriteBufferPointers[ch + 2]] =
|
robert@464
|
98 context->analogIn[n * context->analogChannels + ch];
|
robert@464
|
99 gWriteBufferPointers[ch + 2]++;
|
robert@464
|
100 if(gWriteBufferPointers[ch + 2] >= gBufferSize)
|
robert@464
|
101 bufferIsFull = true;
|
robert@464
|
102 }
|
robert@464
|
103
|
robert@464
|
104 // Set all analog outputs to halfway point so they can be more
|
robert@464
|
105 // easily measured for noise
|
robert@464
|
106 context->analogOut[n * context->analogChannels + ch] = 0.5;
|
robert@464
|
107 }
|
robert@464
|
108 }
|
robert@464
|
109 }
|
robert@464
|
110
|
robert@464
|
111
|
robert@464
|
112 if(bufferIsFull) {
|
robert@464
|
113 // Swap buffers and reset write pointers
|
robert@464
|
114 for(int ch = 0; ch < 10; ch++) {
|
robert@464
|
115 gReadBufferPointers[ch] = gWriteBufferPointers[ch];
|
robert@464
|
116 gWriteBufferPointers[ch] = 0;
|
robert@464
|
117
|
robert@464
|
118 if(gReadBuffers[ch] == gBuffers0[ch]) {
|
robert@464
|
119 gReadBuffers[ch] = gBuffers1[ch];
|
robert@464
|
120 gWriteBuffers[ch] = gBuffers0[ch];
|
robert@464
|
121 }
|
robert@464
|
122 else {
|
robert@464
|
123 gReadBuffers[ch] = gBuffers0[ch];
|
robert@464
|
124 gWriteBuffers[ch] = gBuffers1[ch];
|
robert@464
|
125 }
|
robert@464
|
126 }
|
robert@464
|
127
|
robert@464
|
128 Bela_scheduleAuxiliaryTask(gAnalysisTask);
|
robert@464
|
129 }
|
robert@464
|
130 }
|
robert@464
|
131
|
robert@464
|
132 void analyseResults()
|
robert@464
|
133 {
|
robert@464
|
134 rt_printf("\e[1;1H\e[2J"); // Command to clear the screen
|
robert@464
|
135
|
robert@464
|
136 // Print the analysis results. channels 0-1 are audio, channels 2-9 are analog
|
robert@464
|
137 for(int ch = 0; ch < 10; ch++) {
|
robert@464
|
138 // Skip unused channels
|
robert@464
|
139 if(gReadBufferPointers[ch] == 0)
|
robert@464
|
140 continue;
|
robert@464
|
141
|
robert@464
|
142 float mean = 0;
|
robert@464
|
143 for(int n = 0; n < gReadBufferPointers[ch]; n++) {
|
robert@464
|
144 mean += gReadBuffers[ch][n];
|
robert@464
|
145 }
|
robert@464
|
146 mean /= (float)gReadBufferPointers[ch];
|
robert@464
|
147
|
robert@464
|
148 float rms = 0;
|
robert@464
|
149 for(int n = 0; n < gReadBufferPointers[ch]; n++) {
|
robert@464
|
150 rms += (gReadBuffers[ch][n] - mean) * (gReadBuffers[ch][n] - mean);
|
robert@464
|
151 }
|
robert@464
|
152 rms = sqrtf(rms / (float)gReadBufferPointers[ch]);
|
robert@464
|
153
|
robert@464
|
154 if(ch == 0)
|
robert@464
|
155 rt_printf("Audio In L: ");
|
robert@464
|
156 else if(ch == 1)
|
robert@464
|
157 rt_printf("Audio In R: ");
|
robert@464
|
158 else
|
robert@464
|
159 rt_printf("Analog In %d: ", ch - 2);
|
robert@464
|
160
|
robert@464
|
161 rt_printf("Noise %6.1fdB DC offset %6.4f (%6.1fdB) window size: %d\n",
|
robert@464
|
162 20.0f * log10f(rms),
|
robert@464
|
163 mean,
|
robert@464
|
164 20.0f * log10f(fabsf(mean)),
|
robert@464
|
165 gReadBufferPointers[ch]);
|
robert@464
|
166 }
|
robert@464
|
167 }
|
robert@464
|
168
|
robert@464
|
169 // cleanup() is called once at the end, after the audio has stopped.
|
robert@464
|
170 // Release any resources that were allocated in setup().
|
robert@464
|
171
|
robert@464
|
172 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
173 {
|
robert@464
|
174 for(int i = 0; i < 10; i++) {
|
robert@464
|
175 delete gBuffers0[i];
|
robert@464
|
176 delete gBuffers1[i];
|
robert@464
|
177 }
|
robert@464
|
178 }
|