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