annotate projects/measure_noisefloor/render.cpp @ 89:d41631e0fe0e

Added noise floor measurement project; also added option to run script to run without screen
author andrewm
date Sun, 19 Jul 2015 16:15:28 +0100
parents
children 241d4d5df929
rev   line source
andrewm@89 1 /*
andrewm@89 2 * render.cpp
andrewm@89 3 *
andrewm@89 4 * Created on: Oct 24, 2014
andrewm@89 5 * Author: parallels
andrewm@89 6 */
andrewm@89 7
andrewm@89 8
andrewm@89 9 #include <BeagleRT.h>
andrewm@89 10 #include <Utilities.h>
andrewm@89 11 #include <cmath>
andrewm@89 12
andrewm@89 13 int gBufferSize = 8192;
andrewm@89 14
andrewm@89 15 // Buffers to hold samples for noise analysis
andrewm@89 16 float* gBuffers[10];
andrewm@89 17 int gBufferPointers[10];
andrewm@89 18
andrewm@89 19 // Outputs to display
andrewm@89 20 float gDCLevels[10];
andrewm@89 21 float gNoiseLevels[10];
andrewm@89 22 float gNumSamplesAnalysed[10];
andrewm@89 23
andrewm@89 24 // Task to print results which would otherwise be too slow for render()
andrewm@89 25 AuxiliaryTask gPrintTask;
andrewm@89 26
andrewm@89 27 void printResults();
andrewm@89 28
andrewm@89 29 // setup() is called once before the audio rendering starts.
andrewm@89 30 // Use it to perform any initialisation and allocation which is dependent
andrewm@89 31 // on the period size or sample rate.
andrewm@89 32 //
andrewm@89 33 // userData holds an opaque pointer to a data structure that was passed
andrewm@89 34 // in from the call to initAudio().
andrewm@89 35 //
andrewm@89 36 // Return true on success; returning false halts the program.
andrewm@89 37
andrewm@89 38 bool setup(BeagleRTContext *context, void *userData)
andrewm@89 39 {
andrewm@89 40 // Clear the filter data structures
andrewm@89 41 for(int i = 0; i < 10; i++) {
andrewm@89 42 gBufferPointers[i] = 0;
andrewm@89 43 gBuffers[i] = new float[gBufferSize];
andrewm@89 44 if(gBuffers[i] == 0) {
andrewm@89 45 rt_printf("Error allocating buffer %d\n", i);
andrewm@89 46 return false;
andrewm@89 47 }
andrewm@89 48 }
andrewm@89 49
andrewm@89 50 gPrintTask = BeagleRT_createAuxiliaryTask(printResults, 50, "beaglert-print-results");
andrewm@89 51
andrewm@89 52 return true;
andrewm@89 53 }
andrewm@89 54
andrewm@89 55 // render() is called regularly at the highest priority by the audio engine.
andrewm@89 56 // Input and output are given from the audio hardware and the other
andrewm@89 57 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@89 58 // will be 0.
andrewm@89 59
andrewm@89 60 void render(BeagleRTContext *context, void *userData)
andrewm@89 61 {
andrewm@89 62 bool bufferIsFull = false; // Whether at least one buffer has filled
andrewm@89 63
andrewm@89 64 for(unsigned int n = 0; n < context->audioFrames; n++) {
andrewm@89 65 // Store audio inputs in buffer
andrewm@89 66 for(unsigned int ch = 0; ch < context->audioChannels; ch++) {
andrewm@89 67 if(gBufferPointers[ch] < gBufferSize) {
andrewm@89 68 gBuffers[ch][gBufferPointers[ch]] =
andrewm@89 69 context->audioIn[n * context->audioChannels + ch];
andrewm@89 70 gBufferPointers[ch]++;
andrewm@89 71 if(gBufferPointers[ch] >= gBufferSize)
andrewm@89 72 bufferIsFull = true;
andrewm@89 73 }
andrewm@89 74 }
andrewm@89 75 }
andrewm@89 76
andrewm@89 77 if(context->analogChannels != 0) {
andrewm@89 78 for(unsigned int n = 0; n < context->analogFrames; n++) {
andrewm@89 79 // Store analog inputs in buffer, starting at channel 2
andrewm@89 80 for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
andrewm@89 81 if(gBufferPointers[ch + 2] < gBufferSize) {
andrewm@89 82 gBuffers[ch + 2][gBufferPointers[ch + 2]] =
andrewm@89 83 context->analogIn[n * context->analogChannels + ch];
andrewm@89 84 gBufferPointers[ch + 2]++;
andrewm@89 85 if(gBufferPointers[ch + 2] >= gBufferSize)
andrewm@89 86 bufferIsFull = true;
andrewm@89 87 }
andrewm@89 88 }
andrewm@89 89 }
andrewm@89 90 }
andrewm@89 91
andrewm@89 92 if(bufferIsFull) {
andrewm@89 93 // Analyse all active channels at once
andrewm@89 94 for(int ch = 0; ch < 10; ch++) {
andrewm@89 95 // gBufferPointers[ch] tells us how many samples were stored in the buffer
andrewm@89 96 gNumSamplesAnalysed[ch] = gBufferPointers[ch];
andrewm@89 97
andrewm@89 98 if(gBufferPointers[ch] != 0) {
andrewm@89 99 float mean = 0;
andrewm@89 100 for(int n = 0; n < gBufferPointers[ch]; n++) {
andrewm@89 101 mean += gBuffers[ch][n];
andrewm@89 102 }
andrewm@89 103 mean /= (float)gBufferPointers[ch];
andrewm@89 104
andrewm@89 105 float rms = 0;
andrewm@89 106 for(int n = 0; n < gBufferPointers[ch]; n++) {
andrewm@89 107 rms += (gBuffers[ch][n] - mean) * (gBuffers[ch][n] - mean);
andrewm@89 108 }
andrewm@89 109 rms = sqrtf(rms / (float)gBufferPointers[ch]);
andrewm@89 110
andrewm@89 111 gDCLevels[ch] = mean;
andrewm@89 112 gNoiseLevels[ch] = rms;
andrewm@89 113 }
andrewm@89 114
andrewm@89 115 // Reset pointer to 0 for next time
andrewm@89 116 gBufferPointers[ch] = 0;
andrewm@89 117 }
andrewm@89 118
andrewm@89 119 BeagleRT_scheduleAuxiliaryTask(gPrintTask);
andrewm@89 120 }
andrewm@89 121 }
andrewm@89 122
andrewm@89 123 void printResults()
andrewm@89 124 {
andrewm@89 125 rt_printf("\e[1;1H\e[2J"); // Command to clear the screen
andrewm@89 126
andrewm@89 127 // Print the analysis results. channels 0-1 are audio, channels 2-9 are analog
andrewm@89 128 for(int ch = 0; ch < 10; ch++) {
andrewm@89 129 int samples = gNumSamplesAnalysed[ch];
andrewm@89 130 if(samples == 0)
andrewm@89 131 continue;
andrewm@89 132
andrewm@89 133 if(ch == 0)
andrewm@89 134 rt_printf("Audio In L: ");
andrewm@89 135 else if(ch == 1)
andrewm@89 136 rt_printf("Audio In R: ");
andrewm@89 137 else
andrewm@89 138 rt_printf("Analog In %d: ", ch - 2);
andrewm@89 139
andrewm@89 140 rt_printf("Noise %6.1fdB DC offset %6.4f (%6.1fdB) window size: %d\n",
andrewm@89 141 20.0f * log10f(gNoiseLevels[ch]),
andrewm@89 142 gDCLevels[ch],
andrewm@89 143 20.0f * log10f(fabsf(gDCLevels[ch])),
andrewm@89 144 samples);
andrewm@89 145 }
andrewm@89 146 }
andrewm@89 147
andrewm@89 148 // cleanup() is called once at the end, after the audio has stopped.
andrewm@89 149 // Release any resources that were allocated in setup().
andrewm@89 150
andrewm@89 151 void cleanup(BeagleRTContext *context, void *userData)
andrewm@89 152 {
andrewm@89 153 for(int i = 0; i < 10; i++)
andrewm@89 154 delete gBuffers[i];
andrewm@89 155 }