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