annotate examples/02-Digital/level-meter/render.cpp @ 543:8f8809c77dda prerelease

updated basics, digital, instruments, extras examples
author chnrx <chris.heinrichs@gmail.com>
date Fri, 24 Jun 2016 13:19:52 +0100
parents 8fcfbfb32aa0
children
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 }
chris@543 60
chris@543 61 // For this example we need the same amount of audio input and output channels
chris@543 62 if(context->audioInChannels != context->audioOutChannels){
chris@543 63 printf("Error: for this project, you need the same number of audio input and output channels.\n");
chris@543 64 return false;
chris@543 65 }
robert@464 66
robert@464 67 // Initialise threshold levels in -3dB steps. One extra for efficiency in render()
robert@464 68 // Level = 10^(dB/20)
robert@464 69 for(int i = 0; i < NUMBER_OF_SEGMENTS + 1; i++) {
robert@464 70 gThresholds[i] = powf(10.0f, (-1.0 * (NUMBER_OF_SEGMENTS - i)) * .05);
robert@464 71 }
robert@464 72
robert@464 73 for(int i = 0; i < NUMBER_OF_SEGMENTS; i++) {
robert@464 74 gSamplesToLight[i] = 0;
robert@464 75 pinMode(context, 0, i, OUTPUT);
robert@464 76 }
robert@464 77
robert@464 78 return true;
robert@464 79 }
robert@464 80
robert@464 81 void render(BelaContext *context, void *userData)
robert@464 82 {
robert@464 83 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 84 // Get average of audio input channels
robert@464 85 float sample = 0;
chris@543 86 for(unsigned int ch = 0; ch < context->audioInChannels; ch++) {
chris@543 87 context->audioOut[n * context->audioOutChannels + ch] =
chris@543 88 context->audioIn[n * context->audioInChannels + ch];
chris@543 89 sample += context->audioIn[n * context->audioInChannels + ch];
robert@464 90 }
robert@464 91
robert@464 92 // Do DC-blocking on the sum
robert@464 93 float out = gB0 * sample + gB1 * gLastX[0] + gB2 * gLastX[1]
robert@464 94 - gA1 * gLastY[0] - gA2 * gLastY[1];
robert@464 95
robert@464 96 gLastX[1] = gLastX[0];
robert@464 97 gLastX[0] = sample;
robert@464 98 gLastY[1] = gLastY[0];
robert@464 99 gLastY[0] = out;
robert@464 100
chris@543 101 out = fabsf(out / (float)context->audioOutChannels);
robert@464 102
robert@464 103 // Do peak detection: fast-responding local level
robert@464 104 if(out > gAudioLocalLevel)
robert@464 105 gAudioLocalLevel = out;
robert@464 106 else
robert@464 107 gAudioLocalLevel *= gLocalDecayRate;
robert@464 108
robert@464 109 // Do peak detection: slow-responding peak level
robert@464 110 if(out > gAudioPeakLevel)
robert@464 111 gAudioPeakLevel = out;
robert@464 112 else {
robert@464 113 // Make peak decay slowly by only multiplying
robert@464 114 // every few samples
robert@464 115 if(((context->audioFramesElapsed + n) & 31) == 0)
robert@464 116 gAudioPeakLevel *= gPeakDecayRate;
robert@464 117 }
robert@464 118 // LED bargraph on digital outputs 0-9
robert@464 119 for(int led = 0; led < NUMBER_OF_SEGMENTS; led++) {
robert@464 120 // All LEDs up to the local level light up. The LED
robert@464 121 // for the peak level also remains lit.
robert@464 122 int state = LOW;
robert@464 123
robert@464 124 if(gAudioLocalLevel > gThresholds[led]) {
robert@464 125 state = HIGH;
robert@464 126 gSamplesToLight[led] = 1000;
robert@464 127 }
robert@464 128 /*else if(gAudioPeakLevel > gThresholds[led] && gAudioPeakLevel <= gThresholds[led + 1]) {
robert@464 129 state = HIGH;
robert@464 130 gSamplesToLight[led] = 1000;
robert@464 131 }*/
robert@464 132 else if(--gSamplesToLight[led] > 0)
robert@464 133 state = HIGH;
robert@464 134
robert@464 135 // Write LED
robert@464 136 digitalWriteOnce(context, n, led, state);
robert@464 137 }
robert@464 138 }
robert@464 139 }
robert@464 140
robert@464 141 void cleanup(BelaContext *context, void *userData)
robert@464 142 {
robert@464 143
robert@464 144 }