annotate projects/basic/render.cpp @ 47:643cbee74eda newapi

First draft of Doxygen documentation
author andrewm
date Thu, 28 May 2015 17:06:03 -0400
parents 579c86316008
children 3c3a1357657d
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@45 9 #include "../../include/BeagleRT.h"
andrewm@0 10 #include <cmath>
andrewm@0 11
andrewm@0 12 float gFrequency;
andrewm@0 13 float gPhase;
andrewm@0 14 float gInverseSampleRate;
andrewm@0 15
andrewm@0 16 // initialise_render() is called once before the audio rendering starts.
andrewm@0 17 // Use it to perform any initialisation and allocation which is dependent
andrewm@0 18 // on the period size or sample rate.
andrewm@0 19 //
andrewm@0 20 // userData holds an opaque pointer to a data structure that was passed
andrewm@0 21 // in from the call to initAudio().
andrewm@0 22 //
andrewm@0 23 // Return true on success; returning false halts the program.
andrewm@0 24
andrewm@45 25 bool initialise_render(BeagleRTContext *context, void *userData)
andrewm@0 26 {
andrewm@0 27 // Retrieve a parameter passed in from the initAudio() call
andrewm@0 28 gFrequency = *(float *)userData;
andrewm@0 29
andrewm@45 30 gInverseSampleRate = 1.0 / context->audioSampleRate;
andrewm@0 31 gPhase = 0.0;
andrewm@0 32
andrewm@0 33 return true;
andrewm@0 34 }
andrewm@0 35
andrewm@0 36 // render() is called regularly at the highest priority by the audio engine.
andrewm@0 37 // Input and output are given from the audio hardware and the other
andrewm@0 38 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@0 39 // will be 0.
andrewm@0 40
andrewm@45 41 void render(BeagleRTContext *context, void *userData)
andrewm@0 42 {
andrewm@45 43 for(unsigned int n = 0; n < context->audioFrames; n++) {
andrewm@0 44 float out = 0.8f * sinf(gPhase);
andrewm@0 45 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
andrewm@0 46 if(gPhase > 2.0 * M_PI)
andrewm@0 47 gPhase -= 2.0 * M_PI;
andrewm@0 48
andrewm@45 49 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
andrewm@45 50 context->audioOut[n * context->audioChannels + channel] = out;
andrewm@0 51 }
andrewm@0 52 }
andrewm@0 53
andrewm@0 54 // cleanup_render() is called once at the end, after the audio has stopped.
andrewm@0 55 // Release any resources that were allocated in initialise_render().
andrewm@0 56
andrewm@45 57 void cleanup_render(BeagleRTContext *context, void *userData)
andrewm@0 58 {
andrewm@0 59
andrewm@0 60 }