annotate examples/04-Audio/bucket-brigade-chorus/render.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children cdabbaf3a252
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <Scope.h>
robert@464 27 #include <cmath>
robert@464 28
robert@464 29 float gPhase1, gPhase2;
robert@464 30 float gFrequency1, gFrequency2;
robert@464 31 float gInverseSampleRate;
robert@464 32
robert@464 33 // initialise_render() is called once before the audio rendering starts.
robert@464 34 // Use it to perform any initialisation and allocation which is dependent
robert@464 35 // on the period size or sample rate.
robert@464 36 //
robert@464 37 // userData holds an opaque pointer to a data structure that was passed
robert@464 38 // in from the call to initAudio().
robert@464 39 //
robert@464 40 // Return true on success; returning false halts the program.
robert@464 41 #include <I2c_Codec.h>
robert@464 42 #include <PRU.h>
robert@464 43 extern I2c_Codec *gAudioCodec;
robert@464 44 extern PRU *gPRU;
robert@464 45 float D=5264;
robert@464 46 #define delayLength 256
robert@464 47 float delay[delayLength];
robert@464 48 int writePointer=0;
robert@464 49 int readPointer=writePointer+1;
robert@464 50 AuxiliaryTask updatePll;
robert@464 51
robert@464 52 void updatePllFunction(){
robert@464 53 // gPRU->setGPIOTestPin();
robert@464 54 static int count = 0;
robert@464 55 while(!gShouldStop){
robert@464 56 gAudioCodec->setPllD(D);
robert@464 57 count++;
robert@464 58 if((count&4095)==0)
robert@464 59 printf("sampling rate: %f\n",gAudioCodec->getAudioSamplingRate());
robert@464 60 usleep(100);
robert@464 61 }
robert@464 62 // gPRU->clearGPIOTestPin();
robert@464 63 }
robert@464 64
robert@464 65 bool setup(BelaContext *context, void *userData)
robert@464 66 {
robert@464 67 gInverseSampleRate = 1.0/context->audioSampleRate;
robert@464 68
robert@464 69 gPhase1 = 0.0;
robert@464 70 gPhase2 = 0.0;
robert@464 71
robert@464 72 gFrequency1 = 200.0;
robert@464 73 gFrequency2 = 201.0;
robert@464 74 updatePll=Bela_createAuxiliaryTask(&updatePllFunction, 91, "update PLL");
robert@464 75 for(int n=0; n<delayLength; n++){
robert@464 76 delay[n]=0;
robert@464 77 }
robert@464 78 return true;
robert@464 79 }
robert@464 80
robert@464 81 // render() is called regularly at the highest priority by the audio engine.
robert@464 82 // Input and output are given from the audio hardware and the other
robert@464 83 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
robert@464 84 // will be 0.
robert@464 85
robert@464 86 void render(BelaContext *context, void *userData)
robert@464 87 {
robert@464 88 // printf("here\n");
robert@464 89 static bool init = false;
robert@464 90 if(init == false){
robert@464 91 Bela_scheduleAuxiliaryTask(updatePll);
robert@464 92 // gAudioCodec->setPllP(2);
robert@464 93 // gAudioCodec->setPllR();
robert@464 94 // gAudioCodec->setAudioSamplingRate(43600);
robert@464 95 // printf("samplingRate: %f, k: %f\n", gAudioCodec->getAudioSamplingRate(), gAudioCodec->getPllK());
robert@464 96 init = true;
robert@464 97 }
robert@464 98 static int count=0;
robert@464 99 static float lfoPhase=0;
robert@464 100 static float feedback=0;
robert@464 101 int updateRate=1;
robert@464 102 if((count&(updateRate-1))==0){
robert@464 103 float amplitude = 8000;
robert@464 104 float rate = 2;
robert@464 105 lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate;
robert@464 106 D=amplitude+amplitude*sinf(lfoPhase);
robert@464 107 if((count&255)==0){
robert@464 108 // rt_printf("frequency: %f\n", gAudioCodec->getAudioSamplingRate());
robert@464 109 // rt_printf("D: %.0f\n", D);
robert@464 110 // rt_printf("rate: %f\n", rate);
robert@464 111 // rt_printf("amplitude: %.3f\n", amplitude);
robert@464 112 // rt_printf("feedback: %.3f\n\n", feedback);
robert@464 113 }
robert@464 114 }
robert@464 115 count++;
robert@464 116
robert@464 117 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 118 feedback = 0.4;
robert@464 119 float input = audioRead(context, n, 0) + audioRead(context, n, 1);
robert@464 120 delay[writePointer++] = input + delay[readPointer]*feedback;
robert@464 121 float output = (input + 0.9*delay[readPointer++] ) * 0.5;
robert@464 122 audioWrite(context, n, 0, output);
robert@464 123 audioWrite(context, n, 1, output);
robert@464 124 if(writePointer>=delayLength)
robert@464 125 writePointer-=delayLength;
robert@464 126 if(readPointer>=delayLength)
robert@464 127 readPointer-=delayLength;
robert@464 128
robert@464 129 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate;
robert@464 130 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
robert@464 131 if(gPhase1 > 2.0 * M_PI)
robert@464 132 gPhase1 -= 2.0 * M_PI;
robert@464 133 if(gPhase2 > 2.0 * M_PI)
robert@464 134 gPhase2 -= 2.0 * M_PI;
robert@464 135 }
robert@464 136 }
robert@464 137
robert@464 138 // cleanup_render() is called once at the end, after the audio has stopped.
robert@464 139 // Release any resources that were allocated in initialise_render().
robert@464 140
robert@464 141 void cleanup(BelaContext *context, void *userData)
robert@464 142 {
robert@464 143
robert@464 144 }