robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: robert@464: int gBufferSize = 8192; robert@464: robert@464: // Double buffers to hold samples for noise analysis robert@464: float *gReadBuffers[10], *gWriteBuffers[10]; robert@464: float *gBuffers0[10], *gBuffers1[10]; robert@464: robert@464: int gWriteBufferPointers[10], gReadBufferPointers[10]; robert@464: robert@464: // Task to analyse and print results which would otherwise be too slow for render() robert@464: AuxiliaryTask gAnalysisTask; robert@464: robert@464: void analyseResults(); robert@464: robert@464: // setup() is called once before the audio rendering starts. robert@464: // Use it to perform any initialisation and allocation which is dependent robert@464: // on the period size or sample rate. robert@464: // robert@464: // userData holds an opaque pointer to a data structure that was passed robert@464: // in from the call to initAudio(). robert@464: // robert@464: // Return true on success; returning false halts the program. robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@544: robert@544: // Check that we have the same number of inputs and outputs. robert@544: if(context->audioInChannels != context->audioOutChannels || robert@544: context->analogInChannels != context-> analogOutChannels){ robert@544: printf("Error: for this project, you need the same number of input and output channels.\n"); robert@544: return false; robert@544: } robert@544: robert@464: // Clear the filter data structures robert@464: for(int i = 0; i < 10; i++) { robert@464: gReadBufferPointers[i] = gWriteBufferPointers[i] = 0; robert@464: gBuffers0[i] = new float[gBufferSize]; robert@464: gBuffers1[i] = new float[gBufferSize]; robert@464: gWriteBuffers[i] = gBuffers0[i]; robert@464: gReadBuffers[i] = gBuffers1[i]; robert@464: if(gBuffers0[i] == 0 || gBuffers1[i] == 0) { robert@464: rt_printf("Error allocating buffer %d\n", i); robert@464: return false; robert@464: } robert@464: } robert@464: robert@464: gAnalysisTask = Bela_createAuxiliaryTask(analyseResults, 50, "bela-analyse-results"); robert@464: robert@464: return true; robert@464: } robert@464: robert@464: // render() is called regularly at the highest priority by the audio engine. robert@464: // Input and output are given from the audio hardware and the other robert@464: // ADCs and DACs (if available). If only audio is available, numMatrixFrames robert@464: // will be 0. robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: bool bufferIsFull = false; // Whether at least one buffer has filled robert@464: robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: // Store audio inputs in buffer robert@544: for(unsigned int ch = 0; ch < context->audioOutChannels; ch++) { robert@464: if(gWriteBufferPointers[ch] < gBufferSize) { robert@464: gWriteBuffers[ch][gWriteBufferPointers[ch]] = robert@544: context->audioIn[n * context->audioOutChannels + ch]; robert@464: gWriteBufferPointers[ch]++; robert@464: if(gWriteBufferPointers[ch] >= gBufferSize) robert@464: bufferIsFull = true; robert@464: } robert@464: } robert@464: } robert@464: robert@544: if(context->analogOutChannels != 0) { robert@464: for(unsigned int n = 0; n < context->analogFrames; n++) { robert@464: // Store analog inputs in buffer, starting at channel 2 robert@544: for(unsigned int ch = 0; ch < context->analogOutChannels; ch++) { robert@464: if(gWriteBufferPointers[ch + 2] < gBufferSize) { robert@464: gWriteBuffers[ch + 2][gWriteBufferPointers[ch + 2]] = robert@544: context->analogIn[n * context->analogOutChannels + ch]; robert@464: gWriteBufferPointers[ch + 2]++; robert@464: if(gWriteBufferPointers[ch + 2] >= gBufferSize) robert@464: bufferIsFull = true; robert@464: } robert@464: robert@464: // Set all analog outputs to halfway point so they can be more robert@464: // easily measured for noise robert@544: context->analogOut[n * context->analogOutChannels + ch] = 0.5; robert@464: } robert@464: } robert@464: } robert@464: robert@464: robert@464: if(bufferIsFull) { robert@464: // Swap buffers and reset write pointers robert@464: for(int ch = 0; ch < 10; ch++) { robert@464: gReadBufferPointers[ch] = gWriteBufferPointers[ch]; robert@464: gWriteBufferPointers[ch] = 0; robert@464: robert@464: if(gReadBuffers[ch] == gBuffers0[ch]) { robert@464: gReadBuffers[ch] = gBuffers1[ch]; robert@464: gWriteBuffers[ch] = gBuffers0[ch]; robert@464: } robert@464: else { robert@464: gReadBuffers[ch] = gBuffers0[ch]; robert@464: gWriteBuffers[ch] = gBuffers1[ch]; robert@464: } robert@464: } robert@464: robert@464: Bela_scheduleAuxiliaryTask(gAnalysisTask); robert@464: } robert@464: } robert@464: robert@464: void analyseResults() robert@464: { robert@464: rt_printf("\e[1;1H\e[2J"); // Command to clear the screen robert@464: robert@464: // Print the analysis results. channels 0-1 are audio, channels 2-9 are analog robert@464: for(int ch = 0; ch < 10; ch++) { robert@464: // Skip unused channels robert@464: if(gReadBufferPointers[ch] == 0) robert@464: continue; robert@464: robert@464: float mean = 0; robert@464: for(int n = 0; n < gReadBufferPointers[ch]; n++) { robert@464: mean += gReadBuffers[ch][n]; robert@464: } robert@464: mean /= (float)gReadBufferPointers[ch]; robert@464: robert@464: float rms = 0; robert@464: for(int n = 0; n < gReadBufferPointers[ch]; n++) { robert@464: rms += (gReadBuffers[ch][n] - mean) * (gReadBuffers[ch][n] - mean); robert@464: } robert@464: rms = sqrtf(rms / (float)gReadBufferPointers[ch]); robert@464: robert@464: if(ch == 0) robert@464: rt_printf("Audio In L: "); robert@464: else if(ch == 1) robert@464: rt_printf("Audio In R: "); robert@464: else robert@464: rt_printf("Analog In %d: ", ch - 2); robert@464: robert@464: rt_printf("Noise %6.1fdB DC offset %6.4f (%6.1fdB) window size: %d\n", robert@464: 20.0f * log10f(rms), robert@464: mean, robert@464: 20.0f * log10f(fabsf(mean)), robert@464: gReadBufferPointers[ch]); robert@464: } robert@464: } robert@464: robert@464: // cleanup() is called once at the end, after the audio has stopped. robert@464: // Release any resources that were allocated in setup(). robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: for(int i = 0; i < 10; i++) { robert@464: delete gBuffers0[i]; robert@464: delete gBuffers1[i]; robert@464: } robert@464: }