annotate projects/basic_sensor/render.cpp @ 55:41d24dba6b74 newapi

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