annotate projects/basic_sensor/render.cpp @ 45:579c86316008 newapi

Major API overhaul. Moved to a single data structure for handling render functions. Functionally, generally similar except for scheduling within PRU loop function, which now uses interrupts from the PRU rather than polling. This requires an updated kernel.
author andrewm
date Thu, 28 May 2015 14:35:55 -0400
parents 6adb088196a7
children a6d223473ea2
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@0 9 #include "../../include/render.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@13 16 int gMatrixFramesPerAudioFrame;
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@12 34 bool initialise_render(int numMatrixChannels, int numAudioChannels,
andrewm@12 35 int numMatrixFramesPerPeriod,
andrewm@12 36 int numAudioFramesPerPeriod,
andrewm@12 37 float matrixSampleRate, float audioSampleRate,
andrewm@12 38 void *userData)
andrewm@0 39 {
andrewm@13 40 if(numMatrixFramesPerPeriod == 0 || numMatrixFramesPerPeriod > numAudioFramesPerPeriod) {
andrewm@13 41 rt_printf("Error: this example needs the matrix enabled, with 4 or 8 channels\n");
andrewm@0 42 return false;
andrewm@0 43 }
andrewm@0 44
andrewm@13 45 gMatrixFramesPerAudioFrame = numAudioFramesPerPeriod / numMatrixFramesPerPeriod;
andrewm@0 46 gInverseSampleRate = 1.0 / audioSampleRate;
andrewm@0 47 gPhase = 0.0;
andrewm@0 48
andrewm@0 49 return true;
andrewm@0 50 }
andrewm@0 51
andrewm@0 52 // render() is called regularly at the highest priority by the audio engine.
andrewm@0 53 // Input and output are given from the audio hardware and the other
andrewm@0 54 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@0 55 // will be 0.
andrewm@0 56
andrewm@0 57 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
andrewm@0 58 uint16_t *matrixIn, uint16_t *matrixOut)
andrewm@0 59 {
andrewm@0 60 float frequency = 0;
andrewm@0 61 float amplitude = 0;
andrewm@0 62
andrewm@0 63 // There are twice as many audio frames as matrix frames since audio sample rate
andrewm@0 64 // is twice as high
andrewm@0 65
andrewm@0 66 for(int n = 0; n < numAudioFrames; n++) {
andrewm@13 67 if(!(n % gMatrixFramesPerAudioFrame)) {
andrewm@0 68 // Even audio samples: update frequency and amplitude from the matrix
andrewm@13 69 frequency = map(analogRead(gSensorInputFrequency, n/gMatrixFramesPerAudioFrame), 0, MATRIX_MAX, 100, 1000);
andrewm@13 70 amplitude = (float)analogRead(gSensorInputAmplitude, n/gMatrixFramesPerAudioFrame) / MATRIX_MAX;
andrewm@0 71 }
andrewm@0 72
andrewm@0 73 float out = amplitude * sinf(gPhase);
andrewm@0 74
andrewm@13 75 for(int channel = 0; channel < gNumAudioChannels; channel++)
andrewm@13 76 audioOut[n * gNumAudioChannels + channel] = out;
andrewm@0 77
andrewm@0 78 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
andrewm@0 79 if(gPhase > 2.0 * M_PI)
andrewm@0 80 gPhase -= 2.0 * M_PI;
andrewm@0 81 }
andrewm@0 82 }
andrewm@0 83
andrewm@0 84 // cleanup_render() is called once at the end, after the audio has stopped.
andrewm@0 85 // Release any resources that were allocated in initialise_render().
andrewm@0 86
andrewm@0 87 void cleanup_render()
andrewm@0 88 {
andrewm@0 89
andrewm@0 90 }