annotate projects/analogDigitalDemo/render.cpp @ 52:a6d223473ea2 newapi

Updated examples for new API. tank_wars not yet updated; audio_in_FFT and oscillator_bank not working properly yet.
author andrewm
date Sun, 31 May 2015 02:13:39 -0500
parents 83baffda5786
children 3c3a1357657d
rev   line source
giuliomoro@23 1 /*
giuliomoro@19 2 *
giuliomoro@19 3 * Andrew McPherson and Victor Zappi
giuliomoro@19 4 * Queen Mary, University of London
giuliomoro@19 5 */
giuliomoro@19 6
andrewm@52 7 #include "../../include/BeagleRT.h"
andrewm@52 8 #include "../../include/Utilities.h"
giuliomoro@19 9 #include <cmath>
giuliomoro@19 10 #include <rtdk.h>
giuliomoro@19 11
giuliomoro@19 12 /* TASK: declare any global variables you need here */
giuliomoro@19 13
giuliomoro@19 14 // initialise_render() is called once before the audio rendering starts.
giuliomoro@19 15 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@19 16 // on the period size or sample rate.
giuliomoro@19 17 //
giuliomoro@19 18 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@19 19 // in from the call to initAudio().
giuliomoro@19 20 //
giuliomoro@19 21 // Return true on success; returning false halts the program.
andrewm@52 22
andrewm@52 23 bool initialise_render(BeagleRTContext *context, void *userData)
giuliomoro@19 24 {
giuliomoro@19 25 return true;
giuliomoro@19 26 }
giuliomoro@19 27
giuliomoro@19 28 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@19 29 // Input and output are given from the audio hardware and the other
giuliomoro@20 30 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
giuliomoro@19 31 // will be 0.
giuliomoro@19 32
andrewm@52 33 void render(BeagleRTContext *context, void *userData)
giuliomoro@19 34 /*
giuliomoro@23 35 we assume that gNumAnalogChannels=8, numAnalogFrames==8 and numDigitalFrames==numAudioFrames
giuliomoro@19 36 * */
giuliomoro@19 37 {
andrewm@52 38 if((context->audioSampleCount&31)==0){ //every 32 frames...
giuliomoro@23 39 //ANALOG channels
andrewm@52 40 analogWriteFrame(context, 0, 0, analogReadFrame(context, 0,0));
andrewm@52 41 // read the input0 at frame0 and write it to output0 frame0. Using analogWrite will fill the rest of the buffer with the same value
giuliomoro@23 42 // The value at the last frame will persist through the successive buffers until is set again.
giuliomoro@23 43 // This effectively is a pass-through with downsampling by 32 times
andrewm@52 44 analogWriteFrame(context, 0, 3, 1.0); // write 1.0 to channel3 from frame0 to the end of the buffer
andrewm@52 45 analogWriteFrame(context, 4, 3, 0.1); // write 0.1 to channel3 from frame4 to the end of the buffer
andrewm@52 46 analogWriteFrameOnce(context, 6, 3, 0.2); //write 0.2 to channel3 only on frame 6
giuliomoro@23 47 //this buffer for channel 3 will look like this: 1 1 1 1 0.1 0.1 0.2 0.1
giuliomoro@23 48 //the next buffers for channel 3 will be filled up with 0.1 ....
giuliomoro@23 49 //DIGITAL channels
andrewm@52 50 digitalWriteFrame(context, 0, P8_07, GPIO_HIGH); //sets all the frames to HIGH for channel 0
andrewm@52 51 digitalWriteFrameOnce(context, 4, P8_07, GPIO_LOW); //only frame 4 will be LOW for channel 0
giuliomoro@23 52 // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1
giuliomoro@23 53 // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer)
andrewm@52 54 digitalWriteFrame(context, 0, P8_08, GPIO_HIGH);
andrewm@52 55 digitalWriteFrame(context, 2, P8_08, GPIO_LOW);
andrewm@52 56 digitalWriteFrame(context, 4, P8_08, GPIO_HIGH);
andrewm@52 57 digitalWriteFrame(context, 5, P8_08, GPIO_LOW);
andrewm@52 58 pinModeFrame(context, 0, P9_16, GPIO_INPUT); // set channel 10 to input
giuliomoro@23 59 // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0
giuliomoro@23 60 // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer)
giuliomoro@19 61 }
andrewm@52 62 for(unsigned int n=0; n<context->audioFrames; n++){
andrewm@52 63 for(unsigned int c=0; c<context->audioChannels; c++){
andrewm@52 64 context->audioOut[n*context->audioChannels + c]=context->audioIn[n*context->audioChannels + c];
giuliomoro@23 65 }
giuliomoro@33 66 //use digital channels 2-8 to create a 7 bit binary counter
andrewm@52 67 context->digital[n]=context->digital[n] & (~0b111111100); // set to zero (GPIO_OUTPUT) the bits in the lower word
andrewm@52 68 context->digital[n]=context->digital[n] & ((~0b111111100<<16) | 0xffff ); //initialize to zero the bits in the higher word (output value)
andrewm@52 69 context->digital[n]=context->digital[n] | ( ((context->audioSampleCount&0b1111111)<<(16+2)) ) ; // set the bits in the higher word to the desired output value, keeping the lower word unchanged
andrewm@52 70 digitalWriteFrame(context, n, P8_29, digitalReadFrame(context, n, P8_30)); // echo the input from from channel 15 to channel 14
andrewm@52 71 digitalWriteFrame(context, n, P8_28, digitalReadFrame(context, n, P9_16)); // echo the input from from channel 10 to channel 13
andrewm@52 72 pinModeFrame(context, 0, P8_30, 0); //set channel 15 to input
giuliomoro@19 73 }
giuliomoro@23 74
andrewm@52 75 for(unsigned int n=0; n<context->analogFrames; n++){
andrewm@52 76 analogWriteFrame(context, n, 1, (context->audioSampleCount&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames
andrewm@52 77 analogWriteFrame(context, n, 2, analogReadFrame(context, n, 2)); // writes a single frame. channel2 is just a passthrough
giuliomoro@23 78 // rt_printf("Analog out frame %d :",n);
giuliomoro@23 79 // for(int c=0; c<gNumAnalogChannels; c++)
giuliomoro@23 80 // rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]);
giuliomoro@23 81 // rt_printf("\n");
giuliomoro@19 82 }
giuliomoro@23 83 return;
giuliomoro@23 84
giuliomoro@19 85 }
giuliomoro@19 86 // cleanup_render() is called once at the end, after the audio has stopped.
giuliomoro@19 87 // Release any resources that were allocated in initialise_render().
giuliomoro@19 88
andrewm@52 89 void cleanup_render(BeagleRTContext *context, void *userData)
giuliomoro@19 90 {
giuliomoro@19 91 /* TASK:
giuliomoro@19 92 * If you allocate any memory, be sure to release it here.
giuliomoro@19 93 * You may or may not need anything in this function, depending
giuliomoro@19 94 * on your implementation.
giuliomoro@19 95 */
giuliomoro@19 96 }