annotate examples/04-Audio/bucket-brigade-chorus/render.cpp @ 552:f8bb6186498d prerelease

added customRender example for predate
author chnrx <chris.heinrichs@gmail.com>
date Fri, 24 Jun 2016 16:22:17 +0100
parents cdabbaf3a252
children
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <Scope.h>
robert@464 27 #include <cmath>
robert@464 28
robert@464 29 float gPhase1, gPhase2;
robert@464 30 float gFrequency1, gFrequency2;
robert@464 31 float gInverseSampleRate;
robert@464 32
robert@464 33 // initialise_render() is called once before the audio rendering starts.
robert@464 34 // Use it to perform any initialisation and allocation which is dependent
robert@464 35 // on the period size or sample rate.
robert@464 36 //
robert@464 37 // userData holds an opaque pointer to a data structure that was passed
robert@464 38 // in from the call to initAudio().
robert@464 39 //
robert@464 40 // Return true on success; returning false halts the program.
robert@464 41 #include <I2c_Codec.h>
robert@464 42 #include <PRU.h>
robert@464 43 extern I2c_Codec *gAudioCodec;
robert@464 44 extern PRU *gPRU;
robert@464 45 float D=5264;
robert@464 46 #define delayLength 256
robert@464 47 float delay[delayLength];
robert@464 48 int writePointer=0;
robert@464 49 int readPointer=writePointer+1;
robert@464 50 AuxiliaryTask updatePll;
robert@464 51
robert@464 52 void updatePllFunction(){
robert@464 53 // gPRU->setGPIOTestPin();
robert@464 54 static int count = 0;
robert@464 55 while(!gShouldStop){
robert@464 56 gAudioCodec->setPllD(D);
robert@464 57 count++;
robert@464 58 if((count&4095)==0)
robert@464 59 printf("sampling rate: %f\n",gAudioCodec->getAudioSamplingRate());
robert@464 60 usleep(100);
robert@464 61 }
robert@464 62 // gPRU->clearGPIOTestPin();
robert@464 63 }
robert@464 64
robert@464 65 bool setup(BelaContext *context, void *userData)
robert@464 66 {
robert@544 67 // Check that we have the same number of inputs and outputs.
robert@544 68 if(context->audioInChannels != context->audioOutChannels ||
robert@544 69 context->analogInChannels != context-> analogOutChannels){
robert@544 70 printf("Error: for this project, you need the same number of input and output channels.\n");
robert@544 71 return false;
robert@544 72 }
robert@544 73
robert@464 74 gInverseSampleRate = 1.0/context->audioSampleRate;
robert@464 75
robert@464 76 gPhase1 = 0.0;
robert@464 77 gPhase2 = 0.0;
robert@464 78
robert@464 79 gFrequency1 = 200.0;
robert@464 80 gFrequency2 = 201.0;
robert@464 81 updatePll=Bela_createAuxiliaryTask(&updatePllFunction, 91, "update PLL");
robert@464 82 for(int n=0; n<delayLength; n++){
robert@464 83 delay[n]=0;
robert@464 84 }
robert@464 85 return true;
robert@464 86 }
robert@464 87
robert@464 88 // render() is called regularly at the highest priority by the audio engine.
robert@464 89 // Input and output are given from the audio hardware and the other
robert@464 90 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
robert@464 91 // will be 0.
robert@464 92
robert@464 93 void render(BelaContext *context, void *userData)
robert@464 94 {
robert@464 95 // printf("here\n");
robert@464 96 static bool init = false;
robert@464 97 if(init == false){
robert@464 98 Bela_scheduleAuxiliaryTask(updatePll);
robert@464 99 // gAudioCodec->setPllP(2);
robert@464 100 // gAudioCodec->setPllR();
robert@464 101 // gAudioCodec->setAudioSamplingRate(43600);
robert@464 102 // printf("samplingRate: %f, k: %f\n", gAudioCodec->getAudioSamplingRate(), gAudioCodec->getPllK());
robert@464 103 init = true;
robert@464 104 }
robert@464 105 static int count=0;
robert@464 106 static float lfoPhase=0;
robert@464 107 static float feedback=0;
robert@464 108 int updateRate=1;
robert@464 109 if((count&(updateRate-1))==0){
robert@464 110 float amplitude = 8000;
robert@464 111 float rate = 2;
robert@464 112 lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate;
robert@464 113 D=amplitude+amplitude*sinf(lfoPhase);
robert@464 114 if((count&255)==0){
robert@464 115 // rt_printf("frequency: %f\n", gAudioCodec->getAudioSamplingRate());
robert@464 116 // rt_printf("D: %.0f\n", D);
robert@464 117 // rt_printf("rate: %f\n", rate);
robert@464 118 // rt_printf("amplitude: %.3f\n", amplitude);
robert@464 119 // rt_printf("feedback: %.3f\n\n", feedback);
robert@464 120 }
robert@464 121 }
robert@464 122 count++;
robert@464 123
robert@464 124 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 125 feedback = 0.4;
robert@464 126 float input = audioRead(context, n, 0) + audioRead(context, n, 1);
robert@464 127 delay[writePointer++] = input + delay[readPointer]*feedback;
robert@464 128 float output = (input + 0.9*delay[readPointer++] ) * 0.5;
robert@464 129 audioWrite(context, n, 0, output);
robert@464 130 audioWrite(context, n, 1, output);
robert@464 131 if(writePointer>=delayLength)
robert@464 132 writePointer-=delayLength;
robert@464 133 if(readPointer>=delayLength)
robert@464 134 readPointer-=delayLength;
robert@464 135
robert@464 136 gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate;
robert@464 137 gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
robert@464 138 if(gPhase1 > 2.0 * M_PI)
robert@464 139 gPhase1 -= 2.0 * M_PI;
robert@464 140 if(gPhase2 > 2.0 * M_PI)
robert@464 141 gPhase2 -= 2.0 * M_PI;
robert@464 142 }
robert@464 143 }
robert@464 144
robert@464 145 // cleanup_render() is called once at the end, after the audio has stopped.
robert@464 146 // Release any resources that were allocated in initialise_render().
robert@464 147
robert@464 148 void cleanup(BelaContext *context, void *userData)
robert@464 149 {
robert@464 150
robert@464 151 }