annotate projects/bucket_brigade_chorus/render.cpp @ 183:2bdb48d1fca6

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