Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 182:4c5acd649d46 | 183:2bdb48d1fca6 |
|---|---|
| 4 #include <Utilities.h> | 4 #include <Utilities.h> |
| 5 | 5 |
| 6 float gPhase1, gPhase2; | 6 float gPhase1, gPhase2; |
| 7 float gFrequency1, gFrequency2; | 7 float gFrequency1, gFrequency2; |
| 8 float gInverseSampleRate; | 8 float gInverseSampleRate; |
| 9 | |
| 10 Scope scope; //create a scope object | |
| 11 | 9 |
| 12 // initialise_render() is called once before the audio rendering starts. | 10 // initialise_render() is called once before the audio rendering starts. |
| 13 // Use it to perform any initialisation and allocation which is dependent | 11 // Use it to perform any initialisation and allocation which is dependent |
| 14 // on the period size or sample rate. | 12 // on the period size or sample rate. |
| 15 // | 13 // |
| 20 #include <I2c_Codec.h> | 18 #include <I2c_Codec.h> |
| 21 #include <PRU.h> | 19 #include <PRU.h> |
| 22 extern I2c_Codec *gAudioCodec; | 20 extern I2c_Codec *gAudioCodec; |
| 23 extern PRU *gPRU; | 21 extern PRU *gPRU; |
| 24 float D=5264; | 22 float D=5264; |
| 25 #define delayLength 512 | 23 #define delayLength 256 |
| 26 float delay[delayLength]; | 24 float delay[delayLength]; |
| 27 int writePointer=0; | 25 int writePointer=0; |
| 28 int readPointer=writePointer+1; | 26 int readPointer=writePointer+1; |
| 29 AuxiliaryTask updatePll; | 27 AuxiliaryTask updatePll; |
| 30 | 28 |
| 31 void updatePllFunction(){ | 29 void updatePllFunction(){ |
| 32 // rt_printf("now\n"); | 30 // gPRU->setGPIOTestPin(); |
| 33 gPRU->setGPIOTestPin(); | 31 static int count = 0; |
| 34 gAudioCodec->setPllD(D); | 32 while(!gShouldStop){ |
| 35 gPRU->clearGPIOTestPin(); | 33 gAudioCodec->setPllD(D); |
| 34 count++; | |
| 35 if((count&4095)==0) | |
| 36 printf("sampling rate: %f\n",gAudioCodec->getAudioSamplingRate()); | |
| 37 usleep(100); | |
| 38 } | |
| 39 // gPRU->clearGPIOTestPin(); | |
| 36 } | 40 } |
| 37 | 41 |
| 38 bool setup(BeagleRTContext *context, void *userData) | 42 bool setup(BeagleRTContext *context, void *userData) |
| 39 { | 43 { |
| 40 scope.setup(context->audioSampleRate); //call this once in setup to initialise the scope | |
| 41 | |
| 42 gInverseSampleRate = 1.0/context->audioSampleRate; | 44 gInverseSampleRate = 1.0/context->audioSampleRate; |
| 43 | 45 |
| 44 gPhase1 = 0.0; | 46 gPhase1 = 0.0; |
| 45 gPhase2 = 0.0; | 47 gPhase2 = 0.0; |
| 46 | 48 |
| 47 gFrequency1 = 200.0; | 49 gFrequency1 = 200.0; |
| 48 gFrequency2 = 201.0; | 50 gFrequency2 = 201.0; |
| 49 updatePll=BeagleRT_createAuxiliaryTask(&updatePllFunction, 98, "update PLL"); | 51 updatePll=BeagleRT_createAuxiliaryTask(&updatePllFunction, 91, "update PLL"); |
| 50 for(int n=0; n<delayLength; n++){ | 52 for(int n=0; n<delayLength; n++){ |
| 51 delay[n]=0; | 53 delay[n]=0; |
| 52 } | 54 } |
| 53 return true; | 55 return true; |
| 54 } | 56 } |
| 58 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 60 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
| 59 // will be 0. | 61 // will be 0. |
| 60 | 62 |
| 61 void render(BeagleRTContext *context, void *userData) | 63 void render(BeagleRTContext *context, void *userData) |
| 62 { | 64 { |
| 65 // printf("here\n"); | |
| 66 static bool init = false; | |
| 67 if(init == false){ | |
| 68 BeagleRT_scheduleAuxiliaryTask(updatePll); | |
| 69 // gAudioCodec->setPllP(2); | |
| 70 // gAudioCodec->setPllR(); | |
| 71 // gAudioCodec->setAudioSamplingRate(43600); | |
| 72 // printf("samplingRate: %f, k: %f\n", gAudioCodec->getAudioSamplingRate(), gAudioCodec->getPllK()); | |
| 73 init = true; | |
| 74 } | |
| 63 static int count=0; | 75 static int count=0; |
| 64 static float lfoPhase=0; | 76 static float lfoPhase=0; |
| 65 static float feedback=0; | 77 static float feedback=0; |
| 66 int updateRate=8; | 78 int updateRate=1; |
| 67 if((count&(updateRate-1))==0 && digitalReadFrame(context,0,P8_07)==GPIO_HIGH){ | 79 if((count&(updateRate-1))==0){ |
| 68 float amplitude=context->analogIn[0]/0.84*4990; | 80 float amplitude = 8000; |
| 69 float rate=context->analogIn[1]*20+0.1; | 81 float rate = 2; |
| 70 lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate; | 82 lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate; |
| 71 D=amplitude+amplitude*sinf(lfoPhase); | 83 D=amplitude+amplitude*sinf(lfoPhase); |
| 72 BeagleRT_scheduleAuxiliaryTask(updatePll); | |
| 73 if((count&255)==0){ | 84 if((count&255)==0){ |
| 74 rt_printf("gpio: %d\n",digitalReadFrame(context,0,P8_07)); | 85 // rt_printf("frequency: %f\n", gAudioCodec->getAudioSamplingRate()); |
| 75 rt_printf("D: %.0f\n", D); | 86 // rt_printf("D: %.0f\n", D); |
| 76 rt_printf("rate: %f\n", rate/2); | 87 // rt_printf("rate: %f\n", rate); |
| 77 rt_printf("amplitude: %.3f\n", amplitude); | 88 // rt_printf("amplitude: %.3f\n", amplitude); |
| 78 rt_printf("feedback: %.3f\n\n", feedback); | 89 // rt_printf("feedback: %.3f\n\n", feedback); |
| 79 } | 90 } |
| 80 } | 91 } |
| 81 count++; | 92 count++; |
| 82 | 93 |
| 83 for(unsigned int n = 0; n < context->audioFrames; n++) { | 94 for(unsigned int n = 0; n < context->audioFrames; n++) { |
| 84 feedback=context->analogIn[n/2*context->analogChannels+2]/0.84*1.2; | 95 feedback = 0.4; |
| 85 if(digitalReadFrame(context,n,P8_08)==GPIO_LOW) | 96 float input = audioReadFrame(context, n, 0) + audioReadFrame(context, n, 1); |
| 86 feedback=0; | 97 delay[writePointer++] = input + delay[readPointer]*feedback; |
| 87 delay[writePointer++]=context->audioIn[n*context->audioChannels+0] + delay[readPointer]*feedback; | 98 float output = (input + 0.9*delay[readPointer++] ) * 0.5; |
| 88 context->audioOut[n*context->audioChannels+0]=context->audioIn[n*context->audioChannels+0]+delay[readPointer++]; | 99 audioWriteFrame(context, n, 0, output); |
| 89 // context->audioOut[n*context->audioChannels+1]=sinf(gPhase1); | 100 audioWriteFrame(context, n, 1, output); |
| 90 context->analogOut[n/2*context->analogChannels+0]=D/10000; | |
| 91 if(writePointer>=delayLength) | 101 if(writePointer>=delayLength) |
| 92 writePointer-=delayLength; | 102 writePointer-=delayLength; |
| 93 if(readPointer>=delayLength) | 103 if(readPointer>=delayLength) |
| 94 readPointer-=delayLength; | 104 readPointer-=delayLength; |
| 95 | 105 |
| 97 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; | 107 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; |
| 98 if(gPhase1 > 2.0 * M_PI) | 108 if(gPhase1 > 2.0 * M_PI) |
| 99 gPhase1 -= 2.0 * M_PI; | 109 gPhase1 -= 2.0 * M_PI; |
| 100 if(gPhase2 > 2.0 * M_PI) | 110 if(gPhase2 > 2.0 * M_PI) |
| 101 gPhase2 -= 2.0 * M_PI; | 111 gPhase2 -= 2.0 * M_PI; |
| 102 | |
| 103 } | 112 } |
| 104 } | 113 } |
| 105 | 114 |
| 106 // cleanup_render() is called once at the end, after the audio has stopped. | 115 // cleanup_render() is called once at the end, after the audio has stopped. |
| 107 // Release any resources that were allocated in initialise_render(). | 116 // Release any resources that were allocated in initialise_render(). |
