annotate projects/basic_analog_input/render.cpp @ 200:c3a34eaef0cf

Add -lsndfile to Makefile
author Liam Donovan <l.b.donovan@qmul.ac.uk>
date Fri, 05 Feb 2016 16:01:25 +0000
parents 72726dd4f66c
children ac8eb07afcf5
rev   line source
andrewm@57 1 /*
andrewm@57 2 * render.cpp
andrewm@57 3 *
andrewm@57 4 * Created on: Oct 24, 2014
andrewm@57 5 * Author: parallels
andrewm@57 6 */
andrewm@57 7
andrewm@57 8
andrewm@57 9 #include <BeagleRT.h>
andrewm@57 10 #include <Utilities.h>
andrewm@57 11 #include <rtdk.h>
andrewm@57 12 #include <cmath>
andrewm@57 13
andrewm@57 14 float gPhase;
andrewm@57 15 float gInverseSampleRate;
andrewm@57 16 int gAudioFramesPerAnalogFrame;
andrewm@57 17
andrewm@57 18 // These settings are carried over from main.cpp
andrewm@57 19 // Setting global variables is an alternative approach
andrewm@57 20 // to passing a structure to userData in setup()
andrewm@57 21
andrewm@57 22 extern int gSensorInputFrequency;
andrewm@57 23 extern int gSensorInputAmplitude;
andrewm@57 24
andrewm@57 25 // setup() is called once before the audio rendering starts.
andrewm@57 26 // Use it to perform any initialisation and allocation which is dependent
andrewm@57 27 // on the period size or sample rate.
andrewm@57 28 //
andrewm@57 29 // userData holds an opaque pointer to a data structure that was passed
andrewm@57 30 // in from the call to initAudio().
andrewm@57 31 //
andrewm@57 32 // Return true on success; returning false halts the program.
andrewm@57 33
andrewm@57 34 bool setup(BeagleRTContext *context, void *userData)
andrewm@57 35 {
andrewm@57 36 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
andrewm@57 37 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
andrewm@57 38 return false;
andrewm@57 39 }
andrewm@57 40
andrewm@57 41 gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
andrewm@57 42 gInverseSampleRate = 1.0 / context->audioSampleRate;
andrewm@57 43 gPhase = 0.0;
andrewm@57 44
andrewm@57 45 return true;
andrewm@57 46 }
andrewm@57 47
andrewm@57 48 // render() is called regularly at the highest priority by the audio engine.
andrewm@57 49 // Input and output are given from the audio hardware and the other
andrewm@57 50 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@57 51 // will be 0.
andrewm@57 52
andrewm@57 53 void render(BeagleRTContext *context, void *userData)
andrewm@57 54 {
andrewm@57 55 float frequency = 440.0;
andrewm@57 56 float amplitude = 0.8;
andrewm@57 57
andrewm@57 58 // There are twice as many audio frames as matrix frames since audio sample rate
andrewm@57 59 // is twice as high
andrewm@57 60
andrewm@57 61 for(unsigned int n = 0; n < context->audioFrames; n++) {
andrewm@57 62 if(!(n % gAudioFramesPerAnalogFrame)) {
andrewm@57 63 // Even audio samples: update frequency and amplitude from the matrix
andrewm@57 64 frequency = map(analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000);
andrewm@57 65 amplitude = analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude);
andrewm@57 66 }
andrewm@57 67
andrewm@57 68 float out = amplitude * sinf(gPhase);
andrewm@57 69
andrewm@57 70 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
andrewm@57 71 context->audioOut[n * context->audioChannels + channel] = out;
andrewm@57 72
andrewm@57 73 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
andrewm@57 74 if(gPhase > 2.0 * M_PI)
andrewm@57 75 gPhase -= 2.0 * M_PI;
andrewm@57 76 }
andrewm@57 77 }
andrewm@57 78
andrewm@57 79 // cleanup() is called once at the end, after the audio has stopped.
andrewm@57 80 // Release any resources that were allocated in setup().
andrewm@57 81
andrewm@57 82 void cleanup(BeagleRTContext *context, void *userData)
andrewm@57 83 {
andrewm@57 84
andrewm@57 85 }