Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
542:3016638b4da2 | 543:8f8809c77dda |
---|---|
55 // sample rates are the same. But check it to be sure! | 55 // sample rates are the same. But check it to be sure! |
56 if(context->audioFrames != context->digitalFrames) { | 56 if(context->audioFrames != context->digitalFrames) { |
57 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n"); | 57 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n"); |
58 return false; | 58 return false; |
59 } | 59 } |
60 | |
61 // For this example we need the same amount of audio input and output channels | |
62 if(context->audioInChannels != context->audioOutChannels){ | |
63 printf("Error: for this project, you need the same number of audio input and output channels.\n"); | |
64 return false; | |
65 } | |
60 | 66 |
61 // Initialise threshold levels in -3dB steps. One extra for efficiency in render() | 67 // Initialise threshold levels in -3dB steps. One extra for efficiency in render() |
62 // Level = 10^(dB/20) | 68 // Level = 10^(dB/20) |
63 for(int i = 0; i < NUMBER_OF_SEGMENTS + 1; i++) { | 69 for(int i = 0; i < NUMBER_OF_SEGMENTS + 1; i++) { |
64 gThresholds[i] = powf(10.0f, (-1.0 * (NUMBER_OF_SEGMENTS - i)) * .05); | 70 gThresholds[i] = powf(10.0f, (-1.0 * (NUMBER_OF_SEGMENTS - i)) * .05); |
75 void render(BelaContext *context, void *userData) | 81 void render(BelaContext *context, void *userData) |
76 { | 82 { |
77 for(unsigned int n = 0; n < context->audioFrames; n++) { | 83 for(unsigned int n = 0; n < context->audioFrames; n++) { |
78 // Get average of audio input channels | 84 // Get average of audio input channels |
79 float sample = 0; | 85 float sample = 0; |
80 for(unsigned int ch = 0; ch < context->audioChannels; ch++) { | 86 for(unsigned int ch = 0; ch < context->audioInChannels; ch++) { |
81 context->audioOut[n * context->audioChannels + ch] = | 87 context->audioOut[n * context->audioOutChannels + ch] = |
82 context->audioIn[n * context->audioChannels + ch]; | 88 context->audioIn[n * context->audioInChannels + ch]; |
83 sample += context->audioIn[n * context->audioChannels + ch]; | 89 sample += context->audioIn[n * context->audioInChannels + ch]; |
84 } | 90 } |
85 | 91 |
86 // Do DC-blocking on the sum | 92 // Do DC-blocking on the sum |
87 float out = gB0 * sample + gB1 * gLastX[0] + gB2 * gLastX[1] | 93 float out = gB0 * sample + gB1 * gLastX[0] + gB2 * gLastX[1] |
88 - gA1 * gLastY[0] - gA2 * gLastY[1]; | 94 - gA1 * gLastY[0] - gA2 * gLastY[1]; |
90 gLastX[1] = gLastX[0]; | 96 gLastX[1] = gLastX[0]; |
91 gLastX[0] = sample; | 97 gLastX[0] = sample; |
92 gLastY[1] = gLastY[0]; | 98 gLastY[1] = gLastY[0]; |
93 gLastY[0] = out; | 99 gLastY[0] = out; |
94 | 100 |
95 out = fabsf(out / (float)context->audioChannels); | 101 out = fabsf(out / (float)context->audioOutChannels); |
96 | 102 |
97 // Do peak detection: fast-responding local level | 103 // Do peak detection: fast-responding local level |
98 if(out > gAudioLocalLevel) | 104 if(out > gAudioLocalLevel) |
99 gAudioLocalLevel = out; | 105 gAudioLocalLevel = out; |
100 else | 106 else |