robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: float gPhase1, gPhase2; robert@464: float gFrequency1, gFrequency2; robert@464: float gInverseSampleRate; robert@464: robert@464: // initialise_render() is called once before the audio rendering starts. robert@464: // Use it to perform any initialisation and allocation which is dependent robert@464: // on the period size or sample rate. robert@464: // robert@464: // userData holds an opaque pointer to a data structure that was passed robert@464: // in from the call to initAudio(). robert@464: // robert@464: // Return true on success; returning false halts the program. robert@464: #include robert@464: #include robert@464: extern I2c_Codec *gAudioCodec; robert@464: extern PRU *gPRU; robert@464: float D=5264; robert@464: #define delayLength 256 robert@464: float delay[delayLength]; robert@464: int writePointer=0; robert@464: int readPointer=writePointer+1; robert@464: AuxiliaryTask updatePll; robert@464: robert@464: void updatePllFunction(){ robert@464: // gPRU->setGPIOTestPin(); robert@464: static int count = 0; robert@464: while(!gShouldStop){ robert@464: gAudioCodec->setPllD(D); robert@464: count++; robert@464: if((count&4095)==0) robert@464: printf("sampling rate: %f\n",gAudioCodec->getAudioSamplingRate()); robert@464: usleep(100); robert@464: } robert@464: // gPRU->clearGPIOTestPin(); robert@464: } robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@544: // Check that we have the same number of inputs and outputs. robert@544: if(context->audioInChannels != context->audioOutChannels || robert@544: context->analogInChannels != context-> analogOutChannels){ robert@544: printf("Error: for this project, you need the same number of input and output channels.\n"); robert@544: return false; robert@544: } robert@544: robert@464: gInverseSampleRate = 1.0/context->audioSampleRate; robert@464: robert@464: gPhase1 = 0.0; robert@464: gPhase2 = 0.0; robert@464: robert@464: gFrequency1 = 200.0; robert@464: gFrequency2 = 201.0; robert@464: updatePll=Bela_createAuxiliaryTask(&updatePllFunction, 91, "update PLL"); robert@464: for(int n=0; nsetPllP(2); robert@464: // gAudioCodec->setPllR(); robert@464: // gAudioCodec->setAudioSamplingRate(43600); robert@464: // printf("samplingRate: %f, k: %f\n", gAudioCodec->getAudioSamplingRate(), gAudioCodec->getPllK()); robert@464: init = true; robert@464: } robert@464: static int count=0; robert@464: static float lfoPhase=0; robert@464: static float feedback=0; robert@464: int updateRate=1; robert@464: if((count&(updateRate-1))==0){ robert@464: float amplitude = 8000; robert@464: float rate = 2; robert@464: lfoPhase+=rate*2*M_PI*updateRate*context->analogFrames/context->audioSampleRate; robert@464: D=amplitude+amplitude*sinf(lfoPhase); robert@464: if((count&255)==0){ robert@464: // rt_printf("frequency: %f\n", gAudioCodec->getAudioSamplingRate()); robert@464: // rt_printf("D: %.0f\n", D); robert@464: // rt_printf("rate: %f\n", rate); robert@464: // rt_printf("amplitude: %.3f\n", amplitude); robert@464: // rt_printf("feedback: %.3f\n\n", feedback); robert@464: } robert@464: } robert@464: count++; robert@464: robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: feedback = 0.4; robert@464: float input = audioRead(context, n, 0) + audioRead(context, n, 1); robert@464: delay[writePointer++] = input + delay[readPointer]*feedback; robert@464: float output = (input + 0.9*delay[readPointer++] ) * 0.5; robert@464: audioWrite(context, n, 0, output); robert@464: audioWrite(context, n, 1, output); robert@464: if(writePointer>=delayLength) robert@464: writePointer-=delayLength; robert@464: if(readPointer>=delayLength) robert@464: readPointer-=delayLength; robert@464: robert@464: gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate; robert@464: gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; robert@464: if(gPhase1 > 2.0 * M_PI) robert@464: gPhase1 -= 2.0 * M_PI; robert@464: if(gPhase2 > 2.0 * M_PI) robert@464: gPhase2 -= 2.0 * M_PI; robert@464: } robert@464: } robert@464: robert@464: // cleanup_render() is called once at the end, after the audio has stopped. robert@464: // Release any resources that were allocated in initialise_render(). robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: }