annotate projects/bucket_brigade_chorus/render.cpp @ 156:89f28a867a09

Imported stats.hpp from other branch. This includes moving average template class
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 07 Oct 2015 23:38:52 +0100
parents 9a413516a1fc
children 2bdb48d1fca6
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 Scope scope; //create a scope object
giuliomoro@98 11
giuliomoro@98 12 // initialise_render() is called once before the audio rendering starts.
giuliomoro@98 13 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@98 14 // on the period size or sample rate.
giuliomoro@98 15 //
giuliomoro@98 16 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@98 17 // in from the call to initAudio().
giuliomoro@98 18 //
giuliomoro@98 19 // Return true on success; returning false halts the program.
giuliomoro@98 20 #include <I2c_Codec.h>
giuliomoro@98 21 #include <PRU.h>
giuliomoro@98 22 extern I2c_Codec *gAudioCodec;
giuliomoro@98 23 extern PRU *gPRU;
giuliomoro@98 24 float D=5264;
giuliomoro@98 25 #define delayLength 512
giuliomoro@98 26 float delay[delayLength];
giuliomoro@98 27 int writePointer=0;
giuliomoro@98 28 int readPointer=writePointer+1;
giuliomoro@98 29 AuxiliaryTask updatePll;
giuliomoro@98 30
giuliomoro@98 31 void updatePllFunction(){
giuliomoro@98 32 // rt_printf("now\n");
giuliomoro@98 33 gPRU->setGPIOTestPin();
giuliomoro@98 34 gAudioCodec->setPllD(D);
giuliomoro@98 35 gPRU->clearGPIOTestPin();
giuliomoro@98 36 }
giuliomoro@98 37
giuliomoro@98 38 bool setup(BeagleRTContext *context, void *userData)
giuliomoro@98 39 {
giuliomoro@98 40 scope.setup(context->audioSampleRate); //call this once in setup to initialise the scope
giuliomoro@98 41
giuliomoro@98 42 gInverseSampleRate = 1.0/context->audioSampleRate;
giuliomoro@98 43
giuliomoro@98 44 gPhase1 = 0.0;
giuliomoro@98 45 gPhase2 = 0.0;
giuliomoro@98 46
giuliomoro@98 47 gFrequency1 = 200.0;
giuliomoro@98 48 gFrequency2 = 201.0;
giuliomoro@98 49 updatePll=BeagleRT_createAuxiliaryTask(&updatePllFunction, 98, "update PLL");
giuliomoro@98 50 for(int n=0; n<delayLength; n++){
giuliomoro@98 51 delay[n]=0;
giuliomoro@98 52 }
giuliomoro@98 53 return true;
giuliomoro@98 54 }
giuliomoro@98 55
giuliomoro@98 56 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@98 57 // Input and output are given from the audio hardware and the other
giuliomoro@98 58 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@98 59 // will be 0.
giuliomoro@98 60
giuliomoro@98 61 void render(BeagleRTContext *context, void *userData)
giuliomoro@98 62 {
giuliomoro@98 63 static int count=0;
giuliomoro@98 64 static float lfoPhase=0;
giuliomoro@98 65 static float feedback=0;
giuliomoro@98 66 int updateRate=8;
giuliomoro@98 67 if((count&(updateRate-1))==0 && digitalReadFrame(context,0,P8_07)==GPIO_HIGH){
giuliomoro@98 68 float amplitude=context->analogIn[0]/0.84*4990;
giuliomoro@98 69 float rate=context->analogIn[1]*20+0.1;
giuliomoro@98 70 lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate;
giuliomoro@98 71 D=amplitude+amplitude*sinf(lfoPhase);
giuliomoro@98 72 BeagleRT_scheduleAuxiliaryTask(updatePll);
giuliomoro@98 73 if((count&255)==0){
giuliomoro@98 74 rt_printf("gpio: %d\n",digitalReadFrame(context,0,P8_07));
giuliomoro@98 75 rt_printf("D: %.0f\n", D);
giuliomoro@98 76 rt_printf("rate: %f\n", rate/2);
giuliomoro@98 77 rt_printf("amplitude: %.3f\n", amplitude);
giuliomoro@98 78 rt_printf("feedback: %.3f\n\n", feedback);
giuliomoro@98 79 }
giuliomoro@98 80 }
giuliomoro@98 81 count++;
giuliomoro@98 82
giuliomoro@98 83 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@98 84 feedback=context->analogIn[n/2*context->analogChannels+2]/0.84*1.2;
giuliomoro@98 85 if(digitalReadFrame(context,n,P8_08)==GPIO_LOW)
giuliomoro@98 86 feedback=0;
giuliomoro@98 87 delay[writePointer++]=context->audioIn[n*context->audioChannels+0] + delay[readPointer]*feedback;
giuliomoro@98 88 context->audioOut[n*context->audioChannels+0]=context->audioIn[n*context->audioChannels+0]+delay[readPointer++];
giuliomoro@98 89 // context->audioOut[n*context->audioChannels+1]=sinf(gPhase1);
giuliomoro@98 90 context->analogOut[n/2*context->analogChannels+0]=D/10000;
giuliomoro@98 91 if(writePointer>=delayLength)
giuliomoro@98 92 writePointer-=delayLength;
giuliomoro@98 93 if(readPointer>=delayLength)
giuliomoro@98 94 readPointer-=delayLength;
giuliomoro@98 95
giuliomoro@98 96 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate;
giuliomoro@98 97 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
giuliomoro@98 98 if(gPhase1 > 2.0 * M_PI)
giuliomoro@98 99 gPhase1 -= 2.0 * M_PI;
giuliomoro@98 100 if(gPhase2 > 2.0 * M_PI)
giuliomoro@98 101 gPhase2 -= 2.0 * M_PI;
giuliomoro@98 102
giuliomoro@98 103 }
giuliomoro@98 104 }
giuliomoro@98 105
giuliomoro@98 106 // cleanup_render() is called once at the end, after the audio has stopped.
giuliomoro@98 107 // Release any resources that were allocated in initialise_render().
giuliomoro@98 108
giuliomoro@98 109 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@98 110 {
giuliomoro@98 111
giuliomoro@98 112 }