annotate examples/bucket_brigade_chorus/render.cpp @ 380:9dc5a0ccad25 prerelease

Fixed missing ) in Makefile, and removed unnecessary Utilities.h includes
author andrewm
date Sun, 12 Jun 2016 23:03:57 +0100
parents 1feb9c23ac57
children
rev   line source
giuliomoro@301 1 #include <Bela.h>
giuliomoro@98 2 #include <Scope.h>
giuliomoro@98 3 #include <cmath>
giuliomoro@98 4
giuliomoro@98 5 float gPhase1, gPhase2;
giuliomoro@98 6 float gFrequency1, gFrequency2;
giuliomoro@98 7 float gInverseSampleRate;
giuliomoro@98 8
giuliomoro@98 9 // initialise_render() is called once before the audio rendering starts.
giuliomoro@98 10 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@98 11 // on the period size or sample rate.
giuliomoro@98 12 //
giuliomoro@98 13 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@98 14 // in from the call to initAudio().
giuliomoro@98 15 //
giuliomoro@98 16 // Return true on success; returning false halts the program.
giuliomoro@98 17 #include <I2c_Codec.h>
giuliomoro@98 18 #include <PRU.h>
giuliomoro@98 19 extern I2c_Codec *gAudioCodec;
giuliomoro@98 20 extern PRU *gPRU;
giuliomoro@98 21 float D=5264;
giuliomoro@183 22 #define delayLength 256
giuliomoro@98 23 float delay[delayLength];
giuliomoro@98 24 int writePointer=0;
giuliomoro@98 25 int readPointer=writePointer+1;
giuliomoro@98 26 AuxiliaryTask updatePll;
giuliomoro@98 27
giuliomoro@98 28 void updatePllFunction(){
giuliomoro@183 29 // gPRU->setGPIOTestPin();
giuliomoro@183 30 static int count = 0;
giuliomoro@183 31 while(!gShouldStop){
giuliomoro@183 32 gAudioCodec->setPllD(D);
giuliomoro@183 33 count++;
giuliomoro@183 34 if((count&4095)==0)
giuliomoro@183 35 printf("sampling rate: %f\n",gAudioCodec->getAudioSamplingRate());
giuliomoro@183 36 usleep(100);
giuliomoro@183 37 }
giuliomoro@183 38 // gPRU->clearGPIOTestPin();
giuliomoro@98 39 }
giuliomoro@98 40
giuliomoro@301 41 bool setup(BelaContext *context, void *userData)
giuliomoro@98 42 {
giuliomoro@98 43 gInverseSampleRate = 1.0/context->audioSampleRate;
giuliomoro@98 44
giuliomoro@98 45 gPhase1 = 0.0;
giuliomoro@98 46 gPhase2 = 0.0;
giuliomoro@98 47
giuliomoro@98 48 gFrequency1 = 200.0;
giuliomoro@98 49 gFrequency2 = 201.0;
giuliomoro@301 50 updatePll=Bela_createAuxiliaryTask(&updatePllFunction, 91, "update PLL");
giuliomoro@98 51 for(int n=0; n<delayLength; n++){
giuliomoro@98 52 delay[n]=0;
giuliomoro@98 53 }
giuliomoro@98 54 return true;
giuliomoro@98 55 }
giuliomoro@98 56
giuliomoro@98 57 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@98 58 // Input and output are given from the audio hardware and the other
giuliomoro@98 59 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@98 60 // will be 0.
giuliomoro@98 61
giuliomoro@301 62 void render(BelaContext *context, void *userData)
giuliomoro@98 63 {
giuliomoro@183 64 // printf("here\n");
giuliomoro@183 65 static bool init = false;
giuliomoro@183 66 if(init == false){
giuliomoro@301 67 Bela_scheduleAuxiliaryTask(updatePll);
giuliomoro@183 68 // gAudioCodec->setPllP(2);
giuliomoro@183 69 // gAudioCodec->setPllR();
giuliomoro@183 70 // gAudioCodec->setAudioSamplingRate(43600);
giuliomoro@183 71 // printf("samplingRate: %f, k: %f\n", gAudioCodec->getAudioSamplingRate(), gAudioCodec->getPllK());
giuliomoro@183 72 init = true;
giuliomoro@183 73 }
giuliomoro@98 74 static int count=0;
giuliomoro@98 75 static float lfoPhase=0;
giuliomoro@98 76 static float feedback=0;
giuliomoro@183 77 int updateRate=1;
giuliomoro@183 78 if((count&(updateRate-1))==0){
giuliomoro@183 79 float amplitude = 8000;
giuliomoro@183 80 float rate = 2;
giuliomoro@98 81 lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate;
giuliomoro@98 82 D=amplitude+amplitude*sinf(lfoPhase);
giuliomoro@98 83 if((count&255)==0){
giuliomoro@183 84 // rt_printf("frequency: %f\n", gAudioCodec->getAudioSamplingRate());
giuliomoro@183 85 // rt_printf("D: %.0f\n", D);
giuliomoro@183 86 // rt_printf("rate: %f\n", rate);
giuliomoro@183 87 // rt_printf("amplitude: %.3f\n", amplitude);
giuliomoro@183 88 // rt_printf("feedback: %.3f\n\n", feedback);
giuliomoro@98 89 }
giuliomoro@98 90 }
giuliomoro@98 91 count++;
giuliomoro@98 92
giuliomoro@98 93 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@183 94 feedback = 0.4;
andrewm@308 95 float input = audioRead(context, n, 0) + audioRead(context, n, 1);
giuliomoro@183 96 delay[writePointer++] = input + delay[readPointer]*feedback;
giuliomoro@183 97 float output = (input + 0.9*delay[readPointer++] ) * 0.5;
andrewm@308 98 audioWrite(context, n, 0, output);
andrewm@308 99 audioWrite(context, n, 1, output);
giuliomoro@98 100 if(writePointer>=delayLength)
giuliomoro@98 101 writePointer-=delayLength;
giuliomoro@98 102 if(readPointer>=delayLength)
giuliomoro@98 103 readPointer-=delayLength;
giuliomoro@98 104
giuliomoro@98 105 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate;
giuliomoro@98 106 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
giuliomoro@98 107 if(gPhase1 > 2.0 * M_PI)
giuliomoro@98 108 gPhase1 -= 2.0 * M_PI;
giuliomoro@98 109 if(gPhase2 > 2.0 * M_PI)
giuliomoro@98 110 gPhase2 -= 2.0 * M_PI;
giuliomoro@98 111 }
giuliomoro@98 112 }
giuliomoro@98 113
giuliomoro@98 114 // cleanup_render() is called once at the end, after the audio has stopped.
giuliomoro@98 115 // Release any resources that were allocated in initialise_render().
giuliomoro@98 116
giuliomoro@301 117 void cleanup(BelaContext *context, void *userData)
giuliomoro@98 118 {
giuliomoro@98 119
giuliomoro@98 120 }