annotate examples/02-Digital/level-meter/render.cpp @ 538:58652b93ef7e prerelease

Converted some spaces to tabs
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 24 Jun 2016 01:40:25 +0100
parents 8fcfbfb32aa0
children 8f8809c77dda
rev   line source
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 #define NUMBER_OF_SEGMENTS 10
robert@464 29
robert@464 30 // Two levels of audio: one follows current value, the other holds
robert@464 31 // peaks for longer
robert@464 32 float gAudioLocalLevel = 0, gAudioPeakLevel = 0;
robert@464 33
robert@464 34 // Decay rates for detecting levels
robert@464 35 float gLocalDecayRate = 0.99, gPeakDecayRate = 0.999;
robert@464 36
robert@464 37 // Thresholds for LEDs: set in setup()
robert@464 38 float gThresholds[NUMBER_OF_SEGMENTS + 1];
robert@464 39 int gSamplesToLight[NUMBER_OF_SEGMENTS];
robert@464 40
robert@464 41 // High-pass filter on the input
robert@464 42 float gLastX[2] = {0};
robert@464 43 float gLastY[2] = {0};
robert@464 44
robert@464 45 // These coefficients make a high-pass filter at 5Hz for 44.1kHz sample rate
robert@464 46 double gB0 = 0.99949640;
robert@464 47 double gB1 = -1.99899280;
robert@464 48 double gB2 = gB0;
robert@464 49 double gA1 = -1.99899254;
robert@464 50 double gA2 = 0.99899305;
robert@464 51
robert@464 52 bool setup(BelaContext *context, void *userData)
robert@464 53 {
robert@464 54 // This project makes the assumption that the audio and digital
robert@464 55 // sample rates are the same. But check it to be sure!
robert@464 56 if(context->audioFrames != context->digitalFrames) {
robert@464 57 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n");
robert@464 58 return false;
robert@464 59 }
robert@464 60
robert@464 61 // Initialise threshold levels in -3dB steps. One extra for efficiency in render()
robert@464 62 // Level = 10^(dB/20)
robert@464 63 for(int i = 0; i < NUMBER_OF_SEGMENTS + 1; i++) {
robert@464 64 gThresholds[i] = powf(10.0f, (-1.0 * (NUMBER_OF_SEGMENTS - i)) * .05);
robert@464 65 }
robert@464 66
robert@464 67 for(int i = 0; i < NUMBER_OF_SEGMENTS; i++) {
robert@464 68 gSamplesToLight[i] = 0;
robert@464 69 pinMode(context, 0, i, OUTPUT);
robert@464 70 }
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 // Get average of audio input channels
robert@464 79 float sample = 0;
robert@464 80 for(unsigned int ch = 0; ch < context->audioChannels; ch++) {
robert@464 81 context->audioOut[n * context->audioChannels + ch] =
robert@464 82 context->audioIn[n * context->audioChannels + ch];
robert@464 83 sample += context->audioIn[n * context->audioChannels + ch];
robert@464 84 }
robert@464 85
robert@464 86 // Do DC-blocking on the sum
robert@464 87 float out = gB0 * sample + gB1 * gLastX[0] + gB2 * gLastX[1]
robert@464 88 - gA1 * gLastY[0] - gA2 * gLastY[1];
robert@464 89
robert@464 90 gLastX[1] = gLastX[0];
robert@464 91 gLastX[0] = sample;
robert@464 92 gLastY[1] = gLastY[0];
robert@464 93 gLastY[0] = out;
robert@464 94
robert@464 95 out = fabsf(out / (float)context->audioChannels);
robert@464 96
robert@464 97 // Do peak detection: fast-responding local level
robert@464 98 if(out > gAudioLocalLevel)
robert@464 99 gAudioLocalLevel = out;
robert@464 100 else
robert@464 101 gAudioLocalLevel *= gLocalDecayRate;
robert@464 102
robert@464 103 // Do peak detection: slow-responding peak level
robert@464 104 if(out > gAudioPeakLevel)
robert@464 105 gAudioPeakLevel = out;
robert@464 106 else {
robert@464 107 // Make peak decay slowly by only multiplying
robert@464 108 // every few samples
robert@464 109 if(((context->audioFramesElapsed + n) & 31) == 0)
robert@464 110 gAudioPeakLevel *= gPeakDecayRate;
robert@464 111 }
robert@464 112 // LED bargraph on digital outputs 0-9
robert@464 113 for(int led = 0; led < NUMBER_OF_SEGMENTS; led++) {
robert@464 114 // All LEDs up to the local level light up. The LED
robert@464 115 // for the peak level also remains lit.
robert@464 116 int state = LOW;
robert@464 117
robert@464 118 if(gAudioLocalLevel > gThresholds[led]) {
robert@464 119 state = HIGH;
robert@464 120 gSamplesToLight[led] = 1000;
robert@464 121 }
robert@464 122 /*else if(gAudioPeakLevel > gThresholds[led] && gAudioPeakLevel <= gThresholds[led + 1]) {
robert@464 123 state = HIGH;
robert@464 124 gSamplesToLight[led] = 1000;
robert@464 125 }*/
robert@464 126 else if(--gSamplesToLight[led] > 0)
robert@464 127 state = HIGH;
robert@464 128
robert@464 129 // Write LED
robert@464 130 digitalWriteOnce(context, n, led, state);
robert@464 131 }
robert@464 132 }
robert@464 133 }
robert@464 134
robert@464 135 void cleanup(BelaContext *context, void *userData)
robert@464 136 {
robert@464 137
robert@464 138 }